[Mac] Implement basic hit testing in the scrolling tree
[WebKit-https.git] / Source / WebCore / page / scrolling / ScrollingTreeFrameScrollingNode.cpp
1 /*
2  * Copyright (C) 2014 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 "ScrollingTreeFrameScrollingNode.h"
28
29 #if ENABLE(ASYNC_SCROLLING)
30
31 #include "FrameView.h"
32 #include "Logging.h"
33 #include "ScrollingStateFrameScrollingNode.h"
34 #include "ScrollingStateTree.h"
35 #include "ScrollingTree.h"
36 #include <wtf/text/TextStream.h>
37
38 namespace WebCore {
39
40 ScrollingTreeFrameScrollingNode::ScrollingTreeFrameScrollingNode(ScrollingTree& scrollingTree, ScrollingNodeType nodeType, ScrollingNodeID nodeID)
41     : ScrollingTreeScrollingNode(scrollingTree, nodeType, nodeID)
42 {
43     ASSERT(isFrameScrollingNode());
44 }
45
46 ScrollingTreeFrameScrollingNode::~ScrollingTreeFrameScrollingNode() = default;
47
48 void ScrollingTreeFrameScrollingNode::commitStateBeforeChildren(const ScrollingStateNode& stateNode)
49 {
50     ScrollingTreeScrollingNode::commitStateBeforeChildren(stateNode);
51     
52     const ScrollingStateFrameScrollingNode& state = downcast<ScrollingStateFrameScrollingNode>(stateNode);
53
54     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::FrameScaleFactor))
55         m_frameScaleFactor = state.frameScaleFactor();
56
57     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::ReasonsForSynchronousScrolling))
58         m_synchronousScrollingReasons = state.synchronousScrollingReasons();
59
60     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::HeaderHeight))
61         m_headerHeight = state.headerHeight();
62
63     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::FooterHeight))
64         m_footerHeight = state.footerHeight();
65
66     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::BehaviorForFixedElements))
67         m_behaviorForFixed = state.scrollBehaviorForFixedElements();
68
69     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::TopContentInset))
70         m_topContentInset = state.topContentInset();
71
72     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::FixedElementsLayoutRelativeToFrame))
73         m_fixedElementsLayoutRelativeToFrame = state.fixedElementsLayoutRelativeToFrame();
74
75     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::LayoutViewport))
76         m_layoutViewport = state.layoutViewport();
77
78     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::MinLayoutViewportOrigin))
79         m_minLayoutViewportOrigin = state.minLayoutViewportOrigin();
80
81     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::MaxLayoutViewportOrigin))
82         m_maxLayoutViewportOrigin = state.maxLayoutViewportOrigin();
83 }
84
85 void ScrollingTreeFrameScrollingNode::scrollBy(const FloatSize& delta)
86 {
87     setScrollPosition(scrollPosition() + delta);
88 }
89
90 void ScrollingTreeFrameScrollingNode::scrollByWithoutContentEdgeConstraints(const FloatSize& offset)
91 {
92     setScrollPositionWithoutContentEdgeConstraints(scrollPosition() + offset);
93 }
94
95 void ScrollingTreeFrameScrollingNode::setScrollPosition(const FloatPoint& scrollPosition)
96 {
97     FloatPoint newScrollPosition = scrollPosition.constrainedBetween(minimumScrollPosition(), maximumScrollPosition());
98     setScrollPositionWithoutContentEdgeConstraints(newScrollPosition);
99 }
100
101 FloatRect ScrollingTreeFrameScrollingNode::layoutViewportForScrollPosition(const FloatPoint& visibleContentOrigin, float scale) const
102 {
103     ASSERT(scrollingTree().visualViewportEnabled());
104
105     FloatRect visibleContentRect(visibleContentOrigin, scrollableAreaSize());
106     LayoutRect visualViewport(FrameView::visibleDocumentRect(visibleContentRect, headerHeight(), footerHeight(), totalContentsSize(), scale));
107     LayoutRect layoutViewport(m_layoutViewport);
108
109     LOG_WITH_STREAM(Scrolling, stream << "\nScrolling thread: " << "(visibleContentOrigin " << visibleContentOrigin << ") fixed behavior " << m_behaviorForFixed);
110     LOG_WITH_STREAM(Scrolling, stream << "  layoutViewport: " << layoutViewport);
111     LOG_WITH_STREAM(Scrolling, stream << "  visualViewport: " << visualViewport);
112     LOG_WITH_STREAM(Scrolling, stream << "  scroll positions: min: " << minLayoutViewportOrigin() << " max: "<< maxLayoutViewportOrigin());
113
114     LayoutPoint newLocation = FrameView::computeLayoutViewportOrigin(LayoutRect(visualViewport), LayoutPoint(minLayoutViewportOrigin()), LayoutPoint(maxLayoutViewportOrigin()), layoutViewport, m_behaviorForFixed);
115
116     if (layoutViewport.location() != newLocation) {
117         layoutViewport.setLocation(newLocation);
118         LOG_WITH_STREAM(Scrolling, stream << " new layoutViewport " << layoutViewport);
119     }
120
121     return layoutViewport;
122 }
123
124 FloatSize ScrollingTreeFrameScrollingNode::viewToContentsOffset(const FloatPoint& scrollPosition) const
125 {
126     return toFloatSize(scrollPosition) - FloatSize(0, headerHeight() + topContentInset());
127 }
128
129 LayoutPoint ScrollingTreeFrameScrollingNode::parentToLocalPoint(LayoutPoint point) const
130 {
131     return point - LayoutSize(0, headerHeight() + topContentInset());
132 }
133
134 LayoutPoint ScrollingTreeFrameScrollingNode::localToContentsPoint(LayoutPoint point) const
135 {
136     auto scrolledPoint = point + LayoutPoint(scrollPosition());
137     return scrolledPoint.scaled(1 / frameScaleFactor());
138 }
139
140 void ScrollingTreeFrameScrollingNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
141 {
142     ts << "frame scrolling node";
143     ScrollingTreeScrollingNode::dumpProperties(ts, behavior);
144
145     ts.dumpProperty("layout viewport", m_layoutViewport);
146     ts.dumpProperty("min layoutViewport origin", m_minLayoutViewportOrigin);
147     ts.dumpProperty("max layoutViewport origin", m_maxLayoutViewportOrigin);
148
149     if (m_frameScaleFactor != 1)
150         ts.dumpProperty("frame scale factor", m_frameScaleFactor);
151     if (m_topContentInset)
152         ts.dumpProperty("top content inset", m_topContentInset);
153
154     if (m_headerHeight)
155         ts.dumpProperty("header height", m_headerHeight);
156     if (m_footerHeight)
157         ts.dumpProperty("footer height", m_footerHeight);
158     if (m_synchronousScrollingReasons)
159         ts.dumpProperty("synchronous scrolling reasons", ScrollingCoordinator::synchronousScrollingReasonsAsText(m_synchronousScrollingReasons));
160
161     ts.dumpProperty("behavior for fixed", m_behaviorForFixed);
162     if (m_fixedElementsLayoutRelativeToFrame)
163         ts.dumpProperty("fixed elements lay out relative to frame", m_fixedElementsLayoutRelativeToFrame);
164 }
165
166
167 } // namespace WebCore
168
169 #endif // ENABLE(ASYNC_SCROLLING)