Bug 1082486 - Part 3 - Refactor UpdatePosition(). r=roc
GetTouchCaretPosition() is needed in a later patch.
--- 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);