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

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: weiliangc comment addressed 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 DistanceToScrollbarPart(const gfx::PointF& device_viewport_point,
21 const ScrollbarLayerImplBase& scrollbar,
22 const ScrollbarPart part) {
23 gfx::RectF rect;
24 if (part == ScrollbarPart::THUMB) {
25 rect = gfx::RectF(gfx::Rect(scrollbar.ComputeExpandedThumbQuadRect()));
26 } else {
27 rect = gfx::RectF(gfx::Rect(scrollbar.bounds()));
28 }
29
30 gfx::RectF device_viewport_rect =
31 MathUtil::MapClippedRect(scrollbar.ScreenSpaceTransform(), rect);
32
33 return device_viewport_rect.ManhattanDistanceToPoint(device_viewport_point) /
34 scrollbar.layer_tree_impl()->device_scale_factor();
35 }
36
37 } // namespace
38
18 std::unique_ptr<SingleScrollbarAnimationControllerThinning> 39 std::unique_ptr<SingleScrollbarAnimationControllerThinning>
19 SingleScrollbarAnimationControllerThinning::Create( 40 SingleScrollbarAnimationControllerThinning::Create(
20 ElementId scroll_element_id, 41 ElementId scroll_element_id,
21 ScrollbarOrientation orientation, 42 ScrollbarOrientation orientation,
22 ScrollbarAnimationControllerClient* client, 43 ScrollbarAnimationControllerClient* client,
23 base::TimeDelta thinning_duration) { 44 base::TimeDelta thinning_duration) {
24 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning( 45 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning(
25 scroll_element_id, orientation, client, thinning_duration)); 46 scroll_element_id, orientation, client, thinning_duration));
26 } 47 }
27 48
28 SingleScrollbarAnimationControllerThinning:: 49 SingleScrollbarAnimationControllerThinning::
29 SingleScrollbarAnimationControllerThinning( 50 SingleScrollbarAnimationControllerThinning(
30 ElementId scroll_element_id, 51 ElementId scroll_element_id,
31 ScrollbarOrientation orientation, 52 ScrollbarOrientation orientation,
32 ScrollbarAnimationControllerClient* client, 53 ScrollbarAnimationControllerClient* client,
33 base::TimeDelta thinning_duration) 54 base::TimeDelta thinning_duration)
34 : client_(client), 55 : client_(client),
35 is_animating_(false), 56 is_animating_(false),
36 scroll_element_id_(scroll_element_id), 57 scroll_element_id_(scroll_element_id),
37 orientation_(orientation), 58 orientation_(orientation),
38 captured_(false), 59 captured_(false),
39 mouse_is_over_scrollbar_(false), 60 mouse_is_over_scrollbar_thumb_(false),
40 mouse_is_near_scrollbar_(false), 61 mouse_is_near_scrollbar_thumb_(false),
62 mouse_is_near_scrollbar_track_(false),
41 thickness_change_(NONE), 63 thickness_change_(NONE),
42 thinning_duration_(thinning_duration) { 64 thinning_duration_(thinning_duration) {
43 ApplyThumbThicknessScale(kIdleThicknessScale); 65 ApplyThumbThicknessScale(kIdleThicknessScale);
44 } 66 }
45 67
68 ScrollbarLayerImplBase*
69 SingleScrollbarAnimationControllerThinning::GetScrollbar() const {
70 for (ScrollbarLayerImplBase* scrollbar :
71 client_->ScrollbarsFor(scroll_element_id_)) {
72 DCHECK(scrollbar->is_overlay_scrollbar());
73
74 if (scrollbar->orientation() == orientation_)
75 return scrollbar;
76 }
77
78 return nullptr;
79 }
80
46 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) { 81 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
47 if (!is_animating_) 82 if (!is_animating_)
48 return false; 83 return false;
49 84
50 if (last_awaken_time_.is_null()) 85 if (last_awaken_time_.is_null())
51 last_awaken_time_ = now; 86 last_awaken_time_ = now;
52 87
53 float progress = AnimationProgressAtTime(now); 88 float progress = AnimationProgressAtTime(now);
54 RunAnimationFrame(progress); 89 RunAnimationFrame(progress);
55 90
(...skipping 29 matching lines...) Expand all
85 is_animating_ = true; 120 is_animating_ = true;
86 last_awaken_time_ = base::TimeTicks(); 121 last_awaken_time_ = base::TimeTicks();
87 client_->SetNeedsAnimateForScrollbarAnimation(); 122 client_->SetNeedsAnimateForScrollbarAnimation();
88 } 123 }
89 124
90 void SingleScrollbarAnimationControllerThinning::StopAnimation() { 125 void SingleScrollbarAnimationControllerThinning::StopAnimation() {
91 is_animating_ = false; 126 is_animating_ = false;
92 } 127 }
93 128
94 void SingleScrollbarAnimationControllerThinning::DidMouseDown() { 129 void SingleScrollbarAnimationControllerThinning::DidMouseDown() {
95 if (!mouse_is_over_scrollbar_) 130 if (!mouse_is_over_scrollbar_thumb_)
96 return; 131 return;
97 132
98 StopAnimation(); 133 StopAnimation();
99 captured_ = true; 134 captured_ = true;
100 ApplyThumbThicknessScale(1.f); 135 ApplyThumbThicknessScale(1.f);
101 } 136 }
102 137
103 void SingleScrollbarAnimationControllerThinning::DidMouseUp() { 138 void SingleScrollbarAnimationControllerThinning::DidMouseUp() {
104 if (!captured_) 139 if (!captured_)
105 return; 140 return;
106 141
107 captured_ = false; 142 captured_ = false;
108 StopAnimation(); 143 StopAnimation();
109 144
110 if (!mouse_is_near_scrollbar_) { 145 if (!mouse_is_near_scrollbar_thumb_) {
111 thickness_change_ = DECREASE; 146 thickness_change_ = DECREASE;
112 StartAnimation(); 147 StartAnimation();
113 } else { 148 } else {
114 thickness_change_ = NONE; 149 thickness_change_ = NONE;
115 } 150 }
116 } 151 }
117 152
118 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() { 153 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() {
119 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) 154 if (!mouse_is_over_scrollbar_thumb_ && !mouse_is_near_scrollbar_thumb_)
120 return; 155 return;
121 156
122 mouse_is_over_scrollbar_ = false; 157 mouse_is_over_scrollbar_thumb_ = false;
123 mouse_is_near_scrollbar_ = false; 158 mouse_is_near_scrollbar_thumb_ = false;
159 mouse_is_near_scrollbar_track_ = false;
124 160
125 if (captured_) 161 if (captured_)
126 return; 162 return;
127 163
128 thickness_change_ = DECREASE; 164 thickness_change_ = DECREASE;
129 StartAnimation(); 165 StartAnimation();
130 } 166 }
131 167
132 void SingleScrollbarAnimationControllerThinning::DidMouseMoveNear( 168 void SingleScrollbarAnimationControllerThinning::DidMouseMove(
133 float distance) { 169 const gfx::PointF& device_viewport_point) {
134 bool mouse_is_over_scrollbar = distance == 0.0f; 170 ScrollbarLayerImplBase* scrollbar = GetScrollbar();
135 bool mouse_is_near_scrollbar =
136 distance < kDefaultMouseMoveDistanceToTriggerAnimation;
137 171
138 if (!captured_ && mouse_is_near_scrollbar != mouse_is_near_scrollbar_) { 172 if (!scrollbar)
139 thickness_change_ = mouse_is_near_scrollbar ? INCREASE : DECREASE; 173 return;
174
175 float distance_to_scrollbar_track = DistanceToScrollbarPart(
176 device_viewport_point, *scrollbar, ScrollbarPart::TRACK);
177 float distance_to_scrollbar_thumb = DistanceToScrollbarPart(
178 device_viewport_point, *scrollbar, ScrollbarPart::THUMB);
179
180 mouse_is_near_scrollbar_track_ =
181 distance_to_scrollbar_track <
182 ScrollbarAnimationController::kMouseMoveDistanceToTriggerFadeIn;
183
184 bool mouse_is_over_scrollbar_thumb = distance_to_scrollbar_thumb == 0.0f;
185 bool mouse_is_near_scrollbar_thumb =
186 distance_to_scrollbar_thumb < kMouseMoveDistanceToTriggerExpand;
187
188 if (!captured_ &&
189 mouse_is_near_scrollbar_thumb != mouse_is_near_scrollbar_thumb_) {
190 thickness_change_ = mouse_is_near_scrollbar_thumb ? INCREASE : DECREASE;
140 StartAnimation(); 191 StartAnimation();
141 } 192 }
142 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; 193 mouse_is_near_scrollbar_thumb_ = mouse_is_near_scrollbar_thumb;
143 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; 194 mouse_is_over_scrollbar_thumb_ = mouse_is_over_scrollbar_thumb;
144 } 195 }
145 196
146 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt( 197 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt(
147 float progress) { 198 float progress) {
148 if (thickness_change_ == NONE) 199 if (thickness_change_ == NONE)
149 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; 200 return mouse_is_near_scrollbar_thumb_ ? 1.f : kIdleThicknessScale;
150 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); 201 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
151 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; 202 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
152 } 203 }
153 204
154 float SingleScrollbarAnimationControllerThinning::AdjustScale( 205 float SingleScrollbarAnimationControllerThinning::AdjustScale(
155 float new_value, 206 float new_value,
156 float current_value, 207 float current_value,
157 AnimationChange animation_change, 208 AnimationChange animation_change,
158 float min_value, 209 float min_value,
159 float max_value) { 210 float max_value) {
160 float result; 211 float result;
161 if (animation_change == INCREASE && current_value > new_value) 212 if (animation_change == INCREASE && current_value > new_value)
162 result = current_value; 213 result = current_value;
163 else if (animation_change == DECREASE && current_value < new_value) 214 else if (animation_change == DECREASE && current_value < new_value)
164 result = current_value; 215 result = current_value;
165 else 216 else
166 result = new_value; 217 result = new_value;
167 if (result > max_value) 218 if (result > max_value)
168 return max_value; 219 return max_value;
169 if (result < min_value) 220 if (result < min_value)
170 return min_value; 221 return min_value;
171 return result; 222 return result;
172 } 223 }
173 224
174 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() { 225 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() {
175 StopAnimation(); 226 StopAnimation();
176 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f 227 ApplyThumbThicknessScale(
177 : kIdleThicknessScale); 228 mouse_is_near_scrollbar_thumb_ ? 1.f : kIdleThicknessScale);
178 } 229 }
179 230
180 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale( 231 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale(
181 float thumb_thickness_scale) { 232 float thumb_thickness_scale) {
182 for (auto* scrollbar : client_->ScrollbarsFor(scroll_element_id_)) { 233 for (auto* scrollbar : client_->ScrollbarsFor(scroll_element_id_)) {
183 if (scrollbar->orientation() != orientation_) 234 if (scrollbar->orientation() != orientation_)
184 continue; 235 continue;
185 if (!scrollbar->is_overlay_scrollbar()) 236 DCHECK(scrollbar->is_overlay_scrollbar());
186 continue;
187 237
188 float scale = AdjustScale(thumb_thickness_scale, 238 float scale = AdjustScale(thumb_thickness_scale,
189 scrollbar->thumb_thickness_scale_factor(), 239 scrollbar->thumb_thickness_scale_factor(),
190 thickness_change_, kIdleThicknessScale, 1); 240 thickness_change_, kIdleThicknessScale, 1);
191 241
192 scrollbar->SetThumbThicknessScaleFactor(scale); 242 scrollbar->SetThumbThicknessScaleFactor(scale);
193 } 243 }
194 } 244 }
195 245
196 } // namespace cc 246 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698