From eb1c0616361946a6c98e5a5c0817dd47d797fcad Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Thu, 26 Nov 2020 12:37:30 +0000 Subject: [PATCH] Bug 1678553 - part 2: Make `TSFTextStore::Content` store the last composition with `Maybe` r=m_kato When there is no composition, `Nothing` is clearer to indicate it. Differential Revision: https://phabricator.services.mozilla.com/D97948 --- widget/IMEData.h | 1 + widget/windows/TSFTextStore.cpp | 36 ++++++++------------ widget/windows/TSFTextStore.h | 58 +++++++++++++++++---------------- 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/widget/IMEData.h b/widget/IMEData.h index 33d2b88e4dda..2a148c5fee16 100644 --- a/widget/IMEData.h +++ b/widget/IMEData.h @@ -26,6 +26,7 @@ class WritingMode; class MOZ_STACK_CLASS PrintStringDetail : public nsAutoCString { public: static constexpr uint32_t kMaxLengthForCompositionString = 8; + static constexpr uint32_t kMaxLengthForEditor = 20; PrintStringDetail() = delete; explicit PrintStringDetail(const nsAString& aString, diff --git a/widget/windows/TSFTextStore.cpp b/widget/windows/TSFTextStore.cpp index 759915cd8f2f..6ab132f4d3c4 100644 --- a/widget/windows/TSFTextStore.cpp +++ b/widget/windows/TSFTextStore.cpp @@ -2832,18 +2832,8 @@ TSFTextStore::Content& TSFTextStore::ContentForTSFRef() { } MOZ_LOG(sTextStoreLog, LogLevel::Debug, - ("0x%p TSFTextStore::ContentForTSFRef(): " - "mContentForTSF={ mText=\"%s\" (Length()=%u), " - "mLastCompositionString=\"%s\" (Length()=%u), " - "mMinTextModifiedOffset=%u }", - this, - mContentForTSF.Text().Length() <= 40 - ? GetEscapedUTF8String(mContentForTSF.Text()).get() - : "", - mContentForTSF.Text().Length(), - GetEscapedUTF8String(mContentForTSF.LastCompositionString()).get(), - mContentForTSF.LastCompositionString().Length(), - mContentForTSF.MinTextModifiedOffset())); + ("0x%p TSFTextStore::ContentForTSFRef(): mContentForTSF=%s", this, + mozilla::ToString(mContentForTSF).c_str())); return mContentForTSF; } @@ -4812,9 +4802,9 @@ bool TSFTextStore::MaybeHackNoErrorLayoutBugs(LONG& aACPStart, LONG& aACPEnd) { // first character of composition string is stored since it was // selection start or caret position. LONG maxCachedOffset = mContentForTSF.LatestCompositionEndOffset(); - if (mContentForTSF.WasLastComposition()) { + if (mContentForTSF.LastComposition().isSome()) { maxCachedOffset = std::min( - maxCachedOffset, mContentForTSF.LastCompositionStringEndOffset()); + maxCachedOffset, mContentForTSF.LastComposition()->EndOffset()); } aACPStart = std::min(aACPStart, maxCachedOffset); } @@ -7111,11 +7101,14 @@ void TSFTextStore::Content::ReplaceTextWith(LONG aStart, LONG aLength, static_cast(aLength), aReplaceString); // TIP may set composition string twice or more times during a document // lock. Therefore, we should compute the first difference offset with - // mLastCompositionString. - if (mComposition.DataRef() != mLastCompositionString) { - firstDifferentOffset = mComposition.StartOffset() + - FirstDifferentCharOffset(mComposition.DataRef(), - mLastCompositionString); + // mLastComposition. + if (mLastComposition.isNothing()) { + firstDifferentOffset = mComposition.StartOffset(); + } else if (mComposition.DataRef() != mLastComposition->DataRef()) { + firstDifferentOffset = + mComposition.StartOffset() + + FirstDifferentCharOffset(mComposition.DataRef(), + mLastComposition->DataRef()); // The previous change to the composition string is canceled. if (mMinTextModifiedOffset >= static_cast(mComposition.StartOffset()) && @@ -7134,11 +7127,10 @@ void TSFTextStore::Content::ReplaceTextWith(LONG aStart, LONG aLength, sTextStoreLog, LogLevel::Debug, ("0x%p TSFTextStore::Content::ReplaceTextWith(aStart=%d, " "aLength=%d, aReplaceString=\"%s\"), mComposition=%s, " - "mLastCompositionString=\"%s\", mMinTextModifiedOffset=%u, " + "mLastComposition=%s, mMinTextModifiedOffset=%u, " "firstDifferentOffset=%u", this, aStart, aLength, GetEscapedUTF8String(aReplaceString).get(), - ToString(mComposition).c_str(), - GetEscapedUTF8String(mLastCompositionString).get(), + ToString(mComposition).c_str(), ToString(mLastComposition).c_str(), mMinTextModifiedOffset, firstDifferentOffset)); } else { firstDifferentOffset = diff --git a/widget/windows/TSFTextStore.h b/widget/windows/TSFTextStore.h index c820d3c03b0a..d346057d7a7e 100644 --- a/widget/windows/TSFTextStore.h +++ b/widget/windows/TSFTextStore.h @@ -15,6 +15,7 @@ #include "WritingModes.h" #include "mozilla/Attributes.h" +#include "mozilla/Maybe.h" #include "mozilla/RefPtr.h" #include "mozilla/StaticPtr.h" #include "mozilla/TextEventDispatcher.h" @@ -810,8 +811,7 @@ class TSFTextStore final : public ITextStoreACP, void Clear() { mText.Truncate(); - mLastCompositionString.Truncate(); - mLastCompositionStart = -1; + mLastComposition.reset(); mInitialized = false; } @@ -820,11 +820,9 @@ class TSFTextStore final : public ITextStoreACP, void Init(const nsAString& aText) { mText = aText; if (mComposition.IsComposing()) { - mLastCompositionString = mComposition.DataRef(); - mLastCompositionStart = mComposition.StartOffset(); + mLastComposition = Some(mComposition); } else { - mLastCompositionString.Truncate(); - mLastCompositionStart = -1; + mLastComposition.reset(); } mMinTextModifiedOffset = NOT_MODIFIED; mLatestCompositionStartOffset = mLatestCompositionEndOffset = LONG_MAX; @@ -841,11 +839,9 @@ class TSFTextStore final : public ITextStoreACP, return; } if (mComposition.IsComposing()) { - mLastCompositionString = mComposition.DataRef(); - mLastCompositionStart = mComposition.StartOffset(); + mLastComposition = Some(mComposition); } else { - mLastCompositionString.Truncate(); - mLastCompositionStart = -1; + mLastComposition.reset(); } } @@ -879,18 +875,9 @@ class TSFTextStore final : public ITextStoreACP, MOZ_ASSERT(mInitialized); return mText; } - const nsString& LastCompositionString() const { + const Maybe>& LastComposition() const { MOZ_ASSERT(mInitialized); - return mLastCompositionString; - } - LONG LastCompositionStringEndOffset() const { - MOZ_ASSERT(mInitialized); - MOZ_ASSERT(WasLastComposition()); - return mLastCompositionStart + mLastCompositionString.Length(); - } - bool WasLastComposition() const { - MOZ_ASSERT(mInitialized); - return mLastCompositionStart >= 0; + return mLastComposition; } uint32_t MinTextModifiedOffset() const { MOZ_ASSERT(mInitialized); @@ -930,18 +917,33 @@ class TSFTextStore final : public ITextStoreACP, TSFTextStore::Composition& Composition() { return mComposition; } TSFTextStore::Selection& Selection() { return mSelection; } + friend std::ostream& operator<<(std::ostream& aStream, + const Content& aContent) { + aStream << "{ mText=" + << PrintStringDetail(aContent.mText, + PrintStringDetail::kMaxLengthForEditor) + .get() + << ", mLastComposition=" << aContent.mLastComposition + << ", mLatestCompositionStartOffset=" + << aContent.mLatestCompositionStartOffset + << ", mLatestCompositionEndOffset=" + << aContent.mLatestCompositionEndOffset + << ", mMinTextModifiedOffset=" << aContent.mMinTextModifiedOffset + << ", mInitialized=" << aContent.mInitialized << " }"; + return aStream; + } + private: nsString mText; - // mLastCompositionString stores the composition string when the document - // is locked. This is necessary to compute mMinTextModifiedOffset. - nsString mLastCompositionString; + + // mLastComposition may store the composition string and its start offset + // when the document is locked. This is necessary to compute + // mMinTextModifiedOffset. + Maybe> mLastComposition; + TSFTextStore::Composition& mComposition; TSFTextStore::Selection& mSelection; - // mLastCompositionStart stores the start offset of composition when - // mLastCompositionString is set. - LONG mLastCompositionStart; - // The latest composition's start and end offset. If composition hasn't // been started since this instance is initialized, they are LONG_MAX. LONG mLatestCompositionStartOffset;