1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
cc / input / single_scrollbar_animation_controller_thinning.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/input/single_scrollbar_animation_controller_thinning.h"
#include <algorithm>
#include "base/memory/ptr_util.h"
#include "base/time/time.h"
#include "cc/input/scrollbar_animation_controller.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/scrollbar_layer_impl_base.h"
#include "cc/trees/layer_tree_impl.h"
namespace cc {
namespace {
float DistanceToRect(const gfx::PointF& device_viewport_point,
const ScrollbarLayerImplBase& scrollbar,
const gfx::Rect& rect) {
gfx::RectF device_viewport_rect = MathUtil::MapClippedRect(
scrollbar.ScreenSpaceTransform(), gfx::RectF(rect));
return device_viewport_rect.ManhattanDistanceToPoint(device_viewport_point) /
scrollbar.layer_tree_impl()->device_scale_factor();
}
float DistanceToScrollbar(const gfx::PointF& device_viewport_point,
const ScrollbarLayerImplBase& scrollbar) {
return DistanceToRect(device_viewport_point, scrollbar,
gfx::Rect(scrollbar.bounds()));
}
float DistanceToScrollbarThumb(const gfx::PointF& device_viewport_point,
const ScrollbarLayerImplBase& scrollbar) {
return DistanceToRect(device_viewport_point, scrollbar,
scrollbar.ComputeHitTestableExpandedThumbQuadRect());
}
} // namespace
std::unique_ptr<SingleScrollbarAnimationControllerThinning>
SingleScrollbarAnimationControllerThinning::Create(
ElementId scroll_element_id,
ScrollbarOrientation orientation,
ScrollbarAnimationControllerClient* client,
base::TimeDelta thinning_duration,
float idle_thickness_scale) {
return base::WrapUnique(new SingleScrollbarAnimationControllerThinning(
scroll_element_id, orientation, client, thinning_duration,
idle_thickness_scale));
}
SingleScrollbarAnimationControllerThinning::
SingleScrollbarAnimationControllerThinning(
ElementId scroll_element_id,
ScrollbarOrientation orientation,
ScrollbarAnimationControllerClient* client,
base::TimeDelta thinning_duration,
float idle_thickness_scale)
: client_(client),
scroll_element_id_(scroll_element_id),
orientation_(orientation),
thinning_duration_(thinning_duration),
idle_thickness_scale_(idle_thickness_scale) {
ApplyThumbThicknessScale(idle_thickness_scale_);
}
ScrollbarLayerImplBase*
SingleScrollbarAnimationControllerThinning::GetScrollbar() const {
for (ScrollbarLayerImplBase* scrollbar :
client_->ScrollbarsFor(scroll_element_id_)) {
DCHECK(scrollbar->is_overlay_scrollbar());
if (scrollbar->orientation() == orientation_)
return scrollbar;
}
return nullptr;
}
bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
if (!is_animating_)
return false;
if (last_awaken_time_.is_null())
last_awaken_time_ = now;
float progress = AnimationProgressAtTime(now);
RunAnimationFrame(progress);
return true;
}
float SingleScrollbarAnimationControllerThinning::AnimationProgressAtTime(
base::TimeTicks now) {
// In tests, there may be no duration; snap to the end in such a case.
if (thinning_duration_.is_zero())
return 1.0f;
const base::TimeDelta delta = now - last_awaken_time_;
return std::clamp(static_cast<float>(delta / thinning_duration_), 0.0f, 1.0f);
}
void SingleScrollbarAnimationControllerThinning::RunAnimationFrame(
float progress) {
if (captured_)
return;
ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress));
client_->SetNeedsRedrawForScrollbarAnimation();
if (progress == 1.f) {
StopAnimation();
thickness_change_ = AnimationChange::kNone;
}
}
void SingleScrollbarAnimationControllerThinning::StartAnimation() {
is_animating_ = true;
last_awaken_time_ = base::TimeTicks();
client_->SetNeedsAnimateForScrollbarAnimation();
}
void SingleScrollbarAnimationControllerThinning::StopAnimation() {
is_animating_ = false;
}
void SingleScrollbarAnimationControllerThinning::DidScrollUpdate() {
if (captured_ || !mouse_is_near_scrollbar_) {
return;
}
CalculateThicknessShouldChange(device_viewport_last_pointer_location_);
// If scrolling with the pointer on top of the scrollbar, force the scrollbar
// to expand.
if (thickness_change_ == AnimationChange::kNone) {
UpdateThumbThicknessScale();
}
}
void SingleScrollbarAnimationControllerThinning::DidMouseDown() {
// When invisible, Fluent scrollbars are disabled and their thumb has no
// dimensions, which causes mouse_is_over_scrollbar_thumb_ to always be false.
// This check updates the thumb variable to cover the cases where you mouse
// over the invisible thumb, make it appear by some mechanism (tickmarks,
// scrolling, etc.) and press mouse down without moving your pointer.
if (client_->IsFluentOverlayScrollbar() && !mouse_is_over_scrollbar_thumb_) {
ScrollbarLayerImplBase* scrollbar = GetScrollbar();
if (scrollbar) {
const float distance_to_scrollbar_thumb = DistanceToScrollbarThumb(
device_viewport_last_pointer_location_, *scrollbar);
mouse_is_over_scrollbar_thumb_ = distance_to_scrollbar_thumb == 0.0f;
}
}
if (!mouse_is_over_scrollbar_thumb_)
return;
captured_ = true;
UpdateThumbThicknessScale();
}
void SingleScrollbarAnimationControllerThinning::DidMouseUp() {
if (!captured_)
return;
captured_ = false;
StopAnimation();
// On mouse up, Fluent scrollbars go straight to the scrollbar disappearance
// animation (via ScrollbarAnimationController) without queueing a thinning
// animation.
const bool thickness_should_decrease =
!client_->IsFluentOverlayScrollbar() && !mouse_is_near_scrollbar_thumb_;
if (thickness_should_decrease) {
thickness_change_ = AnimationChange::kDecrease;
StartAnimation();
} else {
thickness_change_ = AnimationChange::kNone;
}
}
void SingleScrollbarAnimationControllerThinning::DidMouseLeave() {
mouse_is_over_scrollbar_thumb_ = false;
mouse_is_near_scrollbar_thumb_ = false;
mouse_is_near_scrollbar_ = false;
if (captured_) {
return;
}
// If fully expanded, Fluent scrollbars don't queue a thinning animation and
// let the ScrollbarAnimationController make the scrollbars disappear.
if (client_->IsFluentOverlayScrollbar() &&
thickness_change_ == AnimationChange::kNone) {
return;
}
thickness_change_ = AnimationChange::kDecrease;
StartAnimation();
}
void SingleScrollbarAnimationControllerThinning::DidMouseMove(
const gfx::PointF& device_viewport_point) {
CalculateThicknessShouldChange(device_viewport_point);
device_viewport_last_pointer_location_ = device_viewport_point;
}
void SingleScrollbarAnimationControllerThinning::CalculateThicknessShouldChange(
const gfx::PointF& device_viewport_point) {
ScrollbarLayerImplBase* scrollbar = GetScrollbar();
if (!scrollbar)
return;
const float distance_to_scrollbar =
DistanceToScrollbar(device_viewport_point, *scrollbar);
const float distance_to_scrollbar_thumb =
DistanceToScrollbarThumb(device_viewport_point, *scrollbar);
const bool mouse_is_near_scrollbar =
distance_to_scrollbar <= MouseMoveDistanceToTriggerFadeIn();
const bool mouse_is_over_scrollbar_thumb =
distance_to_scrollbar_thumb == 0.0f;
const bool mouse_is_near_scrollbar_thumb =
distance_to_scrollbar_thumb <= MouseMoveDistanceToTriggerExpand();
bool thickness_should_change;
if (client_->IsFluentOverlayScrollbar()) {
const bool is_visible = scrollbar->OverlayScrollbarOpacity() > 0.f;
const bool moved_over_scrollbar =
mouse_is_near_scrollbar_ != mouse_is_near_scrollbar;
const bool mouse_far_from_scrollbar =
(!mouse_is_near_scrollbar &&
thickness_change_ == AnimationChange::kNone);
// On mouse move Fluent scrollbars will queue a thinning animation iff the
// scrollbar is visible and either the mouse has moved over the scrollbar
// (increase thickness) or the mouse has moved far away from the scrollbar
// and there is no previously queued animation (decreasse thickness).
// If tickmarks are shown, the scrollbars should be and should remain in
// Full mode.
thickness_should_change =
!tickmarks_showing_ && is_visible &&
(moved_over_scrollbar || mouse_far_from_scrollbar);
} else {
thickness_should_change =
(mouse_is_near_scrollbar_thumb_ != mouse_is_near_scrollbar_thumb);
}
if (!captured_ && thickness_should_change) {
const bool thickness_should_increase = client_->IsFluentOverlayScrollbar()
? mouse_is_near_scrollbar
: mouse_is_near_scrollbar_thumb;
thickness_change_ = thickness_should_increase ? AnimationChange::kIncrease
: AnimationChange::kDecrease;
StartAnimation();
}
mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
mouse_is_near_scrollbar_thumb_ = mouse_is_near_scrollbar_thumb;
mouse_is_over_scrollbar_thumb_ = mouse_is_over_scrollbar_thumb;
}
float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt(
float progress) const {
CHECK_NE(thickness_change_, AnimationChange::kNone);
float factor = thickness_change_ == AnimationChange::kIncrease
? progress
: (1.f - progress);
return ((1.f - idle_thickness_scale_) * factor) + idle_thickness_scale_;
}
float SingleScrollbarAnimationControllerThinning::AdjustScale(
float new_value,
float current_value,
AnimationChange animation_change,
float min_value,
float max_value) {
float result;
if (animation_change == AnimationChange::kIncrease &&
current_value > new_value) {
result = current_value;
} else if (animation_change == AnimationChange::kDecrease &&
current_value < new_value) {
result = current_value;
} else {
result = new_value;
}
if (result > max_value)
return max_value;
if (result < min_value)
return min_value;
return result;
}
float SingleScrollbarAnimationControllerThinning::
CurrentForcedThumbThicknessScale() const {
bool thumb_should_be_expanded;
if (client_->IsFluentOverlayScrollbar()) {
thumb_should_be_expanded = mouse_is_near_scrollbar_ || tickmarks_showing_;
} else {
thumb_should_be_expanded = mouse_is_near_scrollbar_thumb_;
}
thumb_should_be_expanded |= captured_;
return thumb_should_be_expanded ? 1.f : idle_thickness_scale_;
}
void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() {
StopAnimation();
ApplyThumbThicknessScale(CurrentForcedThumbThicknessScale());
}
void SingleScrollbarAnimationControllerThinning::DidRequestShow() {
if (thickness_change_ == AnimationChange::kNone) {
UpdateThumbThicknessScale();
}
}
void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale(
float thumb_thickness_scale) {
for (auto* scrollbar : client_->ScrollbarsFor(scroll_element_id_)) {
if (scrollbar->orientation() != orientation_)
continue;
DCHECK(scrollbar->is_overlay_scrollbar());
float scale = AdjustScale(thumb_thickness_scale,
scrollbar->thumb_thickness_scale_factor(),
thickness_change_, idle_thickness_scale_, 1);
scrollbar->SetThumbThicknessScaleFactor(scale);
}
}
void SingleScrollbarAnimationControllerThinning::UpdateTickmarksVisibility(
bool show) {
tickmarks_showing_ = show;
if (show) {
UpdateThumbThicknessScale();
}
}
float SingleScrollbarAnimationControllerThinning::
MouseMoveDistanceToTriggerExpand() {
return client_->IsFluentOverlayScrollbar() ? 0.0f : 25.0f;
}
float SingleScrollbarAnimationControllerThinning::
MouseMoveDistanceToTriggerFadeIn() {
return client_->IsFluentOverlayScrollbar() ? 0.0f : 30.0f;
}
} // namespace cc