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 #ifndef UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ | 5 #ifndef UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ |
6 #define UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ | 6 #define UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ |
7 | 7 |
| 8 #include "base/time/time.h" |
8 #include "ui/base/touch/selection_bound.h" | 9 #include "ui/base/touch/selection_bound.h" |
9 #include "ui/gfx/geometry/point_f.h" | 10 #include "ui/gfx/geometry/point_f.h" |
10 #include "ui/gfx/geometry/rect_f.h" | 11 #include "ui/gfx/geometry/rect_f.h" |
| 12 #include "ui/gfx/geometry/vector2d_f.h" |
| 13 #include "ui/touch_selection/longpress_drag_selector.h" |
11 #include "ui/touch_selection/selection_event_type.h" | 14 #include "ui/touch_selection/selection_event_type.h" |
12 #include "ui/touch_selection/touch_handle.h" | 15 #include "ui/touch_selection/touch_handle.h" |
13 #include "ui/touch_selection/touch_handle_orientation.h" | 16 #include "ui/touch_selection/touch_handle_orientation.h" |
14 #include "ui/touch_selection/ui_touch_selection_export.h" | 17 #include "ui/touch_selection/ui_touch_selection_export.h" |
15 | 18 |
16 namespace ui { | 19 namespace ui { |
17 class MotionEvent; | 20 class MotionEvent; |
18 | 21 |
19 // Interface through which |TouchSelectionController| issues selection-related | 22 // Interface through which |TouchSelectionController| issues selection-related |
20 // commands, notifications and requests. | 23 // commands, notifications and requests. |
21 class UI_TOUCH_SELECTION_EXPORT TouchSelectionControllerClient { | 24 class UI_TOUCH_SELECTION_EXPORT TouchSelectionControllerClient { |
22 public: | 25 public: |
23 virtual ~TouchSelectionControllerClient() {} | 26 virtual ~TouchSelectionControllerClient() {} |
24 | 27 |
25 virtual bool SupportsAnimation() const = 0; | 28 virtual bool SupportsAnimation() const = 0; |
26 virtual void SetNeedsAnimate() = 0; | 29 virtual void SetNeedsAnimate() = 0; |
27 virtual void MoveCaret(const gfx::PointF& position) = 0; | 30 virtual void MoveCaret(const gfx::PointF& position) = 0; |
28 virtual void MoveRangeSelectionExtent(const gfx::PointF& extent) = 0; | 31 virtual void MoveRangeSelectionExtent(const gfx::PointF& extent) = 0; |
29 virtual void SelectBetweenCoordinates(const gfx::PointF& base, | 32 virtual void SelectBetweenCoordinates(const gfx::PointF& base, |
30 const gfx::PointF& extent) = 0; | 33 const gfx::PointF& extent) = 0; |
31 virtual void OnSelectionEvent(SelectionEventType event) = 0; | 34 virtual void OnSelectionEvent(SelectionEventType event) = 0; |
32 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0; | 35 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0; |
33 }; | 36 }; |
34 | 37 |
35 // Controller for manipulating text selection via touch input. | 38 // Controller for manipulating text selection via touch input. |
36 class UI_TOUCH_SELECTION_EXPORT TouchSelectionController | 39 class UI_TOUCH_SELECTION_EXPORT TouchSelectionController |
37 : public TouchHandleClient { | 40 : public TouchHandleClient, |
| 41 public LongPressDragSelectorClient { |
38 public: | 42 public: |
39 TouchSelectionController(TouchSelectionControllerClient* client, | 43 TouchSelectionController(TouchSelectionControllerClient* client, |
40 base::TimeDelta tap_timeout, | 44 base::TimeDelta tap_timeout, |
41 float tap_slop, | 45 float tap_slop, |
42 bool show_on_tap_for_empty_editable); | 46 bool show_on_tap_for_empty_editable); |
43 ~TouchSelectionController() override; | 47 ~TouchSelectionController() override; |
44 | 48 |
45 // To be called when the selection bounds have changed. | 49 // To be called when the selection bounds have changed. |
46 // Note that such updates will trigger handle updates only if preceded | 50 // Note that such updates will trigger handle updates only if preceded |
47 // by an appropriate call to allow automatic showing. | 51 // by an appropriate call to allow automatic showing. |
48 void OnSelectionBoundsChanged(const SelectionBound& start, | 52 void OnSelectionBoundsChanged(const SelectionBound& start, |
49 const SelectionBound& end); | 53 const SelectionBound& end); |
50 | 54 |
51 // Allows touch-dragging of the handle. | 55 // Allows touch-dragging of the handle. |
52 // Returns true iff the event was consumed, in which case the caller should | 56 // Returns true iff the event was consumed, in which case the caller should |
53 // cease further handling of the event. | 57 // cease further handling of the event. |
54 bool WillHandleTouchEvent(const MotionEvent& event); | 58 bool WillHandleTouchEvent(const MotionEvent& event); |
55 | 59 |
56 // To be called before forwarding a tap event. This allows automatically | 60 // To be called before forwarding a tap event. This allows automatically |
57 // showing the insertion handle from subsequent bounds changes. | 61 // showing the insertion handle from subsequent bounds changes. |
58 void OnTapEvent(); | 62 void OnTapEvent(); |
59 | 63 |
60 // To be called before forwarding a longpress event. This allows automatically | 64 // To be called before forwarding a longpress event. This allows automatically |
61 // showing the selection or insertion handles from subsequent bounds changes. | 65 // showing the selection or insertion handles from subsequent bounds changes. |
62 void OnLongPressEvent(); | 66 void OnLongPressEvent(base::TimeTicks event_time, |
| 67 const gfx::PointF& location); |
63 | 68 |
64 // Allow showing the selection handles from the most recent selection bounds | 69 // Allow showing the selection handles from the most recent selection bounds |
65 // update (if valid), or a future valid bounds update. | 70 // update (if valid), or a future valid bounds update. |
66 void AllowShowingFromCurrentSelection(); | 71 void AllowShowingFromCurrentSelection(); |
67 | 72 |
68 // Hide the handles and suppress bounds updates until the next explicit | 73 // Hide the handles and suppress bounds updates until the next explicit |
69 // showing allowance. | 74 // showing allowance. |
70 void HideAndDisallowShowingAutomatically(); | 75 void HideAndDisallowShowingAutomatically(); |
71 | 76 |
72 // Override the handle visibility according to |hidden|. | 77 // Override the handle visibility according to |hidden|. |
(...skipping 21 matching lines...) Expand all Loading... |
94 | 99 |
95 // Returns the focal point of the start and end bounds, as defined by | 100 // Returns the focal point of the start and end bounds, as defined by |
96 // their bottom coordinate. | 101 // their bottom coordinate. |
97 const gfx::PointF& GetStartPosition() const; | 102 const gfx::PointF& GetStartPosition() const; |
98 const gfx::PointF& GetEndPosition() const; | 103 const gfx::PointF& GetEndPosition() const; |
99 | 104 |
100 private: | 105 private: |
101 enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE }; | 106 enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE }; |
102 | 107 |
103 // TouchHandleClient implementation. | 108 // TouchHandleClient implementation. |
104 void OnHandleDragBegin(const TouchHandle& handle) override; | 109 void OnDragBegin(const TouchSelectionDraggable& draggable, |
105 void OnHandleDragUpdate(const TouchHandle& handle, | 110 const gfx::PointF& drag_position) override; |
106 const gfx::PointF& new_position) override; | 111 void OnDragUpdate(const TouchSelectionDraggable& draggable, |
107 void OnHandleDragEnd(const TouchHandle& handle) override; | 112 const gfx::PointF& drag_position) override; |
| 113 void OnDragEnd(const TouchSelectionDraggable& draggable) override; |
| 114 bool IsWithinTapSlop(const gfx::Vector2dF& delta) const override; |
108 void OnHandleTapped(const TouchHandle& handle) override; | 115 void OnHandleTapped(const TouchHandle& handle) override; |
109 void SetNeedsAnimate() override; | 116 void SetNeedsAnimate() override; |
110 scoped_ptr<TouchHandleDrawable> CreateDrawable() override; | 117 scoped_ptr<TouchHandleDrawable> CreateDrawable() override; |
111 base::TimeDelta GetTapTimeout() const override; | 118 base::TimeDelta GetTapTimeout() const override; |
112 float GetTapSlop() const override; | 119 |
| 120 // LongPressDragSelectorClient implementation. |
| 121 void OnLongPressDragDetectionStateChanged() override; |
| 122 gfx::PointF GetSelectionStart() const override; |
| 123 gfx::PointF GetSelectionEnd() const override; |
113 | 124 |
114 void ShowInsertionHandleAutomatically(); | 125 void ShowInsertionHandleAutomatically(); |
115 void ShowSelectionHandlesAutomatically(); | 126 void ShowSelectionHandlesAutomatically(); |
116 | 127 |
117 void OnInsertionChanged(); | 128 void OnInsertionChanged(); |
118 void OnSelectionChanged(); | 129 void OnSelectionChanged(); |
119 | 130 |
120 void ActivateInsertion(); | 131 void ActivateInsertion(); |
121 void DeactivateInsertion(); | 132 void DeactivateInsertion(); |
122 void ActivateSelection(); | 133 void ActivateSelection(); |
123 void DeactivateSelection(); | 134 void DeactivateSelection(); |
124 void ResetCachedValuesIfInactive(); | 135 void ResetCachedValuesIfInactive(); |
125 | 136 |
| 137 bool WillHandleTouchEventForLongPressDrag(const MotionEvent& event); |
| 138 void SetTemporarilyHiddenForLongPressDrag(bool hidden); |
| 139 void RefreshHandleVisibility(); |
| 140 |
126 gfx::Vector2dF GetStartLineOffset() const; | 141 gfx::Vector2dF GetStartLineOffset() const; |
127 gfx::Vector2dF GetEndLineOffset() const; | 142 gfx::Vector2dF GetEndLineOffset() const; |
128 bool GetStartVisible() const; | 143 bool GetStartVisible() const; |
129 bool GetEndVisible() const; | 144 bool GetEndVisible() const; |
130 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const; | 145 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const; |
131 | 146 |
132 void LogSelectionEnd(); | 147 void LogSelectionEnd(); |
133 | 148 |
134 TouchSelectionControllerClient* const client_; | 149 TouchSelectionControllerClient* const client_; |
135 const base::TimeDelta tap_timeout_; | 150 const base::TimeDelta tap_timeout_; |
136 const float tap_slop_; | 151 const double tap_slop_squared_; |
137 | 152 |
138 // Controls whether an insertion handle is shown on a tap for an empty | 153 // Controls whether an insertion handle is shown on a tap for an empty |
139 // editable text. | 154 // editable text. |
140 bool show_on_tap_for_empty_editable_; | 155 bool show_on_tap_for_empty_editable_; |
141 | 156 |
142 InputEventType response_pending_input_event_; | 157 InputEventType response_pending_input_event_; |
143 | 158 |
144 SelectionBound start_; | 159 SelectionBound start_; |
145 SelectionBound end_; | 160 SelectionBound end_; |
146 TouchHandleOrientation start_orientation_; | 161 TouchHandleOrientation start_orientation_; |
147 TouchHandleOrientation end_orientation_; | 162 TouchHandleOrientation end_orientation_; |
148 | 163 |
149 scoped_ptr<TouchHandle> insertion_handle_; | 164 scoped_ptr<TouchHandle> insertion_handle_; |
150 bool is_insertion_active_; | 165 bool is_insertion_active_; |
151 bool activate_insertion_automatically_; | 166 bool activate_insertion_automatically_; |
152 | 167 |
153 scoped_ptr<TouchHandle> start_selection_handle_; | 168 scoped_ptr<TouchHandle> start_selection_handle_; |
154 scoped_ptr<TouchHandle> end_selection_handle_; | 169 scoped_ptr<TouchHandle> end_selection_handle_; |
155 bool is_selection_active_; | 170 bool is_selection_active_; |
156 bool activate_selection_automatically_; | 171 bool activate_selection_automatically_; |
157 | 172 |
158 bool selection_empty_; | 173 bool selection_empty_; |
159 bool selection_editable_; | 174 bool selection_editable_; |
160 | 175 |
161 bool temporarily_hidden_; | 176 bool temporarily_hidden_; |
162 | 177 |
| 178 // Offset from the start of a drag sequence to a reasonable position on the |
| 179 // corresponding line of text. This helps ensure that the initial selection |
| 180 // induced by the drag doesn't "jump" between lines. |
| 181 gfx::Vector2dF drag_line_offset_; |
| 182 |
| 183 // Longpress drag allows direct manipulation of longpress-initiated selection. |
| 184 LongPressDragSelector longpress_drag_selector_; |
| 185 |
163 base::TimeTicks selection_start_time_; | 186 base::TimeTicks selection_start_time_; |
164 // Whether a selection handle was dragged during the current 'selection | 187 // Whether a selection handle was dragged during the current 'selection |
165 // session' - i.e. since the current selection has been activated. | 188 // session' - i.e. since the current selection has been activated. |
166 bool selection_handle_dragged_; | 189 bool selection_handle_dragged_; |
167 | 190 |
168 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController); | 191 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController); |
169 }; | 192 }; |
170 | 193 |
171 } // namespace ui | 194 } // namespace ui |
172 | 195 |
173 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ | 196 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ |
OLD | NEW |