Chromium Code Reviews| Index: ui/touch_selection/touch_selection_controller_impl.cc |
| diff --git a/ui/touch_selection/touch_selection_controller.cc b/ui/touch_selection/touch_selection_controller_impl.cc |
| similarity index 67% |
| rename from ui/touch_selection/touch_selection_controller.cc |
| rename to ui/touch_selection/touch_selection_controller_impl.cc |
| index 2e18037c658c9b0f7fc02d66e5a8471e8589d0a2..d575c4c7ba08faa301a7212c8b35cda81c1c02a1 100644 |
| --- a/ui/touch_selection/touch_selection_controller.cc |
| +++ b/ui/touch_selection/touch_selection_controller_impl.cc |
| @@ -2,15 +2,19 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "ui/touch_selection/touch_selection_controller.h" |
| +#include "ui/touch_selection/touch_selection_controller_impl.h" |
| #include "base/auto_reset.h" |
| #include "base/logging.h" |
| #include "base/metrics/histogram_macros.h" |
| +#include "ui/gfx/geometry/rect.h" |
| namespace ui { |
| namespace { |
| +// Delay before showing the quick menu, in milliseconds. |
| +const int kQuickMenuDelayInMs = 100; |
| + |
| gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) { |
| gfx::Vector2dF line_offset = |
| gfx::ScaleVector2d(bound.edge_top() - bound.edge_bottom(), 0.5f); |
| @@ -38,9 +42,21 @@ TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { |
| return TouchHandleOrientation::UNDEFINED; |
| } |
| +void ClipPoint(gfx::PointF* point, const gfx::Rect& clip_rect) { |
| + point->SetToMax(clip_rect.origin()); |
| + point->SetToMin(clip_rect.bottom_right()); |
| +} |
| + |
| +void ClipSelectionBound(SelectionBound* bound, const gfx::Rect& clip_rect) { |
| + gfx::PointF edge_top = bound->edge_top(); |
| + gfx::PointF edge_bottom = bound->edge_bottom(); |
| + ClipPoint(&edge_top, clip_rect); |
| + ClipPoint(&edge_bottom, clip_rect); |
| + bound->SetEdge(edge_top, edge_bottom); |
| +} |
| } // namespace |
| -TouchSelectionController::TouchSelectionController( |
| +TouchSelectionControllerImpl::TouchSelectionControllerImpl( |
| TouchSelectionControllerClient* client, |
| base::TimeDelta tap_timeout, |
| float tap_slop, |
| @@ -59,18 +75,26 @@ TouchSelectionController::TouchSelectionController( |
| selection_empty_(false), |
| selection_editable_(false), |
| temporarily_hidden_(false), |
| - selection_handle_dragged_(false) { |
| + quick_menu_timer_(FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(kQuickMenuDelayInMs), |
| + base::Bind(&TouchSelectionControllerImpl::ShowQuickMenu, |
| + base::Unretained(this)), |
| + false), |
| + scroll_in_progress_(false), |
| + overscroll_in_progress_(false), |
| + handle_drag_in_progress_(false), |
| + selection_handle_was_dragged_(false) { |
| DCHECK(client_); |
| } |
| -TouchSelectionController::~TouchSelectionController() { |
| +TouchSelectionControllerImpl::~TouchSelectionControllerImpl() { |
| } |
| -void TouchSelectionController::OnSelectionBoundsChanged( |
| +bool TouchSelectionControllerImpl::OnSelectionBoundsUpdated( |
| const SelectionBound& start, |
| const SelectionBound& end) { |
| if (start == start_ && end_ == end) |
| - return; |
| + return false; |
| start_ = start; |
| end_ = end; |
| @@ -80,7 +104,7 @@ void TouchSelectionController::OnSelectionBoundsChanged( |
| if (!activate_selection_automatically_ && |
| !activate_insertion_automatically_) { |
| DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); |
| - return; |
| + return false; |
| } |
| // Ensure that |response_pending_input_event_| is cleared after the method |
| @@ -113,19 +137,22 @@ void TouchSelectionController::OnSelectionBoundsChanged( |
| start_orientation_ != TouchHandleOrientation::UNDEFINED && |
| end_orientation_ != TouchHandleOrientation::UNDEFINED)) { |
| OnSelectionChanged(); |
| - return; |
| + UpdateQuickMenu(); |
| + return true; |
| } |
| if (start_orientation_ == TouchHandleOrientation::CENTER && |
| selection_editable_) { |
| OnInsertionChanged(); |
| - return; |
| + return true; |
| } |
| HideAndDisallowShowingAutomatically(); |
| + return true; |
| } |
| -bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
| +bool TouchSelectionControllerImpl::WillHandleTouchEvent( |
| + const MotionEvent& event) { |
| if (is_insertion_active_) { |
| DCHECK(insertion_handle_); |
| return insertion_handle_->WillHandleTouchEvent(event); |
| @@ -151,14 +178,14 @@ bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { |
| return false; |
| } |
| -void TouchSelectionController::OnLongPressEvent() { |
| +void TouchSelectionControllerImpl::OnLongPressEvent() { |
| response_pending_input_event_ = LONG_PRESS; |
| ShowSelectionHandlesAutomatically(); |
| ShowInsertionHandleAutomatically(); |
| ResetCachedValuesIfInactive(); |
| } |
| -void TouchSelectionController::AllowShowingFromCurrentSelection() { |
| +void TouchSelectionControllerImpl::AllowShowingFromCurrentSelection() { |
| if (is_selection_active_ || is_insertion_active_) |
| return; |
| @@ -169,9 +196,11 @@ void TouchSelectionController::AllowShowingFromCurrentSelection() { |
| else if (start_orientation_ == TouchHandleOrientation::CENTER && |
| selection_editable_) |
| OnInsertionChanged(); |
| + // TODO(mfomitchev): May not be needed |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::OnTapEvent() { |
| +void TouchSelectionControllerImpl::OnTapEvent() { |
| response_pending_input_event_ = TAP; |
| ShowInsertionHandleAutomatically(); |
| if (selection_empty_ && !show_on_tap_for_empty_editable_) |
| @@ -179,15 +208,16 @@ void TouchSelectionController::OnTapEvent() { |
| ResetCachedValuesIfInactive(); |
| } |
| -void TouchSelectionController::HideAndDisallowShowingAutomatically() { |
| +void TouchSelectionControllerImpl::HideAndDisallowShowingAutomatically() { |
| response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| DeactivateInsertion(); |
| DeactivateSelection(); |
| activate_insertion_automatically_ = false; |
| activate_selection_automatically_ = false; |
| + //UpdateQuickMenu(); - DOn't think this is needed |
| } |
| -void TouchSelectionController::SetTemporarilyHidden(bool hidden) { |
| +void TouchSelectionControllerImpl::SetTemporarilyHidden(bool hidden) { |
| if (temporarily_hidden_ == hidden) |
| return; |
| temporarily_hidden_ = hidden; |
| @@ -201,23 +231,26 @@ void TouchSelectionController::SetTemporarilyHidden(bool hidden) { |
| insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
| } |
| -void TouchSelectionController::OnSelectionEditable(bool editable) { |
| +void TouchSelectionControllerImpl::OnSelectionEditable(bool editable) { |
| if (selection_editable_ == editable) |
| return; |
| selection_editable_ = editable; |
| ResetCachedValuesIfInactive(); |
| if (!selection_editable_) |
| DeactivateInsertion(); |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::OnSelectionEmpty(bool empty) { |
| +void TouchSelectionControllerImpl::OnSelectionEmpty(bool empty) { |
| if (selection_empty_ == empty) |
| return; |
| selection_empty_ = empty; |
| ResetCachedValuesIfInactive(); |
| + // TODO: Do we really need this? |
| + UpdateQuickMenu(); |
| } |
| -bool TouchSelectionController::Animate(base::TimeTicks frame_time) { |
| +bool TouchSelectionControllerImpl::Animate(base::TimeTicks frame_time) { |
| if (is_insertion_active_) |
| return insertion_handle_->Animate(frame_time); |
| @@ -230,9 +263,40 @@ bool TouchSelectionController::Animate(base::TimeTicks frame_time) { |
| return false; |
| } |
| -void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { |
| +void TouchSelectionControllerImpl::OnNativeViewMoved() { |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnScrollStarted() { |
| + scroll_in_progress_ = true; |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnScrollCompleted() { |
| + scroll_in_progress_ = false; |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnOverscrollStarted() { |
| + overscroll_in_progress_ = true; |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnOverscrollCompleted() { |
| + overscroll_in_progress_ = false; |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnFlingCompleted() { |
| + scroll_in_progress_ = false; |
| + UpdateQuickMenu(); |
| +} |
| + |
| +void TouchSelectionControllerImpl::OnHandleDragBegin( |
| + const TouchHandle& handle) { |
| if (&handle == insertion_handle_.get()) { |
| client_->OnSelectionEvent(INSERTION_DRAG_STARTED, handle.position()); |
| + UpdateQuickMenu(); |
| return; |
| } |
| @@ -244,16 +308,17 @@ void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { |
| base = start_selection_handle_->position() + GetStartLineOffset(); |
| extent = end_selection_handle_->position() + GetEndLineOffset(); |
| } |
| - selection_handle_dragged_ = true; |
| + selection_handle_was_dragged_ = true; |
| // When moving the handle we want to move only the extent point. Before doing |
| // so we must make sure that the base point is set correctly. |
| client_->SelectBetweenCoordinates(base, extent); |
| client_->OnSelectionEvent(SELECTION_DRAG_STARTED, handle.position()); |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle, |
| +void TouchSelectionControllerImpl::OnHandleDragUpdate(const TouchHandle& handle, |
| const gfx::PointF& position) { |
| // As the position corresponds to the bottom left point of the selection |
| // bound, offset it by half the corresponding line height. |
| @@ -261,56 +326,56 @@ void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle, |
| ? GetStartLineOffset() |
| : GetEndLineOffset(); |
| gfx::PointF line_position = position + line_offset; |
| - if (&handle == insertion_handle_.get()) { |
| + if (&handle == insertion_handle_.get()) |
| client_->MoveCaret(line_position); |
| - } else { |
| + else |
| client_->MoveRangeSelectionExtent(line_position); |
| - } |
| } |
| -void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) { |
| +void TouchSelectionControllerImpl::OnHandleDragEnd(const TouchHandle& handle) { |
| if (&handle == insertion_handle_.get()) |
| client_->OnSelectionEvent(INSERTION_DRAG_STOPPED, handle.position()); |
| else |
| client_->OnSelectionEvent(SELECTION_DRAG_STOPPED, handle.position()); |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) { |
| +void TouchSelectionControllerImpl::OnHandleTapped(const TouchHandle& handle) { |
| if (insertion_handle_ && &handle == insertion_handle_.get()) |
| client_->OnSelectionEvent(INSERTION_TAPPED, handle.position()); |
| } |
| -void TouchSelectionController::SetNeedsAnimate() { |
| +void TouchSelectionControllerImpl::SetNeedsAnimate() { |
| client_->SetNeedsAnimate(); |
| } |
| -scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { |
| +scoped_ptr<TouchHandleDrawable> TouchSelectionControllerImpl::CreateDrawable() { |
| return client_->CreateDrawable(); |
| } |
| -base::TimeDelta TouchSelectionController::GetTapTimeout() const { |
| +base::TimeDelta TouchSelectionControllerImpl::GetTapTimeout() const { |
| return tap_timeout_; |
| } |
| -float TouchSelectionController::GetTapSlop() const { |
| +float TouchSelectionControllerImpl::GetTapSlop() const { |
| return tap_slop_; |
| } |
| -void TouchSelectionController::ShowInsertionHandleAutomatically() { |
| +void TouchSelectionControllerImpl::ShowInsertionHandleAutomatically() { |
| if (activate_insertion_automatically_) |
| return; |
| activate_insertion_automatically_ = true; |
| ResetCachedValuesIfInactive(); |
| } |
| -void TouchSelectionController::ShowSelectionHandlesAutomatically() { |
| +void TouchSelectionControllerImpl::ShowSelectionHandlesAutomatically() { |
| if (activate_selection_automatically_) |
| return; |
| activate_selection_automatically_ = true; |
| ResetCachedValuesIfInactive(); |
| } |
| -void TouchSelectionController::OnInsertionChanged() { |
| +void TouchSelectionControllerImpl::OnInsertionChanged() { |
| DeactivateSelection(); |
| if (response_pending_input_event_ == TAP && selection_empty_ && |
| @@ -334,7 +399,7 @@ void TouchSelectionController::OnInsertionChanged() { |
| insertion_handle_->SetPosition(position); |
| } |
| -void TouchSelectionController::OnSelectionChanged() { |
| +void TouchSelectionControllerImpl::OnSelectionChanged() { |
| DeactivateInsertion(); |
| if (!activate_selection_automatically_) |
| @@ -351,7 +416,7 @@ void TouchSelectionController::OnSelectionChanged() { |
| end_selection_handle_->SetPosition(GetEndPosition()); |
| } |
| -void TouchSelectionController::ActivateInsertion() { |
| +void TouchSelectionControllerImpl::ActivateInsertion() { |
| DCHECK(!is_selection_active_); |
| if (!insertion_handle_) |
| @@ -362,19 +427,21 @@ void TouchSelectionController::ActivateInsertion() { |
| is_insertion_active_ = true; |
| insertion_handle_->SetEnabled(true); |
| client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition()); |
| + UpdateQuickMenu(); |
| } |
| } |
| -void TouchSelectionController::DeactivateInsertion() { |
| +void TouchSelectionControllerImpl::DeactivateInsertion() { |
| if (!is_insertion_active_) |
| return; |
| DCHECK(insertion_handle_); |
| is_insertion_active_ = false; |
| insertion_handle_->SetEnabled(false); |
| client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::ActivateSelection() { |
| +void TouchSelectionControllerImpl::ActivateSelection() { |
| DCHECK(!is_insertion_active_); |
| if (!start_selection_handle_) { |
| @@ -400,14 +467,15 @@ void TouchSelectionController::ActivateSelection() { |
| LogSelectionEnd(); |
| } |
| is_selection_active_ = true; |
| - selection_handle_dragged_ = false; |
| + selection_handle_was_dragged_ = false; |
| selection_start_time_ = base::TimeTicks::Now(); |
| response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition()); |
| + UpdateQuickMenu(); |
| } |
| } |
| -void TouchSelectionController::DeactivateSelection() { |
| +void TouchSelectionControllerImpl::DeactivateSelection() { |
| if (!is_selection_active_) |
| return; |
| DCHECK(start_selection_handle_); |
| @@ -417,9 +485,10 @@ void TouchSelectionController::DeactivateSelection() { |
| end_selection_handle_->SetEnabled(false); |
| is_selection_active_ = false; |
| client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF()); |
| + UpdateQuickMenu(); |
| } |
| -void TouchSelectionController::ResetCachedValuesIfInactive() { |
| +void TouchSelectionControllerImpl::ResetCachedValuesIfInactive() { |
| if (is_selection_active_ || is_insertion_active_) |
| return; |
| start_ = SelectionBound(); |
| @@ -428,43 +497,97 @@ void TouchSelectionController::ResetCachedValuesIfInactive() { |
| end_orientation_ = TouchHandleOrientation::UNDEFINED; |
| } |
| -const gfx::PointF& TouchSelectionController::GetStartPosition() const { |
| +const gfx::PointF& TouchSelectionControllerImpl::GetStartPosition() const { |
| return start_.edge_bottom(); |
| } |
| -const gfx::PointF& TouchSelectionController::GetEndPosition() const { |
| +const gfx::PointF& TouchSelectionControllerImpl::GetEndPosition() const { |
| return end_.edge_bottom(); |
| } |
| -gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { |
| +gfx::Vector2dF TouchSelectionControllerImpl::GetStartLineOffset() const { |
| return ComputeLineOffsetFromBottom(start_); |
| } |
| -gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { |
| +gfx::Vector2dF TouchSelectionControllerImpl::GetEndLineOffset() const { |
| return ComputeLineOffsetFromBottom(end_); |
| } |
| -bool TouchSelectionController::GetStartVisible() const { |
| +bool TouchSelectionControllerImpl::GetStartVisible() const { |
| return start_.visible() && !temporarily_hidden_; |
| } |
| -bool TouchSelectionController::GetEndVisible() const { |
| +bool TouchSelectionControllerImpl::GetEndVisible() const { |
| return end_.visible() && !temporarily_hidden_; |
| } |
| -TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle( |
| +TouchHandle::AnimationStyle TouchSelectionControllerImpl::GetAnimationStyle( |
| bool was_active) const { |
| return was_active && client_->SupportsAnimation() |
| ? TouchHandle::ANIMATION_SMOOTH |
| : TouchHandle::ANIMATION_NONE; |
| } |
| -void TouchSelectionController::LogSelectionEnd() { |
| +gfx::Rect TouchSelectionControllerImpl::GetMenuAnchorRect() const { |
| + SelectionBound clipped_start = start(); |
| + SelectionBound clipped_end = end(); |
| + const gfx::Rect& client_bounds = client_->GetClientBounds(); |
| + if (clipped_start.visible()) |
| + ClipSelectionBound(&clipped_start, client_bounds); |
| + if (clipped_end.visible()) |
| + ClipSelectionBound(&clipped_end, client_bounds); |
| + |
| + if (clipped_start.visible() && clipped_end.visible()) |
| + return RectBetweenSelectionBounds(clipped_start, clipped_end); |
| + if (clipped_start.visible()) { |
| + return gfx::BoundingRect(clipped_start.edge_top_rounded(), |
| + clipped_start.edge_bottom_rounded()); |
| + } |
| + return gfx::BoundingRect(clipped_end.edge_top_rounded(), |
| + clipped_end.edge_bottom_rounded()); |
| +} |
| + |
| +void TouchSelectionControllerImpl::ShowQuickMenu() { |
|
jdduke (slow)
2015/03/30 15:46:22
All of this menu code will have to be a no-op on A
|
| + DCHECK(is_insertion_active() || is_selection_active()); |
| + |
| + if (!start().visible() && |
| + !end().visible()) |
| + return; |
| + |
| + if (TouchSelectionMenuRunner::GetInstance()) |
| + client_->ShowQuickMenu(GetMenuAnchorRect()); |
| +} |
| + |
| +void TouchSelectionControllerImpl::UpdateQuickMenu() { |
| + // Hide quick menu if there is any. |
| + client_->HideQuickMenu(); |
| + quick_menu_timer_.Stop(); |
| + |
| + // Start timer to show quick menu if necessary. |
| + if (!is_insertion_active() && !is_selection_active()) |
| + return; |
| + |
| + if (!IsQuickMenuAllowed()) |
| + return; |
| + |
| + // TODO: test API |
| +// if (test_api_ && test_api_->immediate_quick_menu()) |
| +// ShowQuickMenu(); |
| +// else |
| + quick_menu_timer_.Reset(); |
| +} |
| + |
| +bool TouchSelectionControllerImpl::IsQuickMenuAllowed() const { |
| + return !scroll_in_progress_ && !overscroll_in_progress_ && |
| + !handle_drag_in_progress_; |
| +} |
| + |
| +void TouchSelectionControllerImpl::LogSelectionEnd() { |
| // TODO(mfomitchev): Once we are able to tell the difference between |
| // 'successful' and 'unsuccessful' selections - log |
| // Event.TouchSelection.Duration instead and get rid of |
| // Event.TouchSelectionD.WasDraggeduration. |
| - if (selection_handle_dragged_) { |
| + if (selection_handle_was_dragged_) { |
| base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
| UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
| duration, |