Bug 1082486 - Part 3 - Refactor UpdatePosition(). r=roc
authorTing-Yu Lin <tlin@mozilla.com>
Wed, 15 Oct 2014 03:05:00 +0200
changeset 211194 b66bcf105809c23610c6e9ad247efb7e2ad102d0
parent 211193 a1c2b6323e6ea305c2952aaa41a0f517210d735e
child 211195 2ec24bc9a5dd981f9db241322d92ea8ab5fde5dd
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersroc
bugs1082486
milestone36.0a1
Bug 1082486 - Part 3 - Refactor UpdatePosition(). r=roc GetTouchCaretPosition() is needed in a later patch.
layout/base/TouchCaret.cpp
layout/base/TouchCaret.h
--- a/layout/base/TouchCaret.cpp
+++ b/layout/base/TouchCaret.cpp
@@ -425,31 +425,45 @@ TouchCaret::IsDisplayable()
   return true;
 }
 
 void
 TouchCaret::UpdatePosition()
 {
   MOZ_ASSERT(mVisible);
 
+  nsPoint pos = GetTouchCaretPosition();
+  pos = ClampPositionToScrollFrame(pos);
+  SetTouchFramePos(pos);
+}
+
+nsPoint
+TouchCaret::GetTouchCaretPosition()
+{
   nsRect focusRect;
   nsIFrame* focusFrame = GetCaretFocusFrame(&focusRect);
   nsIFrame* canvasFrame = GetCanvasFrame();
 
-  if (!focusFrame || !canvasFrame || focusRect.IsEmpty()) {
-    return;
-  }
-
   // Position of the touch caret relative to focusFrame.
   nsPoint pos = nsPoint(focusRect.x + (focusRect.width / 2),
                         focusRect.y + focusRect.height);
 
   // Transform the position to make it relative to canvas frame.
   nsLayoutUtils::TransformPoint(focusFrame, canvasFrame, pos);
 
+  return pos;
+}
+
+nsPoint
+TouchCaret::ClampPositionToScrollFrame(const nsPoint& aPosition)
+{
+  nsPoint pos = aPosition;
+  nsIFrame* focusFrame = GetCaretFocusFrame();
+  nsIFrame* canvasFrame = GetCanvasFrame();
+
   // Clamp the touch caret position to the scrollframe boundary.
   nsIFrame* closestScrollFrame =
     nsLayoutUtils::GetClosestFrameOfType(focusFrame, nsGkAtoms::scrollFrame);
   while (closestScrollFrame) {
     nsIScrollableFrame* sf = do_QueryFrame(closestScrollFrame);
     nsRect visualRect = sf->GetScrollPortRect();
 
     // Clamp the touch caret in the scroll port.
@@ -457,17 +471,17 @@ TouchCaret::UpdatePosition()
     pos = visualRect.ClampPoint(pos);
 
     // Get next ancestor scroll frame.
     closestScrollFrame =
       nsLayoutUtils::GetClosestFrameOfType(closestScrollFrame->GetParent(),
                                            nsGkAtoms::scrollFrame);
   }
 
-  SetTouchFramePos(pos);
+  return pos;
 }
 
 /* static */void
 TouchCaret::DisableTouchCaretCallback(nsITimer* aTimer, void* aTouchCaret)
 {
   nsRefPtr<TouchCaret> self = static_cast<TouchCaret*>(aTouchCaret);
   NS_PRECONDITION(aTimer == self->mTouchCaretExpirationTimer,
                   "Unexpected timer");
--- a/layout/base/TouchCaret.h
+++ b/layout/base/TouchCaret.h
@@ -107,16 +107,28 @@ private:
 
   /**
    * Retrieve the center y position of the caret.
    * The returned point is relative to the canvas frame.
    */
   nscoord GetCaretYCenterPosition();
 
   /**
+   * Retrieve the position of the touch caret.
+   * The returned point is relative to the canvas frame.
+   */
+  nsPoint GetTouchCaretPosition();
+
+  /**
+   * Clamp the position of the touch caret to the scroll frame boundary.
+   * The returned point is relative to the canvas frame.
+   */
+  nsPoint ClampPositionToScrollFrame(const nsPoint& aPosition);
+
+  /**
    * Set the position of the touch caret.
    * Touch caret is an absolute positioned div.
    */
   void SetTouchFramePos(const nsPoint& aOrigin);
 
   void LaunchExpirationTimer();
   void CancelExpirationTimer();
   static void DisableTouchCaretCallback(nsITimer* aTimer, void* aPresShell);