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
{
39 TouchSelectionController(TouchSelectionControllerClient
* client
,
40 base::TimeDelta tap_timeout
,
42 bool show_on_tap_for_empty_editable
);
43 ~TouchSelectionController() override
;
45 // To be called when the selection bounds have changed.
46 // Note that such updates will trigger handle updates only if preceded
47 // by an appropriate call to allow automatic showing.
48 void OnSelectionBoundsChanged(const SelectionBound
& start
,
49 const SelectionBound
& end
);
51 // Allows touch-dragging of the handle.
52 // Returns true iff the event was consumed, in which case the caller should
53 // cease further handling of the event.
54 bool WillHandleTouchEvent(const MotionEvent
& event
);
56 // To be called before forwarding a tap event. This allows automatically
57 // showing the insertion handle from subsequent bounds changes.
60 // To be called before forwarding a longpress event. This allows automatically
61 // showing the selection or insertion handles from subsequent bounds changes.
62 void OnLongPressEvent();
64 // Allow showing the selection handles from the most recent selection bounds
65 // update (if valid), or a future valid bounds update.
66 void AllowShowingFromCurrentSelection();
68 // Hide the handles and suppress bounds updates until the next explicit
70 void HideAndDisallowShowingAutomatically();
72 // Override the handle visibility according to |hidden|.
73 void SetTemporarilyHidden(bool hidden
);
75 // To be called when the editability of the focused region changes.
76 void OnSelectionEditable(bool editable
);
78 // To be called when the contents of the focused region changes.
79 void OnSelectionEmpty(bool empty
);
81 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
82 // Returns true if an animation is active and requires further ticking.
83 bool Animate(base::TimeTicks animate_time
);
85 // Returns the rect between the two active selection bounds. If just one of
86 // the bounds is visible, the rect is simply the (one-dimensional) rect of
87 // that bound. If no selection is active, an empty rect will be returned.
88 gfx::RectF
GetRectBetweenBounds() const;
90 // Returns the visible rect of specified touch handle. For an active insertion
91 // these values will be identical.
92 gfx::RectF
GetStartHandleRect() const;
93 gfx::RectF
GetEndHandleRect() const;
95 // Returns the focal point of the start and end bounds, as defined by
96 // their bottom coordinate.
97 const gfx::PointF
& GetStartPosition() const;
98 const gfx::PointF
& GetEndPosition() const;
101 enum InputEventType
{ TAP
, LONG_PRESS
, INPUT_EVENT_TYPE_NONE
};
103 // TouchHandleClient implementation.
104 void OnHandleDragBegin(const TouchHandle
& handle
) override
;
105 void OnHandleDragUpdate(const TouchHandle
& handle
,
106 const gfx::PointF
& new_position
) override
;
107 void OnHandleDragEnd(const TouchHandle
& handle
) override
;
108 void OnHandleTapped(const TouchHandle
& handle
) override
;
109 void SetNeedsAnimate() override
;
110 scoped_ptr
<TouchHandleDrawable
> CreateDrawable() override
;
111 base::TimeDelta
GetTapTimeout() const override
;
112 float GetTapSlop() const override
;
114 void ShowInsertionHandleAutomatically();
115 void ShowSelectionHandlesAutomatically();
117 void OnInsertionChanged();
118 void OnSelectionChanged();
120 void ActivateInsertion();
121 void DeactivateInsertion();
122 void ActivateSelection();
123 void DeactivateSelection();
124 void ResetCachedValuesIfInactive();
126 gfx::Vector2dF
GetStartLineOffset() const;
127 gfx::Vector2dF
GetEndLineOffset() const;
128 bool GetStartVisible() const;
129 bool GetEndVisible() const;
130 TouchHandle::AnimationStyle
GetAnimationStyle(bool was_active
) const;
132 void LogSelectionEnd();
134 TouchSelectionControllerClient
* const client_
;
135 const base::TimeDelta tap_timeout_
;
136 const float tap_slop_
;
138 // Controls whether an insertion handle is shown on a tap for an empty
140 bool show_on_tap_for_empty_editable_
;
142 InputEventType response_pending_input_event_
;
144 SelectionBound start_
;
146 TouchHandleOrientation start_orientation_
;
147 TouchHandleOrientation end_orientation_
;
149 scoped_ptr
<TouchHandle
> insertion_handle_
;
150 bool is_insertion_active_
;
151 bool activate_insertion_automatically_
;
153 scoped_ptr
<TouchHandle
> start_selection_handle_
;
154 scoped_ptr
<TouchHandle
> end_selection_handle_
;
155 bool is_selection_active_
;
156 bool activate_selection_automatically_
;
158 bool selection_empty_
;
159 bool selection_editable_
;
161 bool temporarily_hidden_
;
163 base::TimeTicks selection_start_time_
;
164 // Whether a selection handle was dragged during the current 'selection
165 // session' - i.e. since the current selection has been activated.
166 bool selection_handle_dragged_
;
168 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController
);
173 #endif // UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_