Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: ui/touch_selection/touch_selection_controller.cc

Issue 481683003: Support for Adaptive Handle Orientation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 : max_tap_duration(base::TimeDelta::FromMilliseconds(300)), 44 : max_tap_duration(base::TimeDelta::FromMilliseconds(300)),
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 TouchSelectionController::Config::~Config() { 50 TouchSelectionController::Config::~Config() {
50 } 51 }
51 52
52 TouchSelectionController::TouchSelectionController( 53 TouchSelectionController::TouchSelectionController(
53 TouchSelectionControllerClient* client, 54 TouchSelectionControllerClient* client,
54 const Config& config) 55 const Config& config)
55 : client_(client), 56 : client_(client),
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 137
137 if (start_orientation_ == TouchHandleOrientation::CENTER && 138 if (start_orientation_ == TouchHandleOrientation::CENTER &&
138 selection_editable_) { 139 selection_editable_) {
139 OnInsertionChanged(); 140 OnInsertionChanged();
140 return; 141 return;
141 } 142 }
142 143
143 HideAndDisallowShowingAutomatically(); 144 HideAndDisallowShowingAutomatically();
144 } 145 }
145 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 viewport_rect_ = viewport_rect;
155
156 if (active_status_ == INACTIVE)
157 return;
158
159 if (active_status_ == INSERTION_ACTIVE) {
160 DCHECK(insertion_handle_);
161 insertion_handle_->SetViewportRect(viewport_rect);
162 } else if (active_status_ == SELECTION_ACTIVE) {
163 DCHECK(start_selection_handle_);
164 DCHECK(end_selection_handle_);
165 start_selection_handle_->SetViewportRect(viewport_rect);
166 end_selection_handle_->SetViewportRect(viewport_rect);
167 }
168
169 // Update handle layout after setting the new Viewport size.
170 UpdateHandleLayoutIfNecessary();
171 }
172
146 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { 173 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) {
147 if (config_.enable_longpress_drag_selection && 174 if (config_.enable_longpress_drag_selection &&
148 longpress_drag_selector_.WillHandleTouchEvent(event)) { 175 longpress_drag_selector_.WillHandleTouchEvent(event)) {
149 return true; 176 return true;
150 } 177 }
151 178
152 if (active_status_ == INSERTION_ACTIVE) { 179 if (active_status_ == INSERTION_ACTIVE) {
153 DCHECK(insertion_handle_); 180 DCHECK(insertion_handle_);
154 return insertion_handle_->WillHandleTouchEvent(event); 181 return insertion_handle_->WillHandleTouchEvent(event);
155 } 182 }
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 } 407 }
381 408
382 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { 409 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() {
383 return client_->CreateDrawable(); 410 return client_->CreateDrawable();
384 } 411 }
385 412
386 base::TimeDelta TouchSelectionController::GetMaxTapDuration() const { 413 base::TimeDelta TouchSelectionController::GetMaxTapDuration() const {
387 return config_.max_tap_duration; 414 return config_.max_tap_duration;
388 } 415 }
389 416
417 bool TouchSelectionController::IsAdaptiveHandleOrientationEnabled() const {
418 return config_.enable_adaptive_handle_orientation;
419 }
420
390 void TouchSelectionController::OnLongPressDragActiveStateChanged() { 421 void TouchSelectionController::OnLongPressDragActiveStateChanged() {
391 // The handles should remain hidden for the duration of a longpress drag, 422 // The handles should remain hidden for the duration of a longpress drag,
392 // including the time between a longpress and the start of drag motion. 423 // including the time between a longpress and the start of drag motion.
393 RefreshHandleVisibility(); 424 RefreshHandleVisibility();
394 } 425 }
395 426
396 gfx::PointF TouchSelectionController::GetSelectionStart() const { 427 gfx::PointF TouchSelectionController::GetSelectionStart() const {
397 return GetStartPosition(); 428 return GetStartPosition();
398 } 429 }
399 430
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 HideAndDisallowShowingAutomatically(); 471 HideAndDisallowShowingAutomatically();
441 return; 472 return;
442 } 473 }
443 474
444 if (!activate_insertion_automatically_) 475 if (!activate_insertion_automatically_)
445 return; 476 return;
446 477
447 const bool activated = ActivateInsertionIfNecessary(); 478 const bool activated = ActivateInsertionIfNecessary();
448 479
449 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); 480 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated);
481 insertion_handle_->SetFocus(start_.edge_top(), start_.edge_bottom());
450 insertion_handle_->SetVisible(GetStartVisible(), animation); 482 insertion_handle_->SetVisible(GetStartVisible(), animation);
451 insertion_handle_->SetPosition(GetStartPosition()); 483
484 UpdateHandleLayoutIfNecessary();
452 485
453 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN 486 client_->OnSelectionEvent(activated ? INSERTION_HANDLE_SHOWN
454 : INSERTION_HANDLE_MOVED); 487 : INSERTION_HANDLE_MOVED);
455 } 488 }
456 489
457 void TouchSelectionController::OnSelectionChanged() { 490 void TouchSelectionController::OnSelectionChanged() {
458 DeactivateInsertion(); 491 DeactivateInsertion();
459 492
460 if (!activate_selection_automatically_) 493 if (!activate_selection_automatically_)
461 return; 494 return;
462 495
463 const bool activated = ActivateSelectionIfNecessary(); 496 const bool activated = ActivateSelectionIfNecessary();
464 497
465 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated); 498 const TouchHandle::AnimationStyle animation = GetAnimationStyle(!activated);
499
500 start_selection_handle_->SetFocus(start_.edge_top(), start_.edge_bottom());
501 end_selection_handle_->SetFocus(end_.edge_top(), end_.edge_bottom());
502
503 start_selection_handle_->SetOrientation(start_orientation_);
504 end_selection_handle_->SetOrientation(end_orientation_);
505
466 start_selection_handle_->SetVisible(GetStartVisible(), animation); 506 start_selection_handle_->SetVisible(GetStartVisible(), animation);
467 end_selection_handle_->SetVisible(GetEndVisible(), animation); 507 end_selection_handle_->SetVisible(GetEndVisible(), animation);
468 start_selection_handle_->SetPosition(GetStartPosition()); 508
469 end_selection_handle_->SetPosition(GetEndPosition()); 509 UpdateHandleLayoutIfNecessary();
470 510
471 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN 511 client_->OnSelectionEvent(activated ? SELECTION_HANDLES_SHOWN
472 : SELECTION_HANDLES_MOVED); 512 : SELECTION_HANDLES_MOVED);
473 } 513 }
474 514
475 bool TouchSelectionController::ActivateInsertionIfNecessary() { 515 bool TouchSelectionController::ActivateInsertionIfNecessary() {
476 DCHECK_NE(SELECTION_ACTIVE, active_status_); 516 DCHECK_NE(SELECTION_ACTIVE, active_status_);
477 517
478 if (!insertion_handle_) { 518 if (!insertion_handle_) {
479 insertion_handle_.reset( 519 insertion_handle_.reset(
480 new TouchHandle(this, TouchHandleOrientation::CENTER)); 520 new TouchHandle(this, TouchHandleOrientation::CENTER, viewport_rect_));
481 } 521 }
482 522
483 if (active_status_ == INACTIVE) { 523 if (active_status_ == INACTIVE) {
484 active_status_ = INSERTION_ACTIVE; 524 active_status_ = INSERTION_ACTIVE;
485 insertion_handle_->SetEnabled(true); 525 insertion_handle_->SetEnabled(true);
526 insertion_handle_->SetViewportRect(viewport_rect_);
486 return true; 527 return true;
487 } 528 }
488 return false; 529 return false;
489 } 530 }
490 531
491 void TouchSelectionController::DeactivateInsertion() { 532 void TouchSelectionController::DeactivateInsertion() {
492 if (active_status_ != INSERTION_ACTIVE) 533 if (active_status_ != INSERTION_ACTIVE)
493 return; 534 return;
494 DCHECK(insertion_handle_); 535 DCHECK(insertion_handle_);
495 active_status_ = INACTIVE; 536 active_status_ = INACTIVE;
496 insertion_handle_->SetEnabled(false); 537 insertion_handle_->SetEnabled(false);
497 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED); 538 client_->OnSelectionEvent(INSERTION_HANDLE_CLEARED);
498 } 539 }
499 540
500 bool TouchSelectionController::ActivateSelectionIfNecessary() { 541 bool TouchSelectionController::ActivateSelectionIfNecessary() {
501 DCHECK_NE(INSERTION_ACTIVE, active_status_); 542 DCHECK_NE(INSERTION_ACTIVE, active_status_);
502 543
503 if (!start_selection_handle_) { 544 if (!start_selection_handle_) {
504 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); 545 start_selection_handle_.reset(
546 new TouchHandle(this, start_orientation_, viewport_rect_));
505 } else { 547 } else {
506 start_selection_handle_->SetEnabled(true); 548 start_selection_handle_->SetEnabled(true);
507 start_selection_handle_->SetOrientation(start_orientation_); 549 start_selection_handle_->SetViewportRect(viewport_rect_);
508 } 550 }
509 551
510 if (!end_selection_handle_) { 552 if (!end_selection_handle_) {
511 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); 553 end_selection_handle_.reset(
554 new TouchHandle(this, end_orientation_, viewport_rect_));
512 } else { 555 } else {
513 end_selection_handle_->SetEnabled(true); 556 end_selection_handle_->SetEnabled(true);
514 end_selection_handle_->SetOrientation(end_orientation_); 557 end_selection_handle_->SetViewportRect(viewport_rect_);
515 } 558 }
516 559
517 // As a long press received while a selection is already active may trigger 560 // As a long press received while a selection is already active may trigger
518 // an entirely new selection, notify the client but avoid sending an 561 // an entirely new selection, notify the client but avoid sending an
519 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state 562 // intervening SELECTION_HANDLES_CLEARED update to avoid unnecessary state
520 // changes. 563 // changes.
521 if (active_status_ == INACTIVE || 564 if (active_status_ == INACTIVE ||
522 response_pending_input_event_ == LONG_PRESS || 565 response_pending_input_event_ == LONG_PRESS ||
523 response_pending_input_event_ == REPEATED_TAP) { 566 response_pending_input_event_ == REPEATED_TAP) {
524 if (active_status_ == SELECTION_ACTIVE) { 567 if (active_status_ == SELECTION_ACTIVE) {
(...skipping 27 matching lines...) Expand all
552 // Only force the update if the reported selection is non-empty but still 595 // Only force the update if the reported selection is non-empty but still
553 // considered "inactive", i.e., it wasn't preceded by a user gesture or 596 // considered "inactive", i.e., it wasn't preceded by a user gesture or
554 // the handles have since been explicitly hidden. 597 // the handles have since been explicitly hidden.
555 if (active_status_ == INACTIVE && 598 if (active_status_ == INACTIVE &&
556 start_.type() != SelectionBound::EMPTY && 599 start_.type() != SelectionBound::EMPTY &&
557 end_.type() != SelectionBound::EMPTY) { 600 end_.type() != SelectionBound::EMPTY) {
558 force_next_update_ = true; 601 force_next_update_ = true;
559 } 602 }
560 } 603 }
561 604
605 void TouchSelectionController::UpdateHandleLayoutIfNecessary() {
606 if (active_status_ == INSERTION_ACTIVE) {
607 DCHECK(insertion_handle_);
608 insertion_handle_->UpdateHandleLayout();
609 } else if (active_status_ == SELECTION_ACTIVE) {
610 DCHECK(start_selection_handle_);
611 DCHECK(end_selection_handle_);
612 start_selection_handle_->UpdateHandleLayout();
613 end_selection_handle_->UpdateHandleLayout();
614 }
615 }
616
562 void TouchSelectionController::RefreshHandleVisibility() { 617 void TouchSelectionController::RefreshHandleVisibility() {
563 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); 618 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true);
564 if (active_status_ == SELECTION_ACTIVE) { 619 if (active_status_ == SELECTION_ACTIVE) {
565 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); 620 start_selection_handle_->SetVisible(GetStartVisible(), animation_style);
566 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); 621 end_selection_handle_->SetVisible(GetEndVisible(), animation_style);
567 } 622 }
568 if (active_status_ == INSERTION_ACTIVE) 623 if (active_status_ == INSERTION_ACTIVE)
569 insertion_handle_->SetVisible(GetStartVisible(), animation_style); 624 insertion_handle_->SetVisible(GetStartVisible(), animation_style);
625
626 // Update handle layout if handle visibility is explicitly changed.
627 UpdateHandleLayoutIfNecessary();
570 } 628 }
571 629
572 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { 630 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const {
573 return ComputeLineOffsetFromBottom(start_); 631 return ComputeLineOffsetFromBottom(start_);
574 } 632 }
575 633
576 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { 634 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const {
577 return ComputeLineOffsetFromBottom(end_); 635 return ComputeLineOffsetFromBottom(end_);
578 } 636 }
579 637
(...skipping 27 matching lines...) Expand all
607 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; 665 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_;
608 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", 666 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration",
609 duration, 667 duration,
610 base::TimeDelta::FromMilliseconds(500), 668 base::TimeDelta::FromMilliseconds(500),
611 base::TimeDelta::FromSeconds(60), 669 base::TimeDelta::FromSeconds(60),
612 60); 670 60);
613 } 671 }
614 } 672 }
615 673
616 } // namespace ui 674 } // namespace ui
OLDNEW
« no previous file with comments | « ui/touch_selection/touch_selection_controller.h ('k') | ui/touch_selection/touch_selection_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698