Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/input/gesture_text_selector.h" | |
| 6 | |
| 7 #include "ui/events/event_constants.h" | |
| 8 #include "ui/events/gesture_detection/gesture_event_data.h" | |
| 9 #include "ui/events/gesture_detection/motion_event.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) | |
| 14 : client_(client), | |
| 15 text_selection_triggered_(false), | |
| 16 anchor_x_(0.0f), | |
| 17 anchor_y_(0.0f) { | |
| 18 } | |
| 19 | |
| 20 GestureTextSelector::~GestureTextSelector() { | |
| 21 } | |
| 22 | |
| 23 bool GestureTextSelector::OnTouchEvent(const ui::MotionEvent& event) { | |
| 24 if (event.GetAction() == ui::MotionEvent::ACTION_DOWN) | |
| 25 text_selection_triggered_ = ShouldStartTextSelection(event); | |
| 26 | |
| 27 if (text_selection_triggered_) { | |
|
jdduke (slow)
2014/06/24 15:38:36
Nit: To avoid nesting invert the condition and ear
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 28 client_->ShowSelectionHandlesAutomatically(); | |
|
jdduke (slow)
2014/06/24 15:38:36
We should probably only call this upon ACTION_DOWN
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 29 switch (event.GetAction()) { | |
| 30 case ui::MotionEvent::ACTION_DOWN: | |
| 31 client_->Unselect(); | |
| 32 anchor_x_ = event.GetX(); | |
| 33 anchor_y_ = event.GetY(); | |
| 34 break; | |
| 35 case ui::MotionEvent::ACTION_MOVE: | |
| 36 case ui::MotionEvent::ACTION_UP: | |
| 37 if (text_selection_triggered_) | |
| 38 client_->SelectRange(anchor_x_, anchor_y_, | |
|
jdduke (slow)
2014/06/24 15:38:36
Hmm, we shouldn't use SelectRange with both ACTION
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 39 event.GetX(), event.GetY()); | |
| 40 break; | |
| 41 case ui::MotionEvent::ACTION_CANCEL: | |
| 42 text_selection_triggered_ = false; | |
|
jdduke (slow)
2014/06/24 15:38:37
ACTION_CANCEL shouldn't reset the state, as the ca
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 43 client_->Unselect(); | |
| 44 break; | |
| 45 default: | |
| 46 break; | |
| 47 } | |
| 48 return true; | |
| 49 } | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 bool GestureTextSelector::OnGestureEvent(const ui::GestureEventData& gesture) { | |
| 54 if (text_selection_triggered_) { | |
|
jdduke (slow)
2014/06/24 15:38:37
Nit:
if (!text_selection_triggered_)
return fal
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 55 switch (gesture.type()) { | |
| 56 case ui::ET_GESTURE_LONG_PRESS: { | |
| 57 client_->SelectWord(gesture.x, gesture.y); | |
| 58 // Exits the text selection mode until ACTION_DOWN resets it. | |
| 59 // Long press is likely to be followed by ACTION_MOVE, and we don't want | |
| 60 // the selected text to be unselected or changed right afterwards. | |
| 61 text_selection_triggered_ = false; | |
|
jdduke (slow)
2014/06/24 15:38:37
The problem with assigning this to false is that t
Changwan Ryu
2014/06/25 07:26:20
Added drop_remaining_sequence_
| |
| 62 break; | |
| 63 } | |
| 64 case ui::ET_GESTURE_SCROLL_BEGIN: { | |
| 65 anchor_x_ = gesture.x; | |
| 66 anchor_y_ = gesture.y; | |
| 67 break; | |
| 68 } | |
| 69 case ui::ET_GESTURE_SCROLL_UPDATE: | |
| 70 case ui::ET_GESTURE_SCROLL_END: { | |
|
jdduke (slow)
2014/06/24 15:38:36
No need to listen to SCROLL_END, SCROLL_UPDATE sho
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 71 client_->SelectRange(anchor_x_, anchor_y_, gesture.x, gesture.y); | |
| 72 break; | |
| 73 } | |
| 74 default: | |
| 75 // Suppress all other gestures when we are selecting text. | |
| 76 break; | |
| 77 } | |
| 78 return true; | |
| 79 } | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 bool GestureTextSelector::ShouldStartTextSelection( | |
| 85 const ui::MotionEvent& event) { | |
| 86 DCHECK(event.GetPointerCount() > 0); | |
|
jdduke (slow)
2014/06/24 15:38:37
Nit: DCHECK_GT(event.GetPointerCount(), 0);
Changwan Ryu
2014/06/25 07:26:20
Done.
| |
| 87 // Currently we are supporting stylus-only cases. | |
| 88 const bool is_stylus = | |
| 89 event.GetToolType(0) == ui::MotionEvent::TOOL_TYPE_STYLUS; | |
| 90 const bool is_only_secondary_button_pressed = | |
| 91 event.GetButtonState() == ui::MotionEvent::BUTTON_SECONDARY; | |
| 92 return is_stylus && is_only_secondary_button_pressed; | |
| 93 } | |
| 94 | |
| 95 } // namespace content | |
| OLD | NEW |