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

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

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Add a short comment. Created 3 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/scroll/ProgrammaticScrollAnimator.h" 5 #include "platform/scroll/ProgrammaticScrollAnimator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include "platform/animation/CompositorAnimation.h" 8 #include "platform/animation/CompositorAnimation.h"
9 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h" 9 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h"
10 #include "platform/geometry/IntSize.h" 10 #include "platform/geometry/IntSize.h"
11 #include "platform/graphics/GraphicsLayer.h" 11 #include "platform/graphics/GraphicsLayer.h"
12 #include "platform/scroll/ScrollableArea.h" 12 #include "platform/scroll/ScrollableArea.h"
13 #include "platform/scroll/SmoothScrollSequencer.h"
13 #include "platform/wtf/PtrUtil.h" 14 #include "platform/wtf/PtrUtil.h"
14 #include "public/platform/Platform.h" 15 #include "public/platform/Platform.h"
15 #include "public/platform/WebCompositorSupport.h" 16 #include "public/platform/WebCompositorSupport.h"
16 17
17 namespace blink { 18 namespace blink {
18 19
19 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator( 20 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(
20 ScrollableArea* scrollable_area) 21 ScrollableArea* scrollable_area)
21 : scrollable_area_(scrollable_area), start_time_(0.0) {} 22 : scrollable_area_(scrollable_area), start_time_(0.0) {}
22 23
23 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {} 24 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {}
24 25
25 void ProgrammaticScrollAnimator::ResetAnimationState() { 26 void ProgrammaticScrollAnimator::ResetAnimationState() {
26 ScrollAnimatorCompositorCoordinator::ResetAnimationState(); 27 ScrollAnimatorCompositorCoordinator::ResetAnimationState();
27 animation_curve_.reset(); 28 animation_curve_.reset();
28 start_time_ = 0.0; 29 start_time_ = 0.0;
29 } 30 }
30 31
31 void ProgrammaticScrollAnimator::NotifyOffsetChanged( 32 void ProgrammaticScrollAnimator::NotifyOffsetChanged(
32 const ScrollOffset& offset) { 33 const ScrollOffset& offset) {
33 ScrollOffsetChanged(offset, kProgrammaticScroll); 34 ScrollOffsetChanged(offset, kProgrammaticScroll);
34 } 35 }
35 36
36 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation( 37 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation(
37 const ScrollOffset& offset) { 38 const ScrollOffset& offset) {
38 CancelAnimation(); 39 CancelAnimation();
39 NotifyOffsetChanged(offset); 40 NotifyOffsetChanged(offset);
40 } 41 }
41 42
42 void ProgrammaticScrollAnimator::AnimateToOffset(const ScrollOffset& offset) { 43 void ProgrammaticScrollAnimator::AnimateToOffset(
44 const ScrollOffset& offset,
45 bool sequenced_for_smooth_scroll) {
43 if (run_state_ == RunState::kPostAnimationCleanup) 46 if (run_state_ == RunState::kPostAnimationCleanup)
44 ResetAnimationState(); 47 ResetAnimationState();
45 48
46 start_time_ = 0.0; 49 start_time_ = 0.0;
47 target_offset_ = offset; 50 target_offset_ = offset;
51 sequenced_for_smooth_scroll_ = sequenced_for_smooth_scroll;
48 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create( 52 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create(
49 CompositorOffsetFromBlinkOffset(target_offset_), 53 CompositorOffsetFromBlinkOffset(target_offset_),
50 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased); 54 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased);
51 55
52 scrollable_area_->RegisterForAnimation(); 56 scrollable_area_->RegisterForAnimation();
53 if (!scrollable_area_->ScheduleAnimation()) { 57 if (!scrollable_area_->ScheduleAnimation()) {
54 ResetAnimationState(); 58 ResetAnimationState();
55 NotifyOffsetChanged(offset); 59 NotifyOffsetChanged(offset);
56 } 60 }
57 run_state_ = RunState::kWaitingToSendToCompositor; 61 run_state_ = RunState::kWaitingToSendToCompositor;
(...skipping 11 matching lines...) Expand all
69 if (!start_time_) 73 if (!start_time_)
70 start_time_ = monotonic_time; 74 start_time_ = monotonic_time;
71 double elapsed_time = monotonic_time - start_time_; 75 double elapsed_time = monotonic_time - start_time_;
72 bool is_finished = (elapsed_time > animation_curve_->Duration()); 76 bool is_finished = (elapsed_time > animation_curve_->Duration());
73 ScrollOffset offset = 77 ScrollOffset offset =
74 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time)); 78 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time));
75 NotifyOffsetChanged(offset); 79 NotifyOffsetChanged(offset);
76 80
77 if (is_finished) { 81 if (is_finished) {
78 run_state_ = RunState::kPostAnimationCleanup; 82 run_state_ = RunState::kPostAnimationCleanup;
83 AnimationFinished();
79 } else if (!scrollable_area_->ScheduleAnimation()) { 84 } else if (!scrollable_area_->ScheduleAnimation()) {
80 NotifyOffsetChanged(offset); 85 NotifyOffsetChanged(offset);
81 ResetAnimationState(); 86 ResetAnimationState();
82 } 87 }
83 } 88 }
84 89
85 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() { 90 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() {
86 if (run_state_ == RunState::kPostAnimationCleanup) { 91 if (run_state_ == RunState::kPostAnimationCleanup) {
87 // No special cleanup, simply reset animation state. We have this state 92 // No special cleanup, simply reset animation state. We have this state
88 // here because the state machine is shared with ScrollAnimator which 93 // here because the state machine is shared with ScrollAnimator which
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 ResetAnimationState(); 167 ResetAnimationState();
163 NotifyOffsetChanged(target_offset_); 168 NotifyOffsetChanged(target_offset_);
164 } 169 }
165 } 170 }
166 } 171 }
167 172
168 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished( 173 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished(
169 int group_id) { 174 int group_id) {
170 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate); 175 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate);
171 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id); 176 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id);
177 AnimationFinished();
178 }
179
180 void ProgrammaticScrollAnimator::AnimationFinished() {
181 if (sequenced_for_smooth_scroll_) {
182 sequenced_for_smooth_scroll_ = false;
183 if (SmoothScrollSequencer* sequencer =
184 GetScrollableArea()->GetSmoothScrollSequencer())
185 sequencer->RunQueuedAnimations();
186 }
172 } 187 }
173 188
174 DEFINE_TRACE(ProgrammaticScrollAnimator) { 189 DEFINE_TRACE(ProgrammaticScrollAnimator) {
175 visitor->Trace(scrollable_area_); 190 visitor->Trace(scrollable_area_);
176 ScrollAnimatorCompositorCoordinator::Trace(visitor); 191 ScrollAnimatorCompositorCoordinator::Trace(visitor);
177 } 192 }
178 193
179 } // namespace blink 194 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698