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
3 // found in the LICENSE file.
5 #ifndef UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_
6 #define UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_
8 #include "ui/base/touch/selection_bound.h"
9 #include "ui/gfx/geometry/point_f.h"
10 #include "ui/gfx/geometry/rect_f.h"
11 #include "ui/touch_selection/selection_event_type.h"
12 #include "ui/touch_selection/touch_handle.h"
13 #include "ui/touch_selection/touch_handle_orientation.h"
14 #include "ui/touch_selection/ui_touch_selection_export.h"
19 // Interface through which |TouchSelectionController| issues selection-related
20 // commands, notifications and requests.
21 class UI_TOUCH_SELECTION_EXPORT TouchSelectionControllerClient
{
23 virtual ~TouchSelectionControllerClient() {}
25 virtual bool SupportsAnimation() const = 0;
26 virtual void SetNeedsAnimate() = 0;
27 virtual void MoveCaret(const gfx::PointF
& position
) = 0;
28 virtual void MoveRangeSelectionExtent(const gfx::PointF
& extent
) = 0;
29 virtual void SelectBetweenCoordinates(const gfx::PointF
& base
,
30 const gfx::PointF
& extent
) = 0;
31 virtual void OnSelectionEvent(SelectionEventType event
) = 0;
32 virtual scoped_ptr
<TouchHandleDrawable
> CreateDrawable() = 0;
35 // Controller for manipulating text selection via touch input.
36 class UI_TOUCH_SELECTION_EXPORT TouchSelectionController
37 : public TouchHandleClient
{
45 TouchSelectionController(TouchSelectionControllerClient
* client
,
46 base::TimeDelta tap_timeout
,
48 bool show_on_tap_for_empty_editable
);
49 ~TouchSelectionController() override
;
51 // To be called when the selection bounds have changed.
52 // Note that such updates will trigger handle updates only if preceded
53 // by an appropriate call to allow automatic showing.
54 void OnSelectionBoundsChanged(const SelectionBound
& start
,
55 const SelectionBound
& end
);
57 // Allows touch-dragging of the handle.
58 // Returns true iff the event was consumed, in which case the caller should
59 // cease further handling of the event.
60 bool WillHandleTouchEvent(const MotionEvent
& event
);
62 // To be called before forwarding a tap event. This allows automatically
63 // showing the insertion handle from subsequent bounds changes.
64 bool WillHandleTapEvent(const gfx::PointF
& location
);
66 // To be called before forwarding a longpress event. This allows automatically
67 // showing the selection or insertion handles from subsequent bounds changes.
68 bool WillHandleLongPressEvent(const gfx::PointF
& location
);
70 // Allow showing the selection handles from the most recent selection bounds
71 // update (if valid), or a future valid bounds update.
72 void AllowShowingFromCurrentSelection();
74 // Hide the handles and suppress bounds updates until the next explicit
76 void HideAndDisallowShowingAutomatically();
78 // Override the handle visibility according to |hidden|.
79 void SetTemporarilyHidden(bool hidden
);
81 // To be called when the editability of the focused region changes.
82 void OnSelectionEditable(bool editable
);
84 // To be called when the contents of the focused region changes.
85 void OnSelectionEmpty(bool empty
);
87 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
88 // Returns true if an animation is active and requires further ticking.
89 bool Animate(base::TimeTicks animate_time
);
91 // Returns the rect between the two active selection bounds. If just one of
92 // the bounds is visible, the rect is simply the (one-dimensional) rect of
93 // that bound. If no selection is active, an empty rect will be returned.
94 gfx::RectF
GetRectBetweenBounds() const;
96 // Returns the visible rect of specified touch handle. For an active insertion
97 // these values will be identical.
98 gfx::RectF
GetStartHandleRect() const;
99 gfx::RectF
GetEndHandleRect() const;
101 // Returns the focal point of the start and end bounds, as defined by
102 // their bottom coordinate.
103 const gfx::PointF
& GetStartPosition() const;
104 const gfx::PointF
& GetEndPosition() const;
106 const SelectionBound
& start() const { return start_
; }
107 const SelectionBound
& end() const { return end_
; }
109 ActiveStatus
active_status() const { return active_status_
; }
112 enum InputEventType
{ TAP
, LONG_PRESS
, INPUT_EVENT_TYPE_NONE
};
114 // TouchHandleClient implementation.
115 void OnHandleDragBegin(const TouchHandle
& handle
) override
;
116 void OnHandleDragUpdate(const TouchHandle
& handle
,
117 const gfx::PointF
& new_position
) override
;
118 void OnHandleDragEnd(const TouchHandle
& handle
) override
;
119 void OnHandleTapped(const TouchHandle
& handle
) override
;
120 void SetNeedsAnimate() override
;
121 scoped_ptr
<TouchHandleDrawable
> CreateDrawable() override
;
122 base::TimeDelta
GetTapTimeout() const override
;
123 float GetTapSlop() const override
;
125 void ShowInsertionHandleAutomatically();
126 void ShowSelectionHandlesAutomatically();
127 bool WillHandleTapOrLongPress(const gfx::PointF
& location
);
129 void OnInsertionChanged();
130 void OnSelectionChanged();
132 void ActivateInsertion();
133 void DeactivateInsertion();
134 void ActivateSelection();
135 void DeactivateSelection();
136 void ForceNextUpdateIfInactive();
138 gfx::Vector2dF
GetStartLineOffset() const;
139 gfx::Vector2dF
GetEndLineOffset() const;
140 bool GetStartVisible() const;
141 bool GetEndVisible() const;
142 TouchHandle::AnimationStyle
GetAnimationStyle(bool was_active
) const;
144 void LogSelectionEnd();
146 TouchSelectionControllerClient
* const client_
;
147 const base::TimeDelta tap_timeout_
;
148 const float tap_slop_
;
150 // Whether to force an update on the next selection event even if the
151 // cached selection matches the new selection.
152 bool force_next_update_
;
154 // Controls whether an insertion handle is shown on a tap for an empty
156 bool show_on_tap_for_empty_editable_
;
158 InputEventType response_pending_input_event_
;
160 SelectionBound start_
;
162 TouchHandleOrientation start_orientation_
;
163 TouchHandleOrientation end_orientation_
;
165 ActiveStatus active_status_
;
167 scoped_ptr
<TouchHandle
> insertion_handle_
;
168 bool activate_insertion_automatically_
;
170 scoped_ptr
<TouchHandle
> start_selection_handle_
;
171 scoped_ptr
<TouchHandle
> end_selection_handle_
;
172 bool activate_selection_automatically_
;
174 bool selection_empty_
;
175 bool selection_editable_
;
177 bool temporarily_hidden_
;
179 base::TimeTicks selection_start_time_
;
180 // Whether a selection handle was dragged during the current 'selection
181 // session' - i.e. since the current selection has been activated.
182 bool selection_handle_dragged_
;
184 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController
);
189 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_