From 60406e63b702939f51737cd783235f8f08ecdec3 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Tue, 31 Mar 2015 18:20:27 -0400 Subject: [PATCH] Bug 1149189 - Add dummy text change when setting composition to the same text; r=esawin --- widget/android/nsWindow.cpp | 29 ++++++++++++++++------ widget/android/nsWindow.h | 49 +++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/widget/android/nsWindow.cpp b/widget/android/nsWindow.cpp index 7a20a4cebea8..5dadd77f7cd3 100644 --- a/widget/android/nsWindow.cpp +++ b/widget/android/nsWindow.cpp @@ -1827,6 +1827,16 @@ nsWindow::OnIMEEvent(AndroidGeckoEvent *ae) InitEvent(event, nullptr); DispatchEvent(&event); } + + } else if (composition->String().Equals(ae->Characters())) { + /* If the new text is the same as the existing composition text, + * the NS_COMPOSITION_CHANGE event does not generate a text + * change notification. However, the Java side still expects + * one, so we manually generate a notification. */ + IMEChange dummyChange; + dummyChange.mStart = ae->Start(); + dummyChange.mOldEnd = dummyChange.mNewEnd = ae->End(); + AddIMETextChange(dummyChange); } { @@ -2244,19 +2254,25 @@ nsWindow::NotifyIMEOfTextChange(const IMENotification& aIMENotification) /* Make sure Java's selection is up-to-date */ mIMESelectionChanged = false; NotifyIME(NOTIFY_IME_OF_SELECTION_CHANGE); - PostFlushIMEChanges(); - mIMETextChanges.AppendElement(IMEChange(aIMENotification)); + AddIMETextChange(IMEChange(aIMENotification)); + PostFlushIMEChanges(); + return NS_OK; +} + +void +nsWindow::AddIMETextChange(const IMEChange& aChange) { + + mIMETextChanges.AppendElement(aChange); + // Now that we added a new range we need to go back and // update all the ranges before that. // Ranges that have offsets which follow this new range // need to be updated to reflect new offsets - int32_t delta = aIMENotification.mTextChangeData.AdditionalLength(); + const int32_t delta = aChange.mNewEnd - aChange.mOldEnd; for (int32_t i = mIMETextChanges.Length() - 2; i >= 0; i--) { IMEChange &previousChange = mIMETextChanges[i]; - if (previousChange.mStart > - static_cast( - aIMENotification.mTextChangeData.mOldEndOffset)) { + if (previousChange.mStart > aChange.mOldEnd) { previousChange.mStart += delta; previousChange.mOldEnd += delta; previousChange.mNewEnd += delta; @@ -2304,7 +2320,6 @@ nsWindow::NotifyIMEOfTextChange(const IMENotification& aIMENotification) // so we can safely continue the merge starting at dst srcIndex = dstIndex; } - return NS_OK; } nsIMEUpdatePreference diff --git a/widget/android/nsWindow.h b/widget/android/nsWindow.h index fc3383cc30b3..407b36dcb739 100644 --- a/widget/android/nsWindow.h +++ b/widget/android/nsWindow.h @@ -170,8 +170,33 @@ protected: nsWindow *FindTopLevel(); bool IsTopLevel(); + struct IMEChange { + int32_t mStart, mOldEnd, mNewEnd; + + IMEChange() : + mStart(-1), mOldEnd(-1), mNewEnd(-1) + { + } + IMEChange(const IMENotification& aIMENotification) + : mStart(aIMENotification.mTextChangeData.mStartOffset) + , mOldEnd(aIMENotification.mTextChangeData.mOldEndOffset) + , mNewEnd(aIMENotification.mTextChangeData.mNewEndOffset) + { + MOZ_ASSERT(aIMENotification.mMessage == + mozilla::widget::NOTIFY_IME_OF_TEXT_CHANGE, + "IMEChange initialized with wrong notification"); + MOZ_ASSERT(aIMENotification.mTextChangeData.IsInInt32Range(), + "The text change notification is out of range"); + } + bool IsEmpty() const + { + return mStart < 0; + } + }; + nsRefPtr GetIMEComposition(); void RemoveIMEComposition(); + void AddIMETextChange(const IMEChange& aChange); void PostFlushIMEChanges(); void FlushIMEChanges(); @@ -197,30 +222,6 @@ protected: nsRefPtr mIMERanges; bool mIMEUpdatingContext; nsAutoTArray mIMEKeyEvents; - - struct IMEChange { - int32_t mStart, mOldEnd, mNewEnd; - - IMEChange() : - mStart(-1), mOldEnd(-1), mNewEnd(-1) - { - } - IMEChange(const IMENotification& aIMENotification) - : mStart(aIMENotification.mTextChangeData.mStartOffset) - , mOldEnd(aIMENotification.mTextChangeData.mOldEndOffset) - , mNewEnd(aIMENotification.mTextChangeData.mNewEndOffset) - { - MOZ_ASSERT(aIMENotification.mMessage == - mozilla::widget::NOTIFY_IME_OF_TEXT_CHANGE, - "IMEChange initialized with wrong notification"); - MOZ_ASSERT(aIMENotification.mTextChangeData.IsInInt32Range(), - "The text change notification is out of range"); - } - bool IsEmpty() - { - return mStart < 0; - } - }; nsAutoTArray mIMETextChanges; bool mIMESelectionChanged;