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