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" |
38 | 39 |
39 namespace blink { | 40 namespace blink { |
40 | 41 |
41 namespace { | 42 namespace { |
42 | 43 |
43 static const float viewportAnchorRelativeEpsilon = 0.1f; | 44 static const float viewportAnchorRelativeEpsilon = 0.1f; |
44 static const int viewportToNodeMaxRelativeArea = 2; | 45 static const int viewportToNodeMaxRelativeArea = 2; |
45 | 46 |
46 template <typename RectType> | 47 template <typename RectType> |
47 int area(const RectType& rect) { | 48 int area(const RectType& rect) { |
(...skipping 13 matching lines...) Expand all Loading... |
61 pointOffset.scale(viewportAnchorRelativeEpsilon); | 62 pointOffset.scale(viewportAnchorRelativeEpsilon); |
62 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRe
quest::ReadOnly | HitTestRequest::Active).innerNode(); | 63 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRe
quest::ReadOnly | HitTestRequest::Active).innerNode(); |
63 } | 64 } |
64 | 65 |
65 while (node && node->boundingBox().isEmpty()) | 66 while (node && node->boundingBox().isEmpty()) |
66 node = node->parentNode(); | 67 node = node->parentNode(); |
67 | 68 |
68 return node; | 69 return node; |
69 } | 70 } |
70 | 71 |
| 72 void moveToEncloseRect(IntRect * outer, const FloatRect & inner) |
| 73 { |
| 74 ASSERT(outer); |
| 75 |
| 76 IntPoint minimumPosition = ceiledIntPoint(inner.location() + inner.size() -
FloatSize(outer->size())); |
| 77 IntPoint maximumPosition = flooredIntPoint(inner.location()); |
| 78 |
| 79 IntPoint outerOrigin = outer->location(); |
| 80 outerOrigin = outerOrigin.expandedTo(minimumPosition); |
| 81 outerOrigin = outerOrigin.shrunkTo(maximumPosition); |
| 82 |
| 83 outer->setLocation(outerOrigin); |
| 84 } |
| 85 |
| 86 void moveIntoRect(FloatRect * inner, const IntRect & outer) |
| 87 { |
| 88 ASSERT(inner); |
| 89 |
| 90 FloatPoint minimumPosition = FloatPoint(outer.location()); |
| 91 FloatPoint maximumPosition = minimumPosition + outer.size() - inner->size(); |
| 92 |
| 93 // Adjust maximumPosition to the nearest lower integer because |
| 94 // PinchViewport::maximumScrollPosition() does the same. |
| 95 // The value of minumumPosition is already adjusted since it is |
| 96 // constructed from an integer point. |
| 97 maximumPosition = flooredIntPoint(maximumPosition); |
| 98 |
| 99 FloatPoint innerOrigin = inner->location(); |
| 100 innerOrigin = innerOrigin.expandedTo(minimumPosition); |
| 101 innerOrigin = innerOrigin.shrunkTo(maximumPosition); |
| 102 |
| 103 inner->setLocation(innerOrigin); |
| 104 } |
| 105 |
71 } // namespace | 106 } // namespace |
72 | 107 |
73 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) | 108 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) |
74 : m_eventHandler(eventHandler) { } | 109 : m_eventHandler(eventHandler) { } |
75 | 110 |
76 void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorI
nViewCoords) | 111 void ViewportAnchor::setAnchor(const IntRect& outerViewRect, const IntRect& inne
rViewRect, |
| 112 const FloatSize& anchorInInnerViewCoords) |
77 { | 113 { |
78 m_viewRect = viewRect; | 114 // Preserve the inner viewport position in document in case we won't find th
e anchor |
| 115 m_pinchViewportInDocument = innerViewRect.location(); |
| 116 |
79 m_anchorNode.clear(); | 117 m_anchorNode.clear(); |
80 m_anchorNodeBounds = LayoutRect(); | 118 m_anchorNodeBounds = LayoutRect(); |
81 m_anchorInNodeCoords = FloatSize(); | 119 m_anchorInNodeCoords = FloatSize(); |
82 m_anchorInViewCoords = anchorInViewCoords; | 120 m_anchorInInnerViewCoords = anchorInInnerViewCoords; |
| 121 m_normalizedPinchViewportOffset = FloatSize(); |
83 | 122 |
84 if (viewRect.isEmpty()) | 123 if (innerViewRect.isEmpty()) |
85 return; | 124 return; |
86 | 125 |
87 // Preserve origins at the absolute screen origin | 126 // Preserve origins at the absolute screen origin |
88 if (viewRect.location() == IntPoint::zero()) | 127 if (innerViewRect.location() == IntPoint::zero()) |
89 return; | 128 return; |
90 | 129 |
91 FloatSize anchorOffset = viewRect.size(); | 130 // Inner rectangle should be within the outer one. |
92 anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height()); | 131 ASSERT(outerViewRect.contains(innerViewRect)); |
93 const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffse
t; | |
94 | 132 |
95 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect,
m_eventHandler); | 133 // Outer rectangle is used as a scale, we need positive width and height. |
| 134 ASSERT(!outerViewRect.isEmpty()); |
| 135 |
| 136 m_normalizedPinchViewportOffset = innerViewRect.location() - outerViewRect.l
ocation(); |
| 137 |
| 138 // Normalize by the size of the outer rect |
| 139 m_normalizedPinchViewportOffset.scale(1.0 / outerViewRect.width(), 1.0 / out
erViewRect.height()); |
| 140 |
| 141 FloatSize anchorOffset = innerViewRect.size(); |
| 142 anchorOffset.scale(anchorInInnerViewCoords.width(), anchorInInnerViewCoords.
height()); |
| 143 const FloatPoint anchorPoint = FloatPoint(innerViewRect.location()) + anchor
Offset; |
| 144 |
| 145 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), innerViewR
ect, m_eventHandler); |
96 if (!node) | 146 if (!node) |
97 return; | 147 return; |
98 | 148 |
99 m_anchorNode = node; | 149 m_anchorNode = node; |
100 m_anchorNodeBounds = node->boundingBox(); | 150 m_anchorNodeBounds = node->boundingBox(); |
101 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location(); | 151 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location(); |
102 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); | 152 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); |
103 } | 153 } |
104 | 154 |
105 IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const | 155 void ViewportAnchor::computeOrigins(const ScrollView& frameView, const FloatSize
& innerSize, |
| 156 IntPoint* mainFrameOffset, FloatPoint* pinchViewportOffset) const |
| 157 { |
| 158 IntSize outerSize = frameView.visibleContentRect().size(); |
| 159 |
| 160 // Compute the viewport origins in CSS pixels relative to the document. |
| 161 FloatSize absPinchViewportOffset = m_normalizedPinchViewportOffset; |
| 162 absPinchViewportOffset.scale(outerSize.width(), outerSize.height()); |
| 163 |
| 164 FloatPoint innerOrigin = getInnerOrigin(innerSize); |
| 165 FloatPoint outerOrigin = innerOrigin - absPinchViewportOffset; |
| 166 |
| 167 // Create the outer and inner rectangles that represent the viewports. |
| 168 IntRect outerRect = IntRect(flooredIntPoint(outerOrigin), outerSize); |
| 169 FloatRect innerRect = FloatRect(innerOrigin, innerSize); |
| 170 |
| 171 // Move outer viewport to enclose inner viewport. |
| 172 moveToEncloseRect(&outerRect, innerRect); |
| 173 |
| 174 // Move outer viewport again to fit the scroll view constraints. |
| 175 outerRect.setLocation(frameView.adjustScrollPositionWithinRange(outerRect.lo
cation())); |
| 176 |
| 177 // Push inner viewport inside the outer. |
| 178 moveIntoRect(&innerRect, outerRect); |
| 179 |
| 180 // Return the final results. |
| 181 *mainFrameOffset = outerRect.location(); |
| 182 *pinchViewportOffset = FloatPoint(innerRect.location() - outerRect.location(
)); |
| 183 } |
| 184 |
| 185 FloatPoint ViewportAnchor::getInnerOrigin(const FloatSize& innerSize) const |
106 { | 186 { |
107 if (!m_anchorNode || !m_anchorNode->inDocument()) | 187 if (!m_anchorNode || !m_anchorNode->inDocument()) |
108 return m_viewRect.location(); | 188 return m_pinchViewportInDocument; |
109 | 189 |
110 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); | 190 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); |
111 if (m_anchorNodeBounds == currentNodeBounds) | 191 if (m_anchorNodeBounds == currentNodeBounds) |
112 return m_viewRect.location(); | 192 return m_pinchViewportInDocument; |
113 | 193 |
114 // Compute the new anchor point relative to the node position | 194 // Compute the new anchor point relative to the node position |
115 FloatSize anchorOffsetFromNode = currentNodeBounds.size(); | 195 FloatSize anchorOffsetFromNode = currentNodeBounds.size(); |
116 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); | 196 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); |
117 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode
; | 197 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode
; |
118 | 198 |
119 // Compute the new origin point relative to the new anchor point | 199 // Compute the new origin point relative to the new anchor point |
120 FloatSize anchorOffsetFromOrigin = currentViewSize; | 200 FloatSize anchorOffsetFromOrigin = innerSize; |
121 anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoo
rds.height()); | 201 anchorOffsetFromOrigin.scale(m_anchorInInnerViewCoords.width(), m_anchorInIn
nerViewCoords.height()); |
122 return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin); | 202 return anchorPoint - anchorOffsetFromOrigin; |
123 } | 203 } |
124 | 204 |
125 } // namespace blink | 205 } // namespace blink |
OLD | NEW |