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
--- 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) {