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