Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(644)

Side by Side Diff: Source/platform/scroll/ProgrammaticScrollAnimator.cpp

Issue 134443003: Implement CSSOM Smooth Scroll API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2014 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "platform/scroll/ProgrammaticScrollAnimator.h"
33
34 #include "platform/geometry/IntPoint.h"
35 #include "platform/graphics/GraphicsLayer.h"
36 #include "platform/scroll/ScrollableArea.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebAnimation.h"
39 #include "public/platform/WebAnimationCurve.h"
40 #include "public/platform/WebCompositorSupport.h"
41 #include "public/platform/WebScrollOffsetAnimationCurve.h"
42
43 namespace WebCore {
44
45 PassOwnPtr<ProgrammaticScrollAnimator> ProgrammaticScrollAnimator::create(Scroll ableArea* scrollableArea)
46 {
47 return adoptPtr(new ProgrammaticScrollAnimator(scrollableArea));
48 }
49
50 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(ScrollableArea* scrollabl eArea)
51 : m_scrollableArea(scrollableArea)
52 , m_animationId(0)
53 , m_startTime(0.0)
54 {
55 }
56
57 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator()
58 {
59 }
60
61 void ProgrammaticScrollAnimator::resetAnimationState()
62 {
63 m_animationCurve.clear();
64 m_animationId = 0;
65 m_startTime = 0.0;
66 m_scrollableArea->deregisterForAnimation();
67 }
68
69 void ProgrammaticScrollAnimator::animateToOffset(FloatPoint offset)
70 {
71 m_startTime = 0.0;
72 m_targetOffset = offset;
73 OwnPtr<blink::WebScrollOffsetAnimationCurve> curve = adoptPtr(blink::Platfor m::current()->compositorSupport()->createScrollOffsetAnimationCurve(m_targetOffs et, blink::WebAnimationCurve::TimingFunctionTypeEaseInOut));
74 if (m_scrollableArea->canUseCompositedScrollAnimations()) {
75 OwnPtr<blink::WebAnimation> animation = adoptPtr(blink::Platform::curren t()->compositorSupport()->createAnimation(*curve, blink::WebAnimation::TargetPro pertyScrollOffset));
76 m_animationId = animation->id();
77 if (m_scrollableArea->layerForScrolling()->addAnimation(animation.releas e()))
78 return;
79 }
80
81 m_animationCurve = curve.release();
82 m_animationCurve->setInitialValue(FloatPoint(m_scrollableArea->scrollPositio n()));
83 m_scrollableArea->registerForAnimation();
84 if (!m_scrollableArea->scheduleAnimation()) {
85 resetAnimationState();
86 m_scrollableArea->notifyScrollPositionChanged(IntPoint(offset.x(), offse t.y()));
87 }
88 }
89
90 void ProgrammaticScrollAnimator::cancelAnimation()
91 {
92 if (m_animationId && m_scrollableArea->layerForScrolling())
93 m_scrollableArea->layerForScrolling()->removeAnimation(m_animationId);
94
95 resetAnimationState();
96 }
97
98 void ProgrammaticScrollAnimator::tickAnimation(double monotonicTime)
99 {
100 if (m_animationCurve) {
101 if (!m_startTime)
102 m_startTime = monotonicTime;
103 double elapsedTime = monotonicTime - m_startTime;
104 bool isFinished = (elapsedTime > m_animationCurve->duration());
105 FloatPoint offset = m_animationCurve->getValue(elapsedTime);
106 m_scrollableArea->notifyScrollPositionChanged(IntPoint(offset.x(), offse t.y()));
107
108 if (isFinished) {
109 resetAnimationState();
110 } else if (!m_scrollableArea->scheduleAnimation()) {
111 m_scrollableArea->notifyScrollPositionChanged(IntPoint(m_targetOffse t.x(), m_targetOffset.y()));
112 resetAnimationState();
113 }
114 }
115 }
116
117 void ProgrammaticScrollAnimator::notifyAnimationStarted(double monotonicTime)
118 {
119 m_startTime = monotonicTime;
120 }
121
122 void ProgrammaticScrollAnimator::notifyAnimationFinished(double monotonicTime)
123 {
124 resetAnimationState();
125 }
126
127 void ProgrammaticScrollAnimator::canUseCompositedScrollAnimationsDidChange()
128 {
129 // If we're running an animation on the compositor thread but composited
130 // scrolling is no longer available, we need to take over the animation on
131 // the main thread.
132 if (m_animationId && !m_scrollableArea->canUseCompositedScrollAnimations()) {
133 cancelAnimation();
134 animateToOffset(m_targetOffset);
135 }
136 }
137
138 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/platform/scroll/ProgrammaticScrollAnimator.h ('k') | Source/platform/scroll/ScrollView.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698