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

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: Rebased Created 6 years, 10 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/FloatPoint.h"
35 #include "platform/geometry/IntPoint.h"
36 #include "platform/graphics/GraphicsLayer.h"
37 #include "platform/scroll/ScrollableArea.h"
38 #include "public/platform/Platform.h"
39 #include "public/platform/WebAnimation.h"
40 #include "public/platform/WebAnimationCurve.h"
41 #include "public/platform/WebCompositorSupport.h"
42 #include "public/platform/WebScrollOffsetAnimationCurve.h"
43
44 namespace WebCore {
45
46 PassOwnPtr<ProgrammaticScrollAnimator> ProgrammaticScrollAnimator::create(Scroll ableArea* scrollableArea)
47 {
48 return adoptPtr(new ProgrammaticScrollAnimator(scrollableArea));
49 }
50
51 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(ScrollableArea* scrollabl eArea)
52 : m_scrollableArea(scrollableArea)
53 , m_animationId(0)
54 , m_startTime(0.0)
55 {
56 }
57
58 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator()
59 {
60 }
61
62 void ProgrammaticScrollAnimator::resetAnimationState()
63 {
64 m_animationCurve.clear();
65 m_animationId = 0;
66 m_startTime = 0.0;
67 m_scrollableArea->deregisterForAnimation();
68 }
69
70 void ProgrammaticScrollAnimator::animateToOffset(FloatPoint offset)
71 {
72 m_startTime = 0.0;
73 m_targetOffset = offset;
74 OwnPtr<blink::WebScrollOffsetAnimationCurve> curve = adoptPtr(blink::Platfor m::current()->compositorSupport()->createScrollOffsetAnimationCurve(m_targetOffs et, blink::WebAnimationCurve::TimingFunctionTypeEaseInOut));
75 if (m_scrollableArea->canUseCompositedScrollAnimations()) {
76 OwnPtr<blink::WebAnimation> animation = adoptPtr(blink::Platform::curren t()->compositorSupport()->createAnimation(*curve, blink::WebAnimation::TargetPro pertyScrollOffset));
77 m_animationId = animation->id();
78 m_scrollableArea->layerForScrolling()->addAnimation(animation.release()) ;
79 return;
80 }
81
82 m_animationCurve = curve.release();
83 m_animationCurve->setInitialValue(FloatPoint(m_scrollableArea->scrollPositio n()));
Ian Vollick 2014/02/06 16:02:31 Will ::tickAnimation continue to be called through
84 if (!m_scrollableArea->registerForAnimation() || !m_scrollableArea->schedule Animation()) {
85 resetAnimationState();
86 m_scrollableArea->setScrollOffsetFromProgrammaticAnimation(IntPoint(offs et.x(), offset.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->setScrollOffsetFromProgrammaticAnimation(IntPoint(offs et.x(), offset.y()));
107
108 if (isFinished) {
109 resetAnimationState();
110 } else if (!m_scrollableArea->scheduleAnimation()) {
111 m_scrollableArea->setScrollOffsetFromProgrammaticAnimation(IntPoint( m_targetOffset.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

Powered by Google App Engine
This is Rietveld 408576698