touch_selection_controller.h revision d9512fb2dc45a909a2812842735be59e78b5171d
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.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
7
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"
14
15namespace blink {
16class WebInputEvent;
17}
18
19namespace ui {
20class MotionEvent;
21}
22
23namespace content {
24
25// Interface through which |TouchSelectionController| issues selection-related
26// commands, notifications and requests.
27class CONTENT_EXPORT TouchSelectionControllerClient {
28 public:
29  virtual ~TouchSelectionControllerClient() {}
30
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;
39};
40
41// Controller for manipulating text selection via touch input.
42class CONTENT_EXPORT TouchSelectionController : public TouchHandleClient {
43 public:
44  TouchSelectionController(TouchSelectionControllerClient* client,
45                           base::TimeDelta tap_timeout,
46                           float tap_slop);
47  virtual ~TouchSelectionController();
48
49  // To be called when the selection bounds have changed.
50  // Note that such updates will trigger handle updates only if preceded
51  // by an appropriate call to allow automatic showing.
52  void OnSelectionBoundsChanged(const cc::ViewportSelectionBound& start,
53                                const cc::ViewportSelectionBound& end);
54
55  // Allows touch-dragging of the handle.
56  // Returns true iff the event was consumed, in which case the caller should
57  // cease further handling of the event.
58  bool WillHandleTouchEvent(const ui::MotionEvent& event);
59
60  // To be called before forwarding a tap event. This allows automatically
61  // showing the insertion handle from subsequent bounds changes.
62  void OnTapEvent();
63
64  // To be called before forwarding a longpress event. This allows automatically
65  // showing the selection or insertion handles from subsequent bounds changes.
66  void OnLongPressEvent();
67
68  // Hide the handles and suppress bounds updates until the next explicit
69  // showing allowance.
70  void HideAndDisallowShowingAutomatically();
71
72  // Override the handle visibility according to |hidden|.
73  void SetTemporarilyHidden(bool hidden);
74
75  // To be called when the editability of the focused region changes.
76  void OnSelectionEditable(bool editable);
77
78  // To be called when the contents of the focused region changes.
79  void OnSelectionEmpty(bool empty);
80
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);
84
85 private:
86  enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
87
88  // TouchHandleClient implementation.
89  virtual void OnHandleDragBegin(const TouchHandle& handle) OVERRIDE;
90  virtual void OnHandleDragUpdate(const TouchHandle& handle,
91                                  const gfx::PointF& new_position) OVERRIDE;
92  virtual void OnHandleDragEnd(const TouchHandle& handle) OVERRIDE;
93  virtual void OnHandleTapped(const TouchHandle& handle) OVERRIDE;
94  virtual void SetNeedsAnimate() OVERRIDE;
95  virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE;
96  virtual base::TimeDelta GetTapTimeout() const OVERRIDE;
97  virtual float GetTapSlop() const OVERRIDE;
98
99  void ShowInsertionHandleAutomatically();
100  void ShowSelectionHandlesAutomatically();
101
102  void OnInsertionChanged();
103  void OnSelectionChanged();
104
105  void ActivateInsertion();
106  void DeactivateInsertion();
107  void ActivateSelection();
108  void DeactivateSelection();
109  void ResetCachedValuesIfInactive();
110
111  const gfx::PointF& GetStartPosition() const;
112  const gfx::PointF& GetEndPosition() const;
113  gfx::Vector2dF GetStartLineOffset() const;
114  gfx::Vector2dF GetEndLineOffset() const;
115  bool GetStartVisible() const;
116  bool GetEndVisible() const;
117  TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const;
118
119  TouchSelectionControllerClient* const client_;
120  const base::TimeDelta tap_timeout_;
121  const float tap_slop_;
122
123  InputEventType response_pending_input_event_;
124
125  cc::ViewportSelectionBound start_;
126  cc::ViewportSelectionBound end_;
127  TouchHandleOrientation start_orientation_;
128  TouchHandleOrientation end_orientation_;
129
130  scoped_ptr<TouchHandle> insertion_handle_;
131  bool is_insertion_active_;
132  bool activate_insertion_automatically_;
133
134  scoped_ptr<TouchHandle> start_selection_handle_;
135  scoped_ptr<TouchHandle> end_selection_handle_;
136  gfx::PointF fixed_handle_position_;
137  bool is_selection_active_;
138  bool activate_selection_automatically_;
139
140  bool selection_empty_;
141  bool selection_editable_;
142
143  bool temporarily_hidden_;
144
145  DISALLOW_COPY_AND_ASSIGN(TouchSelectionController);
146};
147
148}  // namespace content
149
150#endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
151