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

Side by Side Diff: cc/input/scrollbar_animation_controller_thinning.cc

Issue 2554913002: Prevent overlay scrollbars expand or hover together (Closed)
Patch Set: fix for fade in/out together Created 4 years 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "cc/input/scrollbar_animation_controller_thinning.h" 5 #include "cc/input/scrollbar_animation_controller_thinning.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/time/time.h" 8 #include "base/time/time.h"
9 #include "cc/layers/layer_impl.h" 9 #include "cc/layers/layer_impl.h"
10 #include "cc/layers/scrollbar_layer_impl_base.h" 10 #include "cc/layers/scrollbar_layer_impl_base.h"
11 #include "cc/trees/layer_tree_impl.h" 11 #include "cc/trees/layer_tree_impl.h"
12 12
13 namespace {
14 const float kIdleThicknessScale = 0.4f;
15 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
16 }
17
18 namespace cc { 13 namespace cc {
19 14
20 std::unique_ptr<ScrollbarAnimationControllerThinning> 15 std::unique_ptr<ScrollbarAnimationControllerThinning>
21 ScrollbarAnimationControllerThinning::Create( 16 ScrollbarAnimationControllerThinning::Create(
22 int scroll_layer_id, 17 int scroll_layer_id,
23 ScrollbarAnimationControllerClient* client, 18 ScrollbarAnimationControllerClient* client,
24 base::TimeDelta delay_before_starting, 19 base::TimeDelta delay_before_starting,
25 base::TimeDelta resize_delay_before_starting, 20 base::TimeDelta resize_delay_before_starting,
26 base::TimeDelta fade_duration, 21 base::TimeDelta fade_duration,
27 base::TimeDelta thinning_duration) { 22 base::TimeDelta thinning_duration) {
28 return base::WrapUnique(new ScrollbarAnimationControllerThinning( 23 return base::WrapUnique(new ScrollbarAnimationControllerThinning(
29 scroll_layer_id, client, delay_before_starting, 24 scroll_layer_id, client, delay_before_starting,
30 resize_delay_before_starting, fade_duration, thinning_duration)); 25 resize_delay_before_starting, fade_duration, thinning_duration));
31 } 26 }
32 27
33 ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning( 28 ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning(
34 int scroll_layer_id, 29 int scroll_layer_id,
35 ScrollbarAnimationControllerClient* client, 30 ScrollbarAnimationControllerClient* client,
36 base::TimeDelta delay_before_starting, 31 base::TimeDelta delay_before_starting,
37 base::TimeDelta resize_delay_before_starting, 32 base::TimeDelta resize_delay_before_starting,
38 base::TimeDelta fade_duration, 33 base::TimeDelta fade_duration,
39 base::TimeDelta thinning_duration) 34 base::TimeDelta thinning_duration)
40 : ScrollbarAnimationController(scroll_layer_id, 35 : ScrollbarAnimationController(scroll_layer_id,
41 client, 36 client,
42 delay_before_starting, 37 delay_before_starting,
43 resize_delay_before_starting), 38 resize_delay_before_starting) {
44 opacity_(0.0f), 39 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create(
45 captured_(false), 40 scroll_layer_id, ScrollbarOrientation::VERTICAL, client,
46 mouse_is_over_scrollbar_(false), 41 delayed_scrollbar_fade(), fade_duration, thinning_duration);
47 mouse_is_near_scrollbar_(false), 42 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create(
48 thickness_change_(NONE), 43 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client,
49 mouse_move_distance_to_trigger_animation_( 44 delayed_scrollbar_fade(), fade_duration, thinning_duration);
50 kDefaultMouseMoveDistanceToTriggerAnimation),
51 fade_duration_(fade_duration),
52 thinning_duration_(thinning_duration),
53 current_animating_property_(OPACITY) {
54 ApplyOpacity(0.f);
55 ApplyThumbThicknessScale(kIdleThicknessScale);
56 } 45 }
57 46
58 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {} 47 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {}
59 48
60 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { 49 SingleScrollbarAnimationControllerThinning&
61 if (captured_) 50 ScrollbarAnimationControllerThinning::GetScrollbarAnimationController(
62 return; 51 ScrollbarOrientation orientation) const {
52 if (orientation == ScrollbarOrientation::VERTICAL)
53 return *(vertical_controller_.get());
54 else
55 return *(horizontal_controller_.get());
56 }
63 57
64 if (current_animating_property_ == OPACITY) 58 bool ScrollbarAnimationControllerThinning::mouse_is_over_scrollbar(
65 ApplyOpacity(1.f - progress); 59 ScrollbarOrientation orientation) const {
66 else 60 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar();
67 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress)); 61 }
68 62
69 client_->SetNeedsRedrawForScrollbarAnimation(); 63 bool ScrollbarAnimationControllerThinning::mouse_is_near_scrollbar(
70 if (progress == 1.f) { 64 ScrollbarOrientation orientation) const {
71 StopAnimation(); 65 return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar();
72 if (current_animating_property_ == THICKNESS) { 66 }
73 thickness_change_ = NONE; 67
74 SetCurrentAnimatingProperty(OPACITY); 68 bool ScrollbarAnimationControllerThinning::mouse_is_near_any_scrollbar() const {
75 if (!mouse_is_near_scrollbar_) 69 return vertical_controller_->mouse_is_near_scrollbar() ||
76 PostDelayedAnimationTask(false); 70 horizontal_controller_->mouse_is_near_scrollbar();
77 } 71 }
72
73 bool ScrollbarAnimationControllerThinning::captured() const {
74 return vertical_controller_->captured() || horizontal_controller_->captured();
75 }
76
77 bool ScrollbarAnimationControllerThinning::ScrollbarsHidden() const {
78 return hidden_;
79 }
80
81 void ScrollbarAnimationControllerThinning::set_mouse_move_distance_for_test(
82 float distance) {
83 vertical_controller_->set_mouse_move_distance_for_test(distance);
84 horizontal_controller_->set_mouse_move_distance_for_test(distance);
85 }
86
87 void ScrollbarAnimationControllerThinning::DidChangeScrollbarVisibility(
88 bool hidden) {
89 if (hidden_ != hidden) {
90 hidden_ = hidden;
91 client_->DidChangeScrollbarVisibility();
78 } 92 }
79 } 93 }
80 94
95 bool ScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
96 bool need_animate = vertical_controller_->Animate(now);
97 need_animate = horizontal_controller_->Animate(now) || need_animate;
98
99 DidChangeScrollbarVisibility(vertical_controller_->ScrollbarsHidden());
bokan 2016/12/16 14:57:42 The visibility should be a property of this parent
100
101 if (need_animate) {
102 client_->SetNeedsAnimateForScrollbarAnimation();
103 } else {
104 if (vertical_controller_->ShouldFadeOut() &&
105 horizontal_controller_->ShouldFadeOut())
106 PostDelayedAnimationTask(false);
107 }
108
109 return need_animate;
110 }
111
112 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) {
113 // we delegate running the animation to the child controllers.
114 NOTREACHED();
115 }
116
81 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() { 117 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() {
82 if (current_animating_property_ == OPACITY) 118 // This method is deleted in ScrollbarAnimationControllerThinning
83 return fade_duration_; 119 NOTREACHED();
84 else 120 return vertical_controller_->Duration();
85 return thinning_duration_; 121 }
122
123 void ScrollbarAnimationControllerThinning::StartAnimation() {
124 ScrollbarAnimationController::StartAnimation();
125
126 vertical_controller_->StartAnimation();
127 horizontal_controller_->StartAnimation();
86 } 128 }
87 129
88 void ScrollbarAnimationControllerThinning::DidMouseDown() { 130 void ScrollbarAnimationControllerThinning::DidMouseDown() {
89 if (!mouse_is_over_scrollbar_ || opacity_ == 0.0f) 131 vertical_controller_->DidMouseDown();
90 return; 132 horizontal_controller_->DidMouseDown();
91
92 StopAnimation();
93 captured_ = true;
94 ApplyOpacity(1.f);
95 ApplyThumbThicknessScale(1.f);
96 } 133 }
97 134
98 void ScrollbarAnimationControllerThinning::DidMouseUp() { 135 void ScrollbarAnimationControllerThinning::DidMouseUp() {
99 if (!captured_ || opacity_ == 0.0f) 136 vertical_controller_->DidMouseUp();
100 return; 137 horizontal_controller_->DidMouseUp();
101
102 captured_ = false;
103 StopAnimation();
104
105 if (!mouse_is_near_scrollbar_) {
106 SetCurrentAnimatingProperty(THICKNESS);
107 thickness_change_ = DECREASE;
108 StartAnimation();
109 } else {
110 SetCurrentAnimatingProperty(OPACITY);
111 }
112 } 138 }
113 139
114 void ScrollbarAnimationControllerThinning::DidMouseLeave() { 140 void ScrollbarAnimationControllerThinning::DidMouseLeave() {
115 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) 141 vertical_controller_->DidMouseLeave();
116 return; 142 horizontal_controller_->DidMouseLeave();
143 }
117 144
118 mouse_is_over_scrollbar_ = false; 145 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(
119 mouse_is_near_scrollbar_ = false; 146 ScrollbarOrientation orientation,
120 147 float distance) {
121 if (captured_ || opacity_ == 0.0f) 148 GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance);
122 return;
123
124 thickness_change_ = DECREASE;
125 SetCurrentAnimatingProperty(THICKNESS);
126 StartAnimation();
127 } 149 }
128 150
129 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { 151 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) {
130 if (captured_) 152 if (captured())
131 return; 153 return;
132 154
133 ScrollbarAnimationController::DidScrollUpdate(on_resize); 155 vertical_controller_->FadeIn();
134 ApplyOpacity(1.f); 156 horizontal_controller_->FadeIn();
135 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f 157
136 : kIdleThicknessScale); 158 DidChangeScrollbarVisibility(vertical_controller_->ScrollbarsHidden());
137 SetCurrentAnimatingProperty(OPACITY);
138 159
139 // Don't fade out the scrollbar when mouse is near. 160 // Don't fade out the scrollbar when mouse is near.
140 if (mouse_is_near_scrollbar_) 161 if (!mouse_is_near_any_scrollbar())
141 StopAnimation(); 162 PostDelayedAnimationTask(on_resize);
142 } 163 }
143 164
144 void ScrollbarAnimationControllerThinning::DidScrollEnd() { 165 void ScrollbarAnimationControllerThinning::DidScrollEnd() {
145 ScrollbarAnimationController::DidScrollEnd(); 166 ScrollbarAnimationController::DidScrollEnd();
146 167 EnsureScrollbarFadeIn();
147 // Don't fade out the scrollbar when mouse is near.
148 if (mouse_is_near_scrollbar_)
149 StopAnimation();
150 } 168 }
151 169
152 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) { 170 void ScrollbarAnimationControllerThinning::EnsureScrollbarFadeIn() {
153 bool mouse_is_over_scrollbar = distance == 0.0f; 171 // Don't fade out the scrollbar when mouse is near.
154 bool mouse_is_near_scrollbar = 172 if (mouse_is_near_any_scrollbar()) {
155 distance < mouse_move_distance_to_trigger_animation_; 173 vertical_controller_->FadeIn();
156 174 horizontal_controller_->FadeIn();
157 if (captured_ || opacity_ == 0.0f) {
158 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
159 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
160 return;
161 }
162
163 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ &&
164 mouse_is_near_scrollbar == mouse_is_near_scrollbar_)
165 return;
166
167 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar)
168 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
169
170 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) {
171 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
172 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE;
173 }
174
175 SetCurrentAnimatingProperty(THICKNESS);
176 StartAnimation();
177 }
178
179 bool ScrollbarAnimationControllerThinning::ScrollbarsHidden() const {
180 return opacity_ == 0.0f;
181 }
182
183 float ScrollbarAnimationControllerThinning::ThumbThicknessScaleAt(
184 float progress) {
185 if (thickness_change_ == NONE)
186 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale;
187 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
188 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
189 }
190
191 float ScrollbarAnimationControllerThinning::AdjustScale(
192 float new_value,
193 float current_value,
194 AnimationChange animation_change,
195 float min_value,
196 float max_value) {
197 float result;
198 if (animation_change == INCREASE && current_value > new_value)
199 result = current_value;
200 else if (animation_change == DECREASE && current_value < new_value)
201 result = current_value;
202 else
203 result = new_value;
204 if (result > max_value)
205 return max_value;
206 if (result < min_value)
207 return min_value;
208 return result;
209 }
210
211 void ScrollbarAnimationControllerThinning::ApplyOpacity(float opacity) {
212 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) {
213 if (!scrollbar->is_overlay_scrollbar())
214 continue;
215 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0;
216 PropertyTrees* property_trees =
217 scrollbar->layer_tree_impl()->property_trees();
218 // If this method is called during LayerImpl::PushPropertiesTo, we may not
219 // yet have valid effect_id_to_index_map entries as property trees are
220 // pushed after layers during activation. We can skip updating opacity in
221 // that case as we are only registering a scrollbar and because opacity will
222 // be overwritten anyway when property trees are pushed.
223 if (property_trees->IsInIdToIndexMap(PropertyTrees::TreeType::EFFECT,
224 scrollbar->id())) {
225 property_trees->effect_tree.OnOpacityAnimated(
226 effective_opacity,
227 property_trees->effect_id_to_index_map[scrollbar->id()],
228 scrollbar->layer_tree_impl());
229 }
230 }
231
232 bool previouslyVisible = opacity_ > 0.0f;
233 bool currentlyVisible = opacity > 0.0f;
234
235 opacity_ = opacity;
236
237 if (previouslyVisible != currentlyVisible)
238 client_->DidChangeScrollbarVisibility();
239 }
240
241 void ScrollbarAnimationControllerThinning::ApplyThumbThicknessScale(
242 float thumb_thickness_scale) {
243 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) {
244 if (!scrollbar->is_overlay_scrollbar())
245 continue;
246
247 scrollbar->SetThumbThicknessScaleFactor(AdjustScale(
248 thumb_thickness_scale, scrollbar->thumb_thickness_scale_factor(),
249 thickness_change_, kIdleThicknessScale, 1));
250 } 175 }
251 } 176 }
252 177
253 void ScrollbarAnimationControllerThinning::SetCurrentAnimatingProperty(
254 AnimatingProperty property) {
255 if (current_animating_property_ == property)
256 return;
257
258 StopAnimation();
259 current_animating_property_ = property;
260 if (current_animating_property_ == THICKNESS)
261 ApplyOpacity(1.f);
262 }
263
264 } // namespace cc 178 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698