Chromium Code Reviews| Index: Source/web/ViewportAnchor.cpp |
| diff --git a/Source/web/ViewportAnchor.cpp b/Source/web/ViewportAnchor.cpp |
| index 4a6a3d40164d5d6fb0f61dfd69c1936e049087c7..4d39354cad4ea259145a4b1162abf0a5c83be2a1 100644 |
| --- a/Source/web/ViewportAnchor.cpp |
| +++ b/Source/web/ViewportAnchor.cpp |
| @@ -68,31 +68,65 @@ Node* findNonEmptyAnchorNode(const IntPoint& point, const IntRect& viewRect, Eve |
| return node; |
| } |
| +void moveToEncloseRect(FloatRect * outer, const FloatRect & inner) |
| +{ |
| + ASSERT(outer); |
| + |
| + FloatPoint minimumPosition = inner.location() + inner.size() - outer->size(); |
| + |
| + // Minimum position should be positive or zero |
| + minimumPosition = minimumPosition.expandedTo(FloatPoint()); |
| + |
| + FloatPoint maximumPosition = inner.location(); |
| + |
| + FloatPoint outerOrigin = outer->location(); |
| + outerOrigin = outerOrigin.expandedTo(minimumPosition); |
| + outerOrigin = outerOrigin.shrunkTo(maximumPosition); |
| + |
| + outer->setLocation(outerOrigin); |
| +} |
| + |
| } // namespace |
| ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) |
| : m_eventHandler(eventHandler) { } |
| -void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorInViewCoords) |
| +void ViewportAnchor::setAnchor(const IntRect& outerViewRect, const IntRect& innerViewRect, |
| + const FloatSize& anchorInInnerViewCoords) |
| { |
| - m_viewRect = viewRect; |
| + // Preserve the inner viewport position in document in case we won't find the anchor |
| + m_pinchViewportInDocument = innerViewRect.location(); |
| + |
| m_anchorNode.clear(); |
| m_anchorNodeBounds = LayoutRect(); |
| m_anchorInNodeCoords = FloatSize(); |
| - m_anchorInViewCoords = anchorInViewCoords; |
| + m_anchorInInnerViewCoords = anchorInInnerViewCoords; |
| + m_normalizedPinchViewportOffset = FloatSize(); |
| - if (viewRect.isEmpty()) |
| + if (innerViewRect.isEmpty()) |
| return; |
| // Preserve origins at the absolute screen origin |
| - if (viewRect.location() == IntPoint::zero()) |
| + if (innerViewRect.location() == IntPoint::zero()) |
| return; |
| - FloatSize anchorOffset = viewRect.size(); |
| - anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height()); |
| - const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffset; |
| + // Inner rectangle should be within the outer one. |
| + ASSERT(outerViewRect.contains(innerViewRect)); |
| + |
| + // Outer rectangle is used as a scale, we need positive width and height. |
| + ASSERT(!outerViewRect.isEmpty()); |
| + |
| + m_normalizedPinchViewportOffset = innerViewRect.location() - outerViewRect.location(); |
| - Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect, m_eventHandler); |
| + // Normalize by the size of the outer rect |
| + m_normalizedPinchViewportOffset.scale(1.0 / outerViewRect.width(), 1.0 / outerViewRect.height()); |
| + |
| + |
| + FloatSize anchorOffset = innerViewRect.size(); |
| + anchorOffset.scale(anchorInInnerViewCoords.width(), anchorInInnerViewCoords.height()); |
| + const FloatPoint anchorPoint = FloatPoint(innerViewRect.location()) + anchorOffset; |
| + |
| + Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), innerViewRect, m_eventHandler); |
| if (!node) |
| return; |
| @@ -102,14 +136,32 @@ void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorI |
| m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorNodeBounds.height()); |
| } |
| -IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const |
| +void ViewportAnchor::computeOrigins(const IntSize& outerSize, const IntSize& innerSize, |
| + IntPoint* mainFrameOffset, FloatPoint* pinchViewportOffset) const |
| +{ |
| + FloatSize absPinchViewportOffset = m_normalizedPinchViewportOffset; |
| + absPinchViewportOffset.scale(outerSize.width(), outerSize.height()); |
| + |
| + FloatPoint innerOrigin = getInnerOrigin(innerSize); // relative to the document |
| + FloatPoint outerOrigin = innerOrigin - absPinchViewportOffset; |
| + |
| + // Move outer viewport to enclose inner viewport |
| + FloatRect outerRect = FloatRect(outerOrigin, outerSize); |
| + moveToEncloseRect(&outerRect, FloatRect(innerOrigin, FloatSize(innerSize))); |
|
timav
2014/09/11 21:52:05
At first I thought this move is necessary, but now
bokan
2014/09/12 20:53:07
In general, the outer viewport can be scrolled hor
timav
2014/09/12 23:33:35
David, my idea here was different (of course if I
timav
2014/09/12 23:42:28
Correction: I actually wanted to say that this wou
timav
2014/09/12 23:59:52
Correction 2: I did not realize that if I move the
bokan
2014/09/15 15:36:35
Right, the end result is the same though, we want
timav
2014/09/15 20:04:34
Yes, absolutely. I realized it after I submitted m
|
| + |
| + *mainFrameOffset = flooredIntPoint(outerRect.location()); |
| + *pinchViewportOffset = FloatPoint(absPinchViewportOffset); |
| +} |
| + |
| + |
| +FloatPoint ViewportAnchor::getInnerOrigin(const IntSize& innerSize) const |
| { |
| if (!m_anchorNode || !m_anchorNode->inDocument()) |
| - return m_viewRect.location(); |
| + return m_pinchViewportInDocument; |
| const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); |
| if (m_anchorNodeBounds == currentNodeBounds) |
| - return m_viewRect.location(); |
| + return m_pinchViewportInDocument; |
| // Compute the new anchor point relative to the node position |
| FloatSize anchorOffsetFromNode = currentNodeBounds.size(); |
| @@ -117,9 +169,9 @@ IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const |
| FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode; |
| // Compute the new origin point relative to the new anchor point |
| - FloatSize anchorOffsetFromOrigin = currentViewSize; |
| - anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoords.height()); |
| - return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin); |
| + FloatSize anchorOffsetFromOrigin = innerSize; |
| + anchorOffsetFromOrigin.scale(m_anchorInInnerViewCoords.width(), m_anchorInInnerViewCoords.height()); |
| + return anchorPoint - anchorOffsetFromOrigin; |
| } |
| } // namespace blink |