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

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

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Fixed nits. 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 ScrollType scroll_type = sequenced_for_smooth_scroll_ ? kSequencedSmoothScroll
35 : kProgrammaticScroll;
36 ScrollOffsetChanged(offset, scroll_type);
34 } 37 }
35 38
36 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation( 39 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation(
37 const ScrollOffset& offset) { 40 const ScrollOffset& offset) {
38 CancelAnimation(); 41 CancelAnimation();
39 NotifyOffsetChanged(offset); 42 NotifyOffsetChanged(offset);
40 } 43 }
41 44
42 void ProgrammaticScrollAnimator::AnimateToOffset(const ScrollOffset& offset) { 45 void ProgrammaticScrollAnimator::AnimateToOffset(
46 const ScrollOffset& offset,
47 bool sequenced_for_smooth_scroll) {
43 if (run_state_ == RunState::kPostAnimationCleanup) 48 if (run_state_ == RunState::kPostAnimationCleanup)
44 ResetAnimationState(); 49 ResetAnimationState();
45 50
46 start_time_ = 0.0; 51 start_time_ = 0.0;
47 target_offset_ = offset; 52 target_offset_ = offset;
53 sequenced_for_smooth_scroll_ = sequenced_for_smooth_scroll;
48 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create( 54 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create(
49 CompositorOffsetFromBlinkOffset(target_offset_), 55 CompositorOffsetFromBlinkOffset(target_offset_),
50 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased); 56 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased);
51 57
52 scrollable_area_->RegisterForAnimation(); 58 scrollable_area_->RegisterForAnimation();
53 if (!scrollable_area_->ScheduleAnimation()) { 59 if (!scrollable_area_->ScheduleAnimation()) {
54 ResetAnimationState(); 60 ResetAnimationState();
55 NotifyOffsetChanged(offset); 61 NotifyOffsetChanged(offset);
56 } 62 }
57 run_state_ = RunState::kWaitingToSendToCompositor; 63 run_state_ = RunState::kWaitingToSendToCompositor;
(...skipping 11 matching lines...) Expand all
69 if (!start_time_) 75 if (!start_time_)
70 start_time_ = monotonic_time; 76 start_time_ = monotonic_time;
71 double elapsed_time = monotonic_time - start_time_; 77 double elapsed_time = monotonic_time - start_time_;
72 bool is_finished = (elapsed_time > animation_curve_->Duration()); 78 bool is_finished = (elapsed_time > animation_curve_->Duration());
73 ScrollOffset offset = 79 ScrollOffset offset =
74 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time)); 80 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time));
75 NotifyOffsetChanged(offset); 81 NotifyOffsetChanged(offset);
76 82
77 if (is_finished) { 83 if (is_finished) {
78 run_state_ = RunState::kPostAnimationCleanup; 84 run_state_ = RunState::kPostAnimationCleanup;
85 AnimationFinished();
79 } else if (!scrollable_area_->ScheduleAnimation()) { 86 } else if (!scrollable_area_->ScheduleAnimation()) {
80 NotifyOffsetChanged(offset); 87 NotifyOffsetChanged(offset);
81 ResetAnimationState(); 88 ResetAnimationState();
82 } 89 }
83 } 90 }
84 91
85 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() { 92 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() {
86 if (run_state_ == RunState::kPostAnimationCleanup) { 93 if (run_state_ == RunState::kPostAnimationCleanup) {
87 // No special cleanup, simply reset animation state. We have this state 94 // No special cleanup, simply reset animation state. We have this state
88 // here because the state machine is shared with ScrollAnimator which 95 // here because the state machine is shared with ScrollAnimator which
(...skipping 20 matching lines...) Expand all
109 } 116 }
110 } 117 }
111 118
112 if (run_state_ == RunState::kWaitingToSendToCompositor) { 119 if (run_state_ == RunState::kWaitingToSendToCompositor) {
113 if (!compositor_animation_attached_to_element_id_) 120 if (!compositor_animation_attached_to_element_id_)
114 ReattachCompositorPlayerIfNeeded( 121 ReattachCompositorPlayerIfNeeded(
115 GetScrollableArea()->GetCompositorAnimationTimeline()); 122 GetScrollableArea()->GetCompositorAnimationTimeline());
116 123
117 bool sent_to_compositor = false; 124 bool sent_to_compositor = false;
118 125
119 if (!scrollable_area_->ShouldScrollOnMainThread()) { 126 // TODO(sunyunjia): Sequenced Smooth Scroll should also be able to
127 // scroll on the compositor thread. We should send the ScrollType
128 // information to the compositor thread.
129 // crbug.com/730705
130 if (!scrollable_area_->ShouldScrollOnMainThread() &&
131 !sequenced_for_smooth_scroll_) {
120 std::unique_ptr<CompositorAnimation> animation = 132 std::unique_ptr<CompositorAnimation> animation =
121 CompositorAnimation::Create( 133 CompositorAnimation::Create(
122 *animation_curve_, CompositorTargetProperty::SCROLL_OFFSET, 0, 0); 134 *animation_curve_, CompositorTargetProperty::SCROLL_OFFSET, 0, 0);
123 135
124 int animation_id = animation->Id(); 136 int animation_id = animation->Id();
125 int animation_group_id = animation->Group(); 137 int animation_group_id = animation->Group();
126 138
127 if (AddAnimation(std::move(animation))) { 139 if (AddAnimation(std::move(animation))) {
128 sent_to_compositor = true; 140 sent_to_compositor = true;
129 run_state_ = RunState::kRunningOnCompositor; 141 run_state_ = RunState::kRunningOnCompositor;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 ResetAnimationState(); 174 ResetAnimationState();
163 NotifyOffsetChanged(target_offset_); 175 NotifyOffsetChanged(target_offset_);
164 } 176 }
165 } 177 }
166 } 178 }
167 179
168 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished( 180 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished(
169 int group_id) { 181 int group_id) {
170 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate); 182 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate);
171 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id); 183 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id);
184 AnimationFinished();
185 }
186
187 void ProgrammaticScrollAnimator::AnimationFinished() {
188 if (sequenced_for_smooth_scroll_) {
189 sequenced_for_smooth_scroll_ = false;
190 if (SmoothScrollSequencer* sequencer =
191 GetScrollableArea()->GetSmoothScrollSequencer())
192 sequencer->RunQueuedAnimations();
193 }
172 } 194 }
173 195
174 DEFINE_TRACE(ProgrammaticScrollAnimator) { 196 DEFINE_TRACE(ProgrammaticScrollAnimator) {
175 visitor->Trace(scrollable_area_); 197 visitor->Trace(scrollable_area_);
176 ScrollAnimatorCompositorCoordinator::Trace(visitor); 198 ScrollAnimatorCompositorCoordinator::Trace(visitor);
177 } 199 }
178 200
179 } // namespace blink 201 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698