| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "web/RotationViewportAnchor.h" | 6 #include "web/RotationViewportAnchor.h" |
| 7 | 7 |
| 8 #include "core/dom/ContainerNode.h" | 8 #include "core/dom/ContainerNode.h" |
| 9 #include "core/dom/Node.h" | 9 #include "core/dom/Node.h" |
| 10 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 RotationViewportAnchor::RotationViewportAnchor( | 82 RotationViewportAnchor::RotationViewportAnchor( |
| 83 FrameView& rootFrameView, | 83 FrameView& rootFrameView, |
| 84 PinchViewport& pinchViewport, | 84 PinchViewport& pinchViewport, |
| 85 const FloatSize& anchorInInnerViewCoords, | 85 const FloatSize& anchorInInnerViewCoords, |
| 86 PageScaleConstraintsSet& pageScaleConstraintsSet) | 86 PageScaleConstraintsSet& pageScaleConstraintsSet) |
| 87 : ViewportAnchor(rootFrameView, pinchViewport) | 87 : ViewportAnchor(rootFrameView, pinchViewport) |
| 88 , m_anchorInInnerViewCoords(anchorInInnerViewCoords) | 88 , m_anchorInInnerViewCoords(anchorInInnerViewCoords) |
| 89 , m_pageScaleConstraintsSet(pageScaleConstraintsSet) { } | 89 , m_pageScaleConstraintsSet(pageScaleConstraintsSet) |
| 90 { |
| 91 setAnchor(); |
| 92 } |
| 93 |
| 94 RotationViewportAnchor::~RotationViewportAnchor() |
| 95 { |
| 96 restoreToAnchor(); |
| 97 } |
| 90 | 98 |
| 91 void RotationViewportAnchor::setAnchor() | 99 void RotationViewportAnchor::setAnchor() |
| 92 { | 100 { |
| 93 // FIXME: Scroll offsets are now fractional (DoublePoint and FloatPoint for
the FrameView and PinchViewport | 101 // FIXME: Scroll offsets are now fractional (DoublePoint and FloatPoint for
the FrameView and PinchViewport |
| 94 // respectively. This path should be rewritten without pixel snapping
. | 102 // respectively. This path should be rewritten without pixel snapping
. |
| 95 IntRect outerViewRect = m_rootFrameView.visibleContentRect(); | 103 IntRect outerViewRect = m_rootFrameView->visibleContentRect(); |
| 96 IntRect innerViewRect = enclosedIntRect(m_pinchViewport.visibleRectInDocumen
t()); | 104 IntRect innerViewRect = enclosedIntRect(m_pinchViewport->visibleRectInDocume
nt()); |
| 97 | 105 |
| 98 m_oldPageScaleFactor = m_pinchViewport.scale(); | 106 m_oldPageScaleFactor = m_pinchViewport->scale(); |
| 99 m_oldMinimumPageScaleFactor = m_pageScaleConstraintsSet.finalConstraints().m
inimumScale; | 107 m_oldMinimumPageScaleFactor = m_pageScaleConstraintsSet.finalConstraints().m
inimumScale; |
| 100 | 108 |
| 101 // Save the absolute location in case we won't find the anchor node, we'll f
all back to that. | 109 // Save the absolute location in case we won't find the anchor node, we'll f
all back to that. |
| 102 m_pinchViewportInDocument = m_pinchViewport.visibleRectInDocument().location
(); | 110 m_pinchViewportInDocument = m_pinchViewport->visibleRectInDocument().locatio
n(); |
| 103 | 111 |
| 104 m_anchorNode.clear(); | 112 m_anchorNode.clear(); |
| 105 m_anchorNodeBounds = LayoutRect(); | 113 m_anchorNodeBounds = LayoutRect(); |
| 106 m_anchorInNodeCoords = FloatSize(); | 114 m_anchorInNodeCoords = FloatSize(); |
| 107 m_normalizedPinchViewportOffset = FloatSize(); | 115 m_normalizedPinchViewportOffset = FloatSize(); |
| 108 | 116 |
| 109 if (innerViewRect.isEmpty()) | 117 if (innerViewRect.isEmpty()) |
| 110 return; | 118 return; |
| 111 | 119 |
| 112 // Preserve origins at the absolute screen origin | 120 // Preserve origins at the absolute screen origin |
| 113 if (innerViewRect.location() == IntPoint::zero()) | 121 if (innerViewRect.location() == IntPoint::zero()) |
| 114 return; | 122 return; |
| 115 | 123 |
| 116 // Inner rectangle should be within the outer one. | 124 // Inner rectangle should be within the outer one. |
| 117 ASSERT(outerViewRect.contains(innerViewRect)); | 125 ASSERT(outerViewRect.contains(innerViewRect)); |
| 118 | 126 |
| 119 // Outer rectangle is used as a scale, we need positive width and height. | 127 // Outer rectangle is used as a scale, we need positive width and height. |
| 120 ASSERT(!outerViewRect.isEmpty()); | 128 ASSERT(!outerViewRect.isEmpty()); |
| 121 | 129 |
| 122 m_normalizedPinchViewportOffset = innerViewRect.location() - outerViewRect.l
ocation(); | 130 m_normalizedPinchViewportOffset = innerViewRect.location() - outerViewRect.l
ocation(); |
| 123 | 131 |
| 124 // Normalize by the size of the outer rect | 132 // Normalize by the size of the outer rect |
| 125 m_normalizedPinchViewportOffset.scale(1.0 / outerViewRect.width(), 1.0 / out
erViewRect.height()); | 133 m_normalizedPinchViewportOffset.scale(1.0 / outerViewRect.width(), 1.0 / out
erViewRect.height()); |
| 126 | 134 |
| 127 FloatSize anchorOffset = innerViewRect.size(); | 135 FloatSize anchorOffset = innerViewRect.size(); |
| 128 anchorOffset.scale(m_anchorInInnerViewCoords.width(), m_anchorInInnerViewCoo
rds.height()); | 136 anchorOffset.scale(m_anchorInInnerViewCoords.width(), m_anchorInInnerViewCoo
rds.height()); |
| 129 const FloatPoint anchorPoint = FloatPoint(innerViewRect.location()) + anchor
Offset; | 137 const FloatPoint anchorPoint = FloatPoint(innerViewRect.location()) + anchor
Offset; |
| 130 | 138 |
| 131 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), innerViewR
ect, m_rootFrameView.frame().eventHandler()); | 139 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), innerViewR
ect, m_rootFrameView->frame().eventHandler()); |
| 132 if (!node) | 140 if (!node) |
| 133 return; | 141 return; |
| 134 | 142 |
| 135 m_anchorNode = node; | 143 m_anchorNode = node; |
| 136 m_anchorNodeBounds = node->boundingBox(); | 144 m_anchorNodeBounds = node->boundingBox(); |
| 137 m_anchorInNodeCoords = anchorPoint - FloatPoint(m_anchorNodeBounds.location(
)); | 145 m_anchorInNodeCoords = anchorPoint - FloatPoint(m_anchorNodeBounds.location(
)); |
| 138 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); | 146 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); |
| 139 } | 147 } |
| 140 | 148 |
| 141 void RotationViewportAnchor::restoreToAnchor() | 149 void RotationViewportAnchor::restoreToAnchor() |
| 142 { | 150 { |
| 143 float newPageScaleFactor = m_oldPageScaleFactor / m_oldMinimumPageScaleFacto
r * m_pageScaleConstraintsSet.finalConstraints().minimumScale; | 151 float newPageScaleFactor = m_oldPageScaleFactor / m_oldMinimumPageScaleFacto
r * m_pageScaleConstraintsSet.finalConstraints().minimumScale; |
| 144 newPageScaleFactor = m_pageScaleConstraintsSet.finalConstraints().clampToCon
straints(newPageScaleFactor); | 152 newPageScaleFactor = m_pageScaleConstraintsSet.finalConstraints().clampToCon
straints(newPageScaleFactor); |
| 145 | 153 |
| 146 FloatSize pinchViewportSize = m_pinchViewport.size(); | 154 FloatSize pinchViewportSize = m_pinchViewport->size(); |
| 147 pinchViewportSize.scale(1 / newPageScaleFactor); | 155 pinchViewportSize.scale(1 / newPageScaleFactor); |
| 148 | 156 |
| 149 IntPoint mainFrameOrigin; | 157 IntPoint mainFrameOrigin; |
| 150 FloatPoint pinchViewportOrigin; | 158 FloatPoint pinchViewportOrigin; |
| 151 | 159 |
| 152 computeOrigins(pinchViewportSize, mainFrameOrigin, pinchViewportOrigin); | 160 computeOrigins(pinchViewportSize, mainFrameOrigin, pinchViewportOrigin); |
| 153 | 161 |
| 154 m_rootFrameView.setScrollOffset(mainFrameOrigin); | 162 m_rootFrameView->setScrollOffset(mainFrameOrigin); |
| 155 | 163 |
| 156 // Set scale before location, since location can be clamped on setting scale
. | 164 // Set scale before location, since location can be clamped on setting scale
. |
| 157 m_pinchViewport.setScale(newPageScaleFactor); | 165 m_pinchViewport->setScale(newPageScaleFactor); |
| 158 m_pinchViewport.setLocation(pinchViewportOrigin); | 166 m_pinchViewport->setLocation(pinchViewportOrigin); |
| 159 } | 167 } |
| 160 | 168 |
| 161 void RotationViewportAnchor::computeOrigins(const FloatSize& innerSize, | 169 void RotationViewportAnchor::computeOrigins(const FloatSize& innerSize, IntPoint
& mainFrameOffset, FloatPoint& pinchViewportOffset) const |
| 162 IntPoint& mainFrameOffset, FloatPoint& pinchViewportOffset) const | |
| 163 { | 170 { |
| 164 IntSize outerSize = m_rootFrameView.visibleContentRect().size(); | 171 IntSize outerSize = m_rootFrameView->visibleContentRect().size(); |
| 165 | 172 |
| 166 // Compute the viewport origins in CSS pixels relative to the document. | 173 // Compute the viewport origins in CSS pixels relative to the document. |
| 167 FloatSize absPinchViewportOffset = m_normalizedPinchViewportOffset; | 174 FloatSize absPinchViewportOffset = m_normalizedPinchViewportOffset; |
| 168 absPinchViewportOffset.scale(outerSize.width(), outerSize.height()); | 175 absPinchViewportOffset.scale(outerSize.width(), outerSize.height()); |
| 169 | 176 |
| 170 FloatPoint innerOrigin = getInnerOrigin(innerSize); | 177 FloatPoint innerOrigin = getInnerOrigin(innerSize); |
| 171 FloatPoint outerOrigin = innerOrigin - absPinchViewportOffset; | 178 FloatPoint outerOrigin = innerOrigin - absPinchViewportOffset; |
| 172 | 179 |
| 173 IntRect outerRect = IntRect(flooredIntPoint(outerOrigin), outerSize); | 180 IntRect outerRect = IntRect(flooredIntPoint(outerOrigin), outerSize); |
| 174 FloatRect innerRect = FloatRect(innerOrigin, innerSize); | 181 FloatRect innerRect = FloatRect(innerOrigin, innerSize); |
| 175 | 182 |
| 176 moveToEncloseRect(outerRect, innerRect); | 183 moveToEncloseRect(outerRect, innerRect); |
| 177 | 184 |
| 178 outerRect.setLocation(m_rootFrameView.adjustScrollPositionWithinRange(outerR
ect.location())); | 185 outerRect.setLocation(m_rootFrameView->adjustScrollPositionWithinRange(outer
Rect.location())); |
| 179 | 186 |
| 180 moveIntoRect(innerRect, outerRect); | 187 moveIntoRect(innerRect, outerRect); |
| 181 | 188 |
| 182 mainFrameOffset = outerRect.location(); | 189 mainFrameOffset = outerRect.location(); |
| 183 pinchViewportOffset = FloatPoint(innerRect.location() - outerRect.location()
); | 190 pinchViewportOffset = FloatPoint(innerRect.location() - outerRect.location()
); |
| 184 } | 191 } |
| 185 | 192 |
| 186 FloatPoint RotationViewportAnchor::getInnerOrigin(const FloatSize& innerSize) co
nst | 193 FloatPoint RotationViewportAnchor::getInnerOrigin(const FloatSize& innerSize) co
nst |
| 187 { | 194 { |
| 188 if (!m_anchorNode || !m_anchorNode->inDocument()) | 195 if (!m_anchorNode || !m_anchorNode->inDocument()) |
| 189 return m_pinchViewportInDocument; | 196 return m_pinchViewportInDocument; |
| 190 | 197 |
| 191 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); | 198 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); |
| 192 if (m_anchorNodeBounds == currentNodeBounds) | 199 if (m_anchorNodeBounds == currentNodeBounds) |
| 193 return m_pinchViewportInDocument; | 200 return m_pinchViewportInDocument; |
| 194 | 201 |
| 195 // Compute the new anchor point relative to the node position | 202 // Compute the new anchor point relative to the node position |
| 196 FloatSize anchorOffsetFromNode(currentNodeBounds.size()); | 203 FloatSize anchorOffsetFromNode(currentNodeBounds.size()); |
| 197 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); | 204 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); |
| 198 FloatPoint anchorPoint = FloatPoint(currentNodeBounds.location()) + anchorOf
fsetFromNode; | 205 FloatPoint anchorPoint = FloatPoint(currentNodeBounds.location()) + anchorOf
fsetFromNode; |
| 199 | 206 |
| 200 // Compute the new origin point relative to the new anchor point | 207 // Compute the new origin point relative to the new anchor point |
| 201 FloatSize anchorOffsetFromOrigin = innerSize; | 208 FloatSize anchorOffsetFromOrigin = innerSize; |
| 202 anchorOffsetFromOrigin.scale(m_anchorInInnerViewCoords.width(), m_anchorInIn
nerViewCoords.height()); | 209 anchorOffsetFromOrigin.scale(m_anchorInInnerViewCoords.width(), m_anchorInIn
nerViewCoords.height()); |
| 203 return anchorPoint - anchorOffsetFromOrigin; | 210 return anchorPoint - anchorOffsetFromOrigin; |
| 204 } | 211 } |
| 205 | 212 |
| 206 } // namespace blink | 213 } // namespace blink |
| OLD | NEW |