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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/selection_controller.h
diff --git a/ui/views/selection_controller.h b/ui/views/selection_controller.h
new file mode 100644
index 0000000000000000000000000000000000000000..fe164338180ab747c1ded9e5be00ba3000b3ebf2
--- /dev/null
+++ b/ui/views/selection_controller.h
@@ -0,0 +1,150 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_VIEWS_SELECTION_CONTROLLER_H_
+#define UI_VIEWS_SELECTION_CONTROLLER_H_
+
+#include "base/time/time.h"
+#include "base/timer/timer.h"
+#include "ui/gfx/geometry/point.h"
+#include "ui/gfx/range/range.h"
+#include "ui/gfx/selection_model.h"
+#include "ui/views/views_export.h"
+
+namespace gfx {
+class RenderText;
+}
+
+namespace ui {
+class MouseEvent;
+}
+
+namespace views {
+
+// Helper class used to facilitate mouse event handling and text selection. To
+// use, clients must implement the Delegate interface.
+class VIEWS_EXPORT SelectionController {
+ public:
+ class Delegate;
+
+ // |delegate| must be non-null.
+ explicit SelectionController(Delegate* delegate);
+
+ // Handle mouse events forwarded by |delegate_|. If |handled| is true, the
+ // 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.
+ // other changes.
+ bool OnMousePressed(const ui::MouseEvent& event, bool handled);
+ bool OnMouseDragged(const ui::MouseEvent& event);
+ void OnMouseReleased(const ui::MouseEvent& event);
+
+ // Returns the latest click location.
+ gfx::Point last_click_location() const { return last_click_location_; }
+
+ private:
+ // Helper methods to call the corresponding methods on the associated
+ // RenderText instance.
+ void SelectAll(bool reversed);
+ void SelectWordAt(const gfx::Point& point);
+ void SelectWord();
+ void SelectRange(const gfx::Range& range);
+ void MoveCursorTo(const gfx::Point& point, bool select);
+ void ClearSelection();
+
+ // Selects till the beginning/end of line in the given |direction|.
+ // 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.
+ void SelectTillEdge(gfx::VisualCursorDirection direction);
+
+ // Tracks the mouse clicks for single/double/triple clicks.
+ void TrackMouseClicks(const ui::MouseEvent& event);
+
+ // Returns the associated render text instance via the |delegate_|.
+ gfx::RenderText* GetRenderText();
+
+ // Helper function to update the selection on a mouse drag as per
+ // |last_drag_location_|. Can be called asynchronously, through a timer.
+ void SelectThroughLastDragLocation();
+
+ // A timer and point used to modify the selection when dragging.
+ base::RepeatingTimer drag_selection_timer_;
+ gfx::Point last_drag_location_;
+
+ // State variables used to track double and triple clicks.
+ base::TimeTicks last_click_time_;
+ gfx::Point last_click_location_;
+ size_t aggregated_clicks_;
+ gfx::Range double_click_word_;
+
+ // Weak pointer.
+ Delegate* delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(SelectionController);
+};
+
+// An interface implemented/managed by a view which uses the
+// SelectionController.
+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.
+ public:
+ // Returns the associated RenderText instance to be used for selection.
+ 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
+
+ // Methods related to properties of the associated view.
+
+ // Returns true if the associated text view is read only.
+ virtual bool IsReadOnly() const = 0;
+ // Returns whether there is a drag operation originating from the associated
+ // view.
+ virtual bool HasTextBeingDragged() const = 0;
+ // Sets whether text is being dragged from the associated view.
+ virtual void SetTextBeingDragged(bool value) = 0;
+ // Returns the height of the associated view.
+ virtual int GetViewHeight() const = 0;
+ // Returns the width of the associated view.
+ virtual int GetViewWidth() const = 0;
+ // Returns the drag selection timer delay. This is the duration after which a
+ // drag selection is updated when the event location is outside the text
+ // bounds.
+ virtual int GetDragSelectionDelay() const = 0;
+
+ // 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
+ // view's text and/or selection.
+
+ // Called before performing a mouse action. Should not be called in succession
+ // and must always be followed by an OnAfterMouseAction call.
+ virtual void OnBeforeMouseAction() = 0;
+ // Called after the mouse action is performed. |text_changed| and
+ // |selection_changed| can be used by subclasses to make any necessary updates
+ // like redraw the text. Must always be preceeded by a OnBeforeMouseAction
+ // call.
+ virtual void OnAfterMouseAction(bool text_changed,
+ bool selection_changed) = 0;
+
+ // Called to notify when the current selection is updated. Must always be
+ // called during a mouse action.
+
+ // Called before the selection is updated. Not called in succession and always
+ // followed by an OnAfterSelectionUpdated call.
+ virtual void OnBeforeSelectionUpdated() = 0;
+ // Called after the selection is updated. Not called in succession and always
+ // preceeded by an OnBeforeSelectionUpdated call.
+ virtual void OnAfterSelectionUpdated() = 0;
+
+ // Selection clipboard related methods. Called only when a mouse action is in
+ // progress.
+
+ // Moves the cursor to the event location and pastes the text from the
+ // selection clipboard. The view may also request focus. NO-OP if the
+ // associated text view is read-only or the event is not a middle click.
+ 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
+ // Updates the selection clipboard with the currently selected text. Should
+ // empty the selection clipboard if no text is currently selected. NO-OP if
+ // the associated text view is obscured.
+ virtual void UpdateSelectionClipboard() = 0;
+
+ protected:
+ virtual ~Delegate() {}
+};
+
+} // namespace views
+
+#endif // UI_VIEWS_SELECTION_CONTROLLER_H_

Powered by Google App Engine
This is Rietveld 408576698