1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "web/RotationViewportAnchor.h"
8 #include "core/dom/ContainerNode.h"
9 #include "core/dom/Node.h"
10 #include "core/frame/FrameView.h"
11 #include "core/frame/LocalFrame.h"
12 #include "core/frame/PageScaleConstraintsSet.h"
13 #include "core/frame/VisualViewport.h"
14 #include "core/input/EventHandler.h"
15 #include "core/layout/HitTestResult.h"
16 #include "platform/geometry/DoubleRect.h"
22 static const float viewportAnchorRelativeEpsilon
= 0.1f
;
23 static const int viewportToNodeMaxRelativeArea
= 2;
25 template <typename RectType
>
26 int area(const RectType
& rect
)
28 return rect
.width() * rect
.height();
31 Node
* findNonEmptyAnchorNode(const IntPoint
& point
, const IntRect
& viewRect
, EventHandler
& eventHandler
)
33 Node
* node
= eventHandler
.hitTestResultAtPoint(point
, HitTestRequest::ReadOnly
| HitTestRequest::Active
).innerNode();
35 // If the node bounding box is sufficiently large, make a single attempt to
36 // find a smaller node; the larger the node bounds, the greater the
37 // variability under resize.
38 const int maxNodeArea
= area(viewRect
) * viewportToNodeMaxRelativeArea
;
39 if (node
&& area(node
->boundingBox()) > maxNodeArea
) {
40 IntSize pointOffset
= viewRect
.size();
41 pointOffset
.scale(viewportAnchorRelativeEpsilon
);
42 node
= eventHandler
.hitTestResultAtPoint(point
+ pointOffset
, HitTestRequest::ReadOnly
| HitTestRequest::Active
).innerNode();
45 while (node
&& node
->boundingBox().isEmpty())
46 node
= node
->parentNode();
51 void moveToEncloseRect(IntRect
& outer
, const FloatRect
& inner
)
53 IntPoint minimumPosition
= ceiledIntPoint(inner
.location() + inner
.size() - FloatSize(outer
.size()));
54 IntPoint maximumPosition
= flooredIntPoint(inner
.location());
56 IntPoint outerOrigin
= outer
.location();
57 outerOrigin
= outerOrigin
.expandedTo(minimumPosition
);
58 outerOrigin
= outerOrigin
.shrunkTo(maximumPosition
);
60 outer
.setLocation(outerOrigin
);
63 void moveIntoRect(FloatRect
& inner
, const IntRect
& outer
)
65 FloatPoint minimumPosition
= FloatPoint(outer
.location());
66 FloatPoint maximumPosition
= minimumPosition
+ outer
.size() - inner
.size();
68 // Adjust maximumPosition to the nearest lower integer because
69 // VisualViewport::maximumScrollPosition() does the same.
70 // The value of minumumPosition is already adjusted since it is
71 // constructed from an integer point.
72 maximumPosition
= flooredIntPoint(maximumPosition
);
74 FloatPoint innerOrigin
= inner
.location();
75 innerOrigin
= innerOrigin
.expandedTo(minimumPosition
);
76 innerOrigin
= innerOrigin
.shrunkTo(maximumPosition
);
78 inner
.setLocation(innerOrigin
);
83 RotationViewportAnchor::RotationViewportAnchor(
84 FrameView
& rootFrameView
,
85 VisualViewport
& visualViewport
,
86 const FloatSize
& anchorInInnerViewCoords
,
87 PageScaleConstraintsSet
& pageScaleConstraintsSet
)
88 : ViewportAnchor(rootFrameView
, visualViewport
)
89 , m_anchorInInnerViewCoords(anchorInInnerViewCoords
)
90 , m_pageScaleConstraintsSet(pageScaleConstraintsSet
)
95 RotationViewportAnchor::~RotationViewportAnchor()
100 void RotationViewportAnchor::setAnchor()
102 // FIXME: Scroll offsets are now fractional (DoublePoint and FloatPoint for the FrameView and VisualViewport
103 // respectively. This path should be rewritten without pixel snapping.
104 IntRect outerViewRect
= m_rootFrameView
->layoutViewportScrollableArea()->visibleContentRect(IncludeScrollbars
);
105 IntRect innerViewRect
= enclosedIntRect(m_rootFrameView
->scrollableArea()->visibleContentRectDouble());
107 m_oldPageScaleFactor
= m_visualViewport
->scale();
108 m_oldMinimumPageScaleFactor
= m_pageScaleConstraintsSet
.finalConstraints().minimumScale
;
110 // Save the absolute location in case we won't find the anchor node, we'll fall back to that.
111 m_visualViewportInDocument
= FloatPoint(m_rootFrameView
->scrollableArea()->visibleContentRectDouble().location());
113 m_anchorNode
.clear();
114 m_anchorNodeBounds
= LayoutRect();
115 m_anchorInNodeCoords
= FloatSize();
116 m_normalizedVisualViewportOffset
= FloatSize();
118 if (innerViewRect
.isEmpty())
121 // Preserve origins at the absolute screen origin
122 if (innerViewRect
.location() == IntPoint::zero())
125 // Inner rectangle should be within the outer one.
126 ASSERT(outerViewRect
.contains(innerViewRect
));
128 // Outer rectangle is used as a scale, we need positive width and height.
129 ASSERT(!outerViewRect
.isEmpty());
131 m_normalizedVisualViewportOffset
= innerViewRect
.location() - outerViewRect
.location();
133 // Normalize by the size of the outer rect
134 m_normalizedVisualViewportOffset
.scale(1.0 / outerViewRect
.width(), 1.0 / outerViewRect
.height());
136 FloatSize anchorOffset
= innerViewRect
.size();
137 anchorOffset
.scale(m_anchorInInnerViewCoords
.width(), m_anchorInInnerViewCoords
.height());
138 const FloatPoint anchorPoint
= FloatPoint(innerViewRect
.location()) + anchorOffset
;
140 Node
* node
= findNonEmptyAnchorNode(flooredIntPoint(anchorPoint
), innerViewRect
, m_rootFrameView
->frame().eventHandler());
145 m_anchorNodeBounds
= node
->boundingBox();
146 m_anchorInNodeCoords
= anchorPoint
- FloatPoint(m_anchorNodeBounds
.location());
147 m_anchorInNodeCoords
.scale(1.f
/ m_anchorNodeBounds
.width(), 1.f
/ m_anchorNodeBounds
.height());
150 void RotationViewportAnchor::restoreToAnchor()
152 float newPageScaleFactor
= m_oldPageScaleFactor
/ m_oldMinimumPageScaleFactor
* m_pageScaleConstraintsSet
.finalConstraints().minimumScale
;
153 newPageScaleFactor
= m_pageScaleConstraintsSet
.finalConstraints().clampToConstraints(newPageScaleFactor
);
155 FloatSize visualViewportSize
= m_visualViewport
->size();
156 visualViewportSize
.scale(1 / newPageScaleFactor
);
158 IntPoint mainFrameOrigin
;
159 FloatPoint visualViewportOrigin
;
161 computeOrigins(visualViewportSize
, mainFrameOrigin
, visualViewportOrigin
);
163 m_rootFrameView
->layoutViewportScrollableArea()->setScrollPosition(mainFrameOrigin
, ProgrammaticScroll
);
165 // Set scale before location, since location can be clamped on setting scale.
166 m_visualViewport
->setScale(newPageScaleFactor
);
167 m_visualViewport
->setLocation(visualViewportOrigin
);
170 void RotationViewportAnchor::computeOrigins(const FloatSize
& innerSize
, IntPoint
& mainFrameOffset
, FloatPoint
& visualViewportOffset
) const
172 IntSize outerSize
= m_rootFrameView
->layoutViewportScrollableArea()->visibleContentRect().size();
174 // Compute the viewport origins in CSS pixels relative to the document.
175 FloatSize absVisualViewportOffset
= m_normalizedVisualViewportOffset
;
176 absVisualViewportOffset
.scale(outerSize
.width(), outerSize
.height());
178 FloatPoint innerOrigin
= getInnerOrigin(innerSize
);
179 FloatPoint outerOrigin
= innerOrigin
- absVisualViewportOffset
;
181 IntRect outerRect
= IntRect(flooredIntPoint(outerOrigin
), outerSize
);
182 FloatRect innerRect
= FloatRect(innerOrigin
, innerSize
);
184 moveToEncloseRect(outerRect
, innerRect
);
186 outerRect
.setLocation(m_rootFrameView
->layoutViewportScrollableArea()->clampScrollPosition(outerRect
.location()));
188 moveIntoRect(innerRect
, outerRect
);
190 mainFrameOffset
= outerRect
.location();
191 visualViewportOffset
= FloatPoint(innerRect
.location() - outerRect
.location());
194 FloatPoint
RotationViewportAnchor::getInnerOrigin(const FloatSize
& innerSize
) const
196 if (!m_anchorNode
|| !m_anchorNode
->inDocument())
197 return m_visualViewportInDocument
;
199 const LayoutRect currentNodeBounds
= m_anchorNode
->boundingBox();
200 if (m_anchorNodeBounds
== currentNodeBounds
)
201 return m_visualViewportInDocument
;
203 // Compute the new anchor point relative to the node position
204 FloatSize
anchorOffsetFromNode(currentNodeBounds
.size());
205 anchorOffsetFromNode
.scale(m_anchorInNodeCoords
.width(), m_anchorInNodeCoords
.height());
206 FloatPoint anchorPoint
= FloatPoint(currentNodeBounds
.location()) + anchorOffsetFromNode
;
208 // Compute the new origin point relative to the new anchor point
209 FloatSize anchorOffsetFromOrigin
= innerSize
;
210 anchorOffsetFromOrigin
.scale(m_anchorInInnerViewCoords
.width(), m_anchorInInnerViewCoords
.height());
211 return anchorPoint
- anchorOffsetFromOrigin
;