Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1078)

Side by Side Diff: content/browser/renderer_host/input/touch_handle.cc

Issue 335943002: [Android] Composited selection handle rendering (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@input_native_handles_final
Patch Set: Clean up paste popup interaction Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "content/browser/renderer_host/input/touch_handle.h"
6
7 namespace content {
8
9 namespace {
10
11 // Maximum duration of a fade sequence.
12 const double kFadeDurationMs = 200;
13
14 // Maximum amount of travel for a fade sequence. This avoids handle "ghosting"
15 // when the handle is moving rapidly while the fade is active.
16 const double kFadeDistanceSquared = 20.f * 20.f;
17
18 } // namespace
19
20 // Responsible for rendering a selection or insertion handle for text editing.
21 TouchHandle::TouchHandle(TouchHandleClient* client,
22 TouchHandleOrientation orientation)
23 : drawable_(client->CreateDrawable()),
24 client_(client),
25 orientation_(orientation),
26 deferred_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED),
27 alpha_(0.f),
28 is_visible_(false),
29 is_dragging_(false) {
30 DCHECK_NE(orientation, TOUCH_HANDLE_ORIENTATION_UNDEFINED);
31 drawable_->SetOrientation(orientation_);
cjhopman 2014/07/09 22:29:12 Should these initial values be arguments to client
jdduke (slow) 2014/07/10 02:08:39 I'll push through the position_ for completeness.
32 drawable_->SetAlpha(alpha_);
33 drawable_->SetVisible(is_visible_);
34 }
35
36 TouchHandle::~TouchHandle() {
37 }
38
39 void TouchHandle::SetVisible(bool visible, AnimationStyle animation_style) {
40 if (is_visible_ == visible)
41 return;
42 is_visible_ = visible;
43 if (animation_style == ANIMATION_NONE) {
44 drawable_->SetVisible(visible);
45 CancelFade();
46 } else {
47 // The fade will be rescheduled once dragging completes.
48 if (!is_dragging_)
49 ScheduleFade();
50 }
51 }
52
53 void TouchHandle::SetPosition(const gfx::PointF& position) {
54 if (position_ == position)
55 return;
56 position_ = position;
57 // Avoid positioning handles outside their visible bounds during a fade.
58 if (!fade_end_time_.is_null() && !is_visible_)
59 return;
60 drawable_->SetFocus(position_);
61 }
62
63 void TouchHandle::SetOrientation(TouchHandleOrientation orientation) {
64 DCHECK_NE(orientation, TOUCH_HANDLE_ORIENTATION_UNDEFINED);
65 if (is_dragging_) {
66 deferred_orientation_ = orientation;
67 return;
68 }
69
70 deferred_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
71
72 if (orientation_ == orientation)
73 return;
74
75 orientation_ = orientation;
76 drawable_->SetOrientation(orientation);
77 }
78
79 void TouchHandle::Hide() {
80 SetVisible(false, ANIMATION_NONE);
81 CancelFade();
82 is_dragging_ = false;
83 }
84
85 bool TouchHandle::WillHandleTouchEvent(const ui::MotionEvent& event) {
86 if (!is_dragging_ && event.GetAction() != ui::MotionEvent::ACTION_DOWN)
87 return false;
88
89 switch (event.GetAction()) {
90 case ui::MotionEvent::ACTION_DOWN: {
91 gfx::PointF touch_position = gfx::PointF(event.GetX(), event.GetY());
92 if (!is_visible_ || !drawable_->ContainsPoint(touch_position))
93 return false;
94 touch_down_position_ = touch_position;
95 touch_to_focus_offset_ = position_ - touch_down_position_;
96 touch_down_time_ = event.GetEventTime();
97 BeginDrag();
98 } break;
99
100 case ui::MotionEvent::ACTION_MOVE: {
101 gfx::PointF new_position =
102 gfx::PointF(event.GetX(), event.GetY()) + touch_to_focus_offset_;
103 client_->OnHandleDragUpdate(*this, new_position);
104 } break;
105
106 case ui::MotionEvent::ACTION_UP: {
107 base::TimeDelta delay = event.GetEventTime() - touch_down_time_;
108 if (delay < base::TimeDelta::FromMilliseconds(180))
109 client_->OnHandleTapped(*this);
110
111 EndDrag();
112 } break;
113
114 case ui::MotionEvent::ACTION_CANCEL:
115 EndDrag();
116 break;
117
118 default:
119 break;
120 };
121 return true;
122 }
123
124 bool TouchHandle::Animate(base::TimeTicks frame_time) {
125 if (fade_end_time_ == base::TimeTicks())
126 return false;
127
128 float time_u =
129 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs;
130 float position_u =
131 (position_ - fade_start_position_).LengthSquared() / kFadeDistanceSquared;
132 float u = std::max(time_u, position_u);
133 SetAlpha(is_visible_ ? u : 1.f - u);
134
135 if (u >= 1.f) {
136 CancelFade();
137 return false;
138 }
139
140 return true;
141 }
142
143 void TouchHandle::BeginDrag() {
144 if (is_dragging_)
145 return;
146 is_dragging_ = true;
147 client_->OnHandleDragBegin(*this);
148 }
149
150 void TouchHandle::EndDrag() {
151 if (!is_dragging_)
152 return;
153
154 is_dragging_ = false;
155 client_->OnHandleDragEnd(*this);
156
157 if (deferred_orientation_ != TOUCH_HANDLE_ORIENTATION_UNDEFINED)
158 SetOrientation(deferred_orientation_);
159
160 ScheduleFade();
161 }
162
163 void TouchHandle::ScheduleFade() {
164 const float target_alpha = is_visible_ ? 1.f : 0.f;
165 if (target_alpha == alpha_)
166 return;
167
168 drawable_->SetVisible(true);
169 fade_end_time_ = base::TimeTicks::Now() +
170 base::TimeDelta::FromMillisecondsD(
171 kFadeDurationMs * std::abs(target_alpha - alpha_));
172 fade_start_position_ = position_;
173 client_->SetNeedsAnimate();
174 }
175
176 void TouchHandle::CancelFade() {
177 fade_end_time_ = base::TimeTicks();
178 SetAlpha(is_visible_ ? 1.f : 0.f);
179 drawable_->SetVisible(is_visible_);
180 drawable_->SetFocus(position_);
181 }
182
183 void TouchHandle::SetAlpha(float alpha) {
184 alpha = std::max(0.f, std::min(1.f, alpha));
185 if (alpha_ == alpha)
186 return;
187 alpha_ = alpha;
188 drawable_->SetAlpha(alpha);
189 }
190
191 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698