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_selection_controller.h" | 5 #include "ui/touch_selection/touch_selection_controller.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 | 10 |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 | 382 |
383 if (response_pending_input_event_ == TAP && selection_empty_ && | 383 if (response_pending_input_event_ == TAP && selection_empty_ && |
384 !show_on_tap_for_empty_editable_) { | 384 !show_on_tap_for_empty_editable_) { |
385 HideAndDisallowShowingAutomatically(); | 385 HideAndDisallowShowingAutomatically(); |
386 return; | 386 return; |
387 } | 387 } |
388 | 388 |
389 if (!activate_insertion_automatically_) | 389 if (!activate_insertion_automatically_) |
390 return; | 390 return; |
391 | 391 |
392 const bool was_active = active_status_ == INSERTION_ACTIVE; | 392 const bool activated = ActivateInsertionIfNecessary(); |
393 const gfx::PointF position = GetStartPosition(); | |
394 if (!was_active) | |
395 ActivateInsertion(); | |
396 else | |
397 client_->OnSelectionEvent(INSERTION_MOVED); | |
398 | 393 |
399 insertion_handle_->SetVisible(GetStartVisible(), | 394 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
400 GetAnimationStyle(was_active)); | 395 insertion_handle_->SetVisible(GetStartVisible(), animation); |
401 insertion_handle_->SetPosition(position); | 396 insertion_handle_->SetPosition(GetStartPosition()); |
| 397 |
| 398 client_->OnSelectionEvent(activated ? INSERTION_SHOWN : INSERTION_MOVED); |
402 } | 399 } |
403 | 400 |
404 void TouchSelectionController::OnSelectionChanged() { | 401 void TouchSelectionController::OnSelectionChanged() { |
405 DeactivateInsertion(); | 402 DeactivateInsertion(); |
406 | 403 |
407 if (!activate_selection_automatically_) | 404 if (!activate_selection_automatically_) |
408 return; | 405 return; |
409 | 406 |
410 const bool was_active = active_status_ == SELECTION_ACTIVE; | 407 const bool activated = ActivateSelectionIfNecessary(); |
411 if (!was_active || response_pending_input_event_ == LONG_PRESS) | |
412 ActivateSelection(); | |
413 else | |
414 client_->OnSelectionEvent(SELECTION_MOVED); | |
415 | 408 |
416 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active); | 409 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
417 start_selection_handle_->SetVisible(GetStartVisible(), animation); | 410 start_selection_handle_->SetVisible(GetStartVisible(), animation); |
418 end_selection_handle_->SetVisible(GetEndVisible(), animation); | 411 end_selection_handle_->SetVisible(GetEndVisible(), animation); |
419 | |
420 start_selection_handle_->SetPosition(GetStartPosition()); | 412 start_selection_handle_->SetPosition(GetStartPosition()); |
421 end_selection_handle_->SetPosition(GetEndPosition()); | 413 end_selection_handle_->SetPosition(GetEndPosition()); |
| 414 |
| 415 client_->OnSelectionEvent(activated ? SELECTION_SHOWN : SELECTION_MOVED); |
422 } | 416 } |
423 | 417 |
424 void TouchSelectionController::ActivateInsertion() { | 418 bool TouchSelectionController::ActivateInsertionIfNecessary() { |
425 DCHECK_NE(SELECTION_ACTIVE, active_status_); | 419 DCHECK_NE(SELECTION_ACTIVE, active_status_); |
426 | 420 |
427 if (!insertion_handle_) | 421 if (!insertion_handle_) { |
428 insertion_handle_.reset( | 422 insertion_handle_.reset( |
429 new TouchHandle(this, TouchHandleOrientation::CENTER)); | 423 new TouchHandle(this, TouchHandleOrientation::CENTER)); |
| 424 } |
430 | 425 |
431 if (active_status_ == INACTIVE) { | 426 if (active_status_ == INACTIVE) { |
432 active_status_ = INSERTION_ACTIVE; | 427 active_status_ = INSERTION_ACTIVE; |
433 insertion_handle_->SetEnabled(true); | 428 insertion_handle_->SetEnabled(true); |
434 client_->OnSelectionEvent(INSERTION_SHOWN); | 429 return true; |
435 } | 430 } |
| 431 return false; |
436 } | 432 } |
437 | 433 |
438 void TouchSelectionController::DeactivateInsertion() { | 434 void TouchSelectionController::DeactivateInsertion() { |
439 if (active_status_ != INSERTION_ACTIVE) | 435 if (active_status_ != INSERTION_ACTIVE) |
440 return; | 436 return; |
441 DCHECK(insertion_handle_); | 437 DCHECK(insertion_handle_); |
442 active_status_ = INACTIVE; | 438 active_status_ = INACTIVE; |
443 insertion_handle_->SetEnabled(false); | 439 insertion_handle_->SetEnabled(false); |
444 client_->OnSelectionEvent(INSERTION_CLEARED); | 440 client_->OnSelectionEvent(INSERTION_CLEARED); |
445 } | 441 } |
446 | 442 |
447 void TouchSelectionController::ActivateSelection() { | 443 bool TouchSelectionController::ActivateSelectionIfNecessary() { |
448 DCHECK_NE(INSERTION_ACTIVE, active_status_); | 444 DCHECK_NE(INSERTION_ACTIVE, active_status_); |
449 | 445 |
450 if (!start_selection_handle_) { | 446 if (!start_selection_handle_) { |
451 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | 447 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); |
452 } else { | 448 } else { |
453 start_selection_handle_->SetEnabled(true); | 449 start_selection_handle_->SetEnabled(true); |
454 start_selection_handle_->SetOrientation(start_orientation_); | 450 start_selection_handle_->SetOrientation(start_orientation_); |
455 } | 451 } |
456 | 452 |
457 if (!end_selection_handle_) { | 453 if (!end_selection_handle_) { |
458 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | 454 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); |
459 } else { | 455 } else { |
460 end_selection_handle_->SetEnabled(true); | 456 end_selection_handle_->SetEnabled(true); |
461 end_selection_handle_->SetOrientation(end_orientation_); | 457 end_selection_handle_->SetOrientation(end_orientation_); |
462 } | 458 } |
463 | 459 |
464 // As a long press received while a selection is already active may trigger | 460 // As a long press received while a selection is already active may trigger |
465 // an entirely new selection, notify the client but avoid sending an | 461 // an entirely new selection, notify the client but avoid sending an |
466 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. | 462 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. |
467 if (active_status_ == INACTIVE || | 463 if (active_status_ == INACTIVE || |
468 response_pending_input_event_ == LONG_PRESS) { | 464 response_pending_input_event_ == LONG_PRESS) { |
469 if (active_status_ == SELECTION_ACTIVE) { | 465 if (active_status_ == SELECTION_ACTIVE) { |
470 // The active selection session finishes with the start of the new one. | 466 // The active selection session finishes with the start of the new one. |
471 LogSelectionEnd(); | 467 LogSelectionEnd(); |
472 } | 468 } |
473 active_status_ = SELECTION_ACTIVE; | 469 active_status_ = SELECTION_ACTIVE; |
474 selection_handle_dragged_ = false; | 470 selection_handle_dragged_ = false; |
475 selection_start_time_ = base::TimeTicks::Now(); | 471 selection_start_time_ = base::TimeTicks::Now(); |
476 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | 472 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
477 client_->OnSelectionEvent(SELECTION_SHOWN); | 473 return true; |
478 } | 474 } |
| 475 return false; |
479 } | 476 } |
480 | 477 |
481 void TouchSelectionController::DeactivateSelection() { | 478 void TouchSelectionController::DeactivateSelection() { |
482 if (active_status_ != SELECTION_ACTIVE) | 479 if (active_status_ != SELECTION_ACTIVE) |
483 return; | 480 return; |
484 DCHECK(start_selection_handle_); | 481 DCHECK(start_selection_handle_); |
485 DCHECK(end_selection_handle_); | 482 DCHECK(end_selection_handle_); |
486 LogSelectionEnd(); | 483 LogSelectionEnd(); |
487 start_selection_handle_->SetEnabled(false); | 484 start_selection_handle_->SetEnabled(false); |
488 end_selection_handle_->SetEnabled(false); | 485 end_selection_handle_->SetEnabled(false); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 530 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
534 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 531 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
535 duration, | 532 duration, |
536 base::TimeDelta::FromMilliseconds(500), | 533 base::TimeDelta::FromMilliseconds(500), |
537 base::TimeDelta::FromSeconds(60), | 534 base::TimeDelta::FromSeconds(60), |
538 60); | 535 60); |
539 } | 536 } |
540 } | 537 } |
541 | 538 |
542 } // namespace ui | 539 } // namespace ui |
OLD | NEW |