Bug 1166436 part.13 mozilla::ContentCache should guess caret offset if the offset is in the range of cached text rects r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 05 Jun 2015 18:28:20 +0900
changeset 247328 d5f5a8d204d1a69415f6bdaca43342966b475f4d
parent 247327 6ee382b70183f82318b2fd5d3966f87e2fc09ee4
child 247329 27bf218dee2bded4559f8160bb5670f41a2d4920
push id60677
push usermasayuki@d-toybox.com
push dateFri, 05 Jun 2015 09:28:37 +0000
treeherdermozilla-inbound@cbaeacbb9700 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1166436
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1166436 part.13 mozilla::ContentCache should guess caret offset if the offset is in the range of cached text rects r=m_kato
widget/ContentCache.cpp
widget/ContentCache.h
--- a/widget/ContentCache.cpp
+++ b/widget/ContentCache.cpp
@@ -277,18 +277,16 @@ ContentCache::HandleQueryContentEvent(Wi
       MOZ_LOG(sContentCacheLog, LogLevel::Info,
         ("ContentCache: 0x%p (mIsChrome=%s) HandleQueryContentEvent("
          "aEvent={ message=NS_QUERY_CARET_RECT, mInput={ mOffset=%u } }, "
          "aWidget=0x%p), mText.Length()=%u",
          this, GetBoolName(mIsChrome), aEvent.mInput.mOffset, aWidget,
          mText.Length()));
       if (NS_WARN_IF(!GetCaretRect(aEvent.mInput.mOffset,
                                    aEvent.mReply.mRect))) {
-        // XXX If the input offset is in the range of cached text rects,
-        //     we can guess the caret rect.
         MOZ_LOG(sContentCacheLog, LogLevel::Error,
           ("ContentCache: 0x%p (mIsChrome=%s) HandleQueryContentEvent(), "
            "FAILED to get caret rect",
            this, GetBoolName(mIsChrome)));
         return false;
       }
       aEvent.mReply.mOffset = aEvent.mInput.mOffset;
       MOZ_LOG(sContentCacheLog, LogLevel::Info,
@@ -371,23 +369,18 @@ ContentCache::CacheSelection(nsIWidget* 
     mSelection.mFocus = selection.mReply.mOffset;
   } else {
     mSelection.mAnchor = selection.mReply.mOffset;
     mSelection.mFocus =
       selection.mReply.mOffset + selection.mReply.mString.Length();
   }
   mSelection.mWritingMode = selection.GetWritingMode();
 
-  nsRefPtr<TextComposition> textComposition =
-    IMEStateManager::GetTextCompositionFor(aWidget);
-  if (textComposition) {
-    mCaret.mOffset = textComposition->OffsetOfTargetClause();
-  } else {
-    mCaret.mOffset = selection.mReply.mOffset;
-  }
+  // XXX Should be mSelection.mFocus?
+  mCaret.mOffset = selection.mReply.mOffset;
 
   status = nsEventStatus_eIgnore;
   WidgetQueryContentEvent caretRect(true, NS_QUERY_CARET_RECT, aWidget);
   caretRect.InitForQueryCaretRect(mCaret.mOffset);
   aWidget->DispatchEvent(&caretRect, status);
   if (NS_WARN_IF(!caretRect.mSucceeded)) {
     MOZ_LOG(sContentCacheLog, LogLevel::Error,
       ("ContentCache: 0x%p (mIsChrome=%s) CacheSelection(), FAILED, "
@@ -606,25 +599,42 @@ ContentCache::GetUnionTextRects(uint32_t
 }
 
 bool
 ContentCache::GetCaretRect(uint32_t aOffset,
                            LayoutDeviceIntRect& aCaretRect) const
 {
   MOZ_LOG(sContentCacheLog, LogLevel::Info,
     ("ContentCache: 0x%p (mIsChrome=%s) GetCaretRect(aOffset=%u), "
-     "mCaret={ mOffset=%u, mRect=%s }",
+     "mCaret={ mOffset=%u, mRect=%s, IsValid()=%s }, mTextRectArray={ "
+     "mStart=%u, mRects.Length()=%u }, mSelection={ mWritingMode=%s }",
      this, GetBoolName(mIsChrome), aOffset, mCaret.mOffset,
-     GetRectText(mCaret.mRect).get()));
+     GetRectText(mCaret.mRect).get(), GetBoolName(mCaret.IsValid()),
+     mTextRectArray.mStart, mTextRectArray.mRects.Length(),
+     GetWritingModeName(mSelection.mWritingMode).get()));
 
-  if (mCaret.mOffset != aOffset) {
+  if (mCaret.IsValid() && mCaret.mOffset == aOffset) {
+    aCaretRect = mCaret.mRect;
+    return true;
+  }
+
+  // Guess caret rect from the text rect if it's stored.
+  if (!GetTextRect(aOffset, aCaretRect)) {
     aCaretRect.SetEmpty();
     return false;
   }
-  aCaretRect = mCaret.mRect;
+
+  // XXX This is not bidi aware because we don't cache each character's
+  //     direction.  However, this is usually used by IME, so, assuming the
+  //     character is in LRT context must not cause any problem.
+  if (mSelection.mWritingMode.IsVertical()) {
+    aCaretRect.height = mCaret.IsValid() ? mCaret.mRect.height : 1;
+  } else {
+    aCaretRect.width = mCaret.IsValid() ? mCaret.mRect.width : 1;
+  }
   return true;
 }
 
 bool
 ContentCache::OnCompositionEvent(const WidgetCompositionEvent& aEvent)
 {
   MOZ_LOG(sContentCacheLog, LogLevel::Info,
     ("ContentCache: 0x%p (mIsChrome=%s) OnCompositionEvent(aEvent={ "
--- a/widget/ContentCache.h
+++ b/widget/ContentCache.h
@@ -188,16 +188,18 @@ private:
     }
 
     void Clear()
     {
       mOffset = UINT32_MAX;
       mRect.SetEmpty();
     }
 
+    bool IsValid() const { return mOffset != UINT32_MAX; }
+
     uint32_t Offset() const
     {
       NS_WARN_IF(mOffset == UINT32_MAX);
       return mOffset;
     }
   } mCaret;
 
   struct TextRectArray final