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 - position; |
85 gfx::Vector2dF end_delta = selection_end - position; | |
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) { |
mfomitchev
2015/08/04 18:31:36
nit: It makes it a bit harder to parse when the or
jdduke (slow)
2015/08/04 19:14:51
Done.
| |
86 extend_selection_start = | 94 // If we're already moving away from both endpoints, pick the closest. |
87 gfx::DotProduct(start_delta, delta) > gfx::DotProduct(end_delta, delta); | 95 extend_selection_start = |
96 start_delta.LengthSquared() < end_delta.LengthSquared(); | |
97 } else { | |
98 // Otherwise pick the endpoint toward we're which moving. The larger the | |
99 // dot product the more similar the direction. | |
100 extend_selection_start = start_dot_product > end_dot_product; | |
101 } | |
88 } | 102 } |
89 | 103 |
90 gfx::PointF extent = extend_selection_start ? selection_start : selection_end; | 104 gfx::PointF extent = extend_selection_start ? selection_start : selection_end; |
91 longpress_drag_selection_offset_ = extent - position; | 105 longpress_drag_selection_offset_ = extent - position; |
92 client_->OnDragBegin(*this, extent); | 106 client_->OnDragBegin(*this, extent); |
93 SetState(DRAGGING); | 107 SetState(DRAGGING); |
94 return true; | 108 return true; |
95 } | 109 } |
96 | 110 |
97 bool LongPressDragSelector::IsActive() const { | 111 bool LongPressDragSelector::IsActive() const { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 | 146 |
133 // TODO(jdduke): Add UMA for tracking relative longpress drag frequency. | 147 // TODO(jdduke): Add UMA for tracking relative longpress drag frequency. |
134 if (was_dragging) | 148 if (was_dragging) |
135 client_->OnDragEnd(*this); | 149 client_->OnDragEnd(*this); |
136 | 150 |
137 if (was_active != IsActive()) | 151 if (was_active != IsActive()) |
138 client_->OnLongPressDragActiveStateChanged(); | 152 client_->OnLongPressDragActiveStateChanged(); |
139 } | 153 } |
140 | 154 |
141 } // namespace ui | 155 } // namespace ui |
OLD | NEW |