| 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 25 matching lines...) Expand all Loading... |
| 36 } | 36 } |
| 37 NOTREACHED() << "Invalid selection bound type: " << type; | 37 NOTREACHED() << "Invalid selection bound type: " << type; |
| 38 return TouchHandleOrientation::UNDEFINED; | 38 return TouchHandleOrientation::UNDEFINED; |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 TouchSelectionController::Config::Config() | 43 TouchSelectionController::Config::Config() |
| 44 : tap_timeout(base::TimeDelta::FromMilliseconds(100)), | 44 : tap_timeout(base::TimeDelta::FromMilliseconds(100)), |
| 45 tap_slop(8), | 45 tap_slop(8), |
| 46 enable_adaptive_handle_orientation(false), |
| 46 enable_longpress_drag_selection(false), | 47 enable_longpress_drag_selection(false), |
| 47 show_on_tap_for_empty_editable(false) { | 48 show_on_tap_for_empty_editable(false) {} |
| 48 } | |
| 49 | 49 |
| 50 TouchSelectionController::Config::~Config() { | 50 TouchSelectionController::Config::~Config() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 TouchSelectionController::TouchSelectionController( | 53 TouchSelectionController::TouchSelectionController( |
| 54 TouchSelectionControllerClient* client, | 54 TouchSelectionControllerClient* client, |
| 55 const Config& config) | 55 const Config& config) |
| 56 : client_(client), | 56 : client_(client), |
| 57 config_(config), | 57 config_(config), |
| 58 force_next_update_(false), | 58 force_next_update_(false), |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 if (start_orientation_ == TouchHandleOrientation::CENTER && | 138 if (start_orientation_ == TouchHandleOrientation::CENTER && |
| 139 selection_editable_) { | 139 selection_editable_) { |
| 140 OnInsertionChanged(); | 140 OnInsertionChanged(); |
| 141 return; | 141 return; |
| 142 } | 142 } |
| 143 | 143 |
| 144 HideAndDisallowShowingAutomatically(); | 144 HideAndDisallowShowingAutomatically(); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void TouchSelectionController::OnViewportChanged( |
| 148 const gfx::RectF viewport_rect) { |
| 149 // Trigger a force update if the viewport is changed, so that |
| 150 // it triggers a call to change the mirror values if required. |
| 151 if (viewport_rect_ == viewport_rect) { |
| 152 UpdateHandleLayoutIfRequired(); |
| 153 return; |
| 154 } |
| 155 |
| 156 viewport_rect_ = viewport_rect; |
| 157 |
| 158 if (active_status_ == INACTIVE) |
| 159 return; |
| 160 |
| 161 if (active_status_ == INSERTION_ACTIVE) { |
| 162 DCHECK(insertion_handle_); |
| 163 insertion_handle_->SetViewportRect(viewport_rect); |
| 164 } else if (active_status_ == SELECTION_ACTIVE) { |
| 165 DCHECK(start_selection_handle_); |
| 166 DCHECK(end_selection_handle_); |
| 167 start_selection_handle_->SetViewportRect(viewport_rect); |
| 168 end_selection_handle_->SetViewportRect(viewport_rect); |
| 169 } |
| 170 |
| 171 // Update handle layout once per frame update if necessary. |
| 172 UpdateHandleLayoutIfRequired(); |
| 173 } |
| 174 |
| 175 void TouchSelectionController::UpdateHandleLayoutIfRequired() { |
| 176 if (active_status_ == INSERTION_ACTIVE) { |
| 177 DCHECK(insertion_handle_); |
| 178 insertion_handle_->UpdateHandleLayout(); |
| 179 } else if (active_status_ == SELECTION_ACTIVE) { |
| 180 DCHECK(start_selection_handle_); |
| 181 DCHECK(end_selection_handle_); |
| 182 start_selection_handle_->UpdateHandleLayout(); |
| 183 end_selection_handle_->UpdateHandleLayout(); |
| 184 } |
| 185 } |
| 186 |
| 147 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { | 187 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
| 148 if (config_.enable_longpress_drag_selection && | 188 if (config_.enable_longpress_drag_selection && |
| 149 longpress_drag_selector_.WillHandleTouchEvent(event)) { | 189 longpress_drag_selector_.WillHandleTouchEvent(event)) { |
| 150 return true; | 190 return true; |
| 151 } | 191 } |
| 152 | 192 |
| 153 if (active_status_ == INSERTION_ACTIVE) { | 193 if (active_status_ == INSERTION_ACTIVE) { |
| 154 DCHECK(insertion_handle_); | 194 DCHECK(insertion_handle_); |
| 155 return insertion_handle_->WillHandleTouchEvent(event); | 195 return insertion_handle_->WillHandleTouchEvent(event); |
| 156 } | 196 } |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 } | 415 } |
| 376 | 416 |
| 377 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { | 417 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { |
| 378 return client_->CreateDrawable(); | 418 return client_->CreateDrawable(); |
| 379 } | 419 } |
| 380 | 420 |
| 381 base::TimeDelta TouchSelectionController::GetTapTimeout() const { | 421 base::TimeDelta TouchSelectionController::GetTapTimeout() const { |
| 382 return config_.tap_timeout; | 422 return config_.tap_timeout; |
| 383 } | 423 } |
| 384 | 424 |
| 425 bool TouchSelectionController::IsEnabledAdaptiveHandleOrientation() const { |
| 426 return config_.enable_adaptive_handle_orientation; |
| 427 } |
| 428 |
| 385 void TouchSelectionController::OnLongPressDragActiveStateChanged() { | 429 void TouchSelectionController::OnLongPressDragActiveStateChanged() { |
| 386 // The handles should remain hidden for the duration of a longpress drag, | 430 // The handles should remain hidden for the duration of a longpress drag, |
| 387 // including the time between a longpress and the start of drag motion. | 431 // including the time between a longpress and the start of drag motion. |
| 388 RefreshHandleVisibility(); | 432 RefreshHandleVisibility(); |
| 389 } | 433 } |
| 390 | 434 |
| 391 gfx::PointF TouchSelectionController::GetSelectionStart() const { | 435 gfx::PointF TouchSelectionController::GetSelectionStart() const { |
| 392 return GetStartPosition(); | 436 return GetStartPosition(); |
| 393 } | 437 } |
| 394 | 438 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 HideAndDisallowShowingAutomatically(); | 478 HideAndDisallowShowingAutomatically(); |
| 435 return; | 479 return; |
| 436 } | 480 } |
| 437 | 481 |
| 438 if (!activate_insertion_automatically_) | 482 if (!activate_insertion_automatically_) |
| 439 return; | 483 return; |
| 440 | 484 |
| 441 const bool activated = ActivateInsertionIfNecessary(); | 485 const bool activated = ActivateInsertionIfNecessary(); |
| 442 | 486 |
| 443 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); | 487 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
| 488 insertion_handle_->SetFocus(start_.edge_top(), start_.edge_bottom()); |
| 444 insertion_handle_->SetVisible(GetStartVisible(), animation); | 489 insertion_handle_->SetVisible(GetStartVisible(), animation); |
| 445 insertion_handle_->SetPosition(GetStartPosition()); | |
| 446 | 490 |
| 447 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN | 491 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN |
| 448 : INSERTION_HANDLE_MOVED); | 492 : INSERTION_HANDLE_MOVED); |
| 449 } | 493 } |
| 450 | 494 |
| 451 void TouchSelectionController::OnSelectionChanged() { | 495 void TouchSelectionController::OnSelectionChanged() { |
| 452 DeactivateInsertion(); | 496 DeactivateInsertion(); |
| 453 | 497 |
| 454 if (!activate_selection_automatically_) | 498 if (!activate_selection_automatically_) |
| 455 return; | 499 return; |
| 456 | 500 |
| 457 const bool activated = ActivateSelectionIfNecessary(); | 501 const bool activated = ActivateSelectionIfNecessary(); |
| 458 | 502 |
| 459 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); | 503 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
| 504 |
| 505 start_selection_handle_->SetFocus(start_.edge_top(), start_.edge_bottom()); |
| 506 end_selection_handle_->SetFocus(end_.edge_top(), end_.edge_bottom()); |
| 507 |
| 508 start_selection_handle_->SetOrientation(start_orientation_); |
| 509 end_selection_handle_->SetOrientation(end_orientation_); |
| 510 |
| 460 start_selection_handle_->SetVisible(GetStartVisible(), animation); | 511 start_selection_handle_->SetVisible(GetStartVisible(), animation); |
| 461 end_selection_handle_->SetVisible(GetEndVisible(), animation); | 512 end_selection_handle_->SetVisible(GetEndVisible(), animation); |
| 462 start_selection_handle_->SetPosition(GetStartPosition()); | |
| 463 end_selection_handle_->SetPosition(GetEndPosition()); | |
| 464 | 513 |
| 465 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN | 514 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN |
| 466 : SELECTION_HANDLES_MOVED); | 515 : SELECTION_HANDLES_MOVED); |
| 467 } | 516 } |
| 468 | 517 |
| 469 bool TouchSelectionController::ActivateInsertionIfNecessary() { | 518 bool TouchSelectionController::ActivateInsertionIfNecessary() { |
| 470 DCHECK_NE(SELECTION_ACTIVE, active_status_); | 519 DCHECK_NE(SELECTION_ACTIVE, active_status_); |
| 471 | 520 |
| 472 if (!insertion_handle_) { | 521 if (!insertion_handle_) { |
| 473 insertion_handle_.reset( | 522 insertion_handle_.reset( |
| 474 new TouchHandle(this, TouchHandleOrientation::CENTER)); | 523 new TouchHandle(this, TouchHandleOrientation::CENTER, viewport_rect_)); |
| 475 } | 524 } |
| 476 | 525 |
| 477 if (active_status_ == INACTIVE) { | 526 if (active_status_ == INACTIVE) { |
| 478 active_status_ = INSERTION_ACTIVE; | 527 active_status_ = INSERTION_ACTIVE; |
| 528 insertion_handle_->SetViewportRect(viewport_rect_); |
| 479 insertion_handle_->SetEnabled(true); | 529 insertion_handle_->SetEnabled(true); |
| 480 return true; | 530 return true; |
| 481 } | 531 } |
| 482 return false; | 532 return false; |
| 483 } | 533 } |
| 484 | 534 |
| 485 void TouchSelectionController::DeactivateInsertion() { | 535 void TouchSelectionController::DeactivateInsertion() { |
| 486 if (active_status_ != INSERTION_ACTIVE) | 536 if (active_status_ != INSERTION_ACTIVE) |
| 487 return; | 537 return; |
| 488 DCHECK(insertion_handle_); | 538 DCHECK(insertion_handle_); |
| 489 active_status_ = INACTIVE; | 539 active_status_ = INACTIVE; |
| 490 insertion_handle_->SetEnabled(false); | 540 insertion_handle_->SetEnabled(false); |
| 491 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED); | 541 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED); |
| 492 } | 542 } |
| 493 | 543 |
| 494 bool TouchSelectionController::ActivateSelectionIfNecessary() { | 544 bool TouchSelectionController::ActivateSelectionIfNecessary() { |
| 495 DCHECK_NE(INSERTION_ACTIVE, active_status_); | 545 DCHECK_NE(INSERTION_ACTIVE, active_status_); |
| 496 | 546 |
| 497 if (!start_selection_handle_) { | 547 if (!start_selection_handle_) { |
| 498 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | 548 start_selection_handle_.reset( |
| 549 new TouchHandle(this, start_orientation_, viewport_rect_)); |
| 499 } else { | 550 } else { |
| 551 start_selection_handle_->SetViewportRect(viewport_rect_); |
| 500 start_selection_handle_->SetEnabled(true); | 552 start_selection_handle_->SetEnabled(true); |
| 501 start_selection_handle_->SetOrientation(start_orientation_); | |
| 502 } | 553 } |
| 503 | 554 |
| 504 if (!end_selection_handle_) { | 555 if (!end_selection_handle_) { |
| 505 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | 556 end_selection_handle_.reset( |
| 557 new TouchHandle(this, end_orientation_, viewport_rect_)); |
| 506 } else { | 558 } else { |
| 559 end_selection_handle_->SetViewportRect(viewport_rect_); |
| 507 end_selection_handle_->SetEnabled(true); | 560 end_selection_handle_->SetEnabled(true); |
| 508 end_selection_handle_->SetOrientation(end_orientation_); | |
| 509 } | 561 } |
| 510 | 562 |
| 511 // As a long press received while a selection is already active may trigger | 563 // As a long press received while a selection is already active may trigger |
| 512 // an entirely new selection, notify the client but avoid sending an | 564 // an entirely new selection, notify the client but avoid sending an |
| 513 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state | 565 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state |
| 514 // changes. | 566 // changes. |
| 515 if (active_status_ == INACTIVE || | 567 if (active_status_ == INACTIVE || |
| 516 response_pending_input_event_ == LONG_PRESS) { | 568 response_pending_input_event_ == LONG_PRESS) { |
| 517 if (active_status_ == SELECTION_ACTIVE) { | 569 if (active_status_ == SELECTION_ACTIVE) { |
| 518 // The active selection session finishes with the start of the new one. | 570 // The active selection session finishes with the start of the new one. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 } | 605 } |
| 554 | 606 |
| 555 void TouchSelectionController::RefreshHandleVisibility() { | 607 void TouchSelectionController::RefreshHandleVisibility() { |
| 556 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); | 608 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
| 557 if (active_status_ == SELECTION_ACTIVE) { | 609 if (active_status_ == SELECTION_ACTIVE) { |
| 558 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); | 610 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
| 559 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); | 611 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
| 560 } | 612 } |
| 561 if (active_status_ == INSERTION_ACTIVE) | 613 if (active_status_ == INSERTION_ACTIVE) |
| 562 insertion_handle_->SetVisible(GetStartVisible(), animation_style); | 614 insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
| 615 UpdateHandleLayoutIfRequired(); |
| 563 } | 616 } |
| 564 | 617 |
| 565 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { | 618 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { |
| 566 return ComputeLineOffsetFromBottom(start_); | 619 return ComputeLineOffsetFromBottom(start_); |
| 567 } | 620 } |
| 568 | 621 |
| 569 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { | 622 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { |
| 570 return ComputeLineOffsetFromBottom(end_); | 623 return ComputeLineOffsetFromBottom(end_); |
| 571 } | 624 } |
| 572 | 625 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 600 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 653 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
| 601 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 654 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
| 602 duration, | 655 duration, |
| 603 base::TimeDelta::FromMilliseconds(500), | 656 base::TimeDelta::FromMilliseconds(500), |
| 604 base::TimeDelta::FromSeconds(60), | 657 base::TimeDelta::FromSeconds(60), |
| 605 60); | 658 60); |
| 606 } | 659 } |
| 607 } | 660 } |
| 608 | 661 |
| 609 } // namespace ui | 662 } // namespace ui |
| OLD | NEW |