[iOS WK2] A top fixed bar can flicker when scrolling with the keyboard up
[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     if (state.hasChangedProperty(ScrollingStateFrameScrollingNode::OverrideVisualViewportSize))
90         m_overrideVisualViewportSize = state.overrideVisualViewportSize();
91 }
92
93 bool ScrollingTreeFrameScrollingNode::scrollPositionAndLayoutViewportMatch(const FloatPoint& position, Optional<FloatRect> overrideLayoutViewport)
94 {
95     return position == currentScrollPosition() && (!overrideLayoutViewport || overrideLayoutViewport.value() == m_layoutViewport);
96 }
97
98 FloatRect ScrollingTreeFrameScrollingNode::layoutViewportForScrollPosition(const FloatPoint& visibleContentOrigin, float scale) const
99 {
100     FloatSize visualViewportSize = m_overrideVisualViewportSize.valueOr(scrollableAreaSize());
101     FloatRect visibleContentRect(visibleContentOrigin, visualViewportSize);
102     LayoutRect visualViewport(FrameView::visibleDocumentRect(visibleContentRect, headerHeight(), footerHeight(), totalContentsSize(), scale));
103     LayoutRect layoutViewport(m_layoutViewport);
104
105     LOG_WITH_STREAM(Scrolling, stream << "\nScrolling thread: " << "(visibleContentOrigin " << visibleContentOrigin << ", visualViewportSize " << visualViewportSize << ") fixed behavior " << m_behaviorForFixed);
106     LOG_WITH_STREAM(Scrolling, stream << "  layoutViewport: " << layoutViewport);
107     LOG_WITH_STREAM(Scrolling, stream << "  visualViewport: " << visualViewport);
108     LOG_WITH_STREAM(Scrolling, stream << "  scroll positions: min: " << minLayoutViewportOrigin() << " max: "<< maxLayoutViewportOrigin());
109
110     LayoutPoint newLocation = FrameView::computeLayoutViewportOrigin(LayoutRect(visualViewport), LayoutPoint(minLayoutViewportOrigin()), LayoutPoint(maxLayoutViewportOrigin()), layoutViewport, m_behaviorForFixed);
111
112     if (layoutViewport.location() != newLocation) {
113         layoutViewport.setLocation(newLocation);
114         LOG_WITH_STREAM(Scrolling, stream << " new layoutViewport " << layoutViewport);
115     }
116
117     return layoutViewport;
118 }
119
120 void ScrollingTreeFrameScrollingNode::updateViewportForCurrentScrollPosition(Optional<FloatRect> overrideLayoutViewport)
121 {
122     if (overrideLayoutViewport)
123         setLayoutViewport(overrideLayoutViewport.value());
124     else
125         setLayoutViewport(layoutViewportForScrollPosition(currentScrollPosition(), frameScaleFactor()));
126 }
127
128 FloatSize ScrollingTreeFrameScrollingNode::viewToContentsOffset(const FloatPoint& scrollPosition) const
129 {
130     return toFloatSize(scrollPosition) - FloatSize(0, headerHeight() + topContentInset());
131 }
132
133 LayoutPoint ScrollingTreeFrameScrollingNode::parentToLocalPoint(LayoutPoint point) const
134 {
135     return point - LayoutSize(0, headerHeight() + topContentInset());
136 }
137
138 LayoutPoint ScrollingTreeFrameScrollingNode::localToContentsPoint(LayoutPoint point) const
139 {
140     auto scrolledPoint = point + LayoutPoint(currentScrollPosition());
141     return scrolledPoint.scaled(1 / frameScaleFactor());
142 }
143
144 void ScrollingTreeFrameScrollingNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
145 {
146     ts << "frame scrolling node";
147     ScrollingTreeScrollingNode::dumpProperties(ts, behavior);
148
149     ts.dumpProperty("layout viewport", m_layoutViewport);
150     ts.dumpProperty("min layoutViewport origin", m_minLayoutViewportOrigin);
151     ts.dumpProperty("max layoutViewport origin", m_maxLayoutViewportOrigin);
152
153     if (m_overrideVisualViewportSize)
154         ts.dumpProperty("override visual viewport size", m_overrideVisualViewportSize.value());
155
156     if (m_frameScaleFactor != 1)
157         ts.dumpProperty("frame scale factor", m_frameScaleFactor);
158     if (m_topContentInset)
159         ts.dumpProperty("top content inset", m_topContentInset);
160
161     if (m_headerHeight)
162         ts.dumpProperty("header height", m_headerHeight);
163     if (m_footerHeight)
164         ts.dumpProperty("footer height", m_footerHeight);
165     if (m_synchronousScrollingReasons)
166         ts.dumpProperty("synchronous scrolling reasons", ScrollingCoordinator::synchronousScrollingReasonsAsText(m_synchronousScrollingReasons));
167
168     ts.dumpProperty("behavior for fixed", m_behaviorForFixed);
169     if (m_fixedElementsLayoutRelativeToFrame)
170         ts.dumpProperty("fixed elements lay out relative to frame", m_fixedElementsLayoutRelativeToFrame);
171     if (m_visualViewportIsSmallerThanLayoutViewport)
172         ts.dumpProperty("visual viewport is smaller than layout viewport", m_visualViewportIsSmallerThanLayoutViewport);
173 }
174
175
176 } // namespace WebCore
177
178 #endif // ENABLE(ASYNC_SCROLLING)