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#pragma once
27
28#if ENABLE(ASYNC_SCROLLING)
29
30#include "IntRect.h"
31#include "ScrollSnapOffsetsInfo.h"
32#include "ScrollTypes.h"
33#include "ScrollingCoordinator.h"
34#include "ScrollingTreeNode.h"
35
36namespace WebCore {
37
38class ScrollingTree;
39class ScrollingStateScrollingNode;
40
41class WEBCORE_EXPORT ScrollingTreeScrollingNode : public ScrollingTreeNode {
42 friend class ScrollingTreeScrollingNodeDelegate;
43#if PLATFORM(MAC)
44 friend class ScrollingTreeScrollingNodeDelegateMac;
45#endif
46 friend class ScrollingTree;
47
48public:
49 virtual ~ScrollingTreeScrollingNode();
50
51 void commitStateBeforeChildren(const ScrollingStateNode&) override;
52 void commitStateAfterChildren(const ScrollingStateNode&) override;
53
54 virtual ScrollingEventResult handleWheelEvent(const PlatformWheelEvent&);
55
56 FloatPoint currentScrollPosition() const { return m_currentScrollPosition; }
57 FloatPoint lastCommittedScrollPosition() const { return m_lastCommittedScrollPosition; }
58 FloatSize scrollDeltaSinceLastCommit() const { return m_currentScrollPosition - m_lastCommittedScrollPosition; }
59
60 // These are imperative; they adjust the scrolling layers.
61 void scrollTo(const FloatPoint&, ScrollType = ScrollType::User, ScrollPositionClamp = ScrollPositionClamp::ToContentEdges);
62 void scrollBy(const FloatSize&, ScrollPositionClamp = ScrollPositionClamp::ToContentEdges);
63
64 void wasScrolledByDelegatedScrolling(const FloatPoint& position, Optional<FloatRect> overrideLayoutViewport = { });
65
66 const FloatSize& scrollableAreaSize() const { return m_scrollableAreaSize; }
67 const FloatSize& totalContentsSize() const { return m_totalContentsSize; }
68
69 bool horizontalScrollbarHiddenByStyle() const { return m_scrollableAreaParameters.horizontalScrollbarHiddenByStyle; }
70 bool verticalScrollbarHiddenByStyle() const { return m_scrollableAreaParameters.verticalScrollbarHiddenByStyle; }
71 bool canHaveScrollbars() const { return m_scrollableAreaParameters.horizontalScrollbarMode != ScrollbarAlwaysOff || m_scrollableAreaParameters.verticalScrollbarMode != ScrollbarAlwaysOff; }
72
73#if ENABLE(CSS_SCROLL_SNAP)
74 const Vector<float>& horizontalSnapOffsets() const { return m_snapOffsetsInfo.horizontalSnapOffsets; }
75 const Vector<float>& verticalSnapOffsets() const { return m_snapOffsetsInfo.verticalSnapOffsets; }
76 const Vector<ScrollOffsetRange<float>>& horizontalSnapOffsetRanges() const { return m_snapOffsetsInfo.horizontalSnapOffsetRanges; }
77 const Vector<ScrollOffsetRange<float>>& verticalSnapOffsetRanges() const { return m_snapOffsetsInfo.verticalSnapOffsetRanges; }
78 unsigned currentHorizontalSnapPointIndex() const { return m_currentHorizontalSnapPointIndex; }
79 unsigned currentVerticalSnapPointIndex() const { return m_currentVerticalSnapPointIndex; }
80 void setCurrentHorizontalSnapPointIndex(unsigned index) { m_currentHorizontalSnapPointIndex = index; }
81 void setCurrentVerticalSnapPointIndex(unsigned index) { m_currentVerticalSnapPointIndex = index; }
82#endif
83
84 bool useDarkAppearanceForScrollbars() const { return m_scrollableAreaParameters.useDarkAppearanceForScrollbars; }
85
86 bool scrollLimitReached(const PlatformWheelEvent&) const;
87 ScrollingTreeScrollingNode* scrollingNodeForPoint(LayoutPoint) const override;
88
89#if PLATFORM(COCOA)
90 CALayer *scrollContainerLayer() const { return m_scrollContainerLayer.get(); }
91 CALayer *scrolledContentsLayer() const { return m_scrolledContentsLayer.get(); }
92#endif
93
94protected:
95 ScrollingTreeScrollingNode(ScrollingTree&, ScrollingNodeType, ScrollingNodeID);
96
97 virtual FloatPoint minimumScrollPosition() const;
98 virtual FloatPoint maximumScrollPosition() const;
99
100 FloatPoint clampScrollPosition(const FloatPoint&) const;
101
102 virtual FloatPoint adjustedScrollPosition(const FloatPoint&, ScrollPositionClamp = ScrollPositionClamp::ToContentEdges) const;
103
104 virtual void currentScrollPositionChanged();
105 virtual void updateViewportForCurrentScrollPosition(Optional<FloatRect> = { }) { }
106 virtual bool scrollPositionAndLayoutViewportMatch(const FloatPoint& position, Optional<FloatRect> overrideLayoutViewport);
107
108 virtual void repositionScrollingLayers() { }
109 virtual void repositionRelatedLayers() { }
110
111 void applyLayerPositions() override;
112
113 const FloatSize& reachableContentsSize() const { return m_reachableContentsSize; }
114 const LayoutRect& parentRelativeScrollableRect() const { return m_parentRelativeScrollableRect; }
115 const IntPoint& scrollOrigin() const { return m_scrollOrigin; }
116
117 // If the totalContentsSize changes in the middle of a rubber-band, we still want to use the old totalContentsSize for the sake of
118 // computing the stretchAmount(). Using the old value will keep the animation smooth. When there is no rubber-band in progress at
119 // all, m_totalContentsSizeForRubberBand should be equivalent to m_totalContentsSize.
120 const FloatSize& totalContentsSizeForRubberBand() const { return m_totalContentsSizeForRubberBand; }
121 void setTotalContentsSizeForRubberBand(const FloatSize& totalContentsSizeForRubberBand) { m_totalContentsSizeForRubberBand = totalContentsSizeForRubberBand; }
122
123 ScrollElasticity horizontalScrollElasticity() const { return m_scrollableAreaParameters.horizontalScrollElasticity; }
124 ScrollElasticity verticalScrollElasticity() const { return m_scrollableAreaParameters.verticalScrollElasticity; }
125
126 bool hasEnabledHorizontalScrollbar() const { return m_scrollableAreaParameters.hasEnabledHorizontalScrollbar; }
127 bool hasEnabledVerticalScrollbar() const { return m_scrollableAreaParameters.hasEnabledVerticalScrollbar; }
128
129 bool expectsWheelEventTestTrigger() const { return m_expectsWheelEventTestTrigger; }
130
131 LayoutPoint parentToLocalPoint(LayoutPoint) const override;
132 LayoutPoint localToContentsPoint(LayoutPoint) const override;
133
134 void dumpProperties(WTF::TextStream&, ScrollingStateTreeAsTextBehavior) const override;
135
136private:
137 FloatSize m_scrollableAreaSize;
138 FloatSize m_totalContentsSize;
139 FloatSize m_totalContentsSizeForRubberBand;
140 FloatSize m_reachableContentsSize;
141 FloatPoint m_lastCommittedScrollPosition;
142 LayoutRect m_parentRelativeScrollableRect;
143 FloatPoint m_currentScrollPosition;
144 IntPoint m_scrollOrigin;
145#if ENABLE(CSS_SCROLL_SNAP)
146 ScrollSnapOffsetsInfo<float> m_snapOffsetsInfo;
147 unsigned m_currentHorizontalSnapPointIndex { 0 };
148 unsigned m_currentVerticalSnapPointIndex { 0 };
149#endif
150 ScrollableAreaParameters m_scrollableAreaParameters;
151 bool m_expectsWheelEventTestTrigger { false };
152
153#if PLATFORM(COCOA)
154 RetainPtr<CALayer> m_scrollContainerLayer;
155 RetainPtr<CALayer> m_scrolledContentsLayer;
156#endif
157};
158
159} // namespace WebCore
160
161SPECIALIZE_TYPE_TRAITS_SCROLLING_NODE(ScrollingTreeScrollingNode, isScrollingNode())
162
163#endif // ENABLE(ASYNC_SCROLLING)
164