Chromium Code Reviews| 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_config_helper.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/gesture_detector.h" |
| 10 #include "ui/events/gesture_detection/motion_event.h" | 10 #include "ui/events/gesture_detection/motion_event.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 new ui::GestureDetector(config, listener, null_double_tap_listener)); | 27 new ui::GestureDetector(config, listener, null_double_tap_listener)); |
| 28 detector->set_longpress_enabled(false); | 28 detector->set_longpress_enabled(false); |
| 29 detector->set_showpress_enabled(false); | 29 detector->set_showpress_enabled(false); |
| 30 | 30 |
| 31 return detector.Pass(); | 31 return detector.Pass(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) | 36 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) |
| 37 : client_(client), text_selection_triggered_(false) { | 37 : client_(client), |
| 38 text_selection_triggered_(false), | |
| 39 secondary_button_pressed_(false) { | |
| 38 DCHECK(client); | 40 DCHECK(client); |
|
jdduke (slow)
2014/10/10 16:23:21
Also initialize anchor_x_/anchor_y_ in the constru
| |
| 39 } | 41 } |
| 40 | 42 |
| 41 GestureTextSelector::~GestureTextSelector() { | 43 GestureTextSelector::~GestureTextSelector() { |
| 42 } | 44 } |
| 43 | 45 |
| 44 bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) { | 46 bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) { |
| 45 if (event.GetAction() == MotionEvent::ACTION_DOWN) { | 47 if (event.GetAction() == MotionEvent::ACTION_DOWN) { |
| 46 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture | 48 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture |
| 47 // sequences from being forwarded. | 49 // sequences from being forwarded. |
| 48 text_selection_triggered_ = ShouldStartTextSelection(event); | 50 text_selection_triggered_ = ShouldStartTextSelection(event); |
| 51 secondary_button_pressed_ = | |
| 52 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; | |
| 53 anchor_x_ = event.GetX(); | |
| 54 anchor_y_ = event.GetY(); | |
| 49 } | 55 } |
| 50 | 56 |
| 51 if (!text_selection_triggered_) | 57 if (!text_selection_triggered_) |
| 52 return false; | 58 return false; |
| 53 | 59 |
| 60 if (event.GetAction() == MotionEvent::ACTION_MOVE) { | |
| 61 secondary_button_pressed_ = | |
| 62 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; | |
| 63 if (!secondary_button_pressed_) { | |
| 64 anchor_x_ = event.GetX(); | |
| 65 anchor_y_ = event.GetY(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 54 if (!gesture_detector_) | 69 if (!gesture_detector_) |
| 55 gesture_detector_ = CreateGestureDetector(this); | 70 gesture_detector_ = CreateGestureDetector(this); |
| 56 | 71 |
| 57 gesture_detector_->OnTouchEvent(event); | 72 gesture_detector_->OnTouchEvent(event); |
| 58 | 73 |
| 59 // Always return true, even if |gesture_detector_| technically doesn't | 74 // Always return true, even if |gesture_detector_| technically doesn't |
| 60 // consume the event, to prevent a partial touch stream from being forwarded. | 75 // consume the event, to prevent a partial touch stream from being forwarded. |
| 61 return true; | 76 return true; |
| 62 } | 77 } |
| 63 | 78 |
| 64 bool GestureTextSelector::OnSingleTapUp(const MotionEvent& e) { | 79 bool GestureTextSelector::OnSingleTapUp(const MotionEvent& e) { |
| 65 DCHECK(text_selection_triggered_); | 80 DCHECK(text_selection_triggered_); |
| 66 client_->LongPress(e.GetEventTime(), e.GetX(), e.GetY()); | 81 client_->LongPress(e.GetEventTime(), e.GetX(), e.GetY()); |
| 67 return true; | 82 return true; |
| 68 } | 83 } |
| 69 | 84 |
| 70 bool GestureTextSelector::OnScroll(const MotionEvent& e1, | 85 bool GestureTextSelector::OnScroll(const MotionEvent& e1, |
| 71 const MotionEvent& e2, | 86 const MotionEvent& e2, |
| 72 float distance_x, | 87 float distance_x, |
| 73 float distance_y) { | 88 float distance_y) { |
| 74 DCHECK(text_selection_triggered_); | 89 DCHECK(text_selection_triggered_); |
| 90 | |
| 91 // Return if Stylus button is not pressed. | |
| 92 if (!secondary_button_pressed_) | |
| 93 return true; | |
| 94 | |
| 75 // TODO(changwan): check if we can show handles after the scroll finishes | 95 // TODO(changwan): check if we can show handles after the scroll finishes |
| 76 // instead. Currently it is not possible as ShowSelectionHandles should | 96 // instead. Currently it is not possible as ShowSelectionHandles should |
| 77 // be called before we change the selection. | 97 // be called before we change the selection. |
| 78 client_->ShowSelectionHandlesAutomatically(); | 98 client_->ShowSelectionHandlesAutomatically(); |
| 79 client_->SelectRange(e1.GetX(), e1.GetY(), e2.GetX(), e2.GetY()); | 99 client_->SelectRange(anchor_x_, anchor_y_, e2.GetX(), e2.GetY()); |
| 80 return true; | 100 return true; |
| 81 } | 101 } |
| 82 | 102 |
| 83 // static | 103 // static |
| 84 bool GestureTextSelector::ShouldStartTextSelection(const MotionEvent& event) { | 104 bool GestureTextSelector::ShouldStartTextSelection(const MotionEvent& event) { |
| 85 DCHECK_GT(event.GetPointerCount(), 0u); | 105 DCHECK_GT(event.GetPointerCount(), 0u); |
| 86 // Currently we are supporting stylus-only cases. | 106 // Currently we are supporting stylus-only cases. |
| 87 const bool is_stylus = event.GetToolType(0) == MotionEvent::TOOL_TYPE_STYLUS; | 107 const bool is_stylus = event.GetToolType(0) == MotionEvent::TOOL_TYPE_STYLUS; |
| 88 const bool is_only_secondary_button_pressed = | 108 const bool is_only_secondary_button_pressed = |
| 89 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; | 109 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; |
| 90 return is_stylus && is_only_secondary_button_pressed; | 110 return is_stylus && is_only_secondary_button_pressed; |
| 91 } | 111 } |
| 92 | 112 |
| 93 } // namespace content | 113 } // namespace content |
| OLD | NEW |