MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / ui / touch_selection / touch_handle.h
blobd9d861d527ab340648588115df4745347b33d369
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.
5 #ifndef UI_TOUCH_SELECTION_TOUCH_HANDLE_H_
6 #define UI_TOUCH_SELECTION_TOUCH_HANDLE_H_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "ui/events/gesture_detection/motion_event.h"
12 #include "ui/gfx/geometry/point_f.h"
13 #include "ui/gfx/geometry/rect_f.h"
14 #include "ui/gfx/geometry/vector2d_f.h"
15 #include "ui/touch_selection/touch_handle_orientation.h"
16 #include "ui/touch_selection/touch_selection_draggable.h"
17 #include "ui/touch_selection/ui_touch_selection_export.h"
19 namespace ui {
21 class TouchHandle;
23 // Interface through which |TouchHandle| delegates rendering-specific duties.
24 class UI_TOUCH_SELECTION_EXPORT TouchHandleDrawable {
25 public:
26 virtual ~TouchHandleDrawable() {}
27 virtual void SetEnabled(bool enabled) = 0;
28 virtual void SetOrientation(TouchHandleOrientation orientation) = 0;
29 virtual void SetAlpha(float alpha) = 0;
30 virtual void SetFocus(const gfx::PointF& position) = 0;
31 virtual gfx::RectF GetVisibleBounds() const = 0;
34 // Interface through which |TouchHandle| communicates handle manipulation and
35 // requests concrete drawable instances.
36 class UI_TOUCH_SELECTION_EXPORT TouchHandleClient
37 : public TouchSelectionDraggableClient {
38 public:
39 ~TouchHandleClient() override {}
40 virtual void OnHandleTapped(const TouchHandle& handle) = 0;
41 virtual void SetNeedsAnimate() = 0;
42 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0;
43 virtual base::TimeDelta GetMaxTapDuration() const = 0;
46 // Responsible for displaying a selection or insertion handle for text
47 // interaction.
48 class UI_TOUCH_SELECTION_EXPORT TouchHandle : public TouchSelectionDraggable {
49 public:
50 // The drawable will be enabled but invisible until otherwise specified.
51 TouchHandle(TouchHandleClient* client, TouchHandleOrientation orientation);
52 ~TouchHandle() override;
54 // TouchSelectionDraggable implementation.
55 bool WillHandleTouchEvent(const MotionEvent& event) override;
56 bool IsActive() const override;
58 // Sets whether the handle is active, allowing resource cleanup if necessary.
59 // If false, active animations or touch drag sequences will be cancelled.
60 // While disabled, manipulation is *explicitly not supported*, and may lead to
61 // undesirable and/or unstable side-effects. The handle can be safely
62 // re-enabled to allow continued operation.
63 void SetEnabled(bool enabled);
65 enum AnimationStyle { ANIMATION_NONE, ANIMATION_SMOOTH };
66 // Update the handle visibility, fading in/out according to |animation_style|.
67 // If an animation is in-progress, it will be overriden appropriately.
68 void SetVisible(bool visible, AnimationStyle animation_style);
70 // Update the handle placement to |position|.
71 // 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.
73 void SetPosition(const gfx::PointF& position);
75 // Update the handle visuals to |orientation|.
76 // Note: If the handle is being dragged, the orientation change will be
77 // deferred until the drag has ceased.
78 void SetOrientation(TouchHandleOrientation orientation);
80 // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
81 // Returns true if an animation is active and requires further ticking.
82 bool Animate(base::TimeTicks frame_time);
84 // 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,
86 // the bounds will be empty.
87 gfx::RectF GetVisibleBounds() const;
89 const gfx::PointF& position() const { return position_; }
90 TouchHandleOrientation orientation() const { return orientation_; }
92 private:
93 void BeginDrag();
94 void EndDrag();
95 void BeginFade();
96 void EndFade();
97 void SetAlpha(float alpha);
99 scoped_ptr<TouchHandleDrawable> drawable_;
101 TouchHandleClient* const client_;
103 gfx::PointF position_;
104 TouchHandleOrientation orientation_;
105 TouchHandleOrientation deferred_orientation_;
107 gfx::PointF touch_down_position_;
108 gfx::Vector2dF touch_drag_offset_;
109 base::TimeTicks touch_down_time_;
111 // Note that when a fade animation is active, |is_visible_| and |position_|
112 // may not reflect the actual visibilty and position of the drawable. This
113 // discrepancy is resolved either upon fade completion or cancellation.
114 base::TimeTicks fade_end_time_;
115 gfx::PointF fade_start_position_;
116 float alpha_;
117 bool animate_deferred_fade_;
119 bool enabled_;
120 bool is_visible_;
121 bool is_dragging_;
122 bool is_drag_within_tap_region_;
124 DISALLOW_COPY_AND_ASSIGN(TouchHandle);
127 } // namespace ui
129 #endif // UI_TOUCH_SELECTION_TOUCH_HANDLE_H_