| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "web/ViewportAnchor.h" | 32 #include "web/ViewportAnchor.h" |
| 33 | 33 |
| 34 #include "core/dom/ContainerNode.h" | 34 #include "core/dom/ContainerNode.h" |
| 35 #include "core/dom/Node.h" | 35 #include "core/dom/Node.h" |
| 36 #include "core/page/EventHandler.h" | 36 #include "core/page/EventHandler.h" |
| 37 #include "core/rendering/HitTestResult.h" | 37 #include "core/rendering/HitTestResult.h" |
| 38 #include "platform/scroll/ScrollView.h" | |
| 39 | 38 |
| 40 namespace blink { | 39 namespace blink { |
| 41 | 40 |
| 42 namespace { | 41 namespace { |
| 43 | 42 |
| 44 static const float viewportAnchorRelativeEpsilon = 0.1f; | 43 static const float viewportAnchorRelativeEpsilon = 0.1f; |
| 45 static const int viewportToNodeMaxRelativeArea = 2; | 44 static const int viewportToNodeMaxRelativeArea = 2; |
| 46 | 45 |
| 47 template <typename RectType> | 46 template <typename RectType> |
| 48 int area(const RectType& rect) { | 47 int area(const RectType& rect) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 62 pointOffset.scale(viewportAnchorRelativeEpsilon); | 61 pointOffset.scale(viewportAnchorRelativeEpsilon); |
| 63 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRe
quest::ReadOnly | HitTestRequest::Active).innerNode(); | 62 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRe
quest::ReadOnly | HitTestRequest::Active).innerNode(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 while (node && node->boundingBox().isEmpty()) | 65 while (node && node->boundingBox().isEmpty()) |
| 67 node = node->parentNode(); | 66 node = node->parentNode(); |
| 68 | 67 |
| 69 return node; | 68 return node; |
| 70 } | 69 } |
| 71 | 70 |
| 72 void moveToEncloseRect(IntRect& outer, const FloatRect& inner) | |
| 73 { | |
| 74 IntPoint minimumPosition = ceiledIntPoint(inner.location() + inner.size() -
FloatSize(outer.size())); | |
| 75 IntPoint maximumPosition = flooredIntPoint(inner.location()); | |
| 76 | |
| 77 IntPoint outerOrigin = outer.location(); | |
| 78 outerOrigin = outerOrigin.expandedTo(minimumPosition); | |
| 79 outerOrigin = outerOrigin.shrunkTo(maximumPosition); | |
| 80 | |
| 81 outer.setLocation(outerOrigin); | |
| 82 } | |
| 83 | |
| 84 void moveIntoRect(FloatRect& inner, const IntRect& outer) | |
| 85 { | |
| 86 FloatPoint minimumPosition = FloatPoint(outer.location()); | |
| 87 FloatPoint maximumPosition = minimumPosition + outer.size() - inner.size(); | |
| 88 | |
| 89 // Adjust maximumPosition to the nearest lower integer because | |
| 90 // PinchViewport::maximumScrollPosition() does the same. | |
| 91 // The value of minumumPosition is already adjusted since it is | |
| 92 // constructed from an integer point. | |
| 93 maximumPosition = flooredIntPoint(maximumPosition); | |
| 94 | |
| 95 FloatPoint innerOrigin = inner.location(); | |
| 96 innerOrigin = innerOrigin.expandedTo(minimumPosition); | |
| 97 innerOrigin = innerOrigin.shrunkTo(maximumPosition); | |
| 98 | |
| 99 inner.setLocation(innerOrigin); | |
| 100 } | |
| 101 | |
| 102 } // namespace | 71 } // namespace |
| 103 | 72 |
| 104 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) | 73 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) |
| 105 : m_eventHandler(eventHandler) { } | 74 : m_eventHandler(eventHandler) { } |
| 106 | 75 |
| 107 void ViewportAnchor::setAnchor(const IntRect& outerViewRect, const IntRect& inne
rViewRect, | 76 void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorI
nViewCoords) |
| 108 const FloatSize& anchorInInnerViewCoords) | |
| 109 { | 77 { |
| 110 // Preserve the inner viewport position in document in case we won't find th
e anchor | 78 m_viewRect = viewRect; |
| 111 m_pinchViewportInDocument = innerViewRect.location(); | |
| 112 | |
| 113 m_anchorNode.clear(); | 79 m_anchorNode.clear(); |
| 114 m_anchorNodeBounds = LayoutRect(); | 80 m_anchorNodeBounds = LayoutRect(); |
| 115 m_anchorInNodeCoords = FloatSize(); | 81 m_anchorInNodeCoords = FloatSize(); |
| 116 m_anchorInInnerViewCoords = anchorInInnerViewCoords; | 82 m_anchorInViewCoords = anchorInViewCoords; |
| 117 m_normalizedPinchViewportOffset = FloatSize(); | |
| 118 | 83 |
| 119 if (innerViewRect.isEmpty()) | 84 if (viewRect.isEmpty()) |
| 120 return; | 85 return; |
| 121 | 86 |
| 122 // Preserve origins at the absolute screen origin | 87 // Preserve origins at the absolute screen origin |
| 123 if (innerViewRect.location() == IntPoint::zero()) | 88 if (viewRect.location() == IntPoint::zero()) |
| 124 return; | 89 return; |
| 125 | 90 |
| 126 // Inner rectangle should be within the outer one. | 91 FloatSize anchorOffset = viewRect.size(); |
| 127 ASSERT(outerViewRect.contains(innerViewRect)); | 92 anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height()); |
| 93 const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffse
t; |
| 128 | 94 |
| 129 // Outer rectangle is used as a scale, we need positive width and height. | 95 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect,
m_eventHandler); |
| 130 ASSERT(!outerViewRect.isEmpty()); | |
| 131 | |
| 132 m_normalizedPinchViewportOffset = innerViewRect.location() - outerViewRect.l
ocation(); | |
| 133 | |
| 134 // Normalize by the size of the outer rect | |
| 135 m_normalizedPinchViewportOffset.scale(1.0 / outerViewRect.width(), 1.0 / out
erViewRect.height()); | |
| 136 | |
| 137 FloatSize anchorOffset = innerViewRect.size(); | |
| 138 anchorOffset.scale(anchorInInnerViewCoords.width(), anchorInInnerViewCoords.
height()); | |
| 139 const FloatPoint anchorPoint = FloatPoint(innerViewRect.location()) + anchor
Offset; | |
| 140 | |
| 141 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), innerViewR
ect, m_eventHandler); | |
| 142 if (!node) | 96 if (!node) |
| 143 return; | 97 return; |
| 144 | 98 |
| 145 m_anchorNode = node; | 99 m_anchorNode = node; |
| 146 m_anchorNodeBounds = node->boundingBox(); | 100 m_anchorNodeBounds = node->boundingBox(); |
| 147 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location(); | 101 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location(); |
| 148 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); | 102 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); |
| 149 } | 103 } |
| 150 | 104 |
| 151 void ViewportAnchor::computeOrigins(const ScrollView& scrollView, const FloatSiz
e& innerSize, | 105 IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const |
| 152 IntPoint& mainFrameOffset, FloatPoint& pinchViewportOffset) const | |
| 153 { | |
| 154 IntSize outerSize = scrollView.visibleContentRect().size(); | |
| 155 | |
| 156 // Compute the viewport origins in CSS pixels relative to the document. | |
| 157 FloatSize absPinchViewportOffset = m_normalizedPinchViewportOffset; | |
| 158 absPinchViewportOffset.scale(outerSize.width(), outerSize.height()); | |
| 159 | |
| 160 FloatPoint innerOrigin = getInnerOrigin(innerSize); | |
| 161 FloatPoint outerOrigin = innerOrigin - absPinchViewportOffset; | |
| 162 | |
| 163 IntRect outerRect = IntRect(flooredIntPoint(outerOrigin), outerSize); | |
| 164 FloatRect innerRect = FloatRect(innerOrigin, innerSize); | |
| 165 | |
| 166 moveToEncloseRect(outerRect, innerRect); | |
| 167 | |
| 168 outerRect.setLocation(scrollView.adjustScrollPositionWithinRange(outerRect.l
ocation())); | |
| 169 | |
| 170 moveIntoRect(innerRect, outerRect); | |
| 171 | |
| 172 mainFrameOffset = outerRect.location(); | |
| 173 pinchViewportOffset = FloatPoint(innerRect.location() - outerRect.location()
); | |
| 174 } | |
| 175 | |
| 176 FloatPoint ViewportAnchor::getInnerOrigin(const FloatSize& innerSize) const | |
| 177 { | 106 { |
| 178 if (!m_anchorNode || !m_anchorNode->inDocument()) | 107 if (!m_anchorNode || !m_anchorNode->inDocument()) |
| 179 return m_pinchViewportInDocument; | 108 return m_viewRect.location(); |
| 180 | 109 |
| 181 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); | 110 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); |
| 182 if (m_anchorNodeBounds == currentNodeBounds) | 111 if (m_anchorNodeBounds == currentNodeBounds) |
| 183 return m_pinchViewportInDocument; | 112 return m_viewRect.location(); |
| 184 | 113 |
| 185 // Compute the new anchor point relative to the node position | 114 // Compute the new anchor point relative to the node position |
| 186 FloatSize anchorOffsetFromNode = currentNodeBounds.size(); | 115 FloatSize anchorOffsetFromNode = currentNodeBounds.size(); |
| 187 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); | 116 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); |
| 188 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode
; | 117 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode
; |
| 189 | 118 |
| 190 // Compute the new origin point relative to the new anchor point | 119 // Compute the new origin point relative to the new anchor point |
| 191 FloatSize anchorOffsetFromOrigin = innerSize; | 120 FloatSize anchorOffsetFromOrigin = currentViewSize; |
| 192 anchorOffsetFromOrigin.scale(m_anchorInInnerViewCoords.width(), m_anchorInIn
nerViewCoords.height()); | 121 anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoo
rds.height()); |
| 193 return anchorPoint - anchorOffsetFromOrigin; | 122 return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin); |
| 194 } | 123 } |
| 195 | 124 |
| 196 } // namespace blink | 125 } // namespace blink |
| OLD | NEW |