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

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

Issue 2554913002: Prevent overlay scrollbars expand or hover together (Closed)
Patch Set: bokan comment addressed. 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 opacity_(0.0f),
45 captured_(false), 40 fade_duration_(fade_duration) {
46 mouse_is_over_scrollbar_(false), 41 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create(
47 mouse_is_near_scrollbar_(false), 42 scroll_layer_id, ScrollbarOrientation::VERTICAL, client,
48 thickness_change_(NONE), 43 thinning_duration);
49 mouse_move_distance_to_trigger_animation_( 44 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create(
50 kDefaultMouseMoveDistanceToTriggerAnimation), 45 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client,
51 fade_duration_(fade_duration), 46 thinning_duration);
52 thinning_duration_(thinning_duration), 47 ApplyOpacity(0.0f);
53 current_animating_property_(OPACITY) {
54 ApplyOpacity(0.f);
55 ApplyThumbThicknessScale(kIdleThicknessScale);
56 } 48 }
57 49
58 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {} 50 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {}
59 51
52 SingleScrollbarAnimationControllerThinning&
53 ScrollbarAnimationControllerThinning::GetScrollbarAnimationController(
54 ScrollbarOrientation orientation) const {
55 if (orientation == ScrollbarOrientation::VERTICAL)
56 return *(vertical_controller_.get());
57 else
58 return *(horizontal_controller_.get());
59 }
60
61 bool ScrollbarAnimationControllerThinning::mouse_is_over_scrollbar(
62 ScrollbarOrientation orientation) const {
63 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar();
64 }
65
66 bool ScrollbarAnimationControllerThinning::mouse_is_near_scrollbar(
67 ScrollbarOrientation orientation) const {
68 return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar();
69 }
70
71 bool ScrollbarAnimationControllerThinning::mouse_is_near_any_scrollbar() const {
72 return vertical_controller_->mouse_is_near_scrollbar() ||
73 horizontal_controller_->mouse_is_near_scrollbar();
74 }
75
76 bool ScrollbarAnimationControllerThinning::Captured() const {
77 return vertical_controller_->captured() || horizontal_controller_->captured();
78 }
79
80 bool ScrollbarAnimationControllerThinning::ScrollbarsHidden() const {
81 return opacity_ == 0.0f;
82 }
83
84 void ScrollbarAnimationControllerThinning::set_mouse_move_distance_for_test(
85 float distance) {
86 vertical_controller_->set_mouse_move_distance_for_test(distance);
87 horizontal_controller_->set_mouse_move_distance_for_test(distance);
88 }
89
90 bool ScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
91 bool animated = ScrollbarAnimationController::Animate(now);
92 animated |= vertical_controller_->Animate(now);
93 animated |= horizontal_controller_->Animate(now);
94
95 return animated;
96 }
97
60 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { 98 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) {
61 if (captured_) 99 ApplyOpacity(1.f - progress);
100 if (progress == 1.f)
101 StopAnimation();
102 }
103
104 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() {
105 return fade_duration_;
106 }
107
108 void ScrollbarAnimationControllerThinning::DidMouseDown() {
109 if (ScrollbarsHidden())
62 return; 110 return;
111 vertical_controller_->DidMouseDown();
112 horizontal_controller_->DidMouseDown();
113 }
63 114
64 if (current_animating_property_ == OPACITY) 115 void ScrollbarAnimationControllerThinning::DidMouseUp() {
65 ApplyOpacity(1.f - progress); 116 vertical_controller_->DidMouseUp();
66 else 117 horizontal_controller_->DidMouseUp();
67 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress)); 118 }
68 119
69 client_->SetNeedsRedrawForScrollbarAnimation(); 120 void ScrollbarAnimationControllerThinning::DidMouseLeave() {
70 if (progress == 1.f) { 121 vertical_controller_->DidMouseLeave();
122 horizontal_controller_->DidMouseLeave();
123 }
124
125 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(
126 ScrollbarOrientation orientation,
127 float distance) {
128 GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance);
129
130 if (mouse_is_near_any_scrollbar() && !ScrollbarsHidden()) {
131 ApplyOpacity(1);
71 StopAnimation(); 132 StopAnimation();
72 if (current_animating_property_ == THICKNESS) { 133 } else if (!animating_fade() && !currently_scrolling()) {
bokan 2016/12/21 23:06:09 I don't think you need !currently_scrolling, it's
73 thickness_change_ = NONE; 134 PostDelayedAnimationTask(false);
74 SetCurrentAnimatingProperty(OPACITY);
75 if (!mouse_is_near_scrollbar_)
76 PostDelayedAnimationTask(false);
77 }
78 } 135 }
79 } 136 }
80 137
81 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() {
82 if (current_animating_property_ == OPACITY)
83 return fade_duration_;
84 else
85 return thinning_duration_;
86 }
87
88 void ScrollbarAnimationControllerThinning::DidMouseDown() {
89 if (!mouse_is_over_scrollbar_ || opacity_ == 0.0f)
90 return;
91
92 StopAnimation();
93 captured_ = true;
94 ApplyOpacity(1.f);
95 ApplyThumbThicknessScale(1.f);
96 }
97
98 void ScrollbarAnimationControllerThinning::DidMouseUp() {
99 if (!captured_ || opacity_ == 0.0f)
100 return;
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 }
113
114 void ScrollbarAnimationControllerThinning::DidMouseLeave() {
115 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_)
116 return;
117
118 mouse_is_over_scrollbar_ = false;
119 mouse_is_near_scrollbar_ = false;
120
121 if (captured_ || opacity_ == 0.0f)
122 return;
123
124 thickness_change_ = DECREASE;
125 SetCurrentAnimatingProperty(THICKNESS);
126 StartAnimation();
127 }
128
129 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { 138 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) {
130 if (captured_) 139 if (Captured())
131 return; 140 return;
132 141
133 ScrollbarAnimationController::DidScrollUpdate(on_resize); 142 ScrollbarAnimationController::DidScrollUpdate(on_resize);
134 ApplyOpacity(1.f);
135 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f
136 : kIdleThicknessScale);
137 SetCurrentAnimatingProperty(OPACITY);
138 143
139 // Don't fade out the scrollbar when mouse is near. 144 ApplyOpacity(1);
140 if (mouse_is_near_scrollbar_) 145 vertical_controller_->UpdateThumbThicknessScale();
146 horizontal_controller_->UpdateThumbThicknessScale();
147
148 if (mouse_is_near_any_scrollbar())
141 StopAnimation(); 149 StopAnimation();
142 } 150 }
143 151
144 void ScrollbarAnimationControllerThinning::DidScrollEnd() { 152 void ScrollbarAnimationControllerThinning::DidScrollEnd() {
145 ScrollbarAnimationController::DidScrollEnd(); 153 ScrollbarAnimationController::DidScrollEnd();
146 154
147 // Don't fade out the scrollbar when mouse is near. 155 // Don't fade out the scrollbar when mouse is near.
148 if (mouse_is_near_scrollbar_) 156 if (mouse_is_near_any_scrollbar())
149 StopAnimation(); 157 StopAnimation();
150 } 158 }
151 159
152 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) {
153 bool mouse_is_over_scrollbar = distance == 0.0f;
154 bool mouse_is_near_scrollbar =
155 distance < mouse_move_distance_to_trigger_animation_;
156
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) { 160 void ScrollbarAnimationControllerThinning::ApplyOpacity(float opacity) {
212 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { 161 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) {
213 if (!scrollbar->is_overlay_scrollbar()) 162 if (!scrollbar->is_overlay_scrollbar())
214 continue; 163 continue;
215 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; 164 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0;
216 PropertyTrees* property_trees = 165 PropertyTrees* property_trees =
217 scrollbar->layer_tree_impl()->property_trees(); 166 scrollbar->layer_tree_impl()->property_trees();
218 // If this method is called during LayerImpl::PushPropertiesTo, we may not 167 // 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 168 // 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 169 // pushed after layers during activation. We can skip updating opacity in
(...skipping 10 matching lines...) Expand all
231 180
232 bool previouslyVisible = opacity_ > 0.0f; 181 bool previouslyVisible = opacity_ > 0.0f;
233 bool currentlyVisible = opacity > 0.0f; 182 bool currentlyVisible = opacity > 0.0f;
234 183
235 opacity_ = opacity; 184 opacity_ = opacity;
236 185
237 if (previouslyVisible != currentlyVisible) 186 if (previouslyVisible != currentlyVisible)
238 client_->DidChangeScrollbarVisibility(); 187 client_->DidChangeScrollbarVisibility();
239 } 188 }
240 189
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 }
251 }
252
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 190 } // namespace cc
OLDNEW
« no previous file with comments | « cc/input/scrollbar_animation_controller_thinning.h ('k') | cc/input/scrollbar_animation_controller_thinning_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698