OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ | 5 #ifndef UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ |
6 #define UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ | 6 #define UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "ui/events/gesture_detection/motion_event.h" | 11 #include "ui/events/gesture_detection/motion_event.h" |
12 #include "ui/gfx/geometry/point_f.h" | 12 #include "ui/gfx/geometry/point_f.h" |
13 #include "ui/gfx/geometry/rect_f.h" | 13 #include "ui/gfx/geometry/rect_f.h" |
14 #include "ui/gfx/geometry/vector2d_f.h" | 14 #include "ui/gfx/geometry/vector2d_f.h" |
15 #include "ui/touch_selection/touch_handle_orientation.h" | 15 #include "ui/touch_selection/touch_handle_orientation.h" |
16 #include "ui/touch_selection/touch_selection_draggable.h" | 16 #include "ui/touch_selection/touch_selection_draggable.h" |
17 #include "ui/touch_selection/ui_touch_selection_export.h" | 17 #include "ui/touch_selection/ui_touch_selection_export.h" |
18 | 18 |
19 namespace ui { | 19 namespace ui { |
20 | 20 |
21 class TouchHandle; | 21 class TouchHandle; |
22 | 22 |
23 // Interface through which |TouchHandle| delegates rendering-specific duties. | 23 // Interface through which |TouchHandle| delegates rendering-specific duties. |
24 class UI_TOUCH_SELECTION_EXPORT TouchHandleDrawable { | 24 class UI_TOUCH_SELECTION_EXPORT TouchHandleDrawable { |
25 public: | 25 public: |
26 virtual ~TouchHandleDrawable() {} | 26 virtual ~TouchHandleDrawable() {} |
| 27 |
| 28 // Sets whether the handle is active, allowing resource cleanup if necessary. |
27 virtual void SetEnabled(bool enabled) = 0; | 29 virtual void SetEnabled(bool enabled) = 0; |
28 virtual void SetOrientation(TouchHandleOrientation orientation) = 0; | 30 |
| 31 // Update the handle visuals to |orientation|. |
| 32 // |mirror_vertical| and |mirror_horizontal| are used to invert the drawables |
| 33 // if required for adaptive handle orientation. |
| 34 virtual void SetOrientation(ui::TouchHandleOrientation orientation, |
| 35 bool mirror_vertical, |
| 36 bool mirror_horizontal) = 0; |
| 37 |
| 38 // Sets the origin position of the touch handle. |
| 39 // |origin| takes care of positioning the handle drawable based on |
| 40 // its visible bounds. |
| 41 virtual void SetOrigin(const gfx::PointF& origin) = 0; |
| 42 |
| 43 // Sets the transparency |alpha| for the handle drawable. |
29 virtual void SetAlpha(float alpha) = 0; | 44 virtual void SetAlpha(float alpha) = 0; |
30 virtual void SetFocus(const gfx::PointF& position) = 0; | 45 |
| 46 // Returns the visible bounds of the handle drawable. |
| 47 // The bounds includes the transparent horizontal padding. |
31 virtual gfx::RectF GetVisibleBounds() const = 0; | 48 virtual gfx::RectF GetVisibleBounds() const = 0; |
| 49 |
| 50 // Returns the transparent horizontal padding ratio of the handle drawable. |
| 51 virtual float GetDrawableHorizontalPaddingRatio() const = 0; |
32 }; | 52 }; |
33 | 53 |
34 // Interface through which |TouchHandle| communicates handle manipulation and | 54 // Interface through which |TouchHandle| communicates handle manipulation and |
35 // requests concrete drawable instances. | 55 // requests concrete drawable instances. |
36 class UI_TOUCH_SELECTION_EXPORT TouchHandleClient | 56 class UI_TOUCH_SELECTION_EXPORT TouchHandleClient |
37 : public TouchSelectionDraggableClient { | 57 : public TouchSelectionDraggableClient { |
38 public: | 58 public: |
39 ~TouchHandleClient() override {} | 59 ~TouchHandleClient() override {} |
40 virtual void OnHandleTapped(const TouchHandle& handle) = 0; | 60 virtual void OnHandleTapped(const TouchHandle& handle) = 0; |
41 virtual void SetNeedsAnimate() = 0; | 61 virtual void SetNeedsAnimate() = 0; |
42 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0; | 62 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0; |
43 virtual base::TimeDelta GetMaxTapDuration() const = 0; | 63 virtual base::TimeDelta GetMaxTapDuration() const = 0; |
| 64 virtual bool IsAdaptiveHandleOrientationEnabled() const = 0; |
44 }; | 65 }; |
45 | 66 |
46 // Responsible for displaying a selection or insertion handle for text | 67 // Responsible for displaying a selection or insertion handle for text |
47 // interaction. | 68 // interaction. |
48 class UI_TOUCH_SELECTION_EXPORT TouchHandle : public TouchSelectionDraggable { | 69 class UI_TOUCH_SELECTION_EXPORT TouchHandle : public TouchSelectionDraggable { |
49 public: | 70 public: |
50 // The drawable will be enabled but invisible until otherwise specified. | 71 // The drawable will be enabled but invisible until otherwise specified. |
51 TouchHandle(TouchHandleClient* client, TouchHandleOrientation orientation); | 72 TouchHandle(TouchHandleClient* client, |
| 73 TouchHandleOrientation orientation, |
| 74 const gfx::RectF& viewport_rect); |
52 ~TouchHandle() override; | 75 ~TouchHandle() override; |
53 | 76 |
54 // TouchSelectionDraggable implementation. | 77 // TouchSelectionDraggable implementation. |
55 bool WillHandleTouchEvent(const MotionEvent& event) override; | 78 bool WillHandleTouchEvent(const MotionEvent& event) override; |
56 bool IsActive() const override; | 79 bool IsActive() const override; |
57 | 80 |
58 // Sets whether the handle is active, allowing resource cleanup if necessary. | 81 // Sets whether the handle is active, allowing resource cleanup if necessary. |
59 // If false, active animations or touch drag sequences will be cancelled. | 82 // If false, active animations or touch drag sequences will be cancelled. |
60 // While disabled, manipulation is *explicitly not supported*, and may lead to | 83 // While disabled, manipulation is *explicitly not supported*, and may lead to |
61 // undesirable and/or unstable side-effects. The handle can be safely | 84 // undesirable and/or unstable side-effects. The handle can be safely |
62 // re-enabled to allow continued operation. | 85 // re-enabled to allow continued operation. |
63 void SetEnabled(bool enabled); | 86 void SetEnabled(bool enabled); |
64 | 87 |
65 enum AnimationStyle { ANIMATION_NONE, ANIMATION_SMOOTH }; | 88 enum AnimationStyle { ANIMATION_NONE, ANIMATION_SMOOTH }; |
66 // Update the handle visibility, fading in/out according to |animation_style|. | 89 // Update the handle visibility, fading in/out according to |animation_style|. |
67 // If an animation is in-progress, it will be overriden appropriately. | 90 // If an animation is in-progress, it will be overriden appropriately. |
68 void SetVisible(bool visible, AnimationStyle animation_style); | 91 void SetVisible(bool visible, AnimationStyle animation_style); |
69 | 92 |
70 // Update the handle placement to |position|. | 93 // Update the focus points for the handles. The handle will be positioned |
| 94 // either |top| or |bottom| based on the mirror parameters. |
71 // Note: If a fade out animation is active or the handle is invisible, the | 95 // Note: If a fade out animation is active or the handle is invisible, the |
72 // handle position will not be updated until the handle regains visibility. | 96 // handle position will not be updated until the handle regains visibility. |
73 void SetPosition(const gfx::PointF& position); | 97 void SetFocus(const gfx::PointF& top, const gfx::PointF& bottom); |
| 98 |
| 99 // Update the viewport rect, based on which the handle decide its inversion. |
| 100 void SetViewportRect(const gfx::RectF& viewport_rect); |
74 | 101 |
75 // Update the handle visuals to |orientation|. | 102 // Update the handle visuals to |orientation|. |
76 // Note: If the handle is being dragged, the orientation change will be | 103 // Note: If the handle is being dragged, the orientation change will be |
77 // deferred until the drag has ceased. | 104 // deferred until the drag has ceased. |
78 void SetOrientation(TouchHandleOrientation orientation); | 105 void SetOrientation(TouchHandleOrientation orientation); |
79 | 106 |
80 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|. | 107 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|. |
81 // Returns true if an animation is active and requires further ticking. | 108 // Returns true if an animation is active and requires further ticking. |
82 bool Animate(base::TimeTicks frame_time); | 109 bool Animate(base::TimeTicks frame_time); |
83 | 110 |
84 // Get the visible bounds of the handle, based on the current position and | 111 // Get the visible bounds of the handle, based on the current position and |
85 // the drawable's size/orientation. If the handle is invisible or disabled, | 112 // the drawable's size/orientation. If the handle is invisible or disabled, |
86 // the bounds will be empty. | 113 // the bounds will be empty. |
87 gfx::RectF GetVisibleBounds() const; | 114 gfx::RectF GetVisibleBounds() const; |
88 | 115 |
89 const gfx::PointF& position() const { return position_; } | 116 // Updates the handle layout if the is_handle_layout_update_required_ flag is |
| 117 // set. Will be called once per frame update, avoids multiple updates for |
| 118 // for the same frame update due to more than one parameter updates. |
| 119 void UpdateHandleLayout(); |
| 120 |
| 121 const gfx::PointF& focus_bottom() const { return focus_bottom_; } |
90 TouchHandleOrientation orientation() const { return orientation_; } | 122 TouchHandleOrientation orientation() const { return orientation_; } |
91 | 123 |
92 private: | 124 private: |
| 125 gfx::PointF ComputeHandleOrigin() const; |
93 void BeginDrag(); | 126 void BeginDrag(); |
94 void EndDrag(); | 127 void EndDrag(); |
95 void BeginFade(); | 128 void BeginFade(); |
96 void EndFade(); | 129 void EndFade(); |
97 void SetAlpha(float alpha); | 130 void SetAlpha(float alpha); |
| 131 void SetUpdateLayoutRequired(); |
98 | 132 |
99 scoped_ptr<TouchHandleDrawable> drawable_; | 133 scoped_ptr<TouchHandleDrawable> drawable_; |
100 | 134 |
101 TouchHandleClient* const client_; | 135 TouchHandleClient* const client_; |
102 | 136 |
103 gfx::PointF position_; | 137 gfx::PointF focus_bottom_; |
| 138 gfx::PointF focus_top_; |
| 139 gfx::RectF viewport_rect_; |
104 TouchHandleOrientation orientation_; | 140 TouchHandleOrientation orientation_; |
105 TouchHandleOrientation deferred_orientation_; | 141 TouchHandleOrientation deferred_orientation_; |
106 | 142 |
107 gfx::PointF touch_down_position_; | 143 gfx::PointF touch_down_position_; |
108 gfx::Vector2dF touch_drag_offset_; | 144 gfx::Vector2dF touch_drag_offset_; |
109 base::TimeTicks touch_down_time_; | 145 base::TimeTicks touch_down_time_; |
110 | 146 |
111 // Note that when a fade animation is active, |is_visible_| and |position_| | 147 // Note that when a fade animation is active, |is_visible_| and |position_| |
112 // may not reflect the actual visibility and position of the drawable. This | 148 // may not reflect the actual visibility and position of the drawable. This |
113 // discrepancy is resolved either upon fade completion or cancellation. | 149 // discrepancy is resolved either upon fade completion or cancellation. |
114 base::TimeTicks fade_end_time_; | 150 base::TimeTicks fade_end_time_; |
115 gfx::PointF fade_start_position_; | 151 gfx::PointF fade_start_position_; |
116 float alpha_; | 152 float alpha_; |
117 bool animate_deferred_fade_; | 153 bool animate_deferred_fade_; |
118 | 154 |
119 bool enabled_; | 155 bool enabled_; |
120 bool is_visible_; | 156 bool is_visible_; |
121 bool is_dragging_; | 157 bool is_dragging_; |
122 bool is_drag_within_tap_region_; | 158 bool is_drag_within_tap_region_; |
| 159 bool is_handle_layout_update_required_; |
| 160 |
| 161 // Mirror variables determine if the handles should be inverted or not. |
| 162 bool mirror_vertical_; |
| 163 bool mirror_horizontal_; |
| 164 float handle_horizontal_padding_; |
123 | 165 |
124 DISALLOW_COPY_AND_ASSIGN(TouchHandle); | 166 DISALLOW_COPY_AND_ASSIGN(TouchHandle); |
125 }; | 167 }; |
126 | 168 |
127 } // namespace ui | 169 } // namespace ui |
128 | 170 |
129 #endif // UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ | 171 #endif // UI_TOUCH_SELECTION_TOUCH_HANDLE_H_ |
OLD | NEW |