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

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

Issue 2554913002: Prevent overlay scrollbars expand or hover together (Closed)
Patch Set: rebase update Created 3 years, 11 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "cc/input/single_scrollbar_animation_controller_thinning.h"
6
7 #include <algorithm>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/time/time.h"
11 #include "cc/layers/layer_impl.h"
12 #include "cc/layers/scrollbar_layer_impl_base.h"
13 #include "cc/trees/layer_tree_impl.h"
14
15 namespace {
16 const float kIdleThicknessScale = 0.4f;
17 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
18 }
19
20 namespace cc {
21
22 std::unique_ptr<SingleScrollbarAnimationControllerThinning>
23 SingleScrollbarAnimationControllerThinning::Create(
24 int scroll_layer_id,
25 ScrollbarOrientation orientation,
26 ScrollbarAnimationControllerClient* client,
27 base::TimeDelta thinning_duration) {
28 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning(
29 scroll_layer_id, orientation, client, thinning_duration));
30 }
31
32 SingleScrollbarAnimationControllerThinning::
33 SingleScrollbarAnimationControllerThinning(
34 int scroll_layer_id,
35 ScrollbarOrientation orientation,
36 ScrollbarAnimationControllerClient* client,
37 base::TimeDelta thinning_duration)
38 : client_(client),
39 is_animating_(false),
40 scroll_layer_id_(scroll_layer_id),
41 orientation_(orientation),
42 captured_(false),
43 mouse_is_over_scrollbar_(false),
44 mouse_is_near_scrollbar_(false),
45 thickness_change_(NONE),
46 mouse_move_distance_to_trigger_animation_(
47 kDefaultMouseMoveDistanceToTriggerAnimation),
48 thinning_duration_(thinning_duration) {
49 ApplyThumbThicknessScale(kIdleThicknessScale);
50 }
51
52 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
53 if (!is_animating_)
54 return false;
55
56 if (last_awaken_time_.is_null())
57 last_awaken_time_ = now;
58
59 float progress = AnimationProgressAtTime(now);
60 RunAnimationFrame(progress);
61
62 return true;
63 }
64
65 float SingleScrollbarAnimationControllerThinning::AnimationProgressAtTime(
66 base::TimeTicks now) {
67 base::TimeDelta delta = now - last_awaken_time_;
68 float progress = delta.InSecondsF() / Duration().InSecondsF();
69 return std::max(std::min(progress, 1.f), 0.f);
70 }
71
72 const base::TimeDelta& SingleScrollbarAnimationControllerThinning::Duration() {
73 return thinning_duration_;
74 }
75
76 void SingleScrollbarAnimationControllerThinning::RunAnimationFrame(
77 float progress) {
78 if (captured_)
79 return;
80
81 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress));
82
83 client_->SetNeedsRedrawForScrollbarAnimation();
84 if (progress == 1.f) {
85 StopAnimation();
86 thickness_change_ = NONE;
87 }
88 }
89
90 void SingleScrollbarAnimationControllerThinning::StartAnimation() {
91 is_animating_ = true;
92 last_awaken_time_ = base::TimeTicks();
93 client_->SetNeedsAnimateForScrollbarAnimation();
94 }
95
96 void SingleScrollbarAnimationControllerThinning::StopAnimation() {
97 is_animating_ = false;
98 }
99
100 void SingleScrollbarAnimationControllerThinning::DidMouseDown() {
101 if (!mouse_is_over_scrollbar_)
102 return;
103
104 StopAnimation();
105 captured_ = true;
106 ApplyThumbThicknessScale(1.f);
107 }
108
109 void SingleScrollbarAnimationControllerThinning::DidMouseUp() {
110 if (!captured_)
111 return;
112
113 captured_ = false;
114 StopAnimation();
115
116 if (!mouse_is_near_scrollbar_) {
117 thickness_change_ = DECREASE;
118 StartAnimation();
119 } else {
120 thickness_change_ = NONE;
121 }
122 }
123
124 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() {
125 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_)
126 return;
127
128 mouse_is_over_scrollbar_ = false;
129 mouse_is_near_scrollbar_ = false;
130
131 if (captured_)
132 return;
133
134 thickness_change_ = DECREASE;
135 StartAnimation();
136 }
137
138 void SingleScrollbarAnimationControllerThinning::DidMouseMoveNear(
139 float distance) {
140 bool mouse_is_over_scrollbar = distance == 0.0f;
141 bool mouse_is_near_scrollbar =
142 distance < mouse_move_distance_to_trigger_animation_;
143
144 if (captured_) {
bokan 2017/01/20 18:57:25 You don't need this block, you can just wrap the t
145 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
146 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
147 return;
148 }
149
150 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ &&
151 mouse_is_near_scrollbar == mouse_is_near_scrollbar_)
152 return;
153
154 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar)
bokan 2017/01/20 18:57:25 Likewise, you don't need these checks.
155 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
156
157 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) {
158 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
159 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE;
160 }
161
162 StartAnimation();
163 }
164
165 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt(
166 float progress) {
167 if (thickness_change_ == NONE)
168 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale;
169 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
170 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
171 }
172
173 float SingleScrollbarAnimationControllerThinning::AdjustScale(
174 float new_value,
175 float current_value,
176 AnimationChange animation_change,
177 float min_value,
178 float max_value) {
179 float result;
180 if (animation_change == INCREASE && current_value > new_value)
181 result = current_value;
182 else if (animation_change == DECREASE && current_value < new_value)
183 result = current_value;
184 else
185 result = new_value;
186 if (result > max_value)
187 return max_value;
188 if (result < min_value)
189 return min_value;
190 return result;
191 }
192
193 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() {
194 StopAnimation();
195 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f
196 : kIdleThicknessScale);
197 }
198
199 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale(
200 float thumb_thickness_scale) {
201 for (ScrollbarLayerImplBase* scrollbar :
202 client_->ScrollbarsFor(scroll_layer_id_)) {
203 if (scrollbar->orientation() != orientation_)
204 continue;
205 if (!scrollbar->is_overlay_scrollbar())
206 continue;
207
208 float scale = AdjustScale(thumb_thickness_scale,
209 scrollbar->thumb_thickness_scale_factor(),
210 thickness_change_, kIdleThicknessScale, 1);
211
212 scrollbar->SetThumbThicknessScaleFactor(scale);
213 }
214 }
215
216 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698