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