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 |
11 namespace ui { | 11 namespace ui { |
12 namespace { | 12 namespace { |
13 | 13 |
14 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) { | 14 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound, |
15 gfx::Vector2dF line_offset = | 15 bool compute_from_top) { |
16 gfx::ScaleVector2d(bound.edge_top() - bound.edge_bottom(), 0.5f); | 16 gfx::Vector2dF vertical_offset = |
| 17 compute_from_top ? (bound.edge_bottom() - bound.edge_top()) |
| 18 : (bound.edge_top() - bound.edge_bottom()); |
| 19 gfx::Vector2dF line_offset = gfx::ScaleVector2d(vertical_offset, 0.5f); |
17 // An offset of 5 DIPs is sufficient for most line sizes. For small lines, | 20 // An offset of 5 DIPs is sufficient for most line sizes. For small lines, |
18 // using half the line height avoids synthesizing a point on a line above | 21 // using half the line height avoids synthesizing a point on a line above |
19 // (or below) the intended line. | 22 // (or below) the intended line. |
20 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f); | 23 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f); |
21 line_offset.SetToMin(kMaxLineOffset); | 24 line_offset.SetToMin(kMaxLineOffset); |
22 line_offset.SetToMax(-kMaxLineOffset); | 25 line_offset.SetToMax(-kMaxLineOffset); |
23 return line_offset; | 26 return line_offset; |
24 } | 27 } |
25 | 28 |
26 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { | 29 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { |
(...skipping 25 matching lines...) Expand all Loading... |
52 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), | 55 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), |
53 start_orientation_(TouchHandleOrientation::UNDEFINED), | 56 start_orientation_(TouchHandleOrientation::UNDEFINED), |
54 end_orientation_(TouchHandleOrientation::UNDEFINED), | 57 end_orientation_(TouchHandleOrientation::UNDEFINED), |
55 is_insertion_active_(false), | 58 is_insertion_active_(false), |
56 activate_insertion_automatically_(false), | 59 activate_insertion_automatically_(false), |
57 is_selection_active_(false), | 60 is_selection_active_(false), |
58 activate_selection_automatically_(false), | 61 activate_selection_automatically_(false), |
59 selection_empty_(false), | 62 selection_empty_(false), |
60 selection_editable_(false), | 63 selection_editable_(false), |
61 temporarily_hidden_(false), | 64 temporarily_hidden_(false), |
| 65 viewport_size_changed_(false), |
62 selection_handle_dragged_(false) { | 66 selection_handle_dragged_(false) { |
63 DCHECK(client_); | 67 DCHECK(client_); |
64 } | 68 } |
65 | 69 |
66 TouchSelectionController::~TouchSelectionController() { | 70 TouchSelectionController::~TouchSelectionController() { |
67 } | 71 } |
68 | 72 |
| 73 void TouchSelectionController::OnViewportChanged( |
| 74 const gfx::RectF viewport_rect) { |
| 75 if (viewport_rect_ == viewport_rect) |
| 76 return; |
| 77 viewport_size_changed_ = true; |
| 78 viewport_rect_ = viewport_rect; |
| 79 } |
| 80 |
69 void TouchSelectionController::OnSelectionBoundsChanged( | 81 void TouchSelectionController::OnSelectionBoundsChanged( |
70 const SelectionBound& start, | 82 const SelectionBound& start, |
71 const SelectionBound& end) { | 83 const SelectionBound& end) { |
72 if (start == start_ && end_ == end) | 84 if (start == start_ && end_ == end && !viewport_size_changed_) |
73 return; | 85 return; |
74 | 86 |
75 start_ = start; | 87 start_ = start; |
76 end_ = end; | 88 end_ = end; |
77 start_orientation_ = ToTouchHandleOrientation(start_.type()); | 89 start_orientation_ = ToTouchHandleOrientation(start_.type()); |
78 end_orientation_ = ToTouchHandleOrientation(end_.type()); | 90 end_orientation_ = ToTouchHandleOrientation(end_.type()); |
| 91 viewport_size_changed_ = false; |
79 | 92 |
80 if (!activate_selection_automatically_ && | 93 if (!activate_selection_automatically_ && |
81 !activate_insertion_automatically_) { | 94 !activate_insertion_automatically_) { |
82 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); | 95 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); |
83 return; | 96 return; |
84 } | 97 } |
85 | 98 |
86 // Ensure that |response_pending_input_event_| is cleared after the method | 99 // Ensure that |response_pending_input_event_| is cleared after the method |
87 // completes, while also making its current value available for the duration | 100 // completes, while also making its current value available for the duration |
88 // of the call. | 101 // of the call. |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); | 387 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); |
375 } | 388 } |
376 | 389 |
377 void TouchSelectionController::ActivateSelection() { | 390 void TouchSelectionController::ActivateSelection() { |
378 DCHECK(!is_insertion_active_); | 391 DCHECK(!is_insertion_active_); |
379 | 392 |
380 if (!start_selection_handle_) { | 393 if (!start_selection_handle_) { |
381 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | 394 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); |
382 } else { | 395 } else { |
383 start_selection_handle_->SetEnabled(true); | 396 start_selection_handle_->SetEnabled(true); |
384 start_selection_handle_->SetOrientation(start_orientation_); | 397 start_selection_handle_->SetOrientation(start_orientation_, viewport_rect_, |
| 398 start_.edge_bottom()); |
385 } | 399 } |
386 | 400 |
387 if (!end_selection_handle_) { | 401 if (!end_selection_handle_) { |
388 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | 402 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); |
389 } else { | 403 } else { |
390 end_selection_handle_->SetEnabled(true); | 404 end_selection_handle_->SetEnabled(true); |
391 end_selection_handle_->SetOrientation(end_orientation_); | 405 end_selection_handle_->SetOrientation(end_orientation_, viewport_rect_, |
| 406 end_.edge_bottom()); |
392 } | 407 } |
393 | 408 |
394 // As a long press received while a selection is already active may trigger | 409 // As a long press received while a selection is already active may trigger |
395 // an entirely new selection, notify the client but avoid sending an | 410 // an entirely new selection, notify the client but avoid sending an |
396 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. | 411 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. |
397 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) { | 412 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) { |
398 if (is_selection_active_) { | 413 if (is_selection_active_) { |
399 // The active selection session finishes with the start of the new one. | 414 // The active selection session finishes with the start of the new one. |
400 LogSelectionEnd(); | 415 LogSelectionEnd(); |
401 } | 416 } |
(...skipping 20 matching lines...) Expand all Loading... |
422 void TouchSelectionController::ResetCachedValuesIfInactive() { | 437 void TouchSelectionController::ResetCachedValuesIfInactive() { |
423 if (is_selection_active_ || is_insertion_active_) | 438 if (is_selection_active_ || is_insertion_active_) |
424 return; | 439 return; |
425 start_ = SelectionBound(); | 440 start_ = SelectionBound(); |
426 end_ = SelectionBound(); | 441 end_ = SelectionBound(); |
427 start_orientation_ = TouchHandleOrientation::UNDEFINED; | 442 start_orientation_ = TouchHandleOrientation::UNDEFINED; |
428 end_orientation_ = TouchHandleOrientation::UNDEFINED; | 443 end_orientation_ = TouchHandleOrientation::UNDEFINED; |
429 } | 444 } |
430 | 445 |
431 const gfx::PointF& TouchSelectionController::GetStartPosition() const { | 446 const gfx::PointF& TouchSelectionController::GetStartPosition() const { |
| 447 if (start_selection_handle_ && start_selection_handle_->is_inverted()) |
| 448 return start_.edge_top(); |
432 return start_.edge_bottom(); | 449 return start_.edge_bottom(); |
433 } | 450 } |
434 | 451 |
435 const gfx::PointF& TouchSelectionController::GetEndPosition() const { | 452 const gfx::PointF& TouchSelectionController::GetEndPosition() const { |
| 453 if (end_selection_handle_ && end_selection_handle_->is_inverted()) |
| 454 return end_.edge_top(); |
436 return end_.edge_bottom(); | 455 return end_.edge_bottom(); |
437 } | 456 } |
438 | 457 |
439 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { | 458 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { |
440 return ComputeLineOffsetFromBottom(start_); | 459 DCHECK(start_selection_handle_); |
| 460 bool compute_from_top = start_selection_handle_->is_inverted(); |
| 461 return ComputeLineOffsetFromBottom(start_, compute_from_top); |
441 } | 462 } |
442 | 463 |
443 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { | 464 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { |
444 return ComputeLineOffsetFromBottom(end_); | 465 DCHECK(end_selection_handle_); |
| 466 bool compute_from_top = end_selection_handle_->is_inverted(); |
| 467 return ComputeLineOffsetFromBottom(end_, compute_from_top); |
445 } | 468 } |
446 | 469 |
447 bool TouchSelectionController::GetStartVisible() const { | 470 bool TouchSelectionController::GetStartVisible() const { |
448 return start_.visible() && !temporarily_hidden_; | 471 return start_.visible() && !temporarily_hidden_; |
449 } | 472 } |
450 | 473 |
451 bool TouchSelectionController::GetEndVisible() const { | 474 bool TouchSelectionController::GetEndVisible() const { |
452 return end_.visible() && !temporarily_hidden_; | 475 return end_.visible() && !temporarily_hidden_; |
453 } | 476 } |
454 | 477 |
(...skipping 13 matching lines...) Expand all Loading... |
468 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 491 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
469 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 492 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
470 duration, | 493 duration, |
471 base::TimeDelta::FromMilliseconds(500), | 494 base::TimeDelta::FromMilliseconds(500), |
472 base::TimeDelta::FromSeconds(60), | 495 base::TimeDelta::FromSeconds(60), |
473 60); | 496 60); |
474 } | 497 } |
475 } | 498 } |
476 | 499 |
477 } // namespace ui | 500 } // namespace ui |
OLD | NEW |