4c695f9d829915210f9dc1ba280e350a5d22ab40
[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::VisualViewportIsSmallerThanLayoutViewport))
73         m_visualViewportIsSmallerThanLayoutViewport = state.visualViewportIsSmallerThanLayoutViewport();
74
75     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::FixedElementsLayoutRelativeToFrame))
76         m_fixedElementsLayoutRelativeToFrame = state.fixedElementsLayoutRelativeToFrame();
77
78     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::LayoutViewport)) {
79         m_layoutViewport = state.layoutViewport();
80         updateViewportForCurrentScrollPosition({ });
81     }
82
83     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::MinLayoutViewportOrigin))
84         m_minLayoutViewportOrigin = state.minLayoutViewportOrigin();
85
86     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::MaxLayoutViewportOrigin))
87         m_maxLayoutViewportOrigin = state.maxLayoutViewportOrigin();
88 }
89
90 bool ScrollingTreeFrameScrollingNode::scrollPositionAndLayoutViewportMatch(const FloatPoint& position, Optional<FloatRect> overrideLayoutViewport)
91 {
92     return position == currentScrollPosition() && (!overrideLayoutViewport || overrideLayoutViewport.value() == m_layoutViewport);
93 }
94
95 FloatRect ScrollingTreeFrameScrollingNode::layoutViewportForScrollPosition(const FloatPoint& visibleContentOrigin, float scale) const
96 {
97     FloatRect visibleContentRect(visibleContentOrigin, scrollableAreaSize());
98     LayoutRect visualViewport(FrameView::visibleDocumentRect(visibleContentRect, headerHeight(), footerHeight(), totalContentsSize(), scale));
99     LayoutRect layoutViewport(m_layoutViewport);
100
101     LOG_WITH_STREAM(Scrolling, stream << "\nScrolling thread: " << "(visibleContentOrigin " << visibleContentOrigin << ") fixed behavior " << m_behaviorForFixed);
102     LOG_WITH_STREAM(Scrolling, stream << "  layoutViewport: " << layoutViewport);
103     LOG_WITH_STREAM(Scrolling, stream << "  visualViewport: " << visualViewport);
104     LOG_WITH_STREAM(Scrolling, stream << "  scroll positions: min: " << minLayoutViewportOrigin() << " max: "<< maxLayoutViewportOrigin());
105
106     LayoutPoint newLocation = FrameView::computeLayoutViewportOrigin(LayoutRect(visualViewport), LayoutPoint(minLayoutViewportOrigin()), LayoutPoint(maxLayoutViewportOrigin()), layoutViewport, m_behaviorForFixed);
107
108     if (layoutViewport.location() != newLocation) {
109         layoutViewport.setLocation(newLocation);
110         LOG_WITH_STREAM(Scrolling, stream << " new layoutViewport " << layoutViewport);
111     }
112
113     return layoutViewport;
114 }
115
116 void ScrollingTreeFrameScrollingNode::updateViewportForCurrentScrollPosition(Optional<FloatRect> overrideLayoutViewport)
117 {
118     if (overrideLayoutViewport)
119         setLayoutViewport(overrideLayoutViewport.value());
120     else
121         setLayoutViewport(layoutViewportForScrollPosition(currentScrollPosition(), frameScaleFactor()));
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(currentScrollPosition());
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     if (m_visualViewportIsSmallerThanLayoutViewport)
165         ts.dumpProperty("visual viewport is smaller than layout viewport", m_visualViewportIsSmallerThanLayoutViewport);
166 }
167
168
169 } // namespace WebCore
170
171 #endif // ENABLE(ASYNC_SCROLLING)