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

Side by Side Diff: ui/views/selection_controller.h

Issue 2408623002: Views: Extract text selection code from Textfield. (Closed)
Patch Set: Created 4 years, 2 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
(Empty)
1 // Copyright 2016 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 #ifndef UI_VIEWS_SELECTION_CONTROLLER_H_
6 #define UI_VIEWS_SELECTION_CONTROLLER_H_
7
8 #include "base/time/time.h"
9 #include "base/timer/timer.h"
10 #include "ui/gfx/geometry/point.h"
11 #include "ui/gfx/range/range.h"
12 #include "ui/gfx/selection_model.h"
13 #include "ui/views/views_export.h"
14
15 namespace gfx {
16 class RenderText;
17 }
18
19 namespace ui {
20 class MouseEvent;
21 }
22
23 namespace views {
24
25 // Helper class used to facilitate mouse event handling and text selection. To
26 // use, clients must implement the Delegate interface.
27 class VIEWS_EXPORT SelectionController {
28 public:
29 class Delegate;
30
31 // |delegate| must be non-null.
32 explicit SelectionController(Delegate* delegate);
33
34 // Handle mouse events forwarded by |delegate_|. If |handled| is true, the
35 // mouse event is just used to update the internal state without making any
msw 2016/10/14 18:54:20 nit: It's not entirely obvious what this means...
karandeepb 2016/10/17 07:08:15 Yeah I meant already handled by the delegate. Have
msw 2016/10/18 23:15:05 Acknowledged.
36 // other changes.
37 bool OnMousePressed(const ui::MouseEvent& event, bool handled);
38 bool OnMouseDragged(const ui::MouseEvent& event);
39 void OnMouseReleased(const ui::MouseEvent& event);
40
41 // Returns the latest click location.
42 gfx::Point last_click_location() const { return last_click_location_; }
43
44 private:
45 // Helper methods to call the corresponding methods on the associated
46 // RenderText instance.
47 void SelectAll(bool reversed);
48 void SelectWordAt(const gfx::Point& point);
49 void SelectWord();
50 void SelectRange(const gfx::Range& range);
51 void MoveCursorTo(const gfx::Point& point, bool select);
52 void ClearSelection();
53
54 // Selects till the beginning/end of line in the given |direction|.
55 // Todo(karandeepb): Remove once multi-line text selection is supported.
msw 2016/10/14 18:54:21 nit: TODO
karandeepb 2016/10/17 07:08:15 Done.
56 void SelectTillEdge(gfx::VisualCursorDirection direction);
57
58 // Tracks the mouse clicks for single/double/triple clicks.
59 void TrackMouseClicks(const ui::MouseEvent& event);
60
61 // Returns the associated render text instance via the |delegate_|.
62 gfx::RenderText* GetRenderText();
63
64 // Helper function to update the selection on a mouse drag as per
65 // |last_drag_location_|. Can be called asynchronously, through a timer.
66 void SelectThroughLastDragLocation();
67
68 // A timer and point used to modify the selection when dragging.
69 base::RepeatingTimer drag_selection_timer_;
70 gfx::Point last_drag_location_;
71
72 // State variables used to track double and triple clicks.
73 base::TimeTicks last_click_time_;
74 gfx::Point last_click_location_;
75 size_t aggregated_clicks_;
76 gfx::Range double_click_word_;
77
78 // Weak pointer.
79 Delegate* delegate_;
80
81 DISALLOW_COPY_AND_ASSIGN(SelectionController);
82 };
83
84 // An interface implemented/managed by a view which uses the
85 // SelectionController.
86 class VIEWS_EXPORT SelectionController::Delegate {
sky 2016/10/14 22:29:48 The chromium style guide encourages files like thi
karandeepb 2016/10/17 07:08:15 Done.
87 public:
88 // Returns the associated RenderText instance to be used for selection.
89 virtual gfx::RenderText* GetRenderTextForSelection() = 0;
msw 2016/10/14 18:54:20 Is the ForSelection postfix just there to avoid a
karandeepb 2016/10/17 07:08:15 Changed to ForSelectionController. Didn't want to
90
91 // Methods related to properties of the associated view.
92
93 // Returns true if the associated text view is read only.
94 virtual bool IsReadOnly() const = 0;
95 // Returns whether there is a drag operation originating from the associated
96 // view.
97 virtual bool HasTextBeingDragged() const = 0;
98 // Sets whether text is being dragged from the associated view.
99 virtual void SetTextBeingDragged(bool value) = 0;
100 // Returns the height of the associated view.
101 virtual int GetViewHeight() const = 0;
102 // Returns the width of the associated view.
103 virtual int GetViewWidth() const = 0;
104 // Returns the drag selection timer delay. This is the duration after which a
105 // drag selection is updated when the event location is outside the text
106 // bounds.
107 virtual int GetDragSelectionDelay() const = 0;
108
109 // Methods called to notify a mouse action which may change the associated
msw 2016/10/14 18:54:20 Should these be generalized to support keyboard-ba
karandeepb 2016/10/17 07:08:15 But SelectionController is only managing selection
msw 2016/10/18 23:15:05 Yeah, but it seems reasonable to add keyboard-base
karandeepb 2016/10/20 04:12:57 Oh ok. I didn't realize that text selection could
110 // view's text and/or selection.
111
112 // Called before performing a mouse action. Should not be called in succession
113 // and must always be followed by an OnAfterMouseAction call.
114 virtual void OnBeforeMouseAction() = 0;
115 // Called after the mouse action is performed. |text_changed| and
116 // |selection_changed| can be used by subclasses to make any necessary updates
117 // like redraw the text. Must always be preceeded by a OnBeforeMouseAction
118 // call.
119 virtual void OnAfterMouseAction(bool text_changed,
120 bool selection_changed) = 0;
121
122 // Called to notify when the current selection is updated. Must always be
123 // called during a mouse action.
124
125 // Called before the selection is updated. Not called in succession and always
126 // followed by an OnAfterSelectionUpdated call.
127 virtual void OnBeforeSelectionUpdated() = 0;
128 // Called after the selection is updated. Not called in succession and always
129 // preceeded by an OnBeforeSelectionUpdated call.
130 virtual void OnAfterSelectionUpdated() = 0;
131
132 // Selection clipboard related methods. Called only when a mouse action is in
133 // progress.
134
135 // Moves the cursor to the event location and pastes the text from the
136 // selection clipboard. The view may also request focus. NO-OP if the
137 // associated text view is read-only or the event is not a middle click.
138 virtual void PasteSelectionClipboard(const ui::MouseEvent& event) = 0;
msw 2016/10/14 18:54:21 It seems odd for this to live here, on an interfac
karandeepb 2016/10/17 07:08:15 Agreed, it's a bit odd. Alternatively, we can hand
139 // Updates the selection clipboard with the currently selected text. Should
140 // empty the selection clipboard if no text is currently selected. NO-OP if
141 // the associated text view is obscured.
142 virtual void UpdateSelectionClipboard() = 0;
143
144 protected:
145 virtual ~Delegate() {}
146 };
147
148 } // namespace views
149
150 #endif // UI_VIEWS_SELECTION_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698