OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/touch_selection/touch_handle.h" | 5 #include "ui/touch_selection/touch_handle.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 namespace ui { | 9 namespace ui { |
10 | 10 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 const float touch_radius = std::max( | 160 const float touch_radius = std::max( |
161 kMinTouchMajorForHitTesting, | 161 kMinTouchMajorForHitTesting, |
162 std::min(kMaxTouchMajorForHitTesting, event.GetTouchMajor())) * 0.5f; | 162 std::min(kMaxTouchMajorForHitTesting, event.GetTouchMajor())) * 0.5f; |
163 if (!RectIntersectsCircle(drawable_->GetVisibleBounds(), | 163 if (!RectIntersectsCircle(drawable_->GetVisibleBounds(), |
164 touch_point, | 164 touch_point, |
165 touch_radius)) { | 165 touch_radius)) { |
166 EndDrag(); | 166 EndDrag(); |
167 return false; | 167 return false; |
168 } | 168 } |
169 touch_down_position_ = touch_point; | 169 touch_down_position_ = touch_point; |
170 touch_to_focus_offset_ = position_ - touch_down_position_; | 170 touch_drag_offset_ = position_ - touch_down_position_; |
171 touch_down_time_ = event.GetEventTime(); | 171 touch_down_time_ = event.GetEventTime(); |
172 BeginDrag(); | 172 BeginDrag(); |
173 } break; | 173 } break; |
174 | 174 |
175 case MotionEvent::ACTION_MOVE: { | 175 case MotionEvent::ACTION_MOVE: { |
176 gfx::PointF touch_move_position(event.GetX(), event.GetY()); | 176 gfx::PointF touch_move_position(event.GetX(), event.GetY()); |
177 if (is_drag_within_tap_region_) { | 177 is_drag_within_tap_region_ &= |
178 const float tap_slop = client_->GetTapSlop(); | 178 client_->IsWithinTapSlop(touch_down_position_ - touch_move_position); |
179 is_drag_within_tap_region_ = | |
180 (touch_move_position - touch_down_position_).LengthSquared() < | |
181 tap_slop * tap_slop; | |
182 } | |
183 | 179 |
184 // Note that we signal drag update even if we're inside the tap region, | 180 // Note that we signal drag update even if we're inside the tap region, |
185 // as there are cases where characters are narrower than the slop length. | 181 // as there are cases where characters are narrower than the slop length. |
186 client_->OnHandleDragUpdate(*this, | 182 client_->OnDragUpdate(*this, touch_move_position + touch_drag_offset_); |
187 touch_move_position + touch_to_focus_offset_); | |
188 } break; | 183 } break; |
189 | 184 |
190 case MotionEvent::ACTION_UP: { | 185 case MotionEvent::ACTION_UP: { |
191 if (is_drag_within_tap_region_ && | 186 if (is_drag_within_tap_region_ && |
192 (event.GetEventTime() - touch_down_time_) < | 187 (event.GetEventTime() - touch_down_time_) < |
193 client_->GetTapTimeout()) { | 188 client_->GetTapTimeout()) { |
194 client_->OnHandleTapped(*this); | 189 client_->OnHandleTapped(*this); |
195 } | 190 } |
196 | 191 |
197 EndDrag(); | 192 EndDrag(); |
198 } break; | 193 } break; |
199 | 194 |
200 case MotionEvent::ACTION_CANCEL: | 195 case MotionEvent::ACTION_CANCEL: |
201 EndDrag(); | 196 EndDrag(); |
202 break; | 197 break; |
203 | 198 |
204 default: | 199 default: |
205 break; | 200 break; |
206 }; | 201 }; |
207 return true; | 202 return true; |
208 } | 203 } |
209 | 204 |
| 205 bool TouchHandle::IsActive() const { |
| 206 return is_dragging_; |
| 207 } |
| 208 |
210 bool TouchHandle::Animate(base::TimeTicks frame_time) { | 209 bool TouchHandle::Animate(base::TimeTicks frame_time) { |
211 if (fade_end_time_ == base::TimeTicks()) | 210 if (fade_end_time_ == base::TimeTicks()) |
212 return false; | 211 return false; |
213 | 212 |
214 DCHECK(enabled_); | 213 DCHECK(enabled_); |
215 | 214 |
216 float time_u = | 215 float time_u = |
217 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs; | 216 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs; |
218 float position_u = | 217 float position_u = |
219 (position_ - fade_start_position_).LengthSquared() / kFadeDistanceSquared; | 218 (position_ - fade_start_position_).LengthSquared() / kFadeDistanceSquared; |
(...skipping 15 matching lines...) Expand all Loading... |
235 return drawable_->GetVisibleBounds(); | 234 return drawable_->GetVisibleBounds(); |
236 } | 235 } |
237 | 236 |
238 void TouchHandle::BeginDrag() { | 237 void TouchHandle::BeginDrag() { |
239 DCHECK(enabled_); | 238 DCHECK(enabled_); |
240 if (is_dragging_) | 239 if (is_dragging_) |
241 return; | 240 return; |
242 EndFade(); | 241 EndFade(); |
243 is_dragging_ = true; | 242 is_dragging_ = true; |
244 is_drag_within_tap_region_ = true; | 243 is_drag_within_tap_region_ = true; |
245 client_->OnHandleDragBegin(*this); | 244 client_->OnDragBegin(*this, position()); |
246 } | 245 } |
247 | 246 |
248 void TouchHandle::EndDrag() { | 247 void TouchHandle::EndDrag() { |
249 DCHECK(enabled_); | 248 DCHECK(enabled_); |
250 if (!is_dragging_) | 249 if (!is_dragging_) |
251 return; | 250 return; |
252 | 251 |
253 is_dragging_ = false; | 252 is_dragging_ = false; |
254 is_drag_within_tap_region_ = false; | 253 is_drag_within_tap_region_ = false; |
255 client_->OnHandleDragEnd(*this); | 254 client_->OnDragEnd(*this); |
256 | 255 |
257 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) { | 256 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) { |
258 TouchHandleOrientation deferred_orientation = deferred_orientation_; | 257 TouchHandleOrientation deferred_orientation = deferred_orientation_; |
259 deferred_orientation_ = TouchHandleOrientation::UNDEFINED; | 258 deferred_orientation_ = TouchHandleOrientation::UNDEFINED; |
260 SetOrientation(deferred_orientation); | 259 SetOrientation(deferred_orientation); |
261 } | 260 } |
262 | 261 |
263 if (animate_deferred_fade_) { | 262 if (animate_deferred_fade_) { |
264 BeginFade(); | 263 BeginFade(); |
265 } else { | 264 } else { |
(...skipping 29 matching lines...) Expand all Loading... |
295 | 294 |
296 void TouchHandle::SetAlpha(float alpha) { | 295 void TouchHandle::SetAlpha(float alpha) { |
297 alpha = std::max(0.f, std::min(1.f, alpha)); | 296 alpha = std::max(0.f, std::min(1.f, alpha)); |
298 if (alpha_ == alpha) | 297 if (alpha_ == alpha) |
299 return; | 298 return; |
300 alpha_ = alpha; | 299 alpha_ = alpha; |
301 drawable_->SetAlpha(alpha); | 300 drawable_->SetAlpha(alpha); |
302 } | 301 } |
303 | 302 |
304 } // namespace ui | 303 } // namespace ui |
OLD | NEW |