blob: ef56e1a8476d1e9e40ad53b47c07371aa1e3c3ce [file] [log] [blame]
[email protected]3a10aee2014-07-21 19:58:441// 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
mohsenf837da7c2014-12-09 19:01:345#ifndef UI_TOUCH_SELECTION_TOUCH_HANDLE_H_
6#define UI_TOUCH_SELECTION_TOUCH_HANDLE_H_
[email protected]3a10aee2014-07-21 19:58:447
danakj25c52c32016-04-12 21:51:088#include <memory>
9
Keishi Hattori0e45c022021-11-27 09:25:5210#include "base/memory/raw_ptr.h"
[email protected]3a10aee2014-07-21 19:58:4411#include "base/time/time.h"
[email protected]3a10aee2014-07-21 19:58:4412#include "ui/events/gesture_detection/motion_event.h"
13#include "ui/gfx/geometry/point_f.h"
[email protected]ba46e5652014-07-31 21:12:5414#include "ui/gfx/geometry/rect_f.h"
[email protected]3a10aee2014-07-21 19:58:4415#include "ui/gfx/geometry/vector2d_f.h"
avi.nitk083c38c2015-03-02 06:47:3616#include "ui/touch_selection/touch_handle_orientation.h"
jdduke3f2a3ab2015-06-02 19:01:0217#include "ui/touch_selection/touch_selection_draggable.h"
mohsenf837da7c2014-12-09 19:01:3418#include "ui/touch_selection/ui_touch_selection_export.h"
[email protected]3a10aee2014-07-21 19:58:4419
mohsenf837da7c2014-12-09 19:01:3420namespace ui {
[email protected]3a10aee2014-07-21 19:58:4421
22class TouchHandle;
23
[email protected]3a10aee2014-07-21 19:58:4424// Interface through which |TouchHandle| delegates rendering-specific duties.
mohsenf837da7c2014-12-09 19:01:3425class UI_TOUCH_SELECTION_EXPORT TouchHandleDrawable {
[email protected]3a10aee2014-07-21 19:58:4426 public:
27 virtual ~TouchHandleDrawable() {}
avi.nitk348be682015-10-05 12:12:1728
29 // Sets whether the handle is active, allowing resource cleanup if necessary.
[email protected]3a10aee2014-07-21 19:58:4430 virtual void SetEnabled(bool enabled) = 0;
avi.nitk348be682015-10-05 12:12:1731
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]3a10aee2014-07-21 19:58:4445 virtual void SetAlpha(float alpha) = 0;
avi.nitk348be682015-10-05 12:12:1746
47 // Returns the visible bounds of the handle drawable.
48 // The bounds includes the transparent horizontal padding.
jdduke634a76e2014-12-16 21:56:3649 virtual gfx::RectF GetVisibleBounds() const = 0;
avi.nitk348be682015-10-05 12:12:1750
51 // Returns the transparent horizontal padding ratio of the handle drawable.
52 virtual float GetDrawableHorizontalPaddingRatio() const = 0;
[email protected]3a10aee2014-07-21 19:58:4453};
54
55// Interface through which |TouchHandle| communicates handle manipulation and
56// requests concrete drawable instances.
jdduke3f2a3ab2015-06-02 19:01:0257class UI_TOUCH_SELECTION_EXPORT TouchHandleClient
58 : public TouchSelectionDraggableClient {
[email protected]3a10aee2014-07-21 19:58:4459 public:
jdduke3f2a3ab2015-06-02 19:01:0260 ~TouchHandleClient() override {}
[email protected]3a10aee2014-07-21 19:58:4461 virtual void OnHandleTapped(const TouchHandle& handle) = 0;
62 virtual void SetNeedsAnimate() = 0;
danakj25c52c32016-04-12 21:51:0863 virtual std::unique_ptr<TouchHandleDrawable> CreateDrawable() = 0;
jdduke065d3a52015-08-31 18:57:3864 virtual base::TimeDelta GetMaxTapDuration() const = 0;
avi.nitk348be682015-10-05 12:12:1765 virtual bool IsAdaptiveHandleOrientationEnabled() const = 0;
[email protected]3a10aee2014-07-21 19:58:4466};
67
68// Responsible for displaying a selection or insertion handle for text
69// interaction.
jdduke3f2a3ab2015-06-02 19:01:0270class UI_TOUCH_SELECTION_EXPORT TouchHandle : public TouchSelectionDraggable {
[email protected]3a10aee2014-07-21 19:58:4471 public:
72 // The drawable will be enabled but invisible until otherwise specified.
avi.nitk348be682015-10-05 12:12:1773 TouchHandle(TouchHandleClient* client,
74 TouchHandleOrientation orientation,
75 const gfx::RectF& viewport_rect);
Peter Boströmc8c12352021-09-21 23:37:1576
77 TouchHandle(const TouchHandle&) = delete;
78 TouchHandle& operator=(const TouchHandle&) = delete;
79
jdduke3f2a3ab2015-06-02 19:01:0280 ~TouchHandle() override;
81
82 // TouchSelectionDraggable implementation.
83 bool WillHandleTouchEvent(const MotionEvent& event) override;
84 bool IsActive() const override;
[email protected]3a10aee2014-07-21 19:58:4485
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.nitk348be682015-10-05 12:12:1798 // Update the focus points for the handles. The handle will be positioned
99 // either |top| or |bottom| based on the mirror parameters.
[email protected]3a10aee2014-07-21 19:58:44100 // 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.nitk348be682015-10-05 12:12:17102 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]3a10aee2014-07-21 19:58:44106
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]3a10aee2014-07-21 19:58:44112 // 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
jdduke3628fb382015-04-15 00:08:41116 // 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.nitk348be682015-10-05 12:12:17121 // 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 Zhangfa804f32018-04-19 23:16:14126 // Set the handle to transparent. Handle will be set to opaque again in
127 // EndDrag() call.
128 void SetTransparent();
129
avi.nitk348be682015-10-05 12:12:17130 const gfx::PointF& focus_bottom() const { return focus_bottom_; }
[email protected]3a10aee2014-07-21 19:58:44131 TouchHandleOrientation orientation() const { return orientation_; }
AJITH KUMAR V576b1e32017-07-24 19:12:38132 float alpha() const { return alpha_; }
[email protected]3a10aee2014-07-21 19:58:44133
134 private:
avi.nitk348be682015-10-05 12:12:17135 gfx::PointF ComputeHandleOrigin() const;
[email protected]3a10aee2014-07-21 19:58:44136 void BeginDrag();
137 void EndDrag();
138 void BeginFade();
139 void EndFade();
140 void SetAlpha(float alpha);
avi.nitk348be682015-10-05 12:12:17141 void SetUpdateLayoutRequired();
[email protected]3a10aee2014-07-21 19:58:44142
danakj25c52c32016-04-12 21:51:08143 std::unique_ptr<TouchHandleDrawable> drawable_;
[email protected]3a10aee2014-07-21 19:58:44144
Arthur Sonzogni4c9cdac2022-06-13 17:22:56145 const raw_ptr<TouchHandleClient, DanglingUntriaged> client_;
[email protected]3a10aee2014-07-21 19:58:44146
avi.nitk348be682015-10-05 12:12:17147 gfx::PointF focus_bottom_;
148 gfx::PointF focus_top_;
149 gfx::RectF viewport_rect_;
[email protected]3a10aee2014-07-21 19:58:44150 TouchHandleOrientation orientation_;
151 TouchHandleOrientation deferred_orientation_;
152
153 gfx::PointF touch_down_position_;
jdduke3f2a3ab2015-06-02 19:01:02154 gfx::Vector2dF touch_drag_offset_;
[email protected]3a10aee2014-07-21 19:58:44155 base::TimeTicks touch_down_time_;
156
157 // Note that when a fade animation is active, |is_visible_| and |position_|
avi56603fe2015-09-18 21:10:50158 // may not reflect the actual visibility and position of the drawable. This
[email protected]3a10aee2014-07-21 19:58:44159 // 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_;
jdduke46595ef2014-08-30 01:34:50168 bool is_drag_within_tap_region_;
avi.nitk348be682015-10-05 12:12:17169 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]3a10aee2014-07-21 19:58:44175};
176
mohsenf837da7c2014-12-09 19:01:34177} // namespace ui
[email protected]3a10aee2014-07-21 19:58:44178
mohsenf837da7c2014-12-09 19:01:34179#endif // UI_TOUCH_SELECTION_TOUCH_HANDLE_H_