| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/renderer_host/input/gesture_text_selector.h" | 5 #include "content/browser/renderer_host/input/gesture_text_selector.h" |
| 6 | 6 |
| 7 #include "ui/events/event_constants.h" | 7 #include "ui/events/event_constants.h" |
| 8 #include "ui/events/gesture_detection/gesture_event_data.h" | 8 #include "ui/events/gesture_detection/gesture_config_helper.h" |
| 9 #include "ui/events/gesture_detection/gesture_detector.h" |
| 9 #include "ui/events/gesture_detection/motion_event.h" | 10 #include "ui/events/gesture_detection/motion_event.h" |
| 10 | 11 |
| 12 using ui::GestureDetector; |
| 13 using ui::MotionEvent; |
| 14 |
| 11 namespace content { | 15 namespace content { |
| 16 namespace { |
| 17 scoped_ptr<GestureDetector> CreateGestureDetector( |
| 18 ui::GestureListener* listener) { |
| 19 GestureDetector::Config config = |
| 20 ui::DefaultGestureProviderConfig().gesture_detector_config; |
| 21 |
| 22 ui::DoubleTapListener* null_double_tap_listener = nullptr; |
| 23 |
| 24 // Doubletap, showpress and longpress detection are not required, and |
| 25 // should be explicitly disabled for efficiency. |
| 26 scoped_ptr<ui::GestureDetector> detector( |
| 27 new ui::GestureDetector(config, listener, null_double_tap_listener)); |
| 28 detector->set_longpress_enabled(false); |
| 29 detector->set_showpress_enabled(false); |
| 30 |
| 31 return detector.Pass(); |
| 32 } |
| 33 |
| 34 } // namespace |
| 12 | 35 |
| 13 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) | 36 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) |
| 14 : client_(client), | 37 : client_(client), text_selection_triggered_(false) { |
| 15 text_selection_triggered_(false), | 38 DCHECK(client); |
| 16 anchor_x_(0.0f), | |
| 17 anchor_y_(0.0f) { | |
| 18 } | 39 } |
| 19 | 40 |
| 20 GestureTextSelector::~GestureTextSelector() { | 41 GestureTextSelector::~GestureTextSelector() { |
| 21 } | 42 } |
| 22 | 43 |
| 23 bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) { | 44 bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) { |
| 24 if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) { | 45 if (event.GetAction() == MotionEvent::ACTION_DOWN) { |
| 25 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture | 46 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture |
| 26 // sequences from being forwarded. | 47 // sequences from being forwarded. |
| 27 text_selection_triggered_ = ShouldStartTextSelection(event); | 48 text_selection_triggered_ = ShouldStartTextSelection(event); |
| 28 } | 49 } |
| 29 return text_selection_triggered_; | |
| 30 } | |
| 31 | 50 |
| 32 bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) { | |
| 33 if (!text_selection_triggered_) | 51 if (!text_selection_triggered_) |
| 34 return false; | 52 return false; |
| 35 | 53 |
| 36 switch (gesture.type()) { | 54 if (!gesture_detector_) |
| 37 case ui::ET_GESTURE_TAP: { | 55 gesture_detector_ = CreateGestureDetector(this); |
| 38 client_->LongPress(gesture.time, gesture.x, gesture.y); | 56 |
| 39 break; | 57 gesture_detector_->OnTouchEvent(event); |
| 40 } | 58 |
| 41 case ui::ET_GESTURE_SCROLL_BEGIN: { | 59 // Always return true, even if |gesture_detector_| technically doesn't |
| 42 client_->Unselect(); | 60 // consume the event, to prevent a partial touch stream from being forwarded. |
| 43 anchor_x_ = gesture.x; | 61 return true; |
| 44 anchor_y_ = gesture.y; | 62 } |
| 45 break; | 63 |
| 46 } | 64 bool GestureTextSelector::OnSingleTapUp(const MotionEvent& e) { |
| 47 case ui::ET_GESTURE_SCROLL_UPDATE: { | 65 DCHECK(text_selection_triggered_); |
| 48 // TODO(changwan): check if we can show handles on ET_GESTURE_SCROLL_END | 66 client_->LongPress(e.GetEventTime(), e.GetX(), e.GetY()); |
| 49 // instead. Currently it is not possible as ShowSelectionHandles should | 67 return true; |
| 50 // be called before we change the selection. | 68 } |
| 51 client_->ShowSelectionHandlesAutomatically(); | 69 |
| 52 client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y); | 70 bool GestureTextSelector::OnScroll(const MotionEvent& e1, |
| 53 break; | 71 const MotionEvent& e2, |
| 54 } | 72 float distance_x, |
| 55 default: | 73 float distance_y) { |
| 56 // Suppress all other gestures when we are selecting text. | 74 DCHECK(text_selection_triggered_); |
| 57 break; | 75 // TODO(changwan): check if we can show handles after the scroll finishes |
| 58 } | 76 // instead. Currently it is not possible as ShowSelectionHandles should |
| 77 // be called before we change the selection. |
| 78 client_->ShowSelectionHandlesAutomatically(); |
| 79 client_->SelectRange(e1.GetX(), e1.GetY(), e2.GetX(), e2.GetY()); |
| 59 return true; | 80 return true; |
| 60 } | 81 } |
| 61 | 82 |
| 62 // static | 83 // static |
| 63 bool GestureTextSelector::ShouldStartTextSelection( | 84 bool GestureTextSelector::ShouldStartTextSelection(const MotionEvent& event) { |
| 64 const ui::MotionEvent& event) { | |
| 65 DCHECK_GT(event.GetPointerCount(), 0u); | 85 DCHECK_GT(event.GetPointerCount(), 0u); |
| 66 // Currently we are supporting stylus-only cases. | 86 // Currently we are supporting stylus-only cases. |
| 67 const bool is_stylus = | 87 const bool is_stylus = event.GetToolType(0) == MotionEvent::TOOL_TYPE_STYLUS; |
| 68 event.GetToolType(0) == ui::MotionEvent::TOOL_TYPE_STYLUS; | |
| 69 const bool is_only_secondary_button_pressed = | 88 const bool is_only_secondary_button_pressed = |
| 70 event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY; | 89 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; |
| 71 return is_stylus && is_only_secondary_button_pressed; | 90 return is_stylus && is_only_secondary_button_pressed; |
| 72 } | 91 } |
| 73 | 92 |
| 74 } // namespace content | 93 } // namespace content |
| OLD | NEW |