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 CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
8 #include "cc/output/viewport_selection_bound.h"
9 #include "content/browser/renderer_host/input/selection_event_type.h"
10 #include "content/browser/renderer_host/input/touch_handle.h"
11 #include "content/common/content_export.h"
12 #include "ui/gfx/geometry/point_f.h"
13 #include "ui/gfx/geometry/rect_f.h"
25 // Interface through which |TouchSelectionController| issues selection-related
26 // commands, notifications and requests.
27 class CONTENT_EXPORT TouchSelectionControllerClient
{
29 virtual ~TouchSelectionControllerClient() {}
31 virtual bool SupportsAnimation() const = 0;
32 virtual void SetNeedsAnimate() = 0;
33 virtual void MoveCaret(const gfx::PointF
& position
) = 0;
34 virtual void SelectBetweenCoordinates(const gfx::PointF
& start
,
35 const gfx::PointF
& end
) = 0;
36 virtual void OnSelectionEvent(SelectionEventType event
,
37 const gfx::PointF
& position
) = 0;
38 virtual scoped_ptr
<TouchHandleDrawable
> CreateDrawable() = 0;
41 // Controller for manipulating text selection via touch input.
42 class CONTENT_EXPORT TouchSelectionController
: public TouchHandleClient
{
44 explicit TouchSelectionController(TouchSelectionControllerClient
* client
);
45 virtual ~TouchSelectionController();
47 // To be called when the selection bounds have changed.
48 // Note that such updates will trigger handle updates only if preceded
49 // by an appropriate call to allow automatic showing.
50 void OnSelectionBoundsChanged(const cc::ViewportSelectionBound
& start
,
51 const cc::ViewportSelectionBound
& end
);
53 // Allows touch-dragging of the handle.
54 // Returns true iff the event was consumed, in which case the caller should
55 // cease further handling of the event.
56 bool WillHandleTouchEvent(const ui::MotionEvent
& event
);
58 // To be called before forwarding a tap event. This allows automatically
59 // showing the insertion handle from subsequent bounds changes.
62 // To be called before forwarding a longpress event. This allows automatically
63 // showing the selection or insertion handles from subsequent bounds changes.
64 void OnLongPressEvent();
66 // Hide the handles and suppress bounds updates until the next explicit
68 void HideAndDisallowShowingAutomatically();
70 // Override the handle visibility according to |hidden|.
71 void SetTemporarilyHidden(bool hidden
);
73 // To be called when the editability of the focused region changes.
74 void OnSelectionEditable(bool editable
);
76 // To be called when the contents of the focused region changes.
77 void OnSelectionEmpty(bool empty
);
79 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
80 // Returns true if an animation is active and requires further ticking.
81 bool Animate(base::TimeTicks animate_time
);
84 enum InputEventType
{ TAP
, LONG_PRESS
, INPUT_EVENT_TYPE_NONE
};
86 // TouchHandleClient implementation.
87 virtual void OnHandleDragBegin(const TouchHandle
& handle
) OVERRIDE
;
88 virtual void OnHandleDragUpdate(const TouchHandle
& handle
,
89 const gfx::PointF
& new_position
) OVERRIDE
;
90 virtual void OnHandleDragEnd(const TouchHandle
& handle
) OVERRIDE
;
91 virtual void OnHandleTapped(const TouchHandle
& handle
) OVERRIDE
;
92 virtual void SetNeedsAnimate() OVERRIDE
;
93 virtual scoped_ptr
<TouchHandleDrawable
> CreateDrawable() OVERRIDE
;
95 void ShowInsertionHandleAutomatically();
96 void ShowSelectionHandlesAutomatically();
98 void OnInsertionChanged();
99 void OnSelectionChanged();
101 void ActivateInsertion();
102 void DeactivateInsertion();
103 void ActivateSelection();
104 void DeactivateSelection();
105 void ResetCachedValuesIfInactive();
107 const gfx::PointF
& GetStartPosition() const;
108 const gfx::PointF
& GetEndPosition() const;
109 gfx::Vector2dF
GetStartLineOffset() const;
110 gfx::Vector2dF
GetEndLineOffset() const;
111 bool GetStartVisible() const;
112 bool GetEndVisible() const;
113 TouchHandle::AnimationStyle
GetAnimationStyle(bool was_active
) const;
115 TouchSelectionControllerClient
* const client_
;
117 InputEventType response_pending_input_event_
;
119 cc::ViewportSelectionBound start_
;
120 cc::ViewportSelectionBound end_
;
121 TouchHandleOrientation start_orientation_
;
122 TouchHandleOrientation end_orientation_
;
124 scoped_ptr
<TouchHandle
> insertion_handle_
;
125 bool is_insertion_active_
;
126 bool activate_insertion_automatically_
;
128 scoped_ptr
<TouchHandle
> start_selection_handle_
;
129 scoped_ptr
<TouchHandle
> end_selection_handle_
;
130 gfx::PointF fixed_handle_position_
;
131 bool is_selection_active_
;
132 bool activate_selection_automatically_
;
134 bool selection_empty_
;
135 bool selection_editable_
;
137 bool temporarily_hidden_
;
139 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController
);
142 } // namespace content
144 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_