Bug 1329474 part.1 Implement IMEInputHandler::MarkSelectedRangeDirty() to make the meaning of setting NSNotFound to NSRange.location clearer r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 07 Feb 2017 14:26:03 +0900
changeset 490457 d5f662dff0bddbfdee76254ee3b236b385db18d3
parent 490433 1bc2ad020aee2830e0a7941f10958dbec108c254
child 490458 83a01842bb17073ec6ad46535b638c04dfdd8610
push id47099
push usermasayuki@d-toybox.com
push dateTue, 28 Feb 2017 13:27:03 +0000
reviewersm_kato
bugs1329474
milestone54.0a1
Bug 1329474 part.1 Implement IMEInputHandler::MarkSelectedRangeDirty() to make the meaning of setting NSNotFound to NSRange.location clearer r?m_kato Now, when NSRange.location is NSNotFound, we treat mSelectedRange as dirty. For making this rule clearer, this patch adds MarkSelectedRangeDirty() and IsSelectedRangeDirty(). MozReview-Commit-ID: 3cjjnqoVWBq
widget/cocoa/TextInputHandler.h
widget/cocoa/TextInputHandler.mm
--- a/widget/cocoa/TextInputHandler.h
+++ b/widget/cocoa/TextInputHandler.h
@@ -927,16 +927,27 @@ protected:
    *
    * @param aAttrString           A string which is committed.
    * @param aReplacementRange     The range which will be replaced with the
    *                              aAttrString instead of current selection.
    */
   void InsertTextAsCommittingComposition(NSAttributedString* aAttrString,
                                          NSRange* aReplacementRange);
 
+protected:
+  /**
+   * Marking mSelectedRange as dirty.
+   */
+  void MarkSelectedRangeDirty();
+
+  /**
+   * Retruns true if mSelectedRange is marked as dirty.
+   */
+  bool IsSelectedRangeDirty() const;
+
 private:
   // If mIsIMEComposing is true, the composition string is stored here.
   NSString* mIMECompositionString;
   // If mIsIMEComposing is true, the start offset of the composition string.
   uint32_t mIMECompositionStart;
 
   NSRange mMarkedRange;
   NSRange mSelectedRange;
--- a/widget/cocoa/TextInputHandler.mm
+++ b/widget/cocoa/TextInputHandler.mm
@@ -3423,17 +3423,17 @@ IMEInputHandler::SelectedRange()
      "location=%lu, length=%lu }",
      this, TrueOrFalse(Destroyed()), static_cast<unsigned long>(mSelectedRange.location),
      static_cast<unsigned long>(mSelectedRange.length)));
 
   if (Destroyed()) {
     return mSelectedRange;
   }
 
-  if (mSelectedRange.location != NSNotFound) {
+  if (!IsSelectedRangeDirty()) {
     MOZ_ASSERT(mIMEHasFocus);
     return mSelectedRange;
   }
 
   RefPtr<IMEInputHandler> kungFuDeathGrip(this);
 
   WidgetQueryContentEvent selection(true, eQuerySelectedText, mWidget);
   DispatchEvent(selection);
@@ -3683,18 +3683,17 @@ IMEInputHandler::IMEInputHandler(nsChild
   , mIgnoreIMECommit(false)
   , mIsInFocusProcessing(false)
   , mIMEHasFocus(false)
 {
   InitStaticMembers();
 
   mMarkedRange.location = NSNotFound;
   mMarkedRange.length = 0;
-  mSelectedRange.location = NSNotFound;
-  mSelectedRange.length = 0;
+  MarkSelectedRangeDirty();
 }
 
 IMEInputHandler::~IMEInputHandler()
 {
   if (mTimer) {
     mTimer->Cancel();
     mTimer = nullptr;
   }
@@ -3703,24 +3702,37 @@ IMEInputHandler::~IMEInputHandler()
   }
   if (mIMECompositionString) {
     [mIMECompositionString release];
     mIMECompositionString = nullptr;
   }
 }
 
 void
+IMEInputHandler::MarkSelectedRangeDirty()
+{
+  mSelectedRange.location = NSNotFound;
+  mSelectedRange.length = 0;
+}
+
+bool
+IMEInputHandler::IsSelectedRangeDirty() const
+{
+  return mSelectedRange.location == NSNotFound && !mSelectedRange.length;
+}
+
+void
 IMEInputHandler::OnFocusChangeInGecko(bool aFocus)
 {
   MOZ_LOG(gLog, LogLevel::Info,
     ("%p IMEInputHandler::OnFocusChangeInGecko, aFocus=%s, Destroyed()=%s, "
      "sFocusedIMEHandler=%p",
      this, TrueOrFalse(aFocus), TrueOrFalse(Destroyed()), sFocusedIMEHandler));
 
-  mSelectedRange.location = NSNotFound; // Marking dirty
+  MarkSelectedRangeDirty();
   mIMEHasFocus = aFocus;
 
   // This is called when the native focus is changed and when the native focus
   // isn't changed but the focus is changed in Gecko.
   if (!aFocus) {
     if (sFocusedIMEHandler == this)
       sFocusedIMEHandler = nullptr;
     return;
@@ -3754,17 +3766,17 @@ IMEInputHandler::OnDestroyWidget(nsChild
     return false;
   }
 
   if (IsIMEComposing()) {
     // If our view is in the composition, we should clean up it.
     CancelIMEComposition();
   }
 
-  mSelectedRange.location = NSNotFound; // Marking dirty
+  MarkSelectedRangeDirty();
   mIMEHasFocus = false;
 
   return true;
 }
 
 void
 IMEInputHandler::SendCommittedText(NSString *aString)
 {
@@ -4026,20 +4038,18 @@ IMEInputHandler::OpenSystemPreferredLang
 
 void
 IMEInputHandler::OnSelectionChange(const IMENotification& aIMENotification)
 {
   MOZ_LOG(gLog, LogLevel::Info,
     ("%p IMEInputHandler::OnSelectionChange", this));
 
   if (aIMENotification.mSelectionChangeData.mOffset == UINT32_MAX) {
-    mSelectedRange.location = NSNotFound;
-    mSelectedRange.length = 0;
-    mRangeForWritingMode.location = NSNotFound;
-    mRangeForWritingMode.length = 0;
+    MarkSelectedRangeDirty();
+    mRangeForWritingMode = mSelectedRange;
     return;
   }
 
   mWritingMode = aIMENotification.mSelectionChangeData.GetWritingMode();
   mRangeForWritingMode =
     NSMakeRange(aIMENotification.mSelectionChangeData.mOffset,
                 aIMENotification.mSelectionChangeData.Length());
   if (mIMEHasFocus) {