OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/longpress_drag_selector.h" | 5 #include "ui/touch_selection/longpress_drag_selector.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "ui/events/gesture_detection/motion_event.h" | 8 #include "ui/events/gesture_detection/motion_event.h" |
9 | 9 |
10 namespace ui { | 10 namespace ui { |
| 11 namespace { |
| 12 |
| 13 gfx::Vector2dF SafeNormalize(const gfx::Vector2dF& v) { |
| 14 return v.IsZero() ? v : ScaleVector2d(v, 1.f / v.Length()); |
| 15 } |
| 16 |
| 17 } // namespace |
11 | 18 |
12 LongPressDragSelector::LongPressDragSelector( | 19 LongPressDragSelector::LongPressDragSelector( |
13 LongPressDragSelectorClient* client) | 20 LongPressDragSelectorClient* client) |
14 : client_(client), | 21 : client_(client), |
15 state_(INACTIVE), | 22 state_(INACTIVE), |
16 has_longpress_drag_start_anchor_(false) { | 23 has_longpress_drag_start_anchor_(false) { |
17 } | 24 } |
18 | 25 |
19 LongPressDragSelector::~LongPressDragSelector() { | 26 LongPressDragSelector::~LongPressDragSelector() { |
20 } | 27 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 if (client_->IsWithinTapSlop(delta)) | 70 if (client_->IsWithinTapSlop(delta)) |
64 return true; | 71 return true; |
65 | 72 |
66 gfx::PointF selection_start = client_->GetSelectionStart(); | 73 gfx::PointF selection_start = client_->GetSelectionStart(); |
67 gfx::PointF selection_end = client_->GetSelectionEnd(); | 74 gfx::PointF selection_end = client_->GetSelectionEnd(); |
68 bool extend_selection_start = false; | 75 bool extend_selection_start = false; |
69 if (std::abs(delta.y()) > std::abs(delta.x())) { | 76 if (std::abs(delta.y()) > std::abs(delta.x())) { |
70 // If initial motion is up/down, extend the start/end selection bound. | 77 // If initial motion is up/down, extend the start/end selection bound. |
71 extend_selection_start = delta.y() < 0; | 78 extend_selection_start = delta.y() < 0; |
72 } else { | 79 } else { |
73 // Otherwise extend the selection bound toward which we're moving. | 80 // Otherwise extend the selection bound toward which we're moving, or |
| 81 // the closest bound if motion is already away from both bounds. |
74 // Note that, for mixed RTL text, or for multiline selections triggered | 82 // Note that, for mixed RTL text, or for multiline selections triggered |
75 // by longpress, this may not pick the most suitable drag target | 83 // by longpress, this may not pick the most suitable drag target |
76 gfx::Vector2dF start_delta = selection_start - position; | 84 gfx::Vector2dF start_delta = selection_start - longpress_drag_start_anchor_; |
| 85 gfx::Vector2dF end_delta = selection_end - longpress_drag_start_anchor_; |
77 | 86 |
78 // The vectors must be normalized to make dot product comparison meaningful. | 87 // The vectors must be normalized to make dot product comparison meaningful. |
79 if (!start_delta.IsZero()) | 88 gfx::Vector2dF normalized_start_delta = SafeNormalize(start_delta); |
80 start_delta.Scale(1.f / start_delta.Length()); | 89 gfx::Vector2dF normalized_end_delta = SafeNormalize(end_delta); |
81 gfx::Vector2dF end_delta = selection_end - position; | 90 double start_dot_product = gfx::DotProduct(normalized_start_delta, delta); |
82 if (!end_delta.IsZero()) | 91 double end_dot_product = gfx::DotProduct(normalized_end_delta, delta); |
83 end_delta.Scale(1.f / start_delta.Length()); | |
84 | 92 |
85 // The larger the dot product the more similar the direction. | 93 if (start_dot_product >= 0 || end_dot_product >= 0) { |
86 extend_selection_start = | 94 // The greater the dot product the more similar the direction. |
87 gfx::DotProduct(start_delta, delta) > gfx::DotProduct(end_delta, delta); | 95 extend_selection_start = start_dot_product > end_dot_product; |
| 96 } else { |
| 97 // If we're already moving away from both endpoints, pick the closest. |
| 98 extend_selection_start = |
| 99 start_delta.LengthSquared() < end_delta.LengthSquared(); |
| 100 } |
88 } | 101 } |
89 | 102 |
90 gfx::PointF extent = extend_selection_start ? selection_start : selection_end; | 103 gfx::PointF extent = extend_selection_start ? selection_start : selection_end; |
91 longpress_drag_selection_offset_ = extent - position; | 104 longpress_drag_selection_offset_ = extent - position; |
92 client_->OnDragBegin(*this, extent); | 105 client_->OnDragBegin(*this, extent); |
93 SetState(DRAGGING); | 106 SetState(DRAGGING); |
94 return true; | 107 return true; |
95 } | 108 } |
96 | 109 |
97 bool LongPressDragSelector::IsActive() const { | 110 bool LongPressDragSelector::IsActive() const { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 145 |
133 // TODO(jdduke): Add UMA for tracking relative longpress drag frequency. | 146 // TODO(jdduke): Add UMA for tracking relative longpress drag frequency. |
134 if (was_dragging) | 147 if (was_dragging) |
135 client_->OnDragEnd(*this); | 148 client_->OnDragEnd(*this); |
136 | 149 |
137 if (was_active != IsActive()) | 150 if (was_active != IsActive()) |
138 client_->OnLongPressDragActiveStateChanged(); | 151 client_->OnLongPressDragActiveStateChanged(); |
139 } | 152 } |
140 | 153 |
141 } // namespace ui | 154 } // namespace ui |
OLD | NEW |