| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/touch_selection/touch_selection_controller.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/metrics/histogram_macros.h" | |
| 10 | |
| 11 namespace ui { | |
| 12 namespace { | |
| 13 | |
| 14 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) { | |
| 15 gfx::Vector2dF line_offset = | |
| 16 gfx::ScaleVector2d(bound.edge_top() - bound.edge_bottom(), 0.5f); | |
| 17 // An offset of 5 DIPs is sufficient for most line sizes. For small lines, | |
| 18 // using half the line height avoids synthesizing a point on a line above | |
| 19 // (or below) the intended line. | |
| 20 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f); | |
| 21 line_offset.SetToMin(kMaxLineOffset); | |
| 22 line_offset.SetToMax(-kMaxLineOffset); | |
| 23 return line_offset; | |
| 24 } | |
| 25 | |
| 26 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { | |
| 27 switch (type) { | |
| 28 case SelectionBound::LEFT: | |
| 29 return TouchHandleOrientation::LEFT; | |
| 30 case SelectionBound::RIGHT: | |
| 31 return TouchHandleOrientation::RIGHT; | |
| 32 case SelectionBound::CENTER: | |
| 33 return TouchHandleOrientation::CENTER; | |
| 34 case SelectionBound::EMPTY: | |
| 35 return TouchHandleOrientation::UNDEFINED; | |
| 36 } | |
| 37 NOTREACHED() << "Invalid selection bound type: " << type; | |
| 38 return TouchHandleOrientation::UNDEFINED; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 TouchSelectionController::TouchSelectionController( | |
| 44 TouchSelectionControllerClient* client, | |
| 45 base::TimeDelta tap_timeout, | |
| 46 float tap_slop, | |
| 47 bool show_on_tap_for_empty_editable) | |
| 48 : client_(client), | |
| 49 tap_timeout_(tap_timeout), | |
| 50 tap_slop_(tap_slop), | |
| 51 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), | |
| 52 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), | |
| 53 start_orientation_(TouchHandleOrientation::UNDEFINED), | |
| 54 end_orientation_(TouchHandleOrientation::UNDEFINED), | |
| 55 is_insertion_active_(false), | |
| 56 activate_insertion_automatically_(false), | |
| 57 is_selection_active_(false), | |
| 58 activate_selection_automatically_(false), | |
| 59 selection_empty_(false), | |
| 60 selection_editable_(false), | |
| 61 temporarily_hidden_(false), | |
| 62 selection_handle_dragged_(false) { | |
| 63 DCHECK(client_); | |
| 64 } | |
| 65 | |
| 66 TouchSelectionController::~TouchSelectionController() { | |
| 67 } | |
| 68 | |
| 69 void TouchSelectionController::OnSelectionBoundsChanged( | |
| 70 const SelectionBound& start, | |
| 71 const SelectionBound& end) { | |
| 72 if (start == start_ && end_ == end) | |
| 73 return; | |
| 74 | |
| 75 start_ = start; | |
| 76 end_ = end; | |
| 77 start_orientation_ = ToTouchHandleOrientation(start_.type()); | |
| 78 end_orientation_ = ToTouchHandleOrientation(end_.type()); | |
| 79 | |
| 80 if (!activate_selection_automatically_ && | |
| 81 !activate_insertion_automatically_) { | |
| 82 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 // Ensure that |response_pending_input_event_| is cleared after the method | |
| 87 // completes, while also making its current value available for the duration | |
| 88 // of the call. | |
| 89 InputEventType causal_input_event = response_pending_input_event_; | |
| 90 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | |
| 91 base::AutoReset<InputEventType> auto_reset_response_pending_input_event( | |
| 92 &response_pending_input_event_, causal_input_event); | |
| 93 | |
| 94 const bool is_selection_dragging = | |
| 95 is_selection_active_ && (start_selection_handle_->is_dragging() || | |
| 96 end_selection_handle_->is_dragging()); | |
| 97 | |
| 98 // It's possible that the bounds temporarily overlap while a selection handle | |
| 99 // is being dragged, incorrectly reporting a CENTER orientation. | |
| 100 // TODO(jdduke): This safeguard is racy, as it's possible the delayed response | |
| 101 // from handle positioning occurs *after* the handle dragging has ceased. | |
| 102 // Instead, prevent selection -> insertion transitions without an intervening | |
| 103 // action or selection clearing of some sort, crbug.com/392696. | |
| 104 if (is_selection_dragging) { | |
| 105 if (start_orientation_ == TouchHandleOrientation::CENTER) | |
| 106 start_orientation_ = start_selection_handle_->orientation(); | |
| 107 if (end_orientation_ == TouchHandleOrientation::CENTER) | |
| 108 end_orientation_ = end_selection_handle_->orientation(); | |
| 109 } | |
| 110 | |
| 111 if (GetStartPosition() != GetEndPosition() || | |
| 112 (is_selection_dragging && | |
| 113 start_orientation_ != TouchHandleOrientation::UNDEFINED && | |
| 114 end_orientation_ != TouchHandleOrientation::UNDEFINED)) { | |
| 115 OnSelectionChanged(); | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 if (start_orientation_ == TouchHandleOrientation::CENTER && | |
| 120 selection_editable_) { | |
| 121 OnInsertionChanged(); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 HideAndDisallowShowingAutomatically(); | |
| 126 } | |
| 127 | |
| 128 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { | |
| 129 if (is_insertion_active_) { | |
| 130 DCHECK(insertion_handle_); | |
| 131 return insertion_handle_->WillHandleTouchEvent(event); | |
| 132 } | |
| 133 | |
| 134 if (is_selection_active_) { | |
| 135 DCHECK(start_selection_handle_); | |
| 136 DCHECK(end_selection_handle_); | |
| 137 if (start_selection_handle_->is_dragging()) | |
| 138 return start_selection_handle_->WillHandleTouchEvent(event); | |
| 139 | |
| 140 if (end_selection_handle_->is_dragging()) | |
| 141 return end_selection_handle_->WillHandleTouchEvent(event); | |
| 142 | |
| 143 const gfx::PointF event_pos(event.GetX(), event.GetY()); | |
| 144 if ((event_pos - GetStartPosition()).LengthSquared() <= | |
| 145 (event_pos - GetEndPosition()).LengthSquared()) | |
| 146 return start_selection_handle_->WillHandleTouchEvent(event); | |
| 147 else | |
| 148 return end_selection_handle_->WillHandleTouchEvent(event); | |
| 149 } | |
| 150 | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 void TouchSelectionController::OnLongPressEvent() { | |
| 155 response_pending_input_event_ = LONG_PRESS; | |
| 156 ShowSelectionHandlesAutomatically(); | |
| 157 ShowInsertionHandleAutomatically(); | |
| 158 ResetCachedValuesIfInactive(); | |
| 159 } | |
| 160 | |
| 161 void TouchSelectionController::AllowShowingFromCurrentSelection() { | |
| 162 if (is_selection_active_ || is_insertion_active_) | |
| 163 return; | |
| 164 | |
| 165 activate_selection_automatically_ = true; | |
| 166 activate_insertion_automatically_ = true; | |
| 167 if (GetStartPosition() != GetEndPosition()) | |
| 168 OnSelectionChanged(); | |
| 169 else if (start_orientation_ == TouchHandleOrientation::CENTER && | |
| 170 selection_editable_) | |
| 171 OnInsertionChanged(); | |
| 172 } | |
| 173 | |
| 174 void TouchSelectionController::OnTapEvent() { | |
| 175 response_pending_input_event_ = TAP; | |
| 176 ShowInsertionHandleAutomatically(); | |
| 177 if (selection_empty_ && !show_on_tap_for_empty_editable_) | |
| 178 DeactivateInsertion(); | |
| 179 ResetCachedValuesIfInactive(); | |
| 180 } | |
| 181 | |
| 182 void TouchSelectionController::HideAndDisallowShowingAutomatically() { | |
| 183 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | |
| 184 DeactivateInsertion(); | |
| 185 DeactivateSelection(); | |
| 186 activate_insertion_automatically_ = false; | |
| 187 activate_selection_automatically_ = false; | |
| 188 } | |
| 189 | |
| 190 void TouchSelectionController::SetTemporarilyHidden(bool hidden) { | |
| 191 if (temporarily_hidden_ == hidden) | |
| 192 return; | |
| 193 temporarily_hidden_ = hidden; | |
| 194 | |
| 195 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); | |
| 196 if (is_selection_active_) { | |
| 197 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); | |
| 198 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); | |
| 199 } | |
| 200 if (is_insertion_active_) | |
| 201 insertion_handle_->SetVisible(GetStartVisible(), animation_style); | |
| 202 } | |
| 203 | |
| 204 void TouchSelectionController::OnSelectionEditable(bool editable) { | |
| 205 if (selection_editable_ == editable) | |
| 206 return; | |
| 207 selection_editable_ = editable; | |
| 208 ResetCachedValuesIfInactive(); | |
| 209 if (!selection_editable_) | |
| 210 DeactivateInsertion(); | |
| 211 } | |
| 212 | |
| 213 void TouchSelectionController::OnSelectionEmpty(bool empty) { | |
| 214 if (selection_empty_ == empty) | |
| 215 return; | |
| 216 selection_empty_ = empty; | |
| 217 ResetCachedValuesIfInactive(); | |
| 218 } | |
| 219 | |
| 220 bool TouchSelectionController::Animate(base::TimeTicks frame_time) { | |
| 221 if (is_insertion_active_) | |
| 222 return insertion_handle_->Animate(frame_time); | |
| 223 | |
| 224 if (is_selection_active_) { | |
| 225 bool needs_animate = start_selection_handle_->Animate(frame_time); | |
| 226 needs_animate |= end_selection_handle_->Animate(frame_time); | |
| 227 return needs_animate; | |
| 228 } | |
| 229 | |
| 230 return false; | |
| 231 } | |
| 232 | |
| 233 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { | |
| 234 if (&handle == insertion_handle_.get()) { | |
| 235 client_->OnSelectionEvent(INSERTION_DRAG_STARTED, handle.position()); | |
| 236 return; | |
| 237 } | |
| 238 | |
| 239 gfx::PointF base, extent; | |
| 240 if (&handle == start_selection_handle_.get()) { | |
| 241 base = end_selection_handle_->position() + GetEndLineOffset(); | |
| 242 extent = start_selection_handle_->position() + GetStartLineOffset(); | |
| 243 } else { | |
| 244 base = start_selection_handle_->position() + GetStartLineOffset(); | |
| 245 extent = end_selection_handle_->position() + GetEndLineOffset(); | |
| 246 } | |
| 247 selection_handle_dragged_ = true; | |
| 248 | |
| 249 // When moving the handle we want to move only the extent point. Before doing | |
| 250 // so we must make sure that the base point is set correctly. | |
| 251 client_->SelectBetweenCoordinates(base, extent); | |
| 252 | |
| 253 client_->OnSelectionEvent(SELECTION_DRAG_STARTED, handle.position()); | |
| 254 } | |
| 255 | |
| 256 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle, | |
| 257 const gfx::PointF& position) { | |
| 258 // As the position corresponds to the bottom left point of the selection | |
| 259 // bound, offset it by half the corresponding line height. | |
| 260 gfx::Vector2dF line_offset = &handle == start_selection_handle_.get() | |
| 261 ? GetStartLineOffset() | |
| 262 : GetEndLineOffset(); | |
| 263 gfx::PointF line_position = position + line_offset; | |
| 264 if (&handle == insertion_handle_.get()) { | |
| 265 client_->MoveCaret(line_position); | |
| 266 } else { | |
| 267 client_->MoveRangeSelectionExtent(line_position); | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) { | |
| 272 if (&handle == insertion_handle_.get()) | |
| 273 client_->OnSelectionEvent(INSERTION_DRAG_STOPPED, handle.position()); | |
| 274 else | |
| 275 client_->OnSelectionEvent(SELECTION_DRAG_STOPPED, handle.position()); | |
| 276 } | |
| 277 | |
| 278 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) { | |
| 279 if (insertion_handle_ && &handle == insertion_handle_.get()) | |
| 280 client_->OnSelectionEvent(INSERTION_TAPPED, handle.position()); | |
| 281 } | |
| 282 | |
| 283 void TouchSelectionController::SetNeedsAnimate() { | |
| 284 client_->SetNeedsAnimate(); | |
| 285 } | |
| 286 | |
| 287 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { | |
| 288 return client_->CreateDrawable(); | |
| 289 } | |
| 290 | |
| 291 base::TimeDelta TouchSelectionController::GetTapTimeout() const { | |
| 292 return tap_timeout_; | |
| 293 } | |
| 294 | |
| 295 float TouchSelectionController::GetTapSlop() const { | |
| 296 return tap_slop_; | |
| 297 } | |
| 298 | |
| 299 void TouchSelectionController::ShowInsertionHandleAutomatically() { | |
| 300 if (activate_insertion_automatically_) | |
| 301 return; | |
| 302 activate_insertion_automatically_ = true; | |
| 303 ResetCachedValuesIfInactive(); | |
| 304 } | |
| 305 | |
| 306 void TouchSelectionController::ShowSelectionHandlesAutomatically() { | |
| 307 if (activate_selection_automatically_) | |
| 308 return; | |
| 309 activate_selection_automatically_ = true; | |
| 310 ResetCachedValuesIfInactive(); | |
| 311 } | |
| 312 | |
| 313 void TouchSelectionController::OnInsertionChanged() { | |
| 314 DeactivateSelection(); | |
| 315 | |
| 316 if (response_pending_input_event_ == TAP && selection_empty_ && | |
| 317 !show_on_tap_for_empty_editable_) { | |
| 318 HideAndDisallowShowingAutomatically(); | |
| 319 return; | |
| 320 } | |
| 321 | |
| 322 if (!activate_insertion_automatically_) | |
| 323 return; | |
| 324 | |
| 325 const bool was_active = is_insertion_active_; | |
| 326 const gfx::PointF position = GetStartPosition(); | |
| 327 if (!is_insertion_active_) | |
| 328 ActivateInsertion(); | |
| 329 else | |
| 330 client_->OnSelectionEvent(INSERTION_MOVED, position); | |
| 331 | |
| 332 insertion_handle_->SetVisible(GetStartVisible(), | |
| 333 GetAnimationStyle(was_active)); | |
| 334 insertion_handle_->SetPosition(position); | |
| 335 } | |
| 336 | |
| 337 void TouchSelectionController::OnSelectionChanged() { | |
| 338 DeactivateInsertion(); | |
| 339 | |
| 340 if (!activate_selection_automatically_) | |
| 341 return; | |
| 342 | |
| 343 const bool was_active = is_selection_active_; | |
| 344 ActivateSelection(); | |
| 345 | |
| 346 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active); | |
| 347 start_selection_handle_->SetVisible(GetStartVisible(), animation); | |
| 348 end_selection_handle_->SetVisible(GetEndVisible(), animation); | |
| 349 | |
| 350 start_selection_handle_->SetPosition(GetStartPosition()); | |
| 351 end_selection_handle_->SetPosition(GetEndPosition()); | |
| 352 } | |
| 353 | |
| 354 void TouchSelectionController::ActivateInsertion() { | |
| 355 DCHECK(!is_selection_active_); | |
| 356 | |
| 357 if (!insertion_handle_) | |
| 358 insertion_handle_.reset( | |
| 359 new TouchHandle(this, TouchHandleOrientation::CENTER)); | |
| 360 | |
| 361 if (!is_insertion_active_) { | |
| 362 is_insertion_active_ = true; | |
| 363 insertion_handle_->SetEnabled(true); | |
| 364 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition()); | |
| 365 } | |
| 366 } | |
| 367 | |
| 368 void TouchSelectionController::DeactivateInsertion() { | |
| 369 if (!is_insertion_active_) | |
| 370 return; | |
| 371 DCHECK(insertion_handle_); | |
| 372 is_insertion_active_ = false; | |
| 373 insertion_handle_->SetEnabled(false); | |
| 374 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); | |
| 375 } | |
| 376 | |
| 377 void TouchSelectionController::ActivateSelection() { | |
| 378 DCHECK(!is_insertion_active_); | |
| 379 | |
| 380 if (!start_selection_handle_) { | |
| 381 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | |
| 382 } else { | |
| 383 start_selection_handle_->SetEnabled(true); | |
| 384 start_selection_handle_->SetOrientation(start_orientation_); | |
| 385 } | |
| 386 | |
| 387 if (!end_selection_handle_) { | |
| 388 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | |
| 389 } else { | |
| 390 end_selection_handle_->SetEnabled(true); | |
| 391 end_selection_handle_->SetOrientation(end_orientation_); | |
| 392 } | |
| 393 | |
| 394 // As a long press received while a selection is already active may trigger | |
| 395 // an entirely new selection, notify the client but avoid sending an | |
| 396 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. | |
| 397 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) { | |
| 398 if (is_selection_active_) { | |
| 399 // The active selection session finishes with the start of the new one. | |
| 400 LogSelectionEnd(); | |
| 401 } | |
| 402 is_selection_active_ = true; | |
| 403 selection_handle_dragged_ = false; | |
| 404 selection_start_time_ = base::TimeTicks::Now(); | |
| 405 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | |
| 406 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition()); | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 void TouchSelectionController::DeactivateSelection() { | |
| 411 if (!is_selection_active_) | |
| 412 return; | |
| 413 DCHECK(start_selection_handle_); | |
| 414 DCHECK(end_selection_handle_); | |
| 415 LogSelectionEnd(); | |
| 416 start_selection_handle_->SetEnabled(false); | |
| 417 end_selection_handle_->SetEnabled(false); | |
| 418 is_selection_active_ = false; | |
| 419 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF()); | |
| 420 } | |
| 421 | |
| 422 void TouchSelectionController::ResetCachedValuesIfInactive() { | |
| 423 if (is_selection_active_ || is_insertion_active_) | |
| 424 return; | |
| 425 start_ = SelectionBound(); | |
| 426 end_ = SelectionBound(); | |
| 427 start_orientation_ = TouchHandleOrientation::UNDEFINED; | |
| 428 end_orientation_ = TouchHandleOrientation::UNDEFINED; | |
| 429 } | |
| 430 | |
| 431 const gfx::PointF& TouchSelectionController::GetStartPosition() const { | |
| 432 return start_.edge_bottom(); | |
| 433 } | |
| 434 | |
| 435 const gfx::PointF& TouchSelectionController::GetEndPosition() const { | |
| 436 return end_.edge_bottom(); | |
| 437 } | |
| 438 | |
| 439 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { | |
| 440 return ComputeLineOffsetFromBottom(start_); | |
| 441 } | |
| 442 | |
| 443 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { | |
| 444 return ComputeLineOffsetFromBottom(end_); | |
| 445 } | |
| 446 | |
| 447 bool TouchSelectionController::GetStartVisible() const { | |
| 448 return start_.visible() && !temporarily_hidden_; | |
| 449 } | |
| 450 | |
| 451 bool TouchSelectionController::GetEndVisible() const { | |
| 452 return end_.visible() && !temporarily_hidden_; | |
| 453 } | |
| 454 | |
| 455 TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle( | |
| 456 bool was_active) const { | |
| 457 return was_active && client_->SupportsAnimation() | |
| 458 ? TouchHandle::ANIMATION_SMOOTH | |
| 459 : TouchHandle::ANIMATION_NONE; | |
| 460 } | |
| 461 | |
| 462 void TouchSelectionController::LogSelectionEnd() { | |
| 463 // TODO(mfomitchev): Once we are able to tell the difference between | |
| 464 // 'successful' and 'unsuccessful' selections - log | |
| 465 // Event.TouchSelection.Duration instead and get rid of | |
| 466 // Event.TouchSelectionD.WasDraggeduration. | |
| 467 if (selection_handle_dragged_) { | |
| 468 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | |
| 469 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | |
| 470 duration, | |
| 471 base::TimeDelta::FromMilliseconds(500), | |
| 472 base::TimeDelta::FromSeconds(60), | |
| 473 60); | |
| 474 } | |
| 475 } | |
| 476 | |
| 477 } // namespace ui | |
| OLD | NEW |