Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(923)

Side by Side Diff: ui/touch_selection/touch_selection_controller.h

Issue 1087893003: Support longpress drag selection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 enum ActiveStatus { 43 enum ActiveStatus {
40 INACTIVE, 44 INACTIVE,
41 INSERTION_ACTIVE, 45 INSERTION_ACTIVE,
42 SELECTION_ACTIVE, 46 SELECTION_ACTIVE,
43 }; 47 };
44 48
49 struct UI_TOUCH_SELECTION_EXPORT Config {
50 Config();
51 ~Config();
52
53 // Defaults to 100 ms.
54 base::TimeDelta tap_timeout;
55
56 // Defaults to 8 DIPs.
57 float tap_slop;
58
59 // Controls whether drag selection after a longpress is enabled.
60 // Defaults to false.
61 bool enable_longpress_drag_selection;
62
63 // Controls whether an insertion handle is shown on a tap for an empty
64 // editable text. Defauls to false.
65 bool show_on_tap_for_empty_editable;
66 };
67
45 TouchSelectionController(TouchSelectionControllerClient* client, 68 TouchSelectionController(TouchSelectionControllerClient* client,
46 base::TimeDelta tap_timeout, 69 const Config& config);
47 float tap_slop,
48 bool show_on_tap_for_empty_editable);
49 ~TouchSelectionController() override; 70 ~TouchSelectionController() override;
50 71
51 // To be called when the selection bounds have changed. 72 // To be called when the selection bounds have changed.
52 // Note that such updates will trigger handle updates only if preceded 73 // Note that such updates will trigger handle updates only if preceded
53 // by an appropriate call to allow automatic showing. 74 // by an appropriate call to allow automatic showing.
54 void OnSelectionBoundsChanged(const SelectionBound& start, 75 void OnSelectionBoundsChanged(const SelectionBound& start,
55 const SelectionBound& end); 76 const SelectionBound& end);
56 77
57 // Allows touch-dragging of the handle. 78 // Allows touch-dragging of the handle.
58 // Returns true iff the event was consumed, in which case the caller should 79 // Returns true iff the event was consumed, in which case the caller should
59 // cease further handling of the event. 80 // cease further handling of the event.
60 bool WillHandleTouchEvent(const MotionEvent& event); 81 bool WillHandleTouchEvent(const MotionEvent& event);
61 82
62 // To be called before forwarding a tap event. This allows automatically 83 // To be called before forwarding a tap event. This allows automatically
63 // showing the insertion handle from subsequent bounds changes. 84 // showing the insertion handle from subsequent bounds changes.
64 bool WillHandleTapEvent(const gfx::PointF& location); 85 bool WillHandleTapEvent(const gfx::PointF& location);
65 86
66 // To be called before forwarding a longpress event. This allows automatically 87 // To be called before forwarding a longpress event. This allows automatically
67 // showing the selection or insertion handles from subsequent bounds changes. 88 // showing the selection or insertion handles from subsequent bounds changes.
68 bool WillHandleLongPressEvent(const gfx::PointF& location); 89 bool WillHandleLongPressEvent(base::TimeTicks event_time,
90 const gfx::PointF& location);
69 91
70 // Allow showing the selection handles from the most recent selection bounds 92 // Allow showing the selection handles from the most recent selection bounds
71 // update (if valid), or a future valid bounds update. 93 // update (if valid), or a future valid bounds update.
72 void AllowShowingFromCurrentSelection(); 94 void AllowShowingFromCurrentSelection();
73 95
74 // Hide the handles and suppress bounds updates until the next explicit 96 // Hide the handles and suppress bounds updates until the next explicit
75 // showing allowance. 97 // showing allowance.
76 void HideAndDisallowShowingAutomatically(); 98 void HideAndDisallowShowingAutomatically();
77 99
78 // Override the handle visibility according to |hidden|. 100 // Override the handle visibility according to |hidden|.
(...skipping 23 matching lines...) Expand all
102 // their bottom coordinate. 124 // their bottom coordinate.
103 const gfx::PointF& GetStartPosition() const; 125 const gfx::PointF& GetStartPosition() const;
104 const gfx::PointF& GetEndPosition() const; 126 const gfx::PointF& GetEndPosition() const;
105 127
106 const SelectionBound& start() const { return start_; } 128 const SelectionBound& start() const { return start_; }
107 const SelectionBound& end() const { return end_; } 129 const SelectionBound& end() const { return end_; }
108 130
109 ActiveStatus active_status() const { return active_status_; } 131 ActiveStatus active_status() const { return active_status_; }
110 132
111 private: 133 private:
134 friend class TouchSelectionControllerTestApi;
135
112 enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE }; 136 enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
113 137
114 // TouchHandleClient implementation. 138 // TouchHandleClient implementation.
115 void OnHandleDragBegin(const TouchHandle& handle) override; 139 void OnDragBegin(const TouchSelectionDraggable& draggable,
116 void OnHandleDragUpdate(const TouchHandle& handle, 140 const gfx::PointF& drag_position) override;
117 const gfx::PointF& new_position) override; 141 void OnDragUpdate(const TouchSelectionDraggable& draggable,
118 void OnHandleDragEnd(const TouchHandle& handle) override; 142 const gfx::PointF& drag_position) override;
143 void OnDragEnd(const TouchSelectionDraggable& draggable) override;
144 bool IsWithinTapSlop(const gfx::Vector2dF& delta) const override;
119 void OnHandleTapped(const TouchHandle& handle) override; 145 void OnHandleTapped(const TouchHandle& handle) override;
120 void SetNeedsAnimate() override; 146 void SetNeedsAnimate() override;
121 scoped_ptr<TouchHandleDrawable> CreateDrawable() override; 147 scoped_ptr<TouchHandleDrawable> CreateDrawable() override;
122 base::TimeDelta GetTapTimeout() const override; 148 base::TimeDelta GetTapTimeout() const override;
123 float GetTapSlop() const override; 149
150 // LongPressDragSelectorClient implementation.
151 void OnLongPressDragActiveStateChanged() override;
152 gfx::PointF GetSelectionStart() const override;
153 gfx::PointF GetSelectionEnd() const override;
124 154
125 void ShowInsertionHandleAutomatically(); 155 void ShowInsertionHandleAutomatically();
126 void ShowSelectionHandlesAutomatically(); 156 void ShowSelectionHandlesAutomatically();
127 bool WillHandleTapOrLongPress(const gfx::PointF& location); 157 bool WillHandleTapOrLongPress(const gfx::PointF& location);
128 158
129 void OnInsertionChanged(); 159 void OnInsertionChanged();
130 void OnSelectionChanged(); 160 void OnSelectionChanged();
131 161
132 // Returns true if insertion mode was newly (re)activated. 162 // Returns true if insertion mode was newly (re)activated.
133 bool ActivateInsertionIfNecessary(); 163 bool ActivateInsertionIfNecessary();
134 void DeactivateInsertion(); 164 void DeactivateInsertion();
135 // Returns true if selection mode was newly (re)activated. 165 // Returns true if selection mode was newly (re)activated.
136 bool ActivateSelectionIfNecessary(); 166 bool ActivateSelectionIfNecessary();
137 void DeactivateSelection(); 167 void DeactivateSelection();
138 void ForceNextUpdateIfInactive(); 168 void ForceNextUpdateIfInactive();
139 169
170 bool WillHandleTouchEventForLongPressDrag(const MotionEvent& event);
171 void SetTemporarilyHiddenForLongPressDrag(bool hidden);
172 void RefreshHandleVisibility();
173
140 gfx::Vector2dF GetStartLineOffset() const; 174 gfx::Vector2dF GetStartLineOffset() const;
141 gfx::Vector2dF GetEndLineOffset() const; 175 gfx::Vector2dF GetEndLineOffset() const;
142 bool GetStartVisible() const; 176 bool GetStartVisible() const;
143 bool GetEndVisible() const; 177 bool GetEndVisible() const;
144 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const; 178 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const;
145 179
146 void LogSelectionEnd(); 180 void LogSelectionEnd();
147 181
148 TouchSelectionControllerClient* const client_; 182 TouchSelectionControllerClient* const client_;
149 const base::TimeDelta tap_timeout_; 183 const Config config_;
150 const float tap_slop_;
151 184
152 // Whether to force an update on the next selection event even if the 185 // Whether to force an update on the next selection event even if the
153 // cached selection matches the new selection. 186 // cached selection matches the new selection.
154 bool force_next_update_; 187 bool force_next_update_;
155 188
156 // Controls whether an insertion handle is shown on a tap for an empty
157 // editable text.
158 bool show_on_tap_for_empty_editable_;
159
160 InputEventType response_pending_input_event_; 189 InputEventType response_pending_input_event_;
161 190
162 SelectionBound start_; 191 SelectionBound start_;
163 SelectionBound end_; 192 SelectionBound end_;
164 TouchHandleOrientation start_orientation_; 193 TouchHandleOrientation start_orientation_;
165 TouchHandleOrientation end_orientation_; 194 TouchHandleOrientation end_orientation_;
166 195
167 ActiveStatus active_status_; 196 ActiveStatus active_status_;
168 197
169 scoped_ptr<TouchHandle> insertion_handle_; 198 scoped_ptr<TouchHandle> insertion_handle_;
170 bool activate_insertion_automatically_; 199 bool activate_insertion_automatically_;
171 200
172 scoped_ptr<TouchHandle> start_selection_handle_; 201 scoped_ptr<TouchHandle> start_selection_handle_;
173 scoped_ptr<TouchHandle> end_selection_handle_; 202 scoped_ptr<TouchHandle> end_selection_handle_;
174 bool activate_selection_automatically_; 203 bool activate_selection_automatically_;
175 204
176 bool selection_empty_; 205 bool selection_empty_;
177 bool selection_editable_; 206 bool selection_editable_;
178 207
179 bool temporarily_hidden_; 208 bool temporarily_hidden_;
180 209
210 // Whether to use the start bound (if false, the end bound) for computing the
211 // appropriate text line offset when performing a selection drag. This helps
212 // ensure that the initial selection induced by the drag doesn't "jump"
213 // between lines.
214 bool anchor_drag_to_selection_start_;
215
216 // Longpress drag allows direct manipulation of longpress-initiated selection.
217 LongPressDragSelector longpress_drag_selector_;
218
181 base::TimeTicks selection_start_time_; 219 base::TimeTicks selection_start_time_;
182 // Whether a selection handle was dragged during the current 'selection 220 // Whether a selection handle was dragged during the current 'selection
183 // session' - i.e. since the current selection has been activated. 221 // session' - i.e. since the current selection has been activated.
184 bool selection_handle_dragged_; 222 bool selection_handle_dragged_;
185 223
186 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController); 224 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController);
187 }; 225 };
188 226
189 } // namespace ui 227 } // namespace ui
190 228
191 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ 229 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_
OLDNEW
« no previous file with comments | « ui/touch_selection/touch_handle_unittest.cc ('k') | ui/touch_selection/touch_selection_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698