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

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

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

Powered by Google App Engine
This is Rietveld 408576698