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), |
15 text_selection_triggered_(false), | 38 text_selection_triggered_(false) { |
16 anchor_x_(0.0f), | 39 DCHECK(client); |
17 anchor_y_(0.0f) { | |
18 } | 40 } |
19 | 41 |
20 GestureTextSelector::~GestureTextSelector() { | 42 GestureTextSelector::~GestureTextSelector() { |
21 } | 43 } |
22 | 44 |
23 bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) { | 45 bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) { |
24 if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) { | 46 if (event.GetAction() == MotionEvent::ACTION_DOWN) { |
25 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture | 47 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture |
26 // sequences from being forwarded. | 48 // sequences from being forwarded. |
27 text_selection_triggered_ = ShouldStartTextSelection(event); | 49 text_selection_triggered_ = ShouldStartTextSelection(event); |
28 } | 50 } |
29 return text_selection_triggered_; | |
30 } | |
31 | 51 |
32 bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) { | |
33 if (!text_selection_triggered_) | 52 if (!text_selection_triggered_) |
34 return false; | 53 return false; |
35 | 54 |
36 switch (gesture.type()) { | 55 if (!gesture_detector_) |
37 case ui::ET_GESTURE_TAP: { | 56 gesture_detector_ = CreateGestureDetector(this); |
38 client_->LongPress(gesture.time, gesture.x, gesture.y); | 57 |
39 break; | 58 gesture_detector_->OnTouchEvent(event); |
Changwan Ryu
2014/10/02 00:39:53
Could you add a comment why we should still return
jdduke (slow)
2014/10/02 19:30:23
Good call, done.
| |
40 } | 59 return true; |
41 case ui::ET_GESTURE_SCROLL_BEGIN: { | 60 } |
42 client_->Unselect(); | 61 |
43 anchor_x_ = gesture.x; | 62 bool GestureTextSelector::OnSingleTapUp(const MotionEvent& e) { |
44 anchor_y_ = gesture.y; | 63 DCHECK(text_selection_triggered_); |
45 break; | 64 client_->LongPress(e.GetEventTime(), e.GetX(), e.GetY()); |
46 } | 65 return true; |
47 case ui::ET_GESTURE_SCROLL_UPDATE: { | 66 } |
48 // TODO(changwan): check if we can show handles on ET_GESTURE_SCROLL_END | 67 |
49 // instead. Currently it is not possible as ShowSelectionHandles should | 68 bool GestureTextSelector::OnScroll(const MotionEvent& e1, |
50 // be called before we change the selection. | 69 const MotionEvent& e2, |
51 client_->ShowSelectionHandlesAutomatically(); | 70 float distance_x, |
52 client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y); | 71 float distance_y) { |
53 break; | 72 DCHECK(text_selection_triggered_); |
54 } | 73 // TODO(changwan): check if we can show handles after the scroll finishes |
55 default: | 74 // instead. Currently it is not possible as ShowSelectionHandles should |
56 // Suppress all other gestures when we are selecting text. | 75 // be called before we change the selection. |
57 break; | 76 client_->ShowSelectionHandlesAutomatically(); |
58 } | 77 client_->SelectRange(e1.GetX(), e1.GetY(), e2.GetX(), e2.GetY()); |
59 return true; | 78 return true; |
60 } | 79 } |
61 | 80 |
62 // static | 81 // static |
63 bool GestureTextSelector::ShouldStartTextSelection( | 82 bool GestureTextSelector::ShouldStartTextSelection(const MotionEvent& event) { |
64 const ui::MotionEvent& event) { | |
65 DCHECK_GT(event.GetPointerCount(), 0u); | 83 DCHECK_GT(event.GetPointerCount(), 0u); |
66 // Currently we are supporting stylus-only cases. | 84 // Currently we are supporting stylus-only cases. |
67 const bool is_stylus = | 85 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 = | 86 const bool is_only_secondary_button_pressed = |
70 event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY; | 87 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; |
71 return is_stylus && is_only_secondary_button_pressed; | 88 return is_stylus && is_only_secondary_button_pressed; |
72 } | 89 } |
73 | 90 |
74 } // namespace content | 91 } // namespace content |
OLD | NEW |