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 MoveRangeSelectionExtent(const gfx::PointF& extent) = 0;
35 virtual void SelectBetweenCoordinates(const gfx::PointF& base,
36 const gfx::PointF& extent) = 0;
37 virtual void OnSelectionEvent(SelectionEventType event,
38 const gfx::PointF& position) = 0;
39 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0;
42 // Controller for manipulating text selection via touch input.
43 class CONTENT_EXPORT TouchSelectionController : public TouchHandleClient {
45 TouchSelectionController(TouchSelectionControllerClient* client,
46 base::TimeDelta tap_timeout,
48 ~TouchSelectionController() override;
50 // To be called when the selection bounds have changed.
51 // Note that such updates will trigger handle updates only if preceded
52 // by an appropriate call to allow automatic showing.
53 void OnSelectionBoundsChanged(const cc::ViewportSelectionBound& start,
54 const cc::ViewportSelectionBound& end);
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 void OnHandleDragBegin(const TouchHandle& handle) override;
91 void OnHandleDragUpdate(const TouchHandle& handle,
92 const gfx::PointF& new_position) override;
93 void OnHandleDragEnd(const TouchHandle& handle) override;
94 void OnHandleTapped(const TouchHandle& handle) override;
95 void SetNeedsAnimate() override;
96 scoped_ptr<TouchHandleDrawable> CreateDrawable() override;
97 base::TimeDelta GetTapTimeout() const override;
98 float GetTapSlop() const override;
100 void ShowInsertionHandleAutomatically();
101 void ShowSelectionHandlesAutomatically();
103 void OnInsertionChanged();
104 void OnSelectionChanged();
106 void ActivateInsertion();
107 void DeactivateInsertion();
108 void ActivateSelection();
109 void DeactivateSelection();
110 void ResetCachedValuesIfInactive();
112 const gfx::PointF& GetStartPosition() const;
113 const gfx::PointF& GetEndPosition() const;
114 gfx::Vector2dF GetStartLineOffset() const;
115 gfx::Vector2dF GetEndLineOffset() const;
116 bool GetStartVisible() const;
117 bool GetEndVisible() const;
118 TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const;
120 TouchSelectionControllerClient* const client_;
121 const base::TimeDelta tap_timeout_;
122 const float tap_slop_;
124 InputEventType response_pending_input_event_;
126 cc::ViewportSelectionBound start_;
127 cc::ViewportSelectionBound end_;
128 TouchHandleOrientation start_orientation_;
129 TouchHandleOrientation end_orientation_;
131 scoped_ptr<TouchHandle> insertion_handle_;
132 bool is_insertion_active_;
133 bool activate_insertion_automatically_;
135 scoped_ptr<TouchHandle> start_selection_handle_;
136 scoped_ptr<TouchHandle> end_selection_handle_;
137 bool is_selection_active_;
138 bool activate_selection_automatically_;
140 bool selection_empty_;
141 bool selection_editable_;
143 bool temporarily_hidden_;
145 DISALLOW_COPY_AND_ASSIGN(TouchSelectionController);
148 } // namespace content
150 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_