1/*
2 * Copyright (c) 2010, Google Inc. All rights reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#pragma once
33
34#include "FloatPoint.h"
35#include "PlatformWheelEvent.h"
36#include "ScrollTypes.h"
37#include "WheelEventTestTrigger.h"
38#include <wtf/FastMalloc.h>
39#include <wtf/Forward.h>
40
41#if ENABLE(RUBBER_BANDING) || ENABLE(CSS_SCROLL_SNAP)
42#include "ScrollController.h"
43#endif
44
45namespace WebCore {
46
47class FloatPoint;
48class PlatformTouchEvent;
49class ScrollableArea;
50class Scrollbar;
51class WheelEventTestTrigger;
52
53#if ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)
54class ScrollAnimator : private ScrollControllerClient {
55#else
56class ScrollAnimator {
57#endif
58 WTF_MAKE_FAST_ALLOCATED;
59public:
60 static std::unique_ptr<ScrollAnimator> create(ScrollableArea&);
61
62 explicit ScrollAnimator(ScrollableArea&);
63 virtual ~ScrollAnimator();
64
65 // Computes a scroll destination for the given parameters. Returns false if
66 // already at the destination. Otherwise, starts scrolling towards the
67 // destination and returns true. Scrolling may be immediate or animated.
68 // The base class implementation always scrolls immediately, never animates.
69 virtual bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier);
70
71 virtual void scrollToOffsetWithoutAnimation(const FloatPoint&, ScrollClamping = ScrollClamping::Clamped);
72
73 ScrollableArea& scrollableArea() const { return m_scrollableArea; }
74
75 virtual bool handleWheelEvent(const PlatformWheelEvent&);
76
77#if ENABLE(TOUCH_EVENTS)
78 virtual bool handleTouchEvent(const PlatformTouchEvent&);
79#endif
80
81#if PLATFORM(COCOA)
82 virtual void handleWheelEventPhase(PlatformWheelEventPhase) { }
83#endif
84
85 void setCurrentPosition(const FloatPoint&);
86 const FloatPoint& currentPosition() const { return m_currentPosition; }
87
88 virtual void cancelAnimations() { }
89 virtual void serviceScrollAnimations() { }
90
91 virtual void contentAreaWillPaint() const { }
92 virtual void mouseEnteredContentArea() { }
93 virtual void mouseExitedContentArea() { }
94 virtual void mouseMovedInContentArea() { }
95 virtual void mouseEnteredScrollbar(Scrollbar*) const { }
96 virtual void mouseExitedScrollbar(Scrollbar*) const { }
97 virtual void mouseIsDownInScrollbar(Scrollbar*, bool) const { }
98 virtual void willStartLiveResize() { }
99 virtual void contentsResized() const { }
100 virtual void willEndLiveResize() { }
101 virtual void contentAreaDidShow() { }
102 virtual void contentAreaDidHide() { }
103
104 virtual void lockOverlayScrollbarStateToHidden(bool) { }
105 virtual bool scrollbarsCanBeActive() const { return true; }
106
107 virtual void didAddVerticalScrollbar(Scrollbar*) { }
108 virtual void willRemoveVerticalScrollbar(Scrollbar*) { }
109 virtual void didAddHorizontalScrollbar(Scrollbar*) { }
110 virtual void willRemoveHorizontalScrollbar(Scrollbar*) { }
111
112 virtual void invalidateScrollbarPartLayers(Scrollbar*) { }
113
114 virtual void verticalScrollbarLayerDidChange() { }
115 virtual void horizontalScrollbarLayerDidChange() { }
116
117 virtual bool shouldScrollbarParticipateInHitTesting(Scrollbar*) { return true; }
118
119 virtual void notifyContentAreaScrolled(const FloatSize& delta) { UNUSED_PARAM(delta); }
120
121 virtual bool isRubberBandInProgress() const { return false; }
122 virtual bool isScrollSnapInProgress() const { return false; }
123
124 void setWheelEventTestTrigger(RefPtr<WheelEventTestTrigger>&& testTrigger) { m_wheelEventTestTrigger = testTrigger; }
125#if (ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)) && PLATFORM(MAC)
126 void deferTestsForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) const override;
127 void removeTestDeferralForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) const override;
128#endif
129
130#if ENABLE(CSS_SCROLL_SNAP)
131#if PLATFORM(MAC)
132 bool processWheelEventForScrollSnap(const PlatformWheelEvent&);
133#endif
134 void updateScrollSnapState();
135 FloatPoint scrollOffset() const override;
136 void immediateScrollOnAxis(ScrollEventAxis, float delta) override;
137 bool activeScrollSnapIndexDidChange() const;
138 unsigned activeScrollSnapIndexForAxis(ScrollEventAxis) const;
139 LayoutSize scrollExtent() const override;
140 FloatSize viewportSize() const override;
141#endif
142
143protected:
144 virtual void notifyPositionChanged(const FloatSize& delta);
145 void updateActiveScrollSnapIndexForOffset();
146
147 ScrollableArea& m_scrollableArea;
148 RefPtr<WheelEventTestTrigger> m_wheelEventTestTrigger;
149#if ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)
150 ScrollController m_scrollController;
151#endif
152 FloatPoint m_currentPosition;
153};
154
155} // namespace WebCore
156
157