Index: ui/touch_selection/touch_selection_controller.cc |
diff --git a/ui/touch_selection/touch_selection_controller.cc b/ui/touch_selection/touch_selection_controller.cc |
index ad010c3807275ac0aee8b74fe7b2dc227e8c4510..5bf999baedc28b09557c46cc326cff83ed67b3bc 100644 |
--- a/ui/touch_selection/touch_selection_controller.cc |
+++ b/ui/touch_selection/touch_selection_controller.cc |
@@ -47,7 +47,7 @@ TouchSelectionController::TouchSelectionController( |
bool show_on_tap_for_empty_editable) |
: client_(client), |
tap_timeout_(tap_timeout), |
- tap_slop_(tap_slop), |
+ tap_slop_squared_(static_cast<double>(tap_slop) * tap_slop), |
force_next_update_(false), |
show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), |
response_pending_input_event_(INPUT_EVENT_TYPE_NONE), |
@@ -59,6 +59,7 @@ TouchSelectionController::TouchSelectionController( |
selection_empty_(false), |
selection_editable_(false), |
temporarily_hidden_(false), |
+ longpress_drag_selector_(this), |
selection_handle_dragged_(false) { |
DCHECK(client_); |
} |
@@ -94,8 +95,8 @@ void TouchSelectionController::OnSelectionBoundsChanged( |
&response_pending_input_event_, causal_input_event); |
const bool is_selection_dragging = active_status_ == SELECTION_ACTIVE && |
- (start_selection_handle_->is_dragging() || |
- end_selection_handle_->is_dragging()); |
+ (start_selection_handle_->IsActive() || |
+ end_selection_handle_->IsActive()); |
// It's possible that the bounds temporarily overlap while a selection handle |
// is being dragged, incorrectly reporting a CENTER orientation. |
@@ -128,6 +129,9 @@ void TouchSelectionController::OnSelectionBoundsChanged( |
} |
bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
+ if (longpress_drag_selector_.WillHandleTouchEvent(event)) |
+ return true; |
+ |
if (active_status_ == INSERTION_ACTIVE) { |
DCHECK(insertion_handle_); |
return insertion_handle_->WillHandleTouchEvent(event); |
@@ -136,10 +140,10 @@ bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
if (active_status_ == SELECTION_ACTIVE) { |
DCHECK(start_selection_handle_); |
DCHECK(end_selection_handle_); |
- if (start_selection_handle_->is_dragging()) |
+ if (start_selection_handle_->IsActive()) |
return start_selection_handle_->WillHandleTouchEvent(event); |
- if (end_selection_handle_->is_dragging()) |
+ if (end_selection_handle_->IsActive()) |
return end_selection_handle_->WillHandleTouchEvent(event); |
const gfx::PointF event_pos(event.GetX(), event.GetY()); |
@@ -168,10 +172,12 @@ bool TouchSelectionController::WillHandleTapEvent(const gfx::PointF& location) { |
} |
bool TouchSelectionController::WillHandleLongPressEvent( |
+ base::TimeTicks event_time, |
const gfx::PointF& location) { |
if (WillHandleTapOrLongPress(location)) |
return true; |
+ longpress_drag_selector_.OnLongPressEvent(event_time, location); |
response_pending_input_event_ = LONG_PRESS; |
ShowSelectionHandlesAutomatically(); |
ShowInsertionHandleAutomatically(); |
@@ -205,14 +211,7 @@ void TouchSelectionController::SetTemporarilyHidden(bool hidden) { |
if (temporarily_hidden_ == hidden) |
return; |
temporarily_hidden_ = hidden; |
- |
- TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
- if (active_status_ == SELECTION_ACTIVE) { |
- start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
- end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
- } else if (active_status_ == INSERTION_ACTIVE) { |
- insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
- } |
+ RefreshHandleVisibility(); |
} |
void TouchSelectionController::OnSelectionEditable(bool editable) { |
@@ -283,20 +282,39 @@ const gfx::PointF& TouchSelectionController::GetEndPosition() const { |
return end_.edge_bottom(); |
} |
-void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { |
- if (&handle == insertion_handle_.get()) { |
+void TouchSelectionController::OnDragBegin( |
+ const TouchSelectionDraggable& draggable, |
+ const gfx::PointF& drag_position) { |
+ if (&draggable == insertion_handle_.get()) { |
+ DCHECK_EQ(active_status_, INSERTION_ACTIVE); |
client_->OnSelectionEvent(INSERTION_DRAG_STARTED); |
+ drag_line_offset_ = GetStartLineOffset(); |
return; |
} |
- gfx::PointF base, extent; |
- if (&handle == start_selection_handle_.get()) { |
- base = end_selection_handle_->position() + GetEndLineOffset(); |
- extent = start_selection_handle_->position() + GetStartLineOffset(); |
+ DCHECK_EQ(active_status_, SELECTION_ACTIVE); |
+ |
+ bool extend_selection_start = false; |
+ if (&draggable == start_selection_handle_.get()) { |
+ extend_selection_start = true; |
+ } else if (&draggable == end_selection_handle_.get()) { |
+ extend_selection_start = false; |
+ } else { |
+ DCHECK_EQ(&draggable, &longpress_drag_selector_); |
+ extend_selection_start = |
+ (drag_position - GetStartPosition()).LengthSquared() < |
+ (drag_position - GetEndPosition()).LengthSquared(); |
+ } |
+ |
+ gfx::PointF base = GetStartPosition() + GetStartLineOffset(); |
+ gfx::PointF extent = GetEndPosition() + GetEndLineOffset(); |
+ if (extend_selection_start) { |
+ std::swap(base, extent); |
+ drag_line_offset_ = GetStartLineOffset(); |
} else { |
- base = start_selection_handle_->position() + GetStartLineOffset(); |
- extent = end_selection_handle_->position() + GetEndLineOffset(); |
+ drag_line_offset_ = GetEndLineOffset(); |
} |
+ |
selection_handle_dragged_ = true; |
// When moving the handle we want to move only the extent point. Before doing |
@@ -305,27 +323,31 @@ void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { |
client_->OnSelectionEvent(SELECTION_DRAG_STARTED); |
} |
-void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle, |
- const gfx::PointF& position) { |
+void TouchSelectionController::OnDragUpdate( |
+ const TouchSelectionDraggable& draggable, |
+ const gfx::PointF& drag_position) { |
// As the position corresponds to the bottom left point of the selection |
// bound, offset it by half the corresponding line height. |
- gfx::Vector2dF line_offset = &handle == start_selection_handle_.get() |
- ? GetStartLineOffset() |
- : GetEndLineOffset(); |
- gfx::PointF line_position = position + line_offset; |
- if (&handle == insertion_handle_.get()) |
+ gfx::PointF line_position = drag_position + drag_line_offset_; |
+ if (&draggable == insertion_handle_.get()) |
client_->MoveCaret(line_position); |
else |
client_->MoveRangeSelectionExtent(line_position); |
} |
-void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) { |
- if (&handle == insertion_handle_.get()) |
+void TouchSelectionController::OnDragEnd( |
+ const TouchSelectionDraggable& draggable) { |
+ if (&draggable == insertion_handle_.get()) |
client_->OnSelectionEvent(INSERTION_DRAG_STOPPED); |
else |
client_->OnSelectionEvent(SELECTION_DRAG_STOPPED); |
} |
+bool TouchSelectionController::IsWithinTapSlop( |
+ const gfx::Vector2dF& delta) const { |
+ return delta.LengthSquared() < tap_slop_squared_; |
+} |
+ |
void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) { |
if (insertion_handle_ && &handle == insertion_handle_.get()) |
client_->OnSelectionEvent(INSERTION_TAPPED); |
@@ -343,8 +365,18 @@ base::TimeDelta TouchSelectionController::GetTapTimeout() const { |
return tap_timeout_; |
} |
-float TouchSelectionController::GetTapSlop() const { |
- return tap_slop_; |
+void TouchSelectionController::OnLongPressDragActiveStateChanged() { |
+ // The handles should remain hidden for the duration of a longpress drag, |
+ // including the time between a longpress and the start of drag motion. |
+ RefreshHandleVisibility(); |
+} |
+ |
+gfx::PointF TouchSelectionController::GetSelectionStart() const { |
+ return GetStartPosition(); |
+} |
+ |
+gfx::PointF TouchSelectionController::GetSelectionEnd() const { |
+ return GetEndPosition(); |
} |
void TouchSelectionController::ShowInsertionHandleAutomatically() { |
@@ -474,6 +506,7 @@ void TouchSelectionController::ActivateSelection() { |
selection_handle_dragged_ = false; |
selection_start_time_ = base::TimeTicks::Now(); |
response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
+ longpress_drag_selector_.OnSelectionActivated(); |
client_->OnSelectionEvent(SELECTION_SHOWN); |
} |
} |
@@ -484,6 +517,7 @@ void TouchSelectionController::DeactivateSelection() { |
DCHECK(start_selection_handle_); |
DCHECK(end_selection_handle_); |
LogSelectionEnd(); |
+ longpress_drag_selector_.OnSelectionDeactivated(); |
start_selection_handle_->SetEnabled(false); |
end_selection_handle_->SetEnabled(false); |
active_status_ = INACTIVE; |
@@ -495,6 +529,16 @@ void TouchSelectionController::ForceNextUpdateIfInactive() { |
force_next_update_ = true; |
} |
+void TouchSelectionController::RefreshHandleVisibility() { |
+ TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
+ if (active_status_ == SELECTION_ACTIVE) { |
+ start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
+ end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
+ } |
+ if (active_status_ == INSERTION_ACTIVE) |
+ insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
+} |
+ |
gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { |
return ComputeLineOffsetFromBottom(start_); |
} |
@@ -504,11 +548,17 @@ gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { |
} |
bool TouchSelectionController::GetStartVisible() const { |
- return start_.visible() && !temporarily_hidden_; |
+ if (!start_.visible()) |
+ return false; |
+ |
+ return !temporarily_hidden_ && !longpress_drag_selector_.IsActive(); |
} |
bool TouchSelectionController::GetEndVisible() const { |
- return end_.visible() && !temporarily_hidden_; |
+ if (!end_.visible()) |
+ return false; |
+ |
+ return !temporarily_hidden_ && !longpress_drag_selector_.IsActive(); |
} |
TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle( |