Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1336)

Unified Diff: content/browser/renderer_host/input/gesture_text_selector.cc

Issue 342633003: [Android] Select text when stylus first button is pressed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed signed unsigned comparison error Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/input/gesture_text_selector.cc
diff --git a/content/browser/renderer_host/input/gesture_text_selector.cc b/content/browser/renderer_host/input/gesture_text_selector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..597bcbf90b90c1f027525805a04cd0d72cc7e5f3
--- /dev/null
+++ b/content/browser/renderer_host/input/gesture_text_selector.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 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/gesture_text_selector.h"
+
+#include "ui/events/event_constants.h"
+#include "ui/events/gesture_detection/gesture_event_data.h"
+#include "ui/events/gesture_detection/motion_event.h"
+
+namespace content {
+
+GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client)
+ : client_(client),
+ text_selection_triggered_(false),
+ anchor_x_(0.0f),
+ anchor_y_(0.0f) {
+}
+
+GestureTextSelector::~GestureTextSelector() {
+}
+
+bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) {
+ if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) {
+ // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture
+ // sequences from being forwarded.
+ text_selection_triggered_ = ShouldStartTextSelection(event);
+ }
+ return text_selection_triggered_;
+}
+
+bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) {
+ if (!text_selection_triggered_)
+ return false;
+
+ switch (gesture.type()) {
+ case ui::ET_GESTURE_TAP: {
+ client_->Unselect();
+ break;
+ }
+ case ui::ET_GESTURE_SCROLL_BEGIN: {
+ client_->Unselect();
+ // TODO(changwan): check if we can show handles on ET_GESTURE_SCROLL_END
+ // instead. Currently it is not possible as ShowSelectionHandles should
+ // be called before we change the selection.
+ client_->ShowSelectionHandlesAutomatically();
+ anchor_x_ = gesture.x;
+ anchor_y_ = gesture.y;
+ break;
+ }
+ case ui::ET_GESTURE_SCROLL_UPDATE: {
+ client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y);
+ break;
+ }
+ default:
+ // Suppress all other gestures when we are selecting text.
+ break;
+ }
+ return true;
+}
+
+// static
+bool GestureTextSelector::ShouldStartTextSelection(
+ const ui::MotionEvent& event) {
+ DCHECK_GT(event.GetPointerCount(), 0u);
+ // Currently we are supporting stylus-only cases.
+ const bool is_stylus =
+ event.GetToolType(0) == ui::MotionEvent::TOOL_TYPE_STYLUS;
+ const bool is_only_secondary_button_pressed =
+ event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY;
+ return is_stylus && is_only_secondary_button_pressed;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698