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

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

Issue 2841943002: Overlay scrollbars expand only when mouse is near thumb (Closed)
Patch Set: pass point to SingleScrollbarController Created 3 years, 7 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
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/single_scrollbar_animation_controller_thinning.h" 5 #include "cc/input/single_scrollbar_animation_controller_thinning.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "cc/input/scrollbar_animation_controller.h" 11 #include "cc/input/scrollbar_animation_controller.h"
12 #include "cc/layers/layer_impl.h" 12 #include "cc/layers/layer_impl.h"
13 #include "cc/layers/scrollbar_layer_impl_base.h" 13 #include "cc/layers/scrollbar_layer_impl_base.h"
14 #include "cc/trees/layer_tree_impl.h" 14 #include "cc/trees/layer_tree_impl.h"
15 15
16 namespace cc { 16 namespace cc {
17 17
18 namespace {
19
20 float DistanceToScrollbarTrack(const gfx::PointF& device_viewport_point,
21 const ScrollbarLayerImplBase* scrollbar) {
22 if (!scrollbar)
bokan 2017/04/28 18:08:11 Make the scrollbar param a ref and avoid calling t
23 return std::numeric_limits<float>::max();
24
25 gfx::Rect scrollbar_bounds(scrollbar->bounds());
26
27 gfx::RectF device_viewport_scrollbar_bounds = MathUtil::MapClippedRect(
28 scrollbar->ScreenSpaceTransform(), gfx::RectF(scrollbar_bounds));
29
30 return device_viewport_scrollbar_bounds.ManhattanDistanceToPoint(
31 device_viewport_point) /
32 scrollbar->layer_tree_impl()->device_scale_factor();
33 }
34
35 float DistanceToScrollbarThumb(const gfx::PointF& device_viewport_point,
36 const ScrollbarLayerImplBase* scrollbar) {
37 if (!scrollbar)
38 return std::numeric_limits<float>::max();
39
40 gfx::Rect thumb_bounds(scrollbar->ComputeExpandedThumbQuadRect());
41
42 gfx::RectF device_viewport_scrollbar_thumb_bounds = MathUtil::MapClippedRect(
43 scrollbar->ScreenSpaceTransform(), gfx::RectF(thumb_bounds));
44
45 return device_viewport_scrollbar_thumb_bounds.ManhattanDistanceToPoint(
46 device_viewport_point) /
47 scrollbar->layer_tree_impl()->device_scale_factor();
48 }
49
50 } // namespace
51
18 std::unique_ptr<SingleScrollbarAnimationControllerThinning> 52 std::unique_ptr<SingleScrollbarAnimationControllerThinning>
19 SingleScrollbarAnimationControllerThinning::Create( 53 SingleScrollbarAnimationControllerThinning::Create(
20 ElementId scroll_element_id, 54 ElementId scroll_element_id,
21 ScrollbarOrientation orientation, 55 ScrollbarOrientation orientation,
22 ScrollbarAnimationControllerClient* client, 56 ScrollbarAnimationControllerClient* client,
23 base::TimeDelta thinning_duration) { 57 base::TimeDelta thinning_duration) {
24 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning( 58 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning(
25 scroll_element_id, orientation, client, thinning_duration)); 59 scroll_element_id, orientation, client, thinning_duration));
26 } 60 }
27 61
28 SingleScrollbarAnimationControllerThinning:: 62 SingleScrollbarAnimationControllerThinning::
29 SingleScrollbarAnimationControllerThinning( 63 SingleScrollbarAnimationControllerThinning(
30 ElementId scroll_element_id, 64 ElementId scroll_element_id,
31 ScrollbarOrientation orientation, 65 ScrollbarOrientation orientation,
32 ScrollbarAnimationControllerClient* client, 66 ScrollbarAnimationControllerClient* client,
33 base::TimeDelta thinning_duration) 67 base::TimeDelta thinning_duration)
34 : client_(client), 68 : client_(client),
35 is_animating_(false), 69 is_animating_(false),
36 scroll_element_id_(scroll_element_id), 70 scroll_element_id_(scroll_element_id),
37 orientation_(orientation), 71 orientation_(orientation),
38 captured_(false), 72 captured_(false),
39 mouse_is_over_scrollbar_(false), 73 mouse_is_over_scrollbar_thumb_(false),
40 mouse_is_near_scrollbar_(false), 74 mouse_is_near_scrollbar_thumb_(false),
75 mouse_is_near_scrollbar_track_(false),
41 thickness_change_(NONE), 76 thickness_change_(NONE),
42 thinning_duration_(thinning_duration) { 77 thinning_duration_(thinning_duration) {
43 ApplyThumbThicknessScale(kIdleThicknessScale); 78 ApplyThumbThicknessScale(kIdleThicknessScale);
44 } 79 }
45 80
81 ScrollbarLayerImplBase*
82 SingleScrollbarAnimationControllerThinning::GetScrollbar() const {
83 for (ScrollbarLayerImplBase* scrollbar :
84 client_->ScrollbarsFor(scroll_element_id_)) {
85 if (!scrollbar->is_overlay_scrollbar())
bokan 2017/04/28 18:08:11 This can be a DCHECK right? We should never have a
86 continue;
87
88 if (scrollbar->orientation() == orientation_)
89 return scrollbar;
90 }
91
92 return nullptr;
93 }
94
46 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) { 95 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
47 if (!is_animating_) 96 if (!is_animating_)
48 return false; 97 return false;
49 98
50 if (last_awaken_time_.is_null()) 99 if (last_awaken_time_.is_null())
51 last_awaken_time_ = now; 100 last_awaken_time_ = now;
52 101
53 float progress = AnimationProgressAtTime(now); 102 float progress = AnimationProgressAtTime(now);
54 RunAnimationFrame(progress); 103 RunAnimationFrame(progress);
55 104
(...skipping 29 matching lines...) Expand all
85 is_animating_ = true; 134 is_animating_ = true;
86 last_awaken_time_ = base::TimeTicks(); 135 last_awaken_time_ = base::TimeTicks();
87 client_->SetNeedsAnimateForScrollbarAnimation(); 136 client_->SetNeedsAnimateForScrollbarAnimation();
88 } 137 }
89 138
90 void SingleScrollbarAnimationControllerThinning::StopAnimation() { 139 void SingleScrollbarAnimationControllerThinning::StopAnimation() {
91 is_animating_ = false; 140 is_animating_ = false;
92 } 141 }
93 142
94 void SingleScrollbarAnimationControllerThinning::DidMouseDown() { 143 void SingleScrollbarAnimationControllerThinning::DidMouseDown() {
95 if (!mouse_is_over_scrollbar_) 144 if (!mouse_is_over_scrollbar_thumb_)
96 return; 145 return;
97 146
98 StopAnimation(); 147 StopAnimation();
99 captured_ = true; 148 captured_ = true;
100 ApplyThumbThicknessScale(1.f); 149 ApplyThumbThicknessScale(1.f);
101 } 150 }
102 151
103 void SingleScrollbarAnimationControllerThinning::DidMouseUp() { 152 void SingleScrollbarAnimationControllerThinning::DidMouseUp() {
104 if (!captured_) 153 if (!captured_)
105 return; 154 return;
106 155
107 captured_ = false; 156 captured_ = false;
108 StopAnimation(); 157 StopAnimation();
109 158
110 if (!mouse_is_near_scrollbar_) { 159 if (!mouse_is_near_scrollbar_thumb_) {
111 thickness_change_ = DECREASE; 160 thickness_change_ = DECREASE;
112 StartAnimation(); 161 StartAnimation();
113 } else { 162 } else {
114 thickness_change_ = NONE; 163 thickness_change_ = NONE;
115 } 164 }
116 } 165 }
117 166
118 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() { 167 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() {
119 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) 168 if (!mouse_is_over_scrollbar_thumb_ && !mouse_is_near_scrollbar_thumb_)
120 return; 169 return;
121 170
122 mouse_is_over_scrollbar_ = false; 171 mouse_is_over_scrollbar_thumb_ = false;
123 mouse_is_near_scrollbar_ = false; 172 mouse_is_near_scrollbar_thumb_ = false;
173 mouse_is_near_scrollbar_track_ = false;
124 174
125 if (captured_) 175 if (captured_)
126 return; 176 return;
127 177
128 thickness_change_ = DECREASE; 178 thickness_change_ = DECREASE;
129 StartAnimation(); 179 StartAnimation();
130 } 180 }
131 181
132 void SingleScrollbarAnimationControllerThinning::DidMouseMoveNear( 182 void SingleScrollbarAnimationControllerThinning::DidMouseMove(
133 float distance) { 183 const gfx::PointF& device_viewport_point) {
134 bool mouse_is_over_scrollbar = distance == 0.0f; 184 ScrollbarLayerImplBase* scrollbar = GetScrollbar();
135 bool mouse_is_near_scrollbar =
136 distance < kDefaultMouseMoveDistanceToTriggerAnimation;
137 185
138 if (!captured_ && mouse_is_near_scrollbar != mouse_is_near_scrollbar_) { 186 float distance_to_scrollbar_track =
139 thickness_change_ = mouse_is_near_scrollbar ? INCREASE : DECREASE; 187 DistanceToScrollbarTrack(device_viewport_point, scrollbar);
188 float distance_to_scrollbar_thumb =
189 DistanceToScrollbarThumb(device_viewport_point, scrollbar);
190
191 mouse_is_near_scrollbar_track_ =
192 distance_to_scrollbar_track <
193 ScrollbarAnimationController::kMouseMoveDistanceToTriggerFadeIn;
194
195 bool mouse_is_over_scrollbar_thumb = distance_to_scrollbar_thumb == 0.0f;
196 bool mouse_is_near_scrollbar_thumb =
197 distance_to_scrollbar_thumb < kMouseMoveDistanceToTriggerExpand;
198
199 if (!captured_ &&
200 mouse_is_near_scrollbar_thumb != mouse_is_near_scrollbar_thumb_) {
201 thickness_change_ = mouse_is_near_scrollbar_thumb ? INCREASE : DECREASE;
140 StartAnimation(); 202 StartAnimation();
141 } 203 }
142 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; 204 mouse_is_near_scrollbar_thumb_ = mouse_is_near_scrollbar_thumb;
143 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; 205 mouse_is_over_scrollbar_thumb_ = mouse_is_over_scrollbar_thumb;
144 } 206 }
145 207
146 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt( 208 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt(
147 float progress) { 209 float progress) {
148 if (thickness_change_ == NONE) 210 if (thickness_change_ == NONE)
149 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; 211 return mouse_is_near_scrollbar_thumb_ ? 1.f : kIdleThicknessScale;
150 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); 212 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
151 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; 213 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
152 } 214 }
153 215
154 float SingleScrollbarAnimationControllerThinning::AdjustScale( 216 float SingleScrollbarAnimationControllerThinning::AdjustScale(
155 float new_value, 217 float new_value,
156 float current_value, 218 float current_value,
157 AnimationChange animation_change, 219 AnimationChange animation_change,
158 float min_value, 220 float min_value,
159 float max_value) { 221 float max_value) {
160 float result; 222 float result;
161 if (animation_change == INCREASE && current_value > new_value) 223 if (animation_change == INCREASE && current_value > new_value)
162 result = current_value; 224 result = current_value;
163 else if (animation_change == DECREASE && current_value < new_value) 225 else if (animation_change == DECREASE && current_value < new_value)
164 result = current_value; 226 result = current_value;
165 else 227 else
166 result = new_value; 228 result = new_value;
167 if (result > max_value) 229 if (result > max_value)
168 return max_value; 230 return max_value;
169 if (result < min_value) 231 if (result < min_value)
170 return min_value; 232 return min_value;
171 return result; 233 return result;
172 } 234 }
173 235
174 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() { 236 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() {
175 StopAnimation(); 237 StopAnimation();
176 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f 238 ApplyThumbThicknessScale(
177 : kIdleThicknessScale); 239 mouse_is_near_scrollbar_thumb_ ? 1.f : kIdleThicknessScale);
178 } 240 }
179 241
180 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale( 242 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale(
181 float thumb_thickness_scale) { 243 float thumb_thickness_scale) {
182 for (auto* scrollbar : client_->ScrollbarsFor(scroll_element_id_)) { 244 for (auto* scrollbar : client_->ScrollbarsFor(scroll_element_id_)) {
183 if (scrollbar->orientation() != orientation_) 245 if (scrollbar->orientation() != orientation_)
184 continue; 246 continue;
185 if (!scrollbar->is_overlay_scrollbar()) 247 if (!scrollbar->is_overlay_scrollbar())
186 continue; 248 continue;
187 249
188 float scale = AdjustScale(thumb_thickness_scale, 250 float scale = AdjustScale(thumb_thickness_scale,
189 scrollbar->thumb_thickness_scale_factor(), 251 scrollbar->thumb_thickness_scale_factor(),
190 thickness_change_, kIdleThicknessScale, 1); 252 thickness_change_, kIdleThicknessScale, 1);
191 253
192 scrollbar->SetThumbThicknessScaleFactor(scale); 254 scrollbar->SetThumbThicknessScaleFactor(scale);
193 } 255 }
194 } 256 }
195 257
196 } // namespace cc 258 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698