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/stylus_text_selector.h" | 5 #include "content/browser/renderer_host/input/stylus_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_detector.h" | 8 #include "ui/events/gesture_detection/gesture_detector.h" |
9 #include "ui/events/gesture_detection/gesture_provider_config_helper.h" | 9 #include "ui/events/gesture_detection/gesture_provider_config_helper.h" |
10 #include "ui/events/gesture_detection/motion_event.h" | 10 #include "ui/events/gesture_detection/motion_event.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
32 | 32 |
33 return detector; | 33 return detector; |
34 } | 34 } |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 StylusTextSelector::StylusTextSelector(StylusTextSelectorClient* client) | 38 StylusTextSelector::StylusTextSelector(StylusTextSelectorClient* client) |
39 : client_(client), | 39 : client_(client), |
40 text_selection_triggered_(false), | 40 text_selection_triggered_(false), |
41 secondary_button_pressed_(false), | 41 secondary_button_pressed_(false), |
42 dragging_(false), | 42 drag_state_(NO_DRAG), |
43 dragged_(false), | |
44 anchor_x_(0.0f), | 43 anchor_x_(0.0f), |
45 anchor_y_(0.0f) { | 44 anchor_y_(0.0f) { |
46 DCHECK(client); | 45 DCHECK(client); |
47 } | 46 } |
48 | 47 |
49 StylusTextSelector::~StylusTextSelector() { | 48 StylusTextSelector::~StylusTextSelector() { |
50 } | 49 } |
51 | 50 |
52 bool StylusTextSelector::OnTouchEvent(const MotionEvent& event) { | 51 bool StylusTextSelector::OnTouchEvent(const MotionEvent& event) { |
53 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture | 52 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture |
54 // sequences from being forwarded. | 53 // sequences from being forwarded. |
55 if (event.GetAction() == MotionEvent::ACTION_DOWN) | 54 if (event.GetAction() == MotionEvent::ACTION_DOWN) |
56 text_selection_triggered_ = ShouldStartTextSelection(event); | 55 text_selection_triggered_ = ShouldStartTextSelection(event); |
57 | 56 |
58 if (!text_selection_triggered_) | 57 if (!text_selection_triggered_) |
59 return false; | 58 return false; |
60 | 59 |
61 secondary_button_pressed_ = | 60 secondary_button_pressed_ = |
62 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; | 61 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; |
63 | 62 |
64 switch (event.GetAction()) { | 63 switch (event.GetAction()) { |
65 case MotionEvent::ACTION_DOWN: | 64 case MotionEvent::ACTION_DOWN: |
66 dragging_ = false; | 65 drag_state_ = NO_DRAG; |
67 dragged_ = false; | |
68 anchor_x_ = event.GetX(); | 66 anchor_x_ = event.GetX(); |
69 anchor_y_ = event.GetY(); | 67 anchor_y_ = event.GetY(); |
70 break; | 68 break; |
71 | 69 |
72 case MotionEvent::ACTION_MOVE: | 70 case MotionEvent::ACTION_MOVE: |
73 if (!secondary_button_pressed_) { | 71 if (!secondary_button_pressed_) { |
74 dragging_ = false; | 72 drag_state_ = |
aelias_OOO_until_Jul13
2017/05/17 20:54:23
Clearer to write this as:
if (drag_state_ == DRAG
AKVT
2017/05/18 10:12:19
Done.
| |
73 (drag_state_ == DRAGGING_IN_PROGRESS || drag_state_ == WAS_DRAGGED) | |
74 ? WAS_DRAGGED | |
75 : NO_DRAG; | |
75 anchor_x_ = event.GetX(); | 76 anchor_x_ = event.GetX(); |
76 anchor_y_ = event.GetY(); | 77 anchor_y_ = event.GetY(); |
77 } | 78 } |
78 break; | 79 break; |
79 | 80 |
80 case MotionEvent::ACTION_UP: | 81 case MotionEvent::ACTION_UP: |
81 case MotionEvent::ACTION_CANCEL: | 82 case MotionEvent::ACTION_CANCEL: |
82 if (dragged_) | 83 if (drag_state_ == DRAGGING_IN_PROGRESS || drag_state_ == WAS_DRAGGED) |
83 client_->OnStylusSelectEnd(); | 84 client_->OnStylusSelectEnd(); |
84 dragged_ = false; | 85 drag_state_ = NO_DRAG; |
85 dragging_ = false; | |
86 break; | 86 break; |
87 | 87 |
88 case MotionEvent::ACTION_POINTER_UP: | 88 case MotionEvent::ACTION_POINTER_UP: |
89 case MotionEvent::ACTION_POINTER_DOWN: | 89 case MotionEvent::ACTION_POINTER_DOWN: |
90 break; | 90 break; |
91 case MotionEvent::ACTION_NONE: | 91 case MotionEvent::ACTION_NONE: |
92 case MotionEvent::ACTION_HOVER_ENTER: | 92 case MotionEvent::ACTION_HOVER_ENTER: |
93 case MotionEvent::ACTION_HOVER_EXIT: | 93 case MotionEvent::ACTION_HOVER_EXIT: |
94 case MotionEvent::ACTION_HOVER_MOVE: | 94 case MotionEvent::ACTION_HOVER_MOVE: |
95 case MotionEvent::ACTION_BUTTON_PRESS: | 95 case MotionEvent::ACTION_BUTTON_PRESS: |
96 case MotionEvent::ACTION_BUTTON_RELEASE: | 96 case MotionEvent::ACTION_BUTTON_RELEASE: |
97 NOTREACHED(); | 97 NOTREACHED(); |
98 break; | 98 break; |
99 } | 99 } |
100 | 100 |
101 if (!gesture_detector_) | 101 if (!gesture_detector_) |
102 gesture_detector_ = CreateGestureDetector(this); | 102 gesture_detector_ = CreateGestureDetector(this); |
103 | 103 |
104 gesture_detector_->OnTouchEvent(event); | 104 gesture_detector_->OnTouchEvent(event); |
105 | 105 |
106 // Always return true, even if |gesture_detector_| technically doesn't | 106 // Always return true, even if |gesture_detector_| technically doesn't |
107 // consume the event. This prevents forwarding of a partial touch stream. | 107 // consume the event. This prevents forwarding of a partial touch stream. |
108 return true; | 108 return true; |
109 } | 109 } |
110 | 110 |
111 bool StylusTextSelector::OnSingleTapUp(const MotionEvent& e, int tap_count) { | 111 bool StylusTextSelector::OnSingleTapUp(const MotionEvent& e, int tap_count) { |
112 DCHECK(text_selection_triggered_); | 112 DCHECK(text_selection_triggered_); |
113 DCHECK(!dragging_); | 113 DCHECK(drag_state_ == NO_DRAG); |
aelias_OOO_until_Jul13
2017/05/17 20:54:23
This isn't equivalent to the previous behavior, I
AKVT
2017/05/18 10:12:19
Done.
| |
114 client_->OnStylusSelectTap(e.GetEventTime(), e.GetX(), e.GetY()); | 114 client_->OnStylusSelectTap(e.GetEventTime(), e.GetX(), e.GetY()); |
115 return true; | 115 return true; |
116 } | 116 } |
117 | 117 |
118 bool StylusTextSelector::OnScroll(const MotionEvent& e1, | 118 bool StylusTextSelector::OnScroll(const MotionEvent& e1, |
119 const MotionEvent& e2, | 119 const MotionEvent& e2, |
120 const MotionEvent& secondary_pointer_down, | 120 const MotionEvent& secondary_pointer_down, |
121 float distance_x, | 121 float distance_x, |
122 float distance_y) { | 122 float distance_y) { |
123 DCHECK(text_selection_triggered_); | 123 DCHECK(text_selection_triggered_); |
124 | 124 |
125 // Return if Stylus button is not pressed. | 125 // Return if Stylus button is not pressed. |
126 if (!secondary_button_pressed_) | 126 if (!secondary_button_pressed_) |
127 return true; | 127 return true; |
128 | 128 |
129 if (!dragging_) { | 129 if (drag_state_ == NO_DRAG || drag_state_ == WAS_DRAGGED) { |
130 dragging_ = true; | 130 drag_state_ = DRAGGING_IN_PROGRESS; |
131 dragged_ = true; | |
132 client_->OnStylusSelectBegin(anchor_x_, anchor_y_, e2.GetX(), e2.GetY()); | 131 client_->OnStylusSelectBegin(anchor_x_, anchor_y_, e2.GetX(), e2.GetY()); |
133 } else { | 132 } else { |
134 client_->OnStylusSelectUpdate(e2.GetX(), e2.GetY()); | 133 client_->OnStylusSelectUpdate(e2.GetX(), e2.GetY()); |
135 } | 134 } |
136 | 135 |
137 return true; | 136 return true; |
138 } | 137 } |
139 | 138 |
140 // static | 139 // static |
141 bool StylusTextSelector::ShouldStartTextSelection(const MotionEvent& event) { | 140 bool StylusTextSelector::ShouldStartTextSelection(const MotionEvent& event) { |
142 DCHECK_GT(event.GetPointerCount(), 0u); | 141 DCHECK_GT(event.GetPointerCount(), 0u); |
143 // Currently we are supporting stylus-only cases. | 142 // Currently we are supporting stylus-only cases. |
144 const bool is_stylus = event.GetToolType(0) == MotionEvent::TOOL_TYPE_STYLUS; | 143 const bool is_stylus = event.GetToolType(0) == MotionEvent::TOOL_TYPE_STYLUS; |
145 const bool is_only_secondary_button_pressed = | 144 const bool is_only_secondary_button_pressed = |
146 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; | 145 event.GetButtonState() == MotionEvent::BUTTON_SECONDARY; |
147 return is_stylus && is_only_secondary_button_pressed; | 146 return is_stylus && is_only_secondary_button_pressed; |
148 } | 147 } |
149 | 148 |
150 } // namespace content | 149 } // namespace content |
OLD | NEW |