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