Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_selection_controller.h
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
15 namespace blink {
16 class WebInputEvent;
17 }
18
19 namespace ui {
20 class MotionEvent;
21 }
22
23 namespace content {
24
25 // Interface through which |TouchSelectionController| issues selection-related
26 // commands, notifications and requests.
27 class 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 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;
40 };
41
42 // Controller for manipulating text selection via touch input.
43 class CONTENT_EXPORT TouchSelectionController : public TouchHandleClient {
44  public:
45   TouchSelectionController(TouchSelectionControllerClient* client,
46                            base::TimeDelta tap_timeout,
47                            float tap_slop);
48   ~TouchSelectionController() override;
49
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);
55
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);
60
61   // To be called before forwarding a tap event. This allows automatically
62   // showing the insertion handle from subsequent bounds changes.
63   void OnTapEvent();
64
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();
68
69   // Hide the handles and suppress bounds updates until the next explicit
70   // showing allowance.
71   void HideAndDisallowShowingAutomatically();
72
73   // Override the handle visibility according to |hidden|.
74   void SetTemporarilyHidden(bool hidden);
75
76   // To be called when the editability of the focused region changes.
77   void OnSelectionEditable(bool editable);
78
79   // To be called when the contents of the focused region changes.
80   void OnSelectionEmpty(bool empty);
81
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);
85
86  private:
87   enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
88
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;
99
100   void ShowInsertionHandleAutomatically();
101   void ShowSelectionHandlesAutomatically();
102
103   void OnInsertionChanged();
104   void OnSelectionChanged();
105
106   void ActivateInsertion();
107   void DeactivateInsertion();
108   void ActivateSelection();
109   void DeactivateSelection();
110   void ResetCachedValuesIfInactive();
111
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;
119
120   TouchSelectionControllerClient* const client_;
121   const base::TimeDelta tap_timeout_;
122   const float tap_slop_;
123
124   InputEventType response_pending_input_event_;
125
126   cc::ViewportSelectionBound start_;
127   cc::ViewportSelectionBound end_;
128   TouchHandleOrientation start_orientation_;
129   TouchHandleOrientation end_orientation_;
130
131   scoped_ptr<TouchHandle> insertion_handle_;
132   bool is_insertion_active_;
133   bool activate_insertion_automatically_;
134
135   scoped_ptr<TouchHandle> start_selection_handle_;
136   scoped_ptr<TouchHandle> end_selection_handle_;
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_