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

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

Issue 335943002: [Android] Composited selection handle rendering (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@input_native_handles_final
Patch Set: Update android_webview.gyp 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_selection_controller.h"
6
7 #include "base/logging.h"
8 #include "third_party/WebKit/public/web/WebInputEvent.h"
9
10 namespace content {
11
12 TouchSelectionController::TouchSelectionController(
13 TouchSelectionControllerClient* client)
14 : client_(client),
15 start_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED),
16 start_visible_(false),
17 end_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED),
18 end_visible_(false),
19 is_insertion_active_(false),
20 activate_insertion_automatically_(false),
21 is_selection_active_(false),
22 activate_selection_automatically_(false),
23 selection_editable_(false),
24 selection_editable_for_last_update_(false),
25 temporarily_hidden_(false) {
26 DCHECK(client_);
27 HideAndDisallowAutomaticShowing();
28 }
29
30 TouchSelectionController::~TouchSelectionController() {
31 }
32
33 void TouchSelectionController::OnSelectionBoundsChanged(
34 const gfx::RectF& start_rect,
35 TouchHandleOrientation start_orientation,
36 bool start_visible,
37 const gfx::RectF& end_rect,
38 TouchHandleOrientation end_orientation,
39 bool end_visible) {
40 if (!activate_selection_automatically_ && !activate_insertion_automatically_)
41 return;
42
43 if (start_rect_ == start_rect && end_rect_ == end_rect &&
44 start_orientation_ == start_orientation &&
45 end_orientation_ == end_orientation && start_visible_ == start_visible &&
46 end_visible_ == end_visible &&
47 selection_editable_ == selection_editable_for_last_update_)
48 return;
49
50 start_rect_ = start_rect;
51 start_orientation_ = start_orientation;
52 start_visible_ = start_visible;
53 end_rect_ = end_rect;
54 end_orientation_ = end_orientation;
55 end_visible_ = end_visible;
56 selection_editable_for_last_update_ = selection_editable_;
57
58 const bool is_selection_dragging =
59 is_selection_active_ && (start_selection_handle_->is_dragging() ||
60 end_selection_handle_->is_dragging());
61
62 // It's possible that the bounds temporarily overlap while a selection handle
63 // is being dragged, incorrectly reporting a CENTER orientation.
64 // TODO(jdduke): This safeguard is racy, as it's possible the delayed response
65 // from handle positioning occurs *after* the handle dragging has ceased.
66 // Instead, prevent selection -> insertion transitions without an intervening
67 // action or selection clearing of some sort, crbug.com/392696.
68 if (is_selection_dragging) {
69 if (start_orientation_ == TOUCH_HANDLE_CENTER)
70 start_orientation_ = start_selection_handle_->orientation();
71 if (end_orientation_ == TOUCH_HANDLE_CENTER)
72 end_orientation_ = end_selection_handle_->orientation();
73 }
74
75 const gfx::PointF start = GetStartPosition();
76 const gfx::PointF end = GetEndPosition();
77 if (start != end ||
78 (is_selection_dragging &&
79 start_orientation_ != TOUCH_HANDLE_ORIENTATION_UNDEFINED &&
80 end_orientation_ != TOUCH_HANDLE_ORIENTATION_UNDEFINED)) {
81 OnSelectionChanged();
82 return;
83 }
84
85 if (start_orientation_ == TOUCH_HANDLE_CENTER) {
86 OnInsertionChanged();
87 return;
88 }
89
90 HideAndDisallowAutomaticShowing();
91 }
92
93 bool TouchSelectionController::WillHandleTouchEvent(
94 const ui::MotionEvent& event) {
95 if (is_insertion_active_) {
96 DCHECK(insertion_handle_);
97 return insertion_handle_->WillHandleTouchEvent(event);
98 }
99
100 if (is_selection_active_) {
101 DCHECK(start_selection_handle_);
102 DCHECK(end_selection_handle_);
103 return start_selection_handle_->WillHandleTouchEvent(event) ||
104 end_selection_handle_->WillHandleTouchEvent(event);
105 }
106
107 return false;
108 }
109
110 void TouchSelectionController::ShowInsertionHandleAutomatically() {
111 if (activate_insertion_automatically_)
112 return;
113 activate_insertion_automatically_ = true;
114 if (!is_insertion_active_ && !is_selection_active_)
115 ResetCachedValues();
116 }
117
118 void TouchSelectionController::ShowSelectionHandlesAutomatically() {
119 if (activate_selection_automatically_)
120 return;
121 activate_selection_automatically_ = true;
122 if (!is_insertion_active_ && !is_selection_active_)
123 ResetCachedValues();
124 }
125
126 void TouchSelectionController::HideAndDisallowAutomaticShowing() {
127 DeactivateInsertion();
128 DeactivateSelection();
129 activate_insertion_automatically_ = false;
130 activate_selection_automatically_ = false;
131 }
132
133 void TouchSelectionController::SetTemporarilyHidden(bool hidden) {
134 if (temporarily_hidden_ == hidden)
135 return;
136 temporarily_hidden_ = hidden;
137
138 // If hiding, avoid animating to prevent "laggy" position updates.
139 TouchHandle::AnimationStyle animation_style =
140 hidden ? TouchHandle::ANIMATION_NONE : GetAnimationStyle(true);
141 if (is_selection_active_) {
142 start_selection_handle_->SetVisible(GetStartVisible(), animation_style);
143 end_selection_handle_->SetVisible(GetEndVisible(), animation_style);
144 }
145 if (is_insertion_active_)
146 insertion_handle_->SetVisible(GetStartVisible(), animation_style);
147 }
148
149 void TouchSelectionController::OnSelectionEditable(bool editable) {
150 if (selection_editable_ == editable)
151 return;
152 selection_editable_ = editable;
153 if (!selection_editable_)
154 DeactivateInsertion();
155 }
156
157 bool TouchSelectionController::Animate(base::TimeTicks frame_time) {
158 if (is_insertion_active_)
159 return insertion_handle_->Animate(frame_time);
160
161 if (is_selection_active_) {
162 bool needs_animate = start_selection_handle_->Animate(frame_time);
163 needs_animate |= end_selection_handle_->Animate(frame_time);
164 return needs_animate;
165 }
166
167 return false;
168 }
169
170 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) {
171 if (&handle == insertion_handle_.get())
172 return;
173
174 if (&handle == start_selection_handle_.get()) {
175 fixed_handle_position_ = end_selection_handle_->position() -
176 gfx::Vector2dF(0, GetEndLineHeight() / 2.f);
177 } else {
178 fixed_handle_position_ = start_selection_handle_->position() -
179 gfx::Vector2dF(0, GetStartLineHeight() / 2.f);
180 }
181 }
182
183 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle,
184 const gfx::PointF& position) {
185 // As the position corresponds to the bottom left point of the selection
186 // bound, offset it by half the corresponding line height.
187 float half_line_height = &handle == end_selection_handle_.get()
188 ? GetEndLineHeight() / 2.f
189 : GetStartLineHeight() / 2.f;
190 gfx::PointF line_position = position - gfx::Vector2dF(0, half_line_height);
191 if (&handle == insertion_handle_.get()) {
192 client_->MoveCaret(line_position);
193 } else {
194 client_->SelectBetweenCoordinates(fixed_handle_position_, line_position);
195 }
196 }
197
198 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) {
199 }
200
201 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) {
202 if (insertion_handle_ && &handle == insertion_handle_.get())
203 client_->OnSelectionEvent(INSERTION_TAPPED, GetStartPosition());
204 }
205
206 void TouchSelectionController::SetNeedsAnimate() {
207 client_->SetNeedsAnimate();
208 }
209
210 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() {
211 return client_->CreateDrawable();
212 }
213
214 void TouchSelectionController::OnInsertionChanged() {
215 DeactivateSelection();
216
217 if (!activate_insertion_automatically_ || !selection_editable_)
218 return;
219
220 const bool was_active = is_insertion_active_;
221 const gfx::PointF position = GetStartPosition();
222 if (!is_insertion_active_)
223 ActivateInsertion();
224 else
225 client_->OnSelectionEvent(INSERTION_MOVED, position);
226
227 insertion_handle_->SetVisible(GetStartVisible(),
228 GetAnimationStyle(was_active));
229 insertion_handle_->SetPosition(position);
230 }
231
232 void TouchSelectionController::OnSelectionChanged() {
233 DeactivateInsertion();
234
235 if (!activate_selection_automatically_)
236 return;
237
238 const bool was_active = is_selection_active_;
239 ActivateSelection();
240
241 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active);
242 start_selection_handle_->SetVisible(GetStartVisible(), animation);
243 end_selection_handle_->SetVisible(GetEndVisible(), animation);
244
245 start_selection_handle_->SetPosition(GetStartPosition());
246 end_selection_handle_->SetPosition(GetEndPosition());
247 }
248
249 void TouchSelectionController::ActivateInsertion() {
250 DCHECK(!is_selection_active_);
251
252 if (!insertion_handle_)
253 insertion_handle_.reset(new TouchHandle(this, TOUCH_HANDLE_CENTER));
254
255 if (!is_insertion_active_) {
256 is_insertion_active_ = true;
257 insertion_handle_->SetEnabled(true);
258 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition());
259 }
260 }
261
262 void TouchSelectionController::DeactivateInsertion() {
263 if (!is_insertion_active_)
264 return;
265 DCHECK(insertion_handle_);
266 is_insertion_active_ = false;
267 insertion_handle_->SetEnabled(false);
268 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF());
269 }
270
271 void TouchSelectionController::ActivateSelection() {
272 DCHECK(!is_insertion_active_);
273
274 if (!start_selection_handle_) {
275 start_selection_handle_.reset(new TouchHandle(this, start_orientation_));
276 } else {
277 start_selection_handle_->SetEnabled(true);
278 start_selection_handle_->SetOrientation(start_orientation_);
279 }
280
281 if (!end_selection_handle_) {
282 end_selection_handle_.reset(new TouchHandle(this, end_orientation_));
283 } else {
284 end_selection_handle_->SetEnabled(true);
285 end_selection_handle_->SetOrientation(end_orientation_);
286 }
287
288 if (!is_selection_active_) {
289 is_selection_active_ = true;
290 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition());
291 }
292 }
293
294 void TouchSelectionController::DeactivateSelection() {
295 if (!is_selection_active_)
296 return;
297 DCHECK(start_selection_handle_);
298 DCHECK(end_selection_handle_);
299 start_selection_handle_->SetEnabled(false);
300 end_selection_handle_->SetEnabled(false);
301 is_selection_active_ = false;
302 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF());
303 }
304
305 void TouchSelectionController::ResetCachedValues() {
306 start_rect_ = gfx::RectF();
307 end_rect_ = gfx::RectF();
308 start_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
309 end_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
310 start_visible_ = false;
311 end_visible_ = false;
312 selection_editable_for_last_update_ = false;
313 }
314
315 gfx::PointF TouchSelectionController::GetStartPosition() const {
316 return start_rect_.bottom_left();
317 }
318
319 gfx::PointF TouchSelectionController::GetEndPosition() const {
320 return end_rect_.bottom_left();
321 }
322
323 float TouchSelectionController::GetStartLineHeight() const {
324 return start_rect_.height();
325 }
326
327 float TouchSelectionController::GetEndLineHeight() const {
328 return end_rect_.height();
329 }
330
331 bool TouchSelectionController::GetStartVisible() const {
332 return start_visible_ && !temporarily_hidden_;
333 }
334
335 bool TouchSelectionController::GetEndVisible() const {
336 return end_visible_ && !temporarily_hidden_;
337 }
338
339 TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle(
340 bool was_active) const {
341 return was_active && client_->SupportsAnimation()
342 ? TouchHandle::ANIMATION_SMOOTH
343 : TouchHandle::ANIMATION_NONE;
344 }
345
346 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698