Index: content/browser/renderer_host/input/touch_selection_controller.cc |
diff --git a/content/browser/renderer_host/input/touch_selection_controller.cc b/content/browser/renderer_host/input/touch_selection_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..39baebb7c1c844269c094fe1fceefb8cd87e54c3 |
--- /dev/null |
+++ b/content/browser/renderer_host/input/touch_selection_controller.cc |
@@ -0,0 +1,303 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/renderer_host/input/touch_selection_controller.h" |
+ |
+#include "base/logging.h" |
+ |
+namespace content { |
+ |
+TouchSelectionController::TouchSelectionController( |
+ TouchSelectionControllerClient* client) |
+ : client_(client), |
+ anchor_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED), |
+ anchor_visible_(false), |
+ focus_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED), |
+ focus_visible_(false), |
+ is_insertion_active_(false), |
+ allow_automatic_insertion_activation_(false), |
+ is_selection_active_(false), |
+ allow_automatic_selection_activation_(false), |
+ selection_editable_(false), |
+ selection_editable_for_last_update_(false) { |
+ DCHECK(client_); |
+ HideAndDisallowAutomaticShowing(); |
+} |
+ |
+TouchSelectionController::~TouchSelectionController() { |
+} |
+ |
+void TouchSelectionController::OnSelectionBoundsChanged( |
+ const gfx::RectF& anchor_rect, |
+ TouchHandleOrientation anchor_orientation, |
+ bool anchor_visible, |
+ const gfx::RectF& focus_rect, |
+ TouchHandleOrientation focus_orientation, |
+ bool focus_visible) { |
+ if (!allow_automatic_selection_activation_ && |
+ !allow_automatic_insertion_activation_) |
+ return; |
+ |
+ if (anchor_rect_ == anchor_rect && focus_rect_ == focus_rect && |
+ anchor_orientation_ == anchor_orientation && |
+ focus_orientation_ == focus_orientation && |
+ anchor_visible_ == anchor_visible && focus_visible_ == focus_visible && |
+ selection_editable_ == selection_editable_for_last_update_) |
+ return; |
+ |
+ anchor_rect_ = anchor_rect; |
+ anchor_orientation_ = anchor_orientation; |
+ anchor_visible_ = anchor_visible; |
+ focus_rect_ = focus_rect; |
+ focus_orientation_ = focus_orientation; |
+ focus_visible_ = focus_visible; |
+ selection_editable_for_last_update_ = selection_editable_; |
+ |
+ const gfx::PointF anchor = GetAnchorPosition(); |
+ const gfx::PointF focus = GetFocusPosition(); |
+ if (anchor.x() != focus.x() || anchor.y() != focus.y() || |
cjhopman
2014/07/07 22:13:41
anchor != focus
jdduke (slow)
2014/07/08 00:54:44
Done.
|
+ (start_selection_handle_ && start_selection_handle_->is_dragging()) || |
cjhopman
2014/07/07 22:13:41
Can this just use is_selection_active_ rather than
jdduke (slow)
2014/07/08 00:54:44
Done.
|
+ (end_selection_handle_ && end_selection_handle_->is_dragging())) { |
+ // It's possible that the bounds temporarily overlap while a handle is |
+ // being dragged, incorrectly reporting a CENTER orientation. |
+ if (anchor_orientation_ == TOUCH_HANDLE_CENTER && start_selection_handle_) |
cjhopman
2014/07/07 22:13:41
AIUI, if anchor_orientation_ == TOUCH_HANDLE_CENTE
jdduke (slow)
2014/07/08 00:54:44
It's definitely better, good call.
|
+ anchor_orientation_ = start_selection_handle_->orientation(); |
+ if (focus_orientation_ == TOUCH_HANDLE_CENTER && end_selection_handle_) |
+ focus_orientation_ = end_selection_handle_->orientation(); |
+ |
+ OnSelectionChanged(); |
+ return; |
+ } |
+ |
+ if (anchor_orientation_ == TOUCH_HANDLE_CENTER) { |
+ OnInsertionChanged(); |
+ return; |
+ } |
+ |
+ HideAndDisallowAutomaticShowing(); |
+} |
+ |
+bool TouchSelectionController::WillHandleTouchEvent( |
+ const ui::MotionEvent& event) { |
+ if (is_insertion_active_) { |
+ DCHECK(insertion_handle_); |
+ return insertion_handle_->WillHandleTouchEvent(event); |
+ } |
+ |
+ if (is_selection_active_) { |
+ DCHECK(start_selection_handle_); |
+ DCHECK(end_selection_handle_); |
+ return start_selection_handle_->WillHandleTouchEvent(event) || |
+ end_selection_handle_->WillHandleTouchEvent(event); |
+ } |
+ |
+ return false; |
+} |
+ |
+void TouchSelectionController::AllowAutomaticInsertionShowing() { |
+ if (allow_automatic_insertion_activation_) |
+ return; |
+ allow_automatic_insertion_activation_ = true; |
+ if (!is_insertion_active_ && !is_selection_active_) |
+ ResetCachedValues(); |
+} |
+ |
+void TouchSelectionController::AllowAutomaticSelectionShowing() { |
+ if (allow_automatic_selection_activation_) |
+ return; |
+ allow_automatic_selection_activation_ = true; |
+ if (!is_insertion_active_ && !is_selection_active_) |
+ ResetCachedValues(); |
+} |
+ |
+void TouchSelectionController::HideAndDisallowAutomaticShowing() { |
+ DeactivateInsertion(); |
+ DeactivateSelection(); |
+ allow_automatic_insertion_activation_ = false; |
+ allow_automatic_selection_activation_ = false; |
+} |
+ |
+void TouchSelectionController::OnSelectionEditable(bool editable) { |
+ if (selection_editable_ == editable) |
+ return; |
+ selection_editable_ = editable; |
+ if (!selection_editable_) |
+ DeactivateInsertion(); |
+} |
+ |
+bool TouchSelectionController::Animate(base::TimeTicks frame_time) { |
+ if (is_insertion_active_) |
+ return insertion_handle_->Animate(frame_time); |
+ |
+ if (is_selection_active_) { |
+ bool needs_animate = start_selection_handle_->Animate(frame_time); |
+ needs_animate |= end_selection_handle_->Animate(frame_time); |
+ return needs_animate; |
+ } |
+ |
+ return false; |
+} |
+ |
+void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { |
+ if (&handle == insertion_handle_.get()) |
+ return; |
+ |
+ if (&handle == start_selection_handle_.get()) { |
+ fixed_handle_position_ = end_selection_handle_->position() - |
+ gfx::Vector2dF(0, GetFocusLineHeight() / 2.f); |
+ } else { |
+ fixed_handle_position_ = start_selection_handle_->position() - |
+ gfx::Vector2dF(0, GetAnchorLineHeight() / 2.f); |
+ } |
+} |
+ |
+void TouchSelectionController::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. |
+ float half_line_height = &handle == end_selection_handle_.get() |
+ ? GetFocusLineHeight() / 2.f |
+ : GetAnchorLineHeight() / 2.f; |
+ gfx::PointF line_position = position - gfx::Vector2dF(0, half_line_height); |
+ if (&handle == insertion_handle_.get()) { |
+ client_->MoveCaret(line_position); |
+ } else { |
+ client_->SelectBetweenCoordinates(fixed_handle_position_, line_position); |
+ } |
+} |
+ |
+void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) { |
+} |
+ |
+void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) { |
+ if (insertion_handle_ && &handle == insertion_handle_.get()) |
+ client_->OnSelectionEvent(INSERTION_TAPPED, GetAnchorPosition()); |
+} |
+ |
+void TouchSelectionController::SetNeedsAnimate() { |
+ client_->SetNeedsAnimate(); |
+} |
+ |
+scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { |
+ return client_->CreateDrawable(); |
+} |
+ |
+void TouchSelectionController::OnInsertionChanged() { |
+ DeactivateSelection(); |
+ |
+ if (!allow_automatic_insertion_activation_ || !selection_editable_) |
+ return; |
+ |
+ bool was_active = is_insertion_active_; |
+ gfx::PointF position = GetAnchorPosition(); |
+ if (!was_active) |
+ ActivateInsertion(); |
+ else |
+ client_->OnSelectionEvent(INSERTION_MOVED, position); |
+ |
+ if (was_active) |
+ insertion_handle_->SetVisibleAnimated(anchor_visible_); |
+ else |
+ insertion_handle_->SetVisible(anchor_visible_); |
+ |
+ insertion_handle_->SetPosition(position); |
+} |
+ |
+void TouchSelectionController::OnSelectionChanged() { |
+ DeactivateInsertion(); |
+ |
+ if (!allow_automatic_selection_activation_) |
+ return; |
+ |
+ const bool was_active = is_selection_active_; |
+ ActivateSelection(); |
+ if (was_active) { |
+ start_selection_handle_->SetVisibleAnimated(anchor_visible_); |
+ end_selection_handle_->SetVisibleAnimated(focus_visible_); |
+ } else { |
+ start_selection_handle_->SetVisible(anchor_visible_); |
+ end_selection_handle_->SetVisible(focus_visible_); |
+ } |
+ start_selection_handle_->SetPosition(GetAnchorPosition()); |
+ end_selection_handle_->SetPosition(GetFocusPosition()); |
+} |
+ |
+void TouchSelectionController::ActivateInsertion() { |
+ DCHECK(!is_selection_active_); |
+ |
+ if (!insertion_handle_) |
+ insertion_handle_.reset(new TouchHandle(this, TOUCH_HANDLE_CENTER)); |
+ |
+ if (!is_insertion_active_) { |
+ is_insertion_active_ = true; |
+ client_->OnSelectionEvent(INSERTION_SHOWN, GetAnchorPosition()); |
+ } |
+} |
+ |
+void TouchSelectionController::DeactivateInsertion() { |
+ if (!is_insertion_active_) |
+ return; |
+ DCHECK(insertion_handle_); |
+ insertion_handle_->Hide(); |
+ is_insertion_active_ = false; |
+ client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); |
+} |
+ |
+void TouchSelectionController::ActivateSelection() { |
+ DCHECK(!is_insertion_active_); |
+ |
+ if (!start_selection_handle_) |
+ start_selection_handle_.reset(new TouchHandle(this, anchor_orientation_)); |
+ else |
+ start_selection_handle_->SetOrientation(anchor_orientation_); |
+ |
+ if (!end_selection_handle_) |
+ end_selection_handle_.reset(new TouchHandle(this, focus_orientation_)); |
+ else |
+ end_selection_handle_->SetOrientation(focus_orientation_); |
+ |
+ if (!is_selection_active_) { |
+ is_selection_active_ = true; |
+ client_->OnSelectionEvent(SELECTION_SHOWN, GetAnchorPosition()); |
+ } |
+} |
+ |
+void TouchSelectionController::DeactivateSelection() { |
+ if (!is_selection_active_) |
+ return; |
+ DCHECK(start_selection_handle_); |
+ DCHECK(end_selection_handle_); |
+ start_selection_handle_->Hide(); |
+ end_selection_handle_->Hide(); |
+ is_selection_active_ = false; |
+ client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF()); |
+} |
+ |
+void TouchSelectionController::ResetCachedValues() { |
+ anchor_rect_ = gfx::RectF(); |
+ focus_rect_ = gfx::RectF(); |
+ anchor_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED; |
+ focus_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED; |
+ anchor_visible_ = false; |
+ focus_visible_ = false; |
+ selection_editable_for_last_update_ = false; |
+} |
+ |
+gfx::PointF TouchSelectionController::GetAnchorPosition() const { |
+ return anchor_rect_.bottom_left(); |
+} |
+ |
+gfx::PointF TouchSelectionController::GetFocusPosition() const { |
+ return focus_rect_.bottom_left(); |
+} |
+ |
+float TouchSelectionController::GetAnchorLineHeight() const { |
+ return anchor_rect_.height(); |
+} |
+ |
+float TouchSelectionController::GetFocusLineHeight() const { |
+ return focus_rect_.height(); |
+} |
+ |
+} // namespace content |