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_event_data.h" |
9 #include "ui/events/gesture_detection/motion_event.h" | 9 #include "ui/events/gesture_detection/motion_event.h" |
10 | 10 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) | 13 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) |
14 : client_(client), | 14 : client_(client), |
15 text_selection_triggered_(false), | 15 text_selection_triggered_(false), |
16 spen_button_pressed_(false), | |
jdduke (slow)
2014/09/19 17:34:22
We're trying to keep this class generic, for any k
AviD
2014/09/23 11:52:08
renamed to secondary_button_pressed_
| |
16 anchor_x_(0.0f), | 17 anchor_x_(0.0f), |
17 anchor_y_(0.0f) { | 18 anchor_y_(0.0f) { |
18 } | 19 } |
19 | 20 |
20 GestureTextSelector::~GestureTextSelector() { | 21 GestureTextSelector::~GestureTextSelector() { |
21 } | 22 } |
22 | 23 |
23 bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) { | 24 bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) { |
24 if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) { | 25 if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) { |
25 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture | 26 // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture |
26 // sequences from being forwarded. | 27 // sequences from being forwarded. |
27 text_selection_triggered_ = ShouldStartTextSelection(event); | 28 text_selection_triggered_ = ShouldStartTextSelection(event); |
28 } | 29 } |
30 if (text_selection_triggered_ && | |
31 event.GetAction() == ui::MotionEvent::ACTION_MOVE) { | |
32 spen_button_pressed_ = ShouldStartTextSelection(event); | |
jdduke (slow)
2014/09/19 17:34:22
Reusing this function with its current name is con
AviD
2014/09/23 11:52:08
Changed to check only Button state.
| |
33 } | |
29 return text_selection_triggered_; | 34 return text_selection_triggered_; |
30 } | 35 } |
31 | 36 |
32 bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) { | 37 bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) { |
33 if (!text_selection_triggered_) | 38 if (!text_selection_triggered_) |
34 return false; | 39 return false; |
35 | 40 |
36 switch (gesture.type()) { | 41 switch (gesture.type()) { |
37 case ui::ET_GESTURE_TAP: { | 42 case ui::ET_GESTURE_TAP: { |
38 client_->LongPress(gesture.time, gesture.x, gesture.y); | 43 client_->LongPress(gesture.time, gesture.x, gesture.y); |
39 break; | 44 break; |
40 } | 45 } |
41 case ui::ET_GESTURE_SCROLL_BEGIN: { | 46 case ui::ET_GESTURE_SCROLL_BEGIN: { |
42 client_->Unselect(); | 47 client_->Unselect(); |
43 anchor_x_ = gesture.x; | 48 anchor_x_ = gesture.x; |
44 anchor_y_ = gesture.y; | 49 anchor_y_ = gesture.y; |
45 break; | 50 break; |
46 } | 51 } |
47 case ui::ET_GESTURE_SCROLL_UPDATE: { | 52 case ui::ET_GESTURE_SCROLL_UPDATE: { |
48 // TODO(changwan): check if we can show handles on ET_GESTURE_SCROLL_END | 53 // TODO(changwan): check if we can show handles on ET_GESTURE_SCROLL_END |
49 // instead. Currently it is not possible as ShowSelectionHandles should | 54 // instead. Currently it is not possible as ShowSelectionHandles should |
50 // be called before we change the selection. | 55 // be called before we change the selection. |
51 client_->ShowSelectionHandlesAutomatically(); | 56 if (spen_button_pressed_) { |
52 client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y); | 57 client_->ShowSelectionHandlesAutomatically(); |
58 client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y); | |
59 } | |
53 break; | 60 break; |
54 } | 61 } |
55 default: | 62 default: |
56 // Suppress all other gestures when we are selecting text. | 63 // Suppress all other gestures when we are selecting text. |
57 break; | 64 break; |
58 } | 65 } |
59 return true; | 66 return true; |
60 } | 67 } |
61 | 68 |
62 // static | 69 // static |
63 bool GestureTextSelector::ShouldStartTextSelection( | 70 bool GestureTextSelector::ShouldStartTextSelection( |
64 const ui::MotionEvent& event) { | 71 const ui::MotionEvent& event) { |
65 DCHECK_GT(event.GetPointerCount(), 0u); | 72 DCHECK_GT(event.GetPointerCount(), 0u); |
66 // Currently we are supporting stylus-only cases. | 73 // Currently we are supporting stylus-only cases. |
67 const bool is_stylus = | 74 const bool is_stylus = |
68 event.GetToolType(0) == ui::MotionEvent::TOOL_TYPE_STYLUS; | 75 event.GetToolType(0) == ui::MotionEvent::TOOL_TYPE_STYLUS; |
69 const bool is_only_secondary_button_pressed = | 76 const bool is_only_secondary_button_pressed = |
70 event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY; | 77 event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY; |
71 return is_stylus && is_only_secondary_button_pressed; | 78 return is_stylus && is_only_secondary_button_pressed; |
72 } | 79 } |
73 | 80 |
74 } // namespace content | 81 } // namespace content |
OLD | NEW |