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 "content/browser/renderer_host/input/selection_event_type.h"
9 #include "content/browser/renderer_host/input/touch_handle.h"
10 #include "content/common/content_export.h"
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/rect_f.h"
24 // Interface through which |TouchSelectionController| issues selection-related
25 // commands, notifications and requests.
26 class CONTENT_EXPORT TouchSelectionControllerClient {
28 virtual ~TouchSelectionControllerClient() {}
30 virtual bool SupportsAnimation() const = 0;
31 virtual void SetNeedsAnimate() = 0;
32 virtual void MoveCaret(const gfx::PointF& position) = 0;
33 virtual void SelectBetweenCoordinates(const gfx::PointF& start,
34 const gfx::PointF& end) = 0;
35 virtual void OnSelectionEvent(SelectionEventType event,
36 const gfx::PointF& position) = 0;
37 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0;
40 // Controller for manipulating text selection via touch input.
41 class CONTENT_EXPORT TouchSelectionController : public TouchHandleClient {
43 explicit TouchSelectionController(TouchSelectionControllerClient* client);
44 virtual ~TouchSelectionController();
46 // To be called when the selection bounds have changed.
47 // Note that such updates will trigger handle updates only if preceded
48 // by an appropriate call to allow automatic showing.
49 void OnSelectionBoundsChanged(const gfx::RectF& start_rect,
50 TouchHandleOrientation start_orientation,
52 const gfx::RectF& end_rect,
53 TouchHandleOrientation end_orientation,
56 // Allows touch-dragging of the handle.
57 // Returns true iff the event was consumed, in which case the caller should
58 // cease further handling of the event.
59 bool WillHandleTouchEvent(const ui::MotionEvent& event);
61 // To be called before forwarding a tap event. This allows automatically
62 // showing the insertion handle from subsequent bounds changes.
65 // To be called before forwarding a longpress event. This allows automatically
66 // showing the selection or insertion handles from subsequent bounds changes.
67 void OnLongPressEvent();
69 // Hide the handles and suppress bounds updates until the next explicit
71 void HideAndDisallowShowingAutomatically();
73 // Override the handle visibility according to |hidden|.
74 void SetTemporarilyHidden(bool hidden);
76 // To be called when the editability of the focused region changes.
77 void OnSelectionEditable(bool editable);
79 // To be called when the contents of the focused region changes.
80 void OnSelectionEmpty(bool empty);
82 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
83 // Returns true if an animation is active and requires further ticking.
84 bool Animate(base::TimeTicks animate_time);
87 enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
89 // TouchHandleClient implementation.
90 virtual void OnHandleDragBegin(const TouchHandle& handle) OVERRIDE;
91 virtual void OnHandleDragUpdate(const TouchHandle& handle,
92 const gfx::PointF& new_position) OVERRIDE;
93 virtual void OnHandleDragEnd(const TouchHandle& handle) OVERRIDE;
94 virtual void OnHandleTapped(const TouchHandle& handle) OVERRIDE;
95 virtual void SetNeedsAnimate() OVERRIDE;
96 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE;
98 void ShowInsertionHandleAutomatically();
99 void ShowSelectionHandlesAutomatically();
101 void OnInsertionChanged();
102 void OnSelectionChanged();
104 void ActivateInsertion();
105 void DeactivateInsertion();
106 void ActivateSelection();
107 void DeactivateSelection();
108 void ResetCachedValuesIfInactive();
110 gfx::PointF GetStartPosition() const;
111 gfx::PointF GetEndPosition() const;
112 float GetStartLineHeight() const;
113 float GetEndLineHeight() const;
114 bool GetStartVisible() const;
115 bool GetEndVisible() const;
116 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const;
118 TouchSelectionControllerClient* const client_;
120 InputEventType last_input_event_type_;
122 gfx::RectF start_rect_;
123 TouchHandleOrientation start_orientation_;
125 gfx::RectF end_rect_;
126 TouchHandleOrientation end_orientation_;
129 scoped_ptr<TouchHandle> insertion_handle_;
130 bool is_insertion_active_;
131 bool activate_insertion_automatically_;
133 scoped_ptr<TouchHandle> start_selection_handle_;
134 scoped_ptr<TouchHandle> end_selection_handle_;
135 gfx::PointF fixed_handle_position_;
136 bool is_selection_active_;
137 bool activate_selection_automatically_;
139 bool selection_empty_;
140 bool selection_editable_;
142 bool temporarily_hidden_;
144 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController);
147 } // namespace content
149 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_