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 return; |
| 153 } |
| 154 |
| 155 viewport_rect_ = viewport_rect; |
| 156 |
| 157 if (active_status_ == INACTIVE) |
| 158 return; |
| 159 |
| 160 if (active_status_ == INSERTION_ACTIVE) { |
| 161 DCHECK(insertion_handle_); |
| 162 insertion_handle_->SetViewportRect(viewport_rect); |
| 163 } else if (active_status_ == SELECTION_ACTIVE) { |
| 164 DCHECK(start_selection_handle_); |
| 165 DCHECK(end_selection_handle_); |
| 166 start_selection_handle_->SetViewportRect(viewport_rect); |
| 167 end_selection_handle_->SetViewportRect(viewport_rect); |
| 168 } |
| 169 |
| 170 // Update handle layout after setting the new Viewport size. |
| 171 UpdateHandleLayoutIfNecessary(); |
| 172 } |
| 173 |
147 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { | 174 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
148 if (config_.enable_longpress_drag_selection && | 175 if (config_.enable_longpress_drag_selection && |
149 longpress_drag_selector_.WillHandleTouchEvent(event)) { | 176 longpress_drag_selector_.WillHandleTouchEvent(event)) { |
150 return true; | 177 return true; |
151 } | 178 } |
152 | 179 |
153 if (active_status_ == INSERTION_ACTIVE) { | 180 if (active_status_ == INSERTION_ACTIVE) { |
154 DCHECK(insertion_handle_); | 181 DCHECK(insertion_handle_); |
155 return insertion_handle_->WillHandleTouchEvent(event); | 182 return insertion_handle_->WillHandleTouchEvent(event); |
156 } | 183 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 402 } |
376 | 403 |
377 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { | 404 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { |
378 return client_->CreateDrawable(); | 405 return client_->CreateDrawable(); |
379 } | 406 } |
380 | 407 |
381 base::TimeDelta TouchSelectionController::GetTapTimeout() const { | 408 base::TimeDelta TouchSelectionController::GetTapTimeout() const { |
382 return config_.tap_timeout; | 409 return config_.tap_timeout; |
383 } | 410 } |
384 | 411 |
| 412 bool TouchSelectionController::IsAdaptiveHandleOrientationEnabled() const { |
| 413 return config_.enable_adaptive_handle_orientation; |
| 414 } |
| 415 |
385 void TouchSelectionController::OnLongPressDragActiveStateChanged() { | 416 void TouchSelectionController::OnLongPressDragActiveStateChanged() { |
386 // The handles should remain hidden for the duration of a longpress drag, | 417 // 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. | 418 // including the time between a longpress and the start of drag motion. |
388 RefreshHandleVisibility(); | 419 RefreshHandleVisibility(); |
389 } | 420 } |
390 | 421 |
391 gfx::PointF TouchSelectionController::GetSelectionStart() const { | 422 gfx::PointF TouchSelectionController::GetSelectionStart() const { |
392 return GetStartPosition(); | 423 return GetStartPosition(); |
393 } | 424 } |
394 | 425 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 HideAndDisallowShowingAutomatically(); | 465 HideAndDisallowShowingAutomatically(); |
435 return; | 466 return; |
436 } | 467 } |
437 | 468 |
438 if (!activate_insertion_automatically_) | 469 if (!activate_insertion_automatically_) |
439 return; | 470 return; |
440 | 471 |
441 const bool activated = ActivateInsertionIfNecessary(); | 472 const bool activated = ActivateInsertionIfNecessary(); |
442 | 473 |
443 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); | 474 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
| 475 insertion_handle_->SetFocus(start_.edge_top(), start_.edge_bottom()); |
444 insertion_handle_->SetVisible(GetStartVisible(), animation); | 476 insertion_handle_->SetVisible(GetStartVisible(), animation); |
445 insertion_handle_->SetPosition(GetStartPosition()); | 477 |
| 478 UpdateHandleLayoutIfNecessary(); |
446 | 479 |
447 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN | 480 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN |
448 : INSERTION_HANDLE_MOVED); | 481 : INSERTION_HANDLE_MOVED); |
449 } | 482 } |
450 | 483 |
451 void TouchSelectionController::OnSelectionChanged() { | 484 void TouchSelectionController::OnSelectionChanged() { |
452 DeactivateInsertion(); | 485 DeactivateInsertion(); |
453 | 486 |
454 if (!activate_selection_automatically_) | 487 if (!activate_selection_automatically_) |
455 return; | 488 return; |
456 | 489 |
457 const bool activated = ActivateSelectionIfNecessary(); | 490 const bool activated = ActivateSelectionIfNecessary(); |
458 | 491 |
459 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); | 492 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); |
| 493 |
| 494 start_selection_handle_->SetFocus(start_.edge_top(), start_.edge_bottom()); |
| 495 end_selection_handle_->SetFocus(end_.edge_top(), end_.edge_bottom()); |
| 496 |
| 497 start_selection_handle_->SetOrientation(start_orientation_); |
| 498 end_selection_handle_->SetOrientation(end_orientation_); |
| 499 |
460 start_selection_handle_->SetVisible(GetStartVisible(), animation); | 500 start_selection_handle_->SetVisible(GetStartVisible(), animation); |
461 end_selection_handle_->SetVisible(GetEndVisible(), animation); | 501 end_selection_handle_->SetVisible(GetEndVisible(), animation); |
462 start_selection_handle_->SetPosition(GetStartPosition()); | 502 |
463 end_selection_handle_->SetPosition(GetEndPosition()); | 503 UpdateHandleLayoutIfNecessary(); |
464 | 504 |
465 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN | 505 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN |
466 : SELECTION_HANDLES_MOVED); | 506 : SELECTION_HANDLES_MOVED); |
467 } | 507 } |
468 | 508 |
469 bool TouchSelectionController::ActivateInsertionIfNecessary() { | 509 bool TouchSelectionController::ActivateInsertionIfNecessary() { |
470 DCHECK_NE(SELECTION_ACTIVE, active_status_); | 510 DCHECK_NE(SELECTION_ACTIVE, active_status_); |
471 | 511 |
472 if (!insertion_handle_) { | 512 if (!insertion_handle_) { |
473 insertion_handle_.reset( | 513 insertion_handle_.reset( |
474 new TouchHandle(this, TouchHandleOrientation::CENTER)); | 514 new TouchHandle(this, TouchHandleOrientation::CENTER, viewport_rect_)); |
475 } | 515 } |
476 | 516 |
477 if (active_status_ == INACTIVE) { | 517 if (active_status_ == INACTIVE) { |
478 active_status_ = INSERTION_ACTIVE; | 518 active_status_ = INSERTION_ACTIVE; |
| 519 insertion_handle_->SetViewportRect(viewport_rect_); |
479 insertion_handle_->SetEnabled(true); | 520 insertion_handle_->SetEnabled(true); |
480 return true; | 521 return true; |
481 } | 522 } |
482 return false; | 523 return false; |
483 } | 524 } |
484 | 525 |
485 void TouchSelectionController::DeactivateInsertion() { | 526 void TouchSelectionController::DeactivateInsertion() { |
486 if (active_status_ != INSERTION_ACTIVE) | 527 if (active_status_ != INSERTION_ACTIVE) |
487 return; | 528 return; |
488 DCHECK(insertion_handle_); | 529 DCHECK(insertion_handle_); |
489 active_status_ = INACTIVE; | 530 active_status_ = INACTIVE; |
490 insertion_handle_->SetEnabled(false); | 531 insertion_handle_->SetEnabled(false); |
491 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED); | 532 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED); |
492 } | 533 } |
493 | 534 |
494 bool TouchSelectionController::ActivateSelectionIfNecessary() { | 535 bool TouchSelectionController::ActivateSelectionIfNecessary() { |
495 DCHECK_NE(INSERTION_ACTIVE, active_status_); | 536 DCHECK_NE(INSERTION_ACTIVE, active_status_); |
496 | 537 |
497 if (!start_selection_handle_) { | 538 if (!start_selection_handle_) { |
498 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | 539 start_selection_handle_.reset( |
| 540 new TouchHandle(this, start_orientation_, viewport_rect_)); |
499 } else { | 541 } else { |
| 542 start_selection_handle_->SetViewportRect(viewport_rect_); |
500 start_selection_handle_->SetEnabled(true); | 543 start_selection_handle_->SetEnabled(true); |
501 start_selection_handle_->SetOrientation(start_orientation_); | |
502 } | 544 } |
503 | 545 |
504 if (!end_selection_handle_) { | 546 if (!end_selection_handle_) { |
505 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | 547 end_selection_handle_.reset( |
| 548 new TouchHandle(this, end_orientation_, viewport_rect_)); |
506 } else { | 549 } else { |
| 550 end_selection_handle_->SetViewportRect(viewport_rect_); |
507 end_selection_handle_->SetEnabled(true); | 551 end_selection_handle_->SetEnabled(true); |
508 end_selection_handle_->SetOrientation(end_orientation_); | |
509 } | 552 } |
510 | 553 |
511 // As a long press received while a selection is already active may trigger | 554 // 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 | 555 // an entirely new selection, notify the client but avoid sending an |
513 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state | 556 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state |
514 // changes. | 557 // changes. |
515 if (active_status_ == INACTIVE || | 558 if (active_status_ == INACTIVE || |
516 response_pending_input_event_ == LONG_PRESS) { | 559 response_pending_input_event_ == LONG_PRESS) { |
517 if (active_status_ == SELECTION_ACTIVE) { | 560 if (active_status_ == SELECTION_ACTIVE) { |
518 // The active selection session finishes with the start of the new one. | 561 // The active selection session finishes with the start of the new one. |
(...skipping 26 matching lines...) Expand all Loading... |
545 // Only force the update if the reported selection is non-empty but still | 588 // Only force the update if the reported selection is non-empty but still |
546 // considered "inactive", i.e., it wasn't preceded by a user gesture or | 589 // considered "inactive", i.e., it wasn't preceded by a user gesture or |
547 // the handles have since been explicitly hidden. | 590 // the handles have since been explicitly hidden. |
548 if (active_status_ == INACTIVE && | 591 if (active_status_ == INACTIVE && |
549 start_.type() != SelectionBound::EMPTY && | 592 start_.type() != SelectionBound::EMPTY && |
550 end_.type() != SelectionBound::EMPTY) { | 593 end_.type() != SelectionBound::EMPTY) { |
551 force_next_update_ = true; | 594 force_next_update_ = true; |
552 } | 595 } |
553 } | 596 } |
554 | 597 |
| 598 void TouchSelectionController::UpdateHandleLayoutIfNecessary() { |
| 599 if (active_status_ == INSERTION_ACTIVE) { |
| 600 DCHECK(insertion_handle_); |
| 601 insertion_handle_->UpdateHandleLayout(); |
| 602 } else if (active_status_ == SELECTION_ACTIVE) { |
| 603 DCHECK(start_selection_handle_); |
| 604 DCHECK(end_selection_handle_); |
| 605 start_selection_handle_->UpdateHandleLayout(); |
| 606 end_selection_handle_->UpdateHandleLayout(); |
| 607 } |
| 608 } |
| 609 |
555 void TouchSelectionController::RefreshHandleVisibility() { | 610 void TouchSelectionController::RefreshHandleVisibility() { |
556 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); | 611 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
557 if (active_status_ == SELECTION_ACTIVE) { | 612 if (active_status_ == SELECTION_ACTIVE) { |
558 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); | 613 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
559 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); | 614 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
560 } | 615 } |
561 if (active_status_ == INSERTION_ACTIVE) | 616 if (active_status_ == INSERTION_ACTIVE) |
562 insertion_handle_->SetVisible(GetStartVisible(), animation_style); | 617 insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
563 } | 618 } |
564 | 619 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 655 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
601 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 656 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
602 duration, | 657 duration, |
603 base::TimeDelta::FromMilliseconds(500), | 658 base::TimeDelta::FromMilliseconds(500), |
604 base::TimeDelta::FromSeconds(60), | 659 base::TimeDelta::FromSeconds(60), |
605 60); | 660 60); |
606 } | 661 } |
607 } | 662 } |
608 | 663 |
609 } // namespace ui | 664 } // namespace ui |
OLD | NEW |