| OLD | NEW |
| (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 class SelectionControllerDelegate; |
| 25 |
| 26 // Helper class used to facilitate mouse event handling and text selection. To |
| 27 // use, clients must implement the SelectionControllerDelegate interface. |
| 28 // TODO(karandeepb): Also make this class handle gesture events. |
| 29 class VIEWS_EXPORT SelectionController { |
| 30 public: |
| 31 // |delegate| must be non-null. |
| 32 explicit SelectionController(SelectionControllerDelegate* delegate); |
| 33 |
| 34 // Handle mouse events forwarded by |delegate_|. |handled| specifies whether |
| 35 // the event has already been handled by the |delegate_|. If |handled| is |
| 36 // true, the mouse event is just used to update the internal state without |
| 37 // updating the state of the associated RenderText instance. |
| 38 bool OnMousePressed(const ui::MouseEvent& event, bool handled); |
| 39 bool OnMouseDragged(const ui::MouseEvent& event); |
| 40 void OnMouseReleased(const ui::MouseEvent& event); |
| 41 void OnMouseCaptureLost(); |
| 42 |
| 43 // Returns the latest click location. |
| 44 const gfx::Point& last_click_location() const { return last_click_location_; } |
| 45 |
| 46 // Sets whether the SelectionController should update or paste the |
| 47 // selection clipboard on middle-click. Default is false. |
| 48 void set_handles_selection_clipboard(bool value) { |
| 49 handles_selection_clipboard_ = value; |
| 50 } |
| 51 |
| 52 private: |
| 53 // Tracks the mouse clicks for single/double/triple clicks. |
| 54 void TrackMouseClicks(const ui::MouseEvent& event); |
| 55 |
| 56 // Returns the associated render text instance via the |delegate_|. |
| 57 gfx::RenderText* GetRenderText(); |
| 58 |
| 59 // Helper function to update the selection on a mouse drag as per |
| 60 // |last_drag_location_|. Can be called asynchronously, through a timer. |
| 61 void SelectThroughLastDragLocation(); |
| 62 |
| 63 // A timer and point used to modify the selection when dragging. |
| 64 base::RepeatingTimer drag_selection_timer_; |
| 65 gfx::Point last_drag_location_; |
| 66 |
| 67 // State variables used to track the last click time and location. |
| 68 base::TimeTicks last_click_time_; |
| 69 gfx::Point last_click_location_; |
| 70 |
| 71 // Used to track double and triple clicks. Can take the values 0, 1 and 2 |
| 72 // which specify a single, double and triple click respectively. Alternates |
| 73 // between a double and triple click for continous clicks. |
| 74 size_t aggregated_clicks_; |
| 75 |
| 76 // The range selected on a double click. |
| 77 gfx::Range double_click_word_; |
| 78 |
| 79 // Weak pointer. |
| 80 SelectionControllerDelegate* delegate_; |
| 81 |
| 82 // Whether the selection clipboard is handled. |
| 83 bool handles_selection_clipboard_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(SelectionController); |
| 86 }; |
| 87 |
| 88 } // namespace views |
| 89 |
| 90 #endif // UI_VIEWS_SELECTION_CONTROLLER_H_ |
| OLD | NEW |