1 | /* |
2 | * Copyright (C) 2016 Igalia S.L. |
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 | #include "ScrollAnimation.h" |
29 | |
30 | #if ENABLE(SMOOTH_SCROLLING) |
31 | |
32 | #include "Timer.h" |
33 | |
34 | namespace WebCore { |
35 | |
36 | class FloatPoint; |
37 | class ScrollableArea; |
38 | |
39 | class ScrollAnimationSmooth final: public ScrollAnimation { |
40 | public: |
41 | ScrollAnimationSmooth(ScrollableArea&, const FloatPoint&, WTF::Function<void (FloatPoint&&)>&& notifyPositionChangedFunction); |
42 | virtual ~ScrollAnimationSmooth(); |
43 | |
44 | enum class Curve { |
45 | Linear, |
46 | Quadratic, |
47 | Cubic, |
48 | Quartic, |
49 | Bounce |
50 | }; |
51 | |
52 | private: |
53 | bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier) override; |
54 | void stop() override; |
55 | void updateVisibleLengths() override; |
56 | void setCurrentPosition(const FloatPoint&) override; |
57 | |
58 | struct PerAxisData { |
59 | PerAxisData() = delete; |
60 | |
61 | PerAxisData(float position, int length) |
62 | : currentPosition(position) |
63 | , desiredPosition(position) |
64 | , visibleLength(length) |
65 | { |
66 | } |
67 | |
68 | float currentPosition { 0 }; |
69 | double currentVelocity { 0 }; |
70 | |
71 | double desiredPosition { 0 }; |
72 | double desiredVelocity { 0 }; |
73 | |
74 | double startPosition { 0 }; |
75 | MonotonicTime startTime; |
76 | double startVelocity { 0 }; |
77 | |
78 | Seconds animationTime; |
79 | MonotonicTime lastAnimationTime; |
80 | |
81 | double attackPosition { 0 }; |
82 | Seconds attackTime; |
83 | Curve attackCurve { Curve::Quadratic }; |
84 | |
85 | double releasePosition { 0 }; |
86 | Seconds releaseTime; |
87 | Curve releaseCurve { Curve::Quadratic }; |
88 | |
89 | int visibleLength { 0 }; |
90 | }; |
91 | |
92 | bool updatePerAxisData(PerAxisData&, ScrollGranularity, float delta, float minScrollPosition, float maxScrollPosition); |
93 | bool animateScroll(PerAxisData&, MonotonicTime currentTime); |
94 | |
95 | void requestAnimationTimerFired(); |
96 | void startNextTimer(Seconds delay); |
97 | void animationTimerFired(); |
98 | bool animationTimerActive() const; |
99 | |
100 | WTF::Function<void (FloatPoint&&)> m_notifyPositionChangedFunction; |
101 | |
102 | PerAxisData m_horizontalData; |
103 | PerAxisData m_verticalData; |
104 | |
105 | MonotonicTime m_startTime; |
106 | Timer m_animationTimer; |
107 | }; |
108 | |
109 | } // namespace WebCore |
110 | |
111 | #endif // ENABLE(SMOOTH_SCROLLING) |
112 | |