[Mac] Implement basic hit testing in the scrolling tree
[WebKit-https.git] / Source / WebCore / page / scrolling / ScrollingTreeScrollingNode.cpp
1 /*
2  * Copyright (C) 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ScrollingTreeScrollingNode.h"
28
29 #if ENABLE(ASYNC_SCROLLING)
30
31 #include "Logging.h"
32 #include "ScrollingStateScrollingNode.h"
33 #include "ScrollingStateTree.h"
34 #include "ScrollingTree.h"
35 #include <wtf/text/TextStream.h>
36
37 namespace WebCore {
38
39 ScrollingTreeScrollingNode::ScrollingTreeScrollingNode(ScrollingTree& scrollingTree, ScrollingNodeType nodeType, ScrollingNodeID nodeID)
40     : ScrollingTreeNode(scrollingTree, nodeType, nodeID)
41 {
42 }
43
44 ScrollingTreeScrollingNode::~ScrollingTreeScrollingNode() = default;
45
46 void ScrollingTreeScrollingNode::commitStateBeforeChildren(const ScrollingStateNode& stateNode)
47 {
48     const ScrollingStateScrollingNode& state = downcast<ScrollingStateScrollingNode>(stateNode);
49
50     if (state.hasChangedProperty(ScrollingStateScrollingNode::ScrollableAreaSize))
51         m_scrollableAreaSize = state.scrollableAreaSize();
52
53     if (state.hasChangedProperty(ScrollingStateScrollingNode::TotalContentsSize)) {
54         if (scrollingTree().isRubberBandInProgress())
55             m_totalContentsSizeForRubberBand = m_totalContentsSize;
56         else
57             m_totalContentsSizeForRubberBand = state.totalContentsSize();
58
59         m_totalContentsSize = state.totalContentsSize();
60     }
61
62     if (state.hasChangedProperty(ScrollingStateScrollingNode::ReachableContentsSize))
63         m_reachableContentsSize = state.reachableContentsSize();
64
65     if (state.hasChangedProperty(ScrollingStateScrollingNode::ScrollPosition))
66         m_lastCommittedScrollPosition = state.scrollPosition();
67
68     if (state.hasChangedProperty(ScrollingStateScrollingNode::ParentRelativeScrollableRect))
69         m_parentRelativeScrollableRect = state.parentRelativeScrollableRect();
70
71     if (state.hasChangedProperty(ScrollingStateScrollingNode::ScrollOrigin))
72         m_scrollOrigin = state.scrollOrigin();
73
74 #if ENABLE(CSS_SCROLL_SNAP)
75     if (state.hasChangedProperty(ScrollingStateScrollingNode::HorizontalSnapOffsets))
76         m_snapOffsetsInfo.horizontalSnapOffsets = state.horizontalSnapOffsets();
77
78     if (state.hasChangedProperty(ScrollingStateScrollingNode::VerticalSnapOffsets))
79         m_snapOffsetsInfo.verticalSnapOffsets = state.verticalSnapOffsets();
80
81     if (state.hasChangedProperty(ScrollingStateScrollingNode::HorizontalSnapOffsetRanges))
82         m_snapOffsetsInfo.horizontalSnapOffsetRanges = state.horizontalSnapOffsetRanges();
83
84     if (state.hasChangedProperty(ScrollingStateScrollingNode::VerticalSnapOffsetRanges))
85         m_snapOffsetsInfo.verticalSnapOffsetRanges = state.verticalSnapOffsetRanges();
86
87     if (state.hasChangedProperty(ScrollingStateScrollingNode::CurrentHorizontalSnapOffsetIndex))
88         m_currentHorizontalSnapPointIndex = state.currentHorizontalSnapPointIndex();
89
90     if (state.hasChangedProperty(ScrollingStateScrollingNode::CurrentVerticalSnapOffsetIndex))
91         m_currentVerticalSnapPointIndex = state.currentVerticalSnapPointIndex();
92 #endif
93
94     if (state.hasChangedProperty(ScrollingStateScrollingNode::ScrollableAreaParams))
95         m_scrollableAreaParameters = state.scrollableAreaParameters();
96 }
97
98 void ScrollingTreeScrollingNode::commitStateAfterChildren(const ScrollingStateNode& stateNode)
99 {
100     const ScrollingStateScrollingNode& scrollingStateNode = downcast<ScrollingStateScrollingNode>(stateNode);
101     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::RequestedScrollPosition))
102         scrollingTree().scrollingTreeNodeRequestsScroll(scrollingNodeID(), scrollingStateNode.requestedScrollPosition(), scrollingStateNode.requestedScrollPositionRepresentsProgrammaticScroll());
103 }
104
105 void ScrollingTreeScrollingNode::updateLayersAfterAncestorChange(const ScrollingTreeNode& changedNode, const FloatRect& fixedPositionRect, const FloatSize& cumulativeDelta)
106 {
107     if (!m_children)
108         return;
109
110     for (auto& child : *m_children)
111         child->updateLayersAfterAncestorChange(changedNode, fixedPositionRect, cumulativeDelta);
112 }
113
114 void ScrollingTreeScrollingNode::setScrollPosition(const FloatPoint& scrollPosition)
115 {
116     FloatPoint newScrollPosition = scrollPosition.constrainedBetween(minimumScrollPosition(), maximumScrollPosition());
117     setScrollPositionWithoutContentEdgeConstraints(newScrollPosition);
118 }
119
120 void ScrollingTreeScrollingNode::setScrollPositionWithoutContentEdgeConstraints(const FloatPoint& scrollPosition)
121 {
122     setScrollLayerPosition(scrollPosition, { });
123     scrollingTree().scrollingTreeNodeDidScroll(scrollingNodeID(), scrollPosition, WTF::nullopt);
124 }
125
126 FloatPoint ScrollingTreeScrollingNode::minimumScrollPosition() const
127 {
128     return FloatPoint();
129 }
130
131 FloatPoint ScrollingTreeScrollingNode::maximumScrollPosition() const
132 {
133     FloatPoint contentSizePoint(totalContentsSize());
134     return FloatPoint(contentSizePoint - scrollableAreaSize()).expandedTo(FloatPoint());
135 }
136
137 bool ScrollingTreeScrollingNode::scrollLimitReached(const PlatformWheelEvent& wheelEvent) const
138 {
139     FloatPoint oldScrollPosition = scrollPosition();
140     FloatPoint newScrollPosition = oldScrollPosition + FloatSize(wheelEvent.deltaX(), -wheelEvent.deltaY());
141     newScrollPosition = newScrollPosition.constrainedBetween(minimumScrollPosition(), maximumScrollPosition());
142     return newScrollPosition == oldScrollPosition;
143 }
144
145 LayoutPoint ScrollingTreeScrollingNode::parentToLocalPoint(LayoutPoint point) const
146 {
147     return point - toLayoutSize(parentRelativeScrollableRect().location());
148 }
149
150 LayoutPoint ScrollingTreeScrollingNode::localToContentsPoint(LayoutPoint point) const
151 {
152     return point + LayoutPoint(scrollPosition());
153 }
154
155 ScrollingTreeScrollingNode* ScrollingTreeScrollingNode::scrollingNodeForPoint(LayoutPoint parentPoint) const
156 {
157     if (auto* node = ScrollingTreeNode::scrollingNodeForPoint(parentPoint))
158         return node;
159
160     if (parentRelativeScrollableRect().contains(parentPoint))
161         return const_cast<ScrollingTreeScrollingNode*>(this);
162
163     return nullptr;
164 }
165
166 void ScrollingTreeScrollingNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
167 {
168     ScrollingTreeNode::dumpProperties(ts, behavior);
169     ts.dumpProperty("scrollable area size", m_scrollableAreaSize);
170     ts.dumpProperty("total content size", m_totalContentsSize);
171     if (m_totalContentsSizeForRubberBand != m_totalContentsSize)
172         ts.dumpProperty("total content size for rubber band", m_totalContentsSizeForRubberBand);
173     if (m_reachableContentsSize != m_totalContentsSize)
174         ts.dumpProperty("reachable content size", m_reachableContentsSize);
175     ts.dumpProperty("last committed scroll position", m_lastCommittedScrollPosition);
176
177     if (!m_parentRelativeScrollableRect.isEmpty())
178         ts.dumpProperty("parent relative scrollable rect", m_parentRelativeScrollableRect);
179
180     if (m_scrollOrigin != IntPoint())
181         ts.dumpProperty("scroll origin", m_scrollOrigin);
182
183 #if ENABLE(CSS_SCROLL_SNAP)
184     if (m_snapOffsetsInfo.horizontalSnapOffsets.size())
185         ts.dumpProperty("horizontal snap offsets", m_snapOffsetsInfo.horizontalSnapOffsets);
186
187     if (m_snapOffsetsInfo.verticalSnapOffsets.size())
188         ts.dumpProperty("vertical snap offsets", m_snapOffsetsInfo.verticalSnapOffsets);
189
190     if (m_currentHorizontalSnapPointIndex)
191         ts.dumpProperty("current horizontal snap point index", m_currentHorizontalSnapPointIndex);
192
193     if (m_currentVerticalSnapPointIndex)
194         ts.dumpProperty("current vertical snap point index", m_currentVerticalSnapPointIndex);
195     
196 #endif
197
198     ts.dumpProperty("scrollable area parameters", m_scrollableAreaParameters);
199 }
200
201 } // namespace WebCore
202
203 #endif // ENABLE(ASYNC_SCROLLING)