Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / touch_selection / touch_selection_controller.cc
blob2e18037c658c9b0f7fc02d66e5a8471e8589d0a2
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 #include "ui/touch_selection/touch_selection_controller.h"
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram_macros.h"
11 namespace ui {
12 namespace {
14 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) {
15 gfx::Vector2dF line_offset =
16 gfx::ScaleVector2d(bound.edge_top() - bound.edge_bottom(), 0.5f);
17 // An offset of 5 DIPs is sufficient for most line sizes. For small lines,
18 // using half the line height avoids synthesizing a point on a line above
19 // (or below) the intended line.
20 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f);
21 line_offset.SetToMin(kMaxLineOffset);
22 line_offset.SetToMax(-kMaxLineOffset);
23 return line_offset;
26 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) {
27 switch (type) {
28 case SelectionBound::LEFT:
29 return TouchHandleOrientation::LEFT;
30 case SelectionBound::RIGHT:
31 return TouchHandleOrientation::RIGHT;
32 case SelectionBound::CENTER:
33 return TouchHandleOrientation::CENTER;
34 case SelectionBound::EMPTY:
35 return TouchHandleOrientation::UNDEFINED;
37 NOTREACHED() << "Invalid selection bound type: " << type;
38 return TouchHandleOrientation::UNDEFINED;
41 } // namespace
43 TouchSelectionController::TouchSelectionController(
44 TouchSelectionControllerClient* client,
45 base::TimeDelta tap_timeout,
46 float tap_slop,
47 bool show_on_tap_for_empty_editable)
48 : client_(client),
49 tap_timeout_(tap_timeout),
50 tap_slop_(tap_slop),
51 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable),
52 response_pending_input_event_(INPUT_EVENT_TYPE_NONE),
53 start_orientation_(TouchHandleOrientation::UNDEFINED),
54 end_orientation_(TouchHandleOrientation::UNDEFINED),
55 is_insertion_active_(false),
56 activate_insertion_automatically_(false),
57 is_selection_active_(false),
58 activate_selection_automatically_(false),
59 selection_empty_(false),
60 selection_editable_(false),
61 temporarily_hidden_(false),
62 selection_handle_dragged_(false) {
63 DCHECK(client_);
66 TouchSelectionController::~TouchSelectionController() {
69 void TouchSelectionController::OnSelectionBoundsChanged(
70 const SelectionBound& start,
71 const SelectionBound& end) {
72 if (start == start_ && end_ == end)
73 return;
75 start_ = start;
76 end_ = end;
77 start_orientation_ = ToTouchHandleOrientation(start_.type());
78 end_orientation_ = ToTouchHandleOrientation(end_.type());
80 if (!activate_selection_automatically_ &&
81 !activate_insertion_automatically_) {
82 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_);
83 return;
86 // Ensure that |response_pending_input_event_| is cleared after the method
87 // completes, while also making its current value available for the duration
88 // of the call.
89 InputEventType causal_input_event = response_pending_input_event_;
90 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE;
91 base::AutoReset<InputEventType> auto_reset_response_pending_input_event(
92 &response_pending_input_event_, causal_input_event);
94 const bool is_selection_dragging =
95 is_selection_active_ && (start_selection_handle_->is_dragging() ||
96 end_selection_handle_->is_dragging());
98 // It's possible that the bounds temporarily overlap while a selection handle
99 // is being dragged, incorrectly reporting a CENTER orientation.
100 // TODO(jdduke): This safeguard is racy, as it's possible the delayed response
101 // from handle positioning occurs *after* the handle dragging has ceased.
102 // Instead, prevent selection -> insertion transitions without an intervening
103 // action or selection clearing of some sort, crbug.com/392696.
104 if (is_selection_dragging) {
105 if (start_orientation_ == TouchHandleOrientation::CENTER)
106 start_orientation_ = start_selection_handle_->orientation();
107 if (end_orientation_ == TouchHandleOrientation::CENTER)
108 end_orientation_ = end_selection_handle_->orientation();
111 if (GetStartPosition() != GetEndPosition() ||
112 (is_selection_dragging &&
113 start_orientation_ != TouchHandleOrientation::UNDEFINED &&
114 end_orientation_ != TouchHandleOrientation::UNDEFINED)) {
115 OnSelectionChanged();
116 return;
119 if (start_orientation_ == TouchHandleOrientation::CENTER &&
120 selection_editable_) {
121 OnInsertionChanged();
122 return;
125 HideAndDisallowShowingAutomatically();
128 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) {
129 if (is_insertion_active_) {
130 DCHECK(insertion_handle_);
131 return insertion_handle_->WillHandleTouchEvent(event);
134 if (is_selection_active_) {
135 DCHECK(start_selection_handle_);
136 DCHECK(end_selection_handle_);
137 if (start_selection_handle_->is_dragging())
138 return start_selection_handle_->WillHandleTouchEvent(event);
140 if (end_selection_handle_->is_dragging())
141 return end_selection_handle_->WillHandleTouchEvent(event);
143 const gfx::PointF event_pos(event.GetX(), event.GetY());
144 if ((event_pos - GetStartPosition()).LengthSquared() <=
145 (event_pos - GetEndPosition()).LengthSquared())
146 return start_selection_handle_->WillHandleTouchEvent(event);
147 else
148 return end_selection_handle_->WillHandleTouchEvent(event);
151 return false;
154 void TouchSelectionController::OnLongPressEvent() {
155 response_pending_input_event_ = LONG_PRESS;
156 ShowSelectionHandlesAutomatically();
157 ShowInsertionHandleAutomatically();
158 ResetCachedValuesIfInactive();
161 void TouchSelectionController::AllowShowingFromCurrentSelection() {
162 if (is_selection_active_ || is_insertion_active_)
163 return;
165 activate_selection_automatically_ = true;
166 activate_insertion_automatically_ = true;
167 if (GetStartPosition() != GetEndPosition())
168 OnSelectionChanged();
169 else if (start_orientation_ == TouchHandleOrientation::CENTER &&
170 selection_editable_)
171 OnInsertionChanged();
174 void TouchSelectionController::OnTapEvent() {
175 response_pending_input_event_ = TAP;
176 ShowInsertionHandleAutomatically();
177 if (selection_empty_ && !show_on_tap_for_empty_editable_)
178 DeactivateInsertion();
179 ResetCachedValuesIfInactive();
182 void TouchSelectionController::HideAndDisallowShowingAutomatically() {
183 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE;
184 DeactivateInsertion();
185 DeactivateSelection();
186 activate_insertion_automatically_ = false;
187 activate_selection_automatically_ = false;
190 void TouchSelectionController::SetTemporarilyHidden(bool hidden) {
191 if (temporarily_hidden_ == hidden)
192 return;
193 temporarily_hidden_ = hidden;
195 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true);
196 if (is_selection_active_) {
197 start_selection_handle_->SetVisible(GetStartVisible(), animation_style);
198 end_selection_handle_->SetVisible(GetEndVisible(), animation_style);
200 if (is_insertion_active_)
201 insertion_handle_->SetVisible(GetStartVisible(), animation_style);
204 void TouchSelectionController::OnSelectionEditable(bool editable) {
205 if (selection_editable_ == editable)
206 return;
207 selection_editable_ = editable;
208 ResetCachedValuesIfInactive();
209 if (!selection_editable_)
210 DeactivateInsertion();
213 void TouchSelectionController::OnSelectionEmpty(bool empty) {
214 if (selection_empty_ == empty)
215 return;
216 selection_empty_ = empty;
217 ResetCachedValuesIfInactive();
220 bool TouchSelectionController::Animate(base::TimeTicks frame_time) {
221 if (is_insertion_active_)
222 return insertion_handle_->Animate(frame_time);
224 if (is_selection_active_) {
225 bool needs_animate = start_selection_handle_->Animate(frame_time);
226 needs_animate |= end_selection_handle_->Animate(frame_time);
227 return needs_animate;
230 return false;
233 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) {
234 if (&handle == insertion_handle_.get()) {
235 client_->OnSelectionEvent(INSERTION_DRAG_STARTED, handle.position());
236 return;
239 gfx::PointF base, extent;
240 if (&handle == start_selection_handle_.get()) {
241 base = end_selection_handle_->position() + GetEndLineOffset();
242 extent = start_selection_handle_->position() + GetStartLineOffset();
243 } else {
244 base = start_selection_handle_->position() + GetStartLineOffset();
245 extent = end_selection_handle_->position() + GetEndLineOffset();
247 selection_handle_dragged_ = true;
249 // When moving the handle we want to move only the extent point. Before doing
250 // so we must make sure that the base point is set correctly.
251 client_->SelectBetweenCoordinates(base, extent);
253 client_->OnSelectionEvent(SELECTION_DRAG_STARTED, handle.position());
256 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle,
257 const gfx::PointF& position) {
258 // As the position corresponds to the bottom left point of the selection
259 // bound, offset it by half the corresponding line height.
260 gfx::Vector2dF line_offset = &handle == start_selection_handle_.get()
261 ? GetStartLineOffset()
262 : GetEndLineOffset();
263 gfx::PointF line_position = position + line_offset;
264 if (&handle == insertion_handle_.get()) {
265 client_->MoveCaret(line_position);
266 } else {
267 client_->MoveRangeSelectionExtent(line_position);
271 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) {
272 if (&handle == insertion_handle_.get())
273 client_->OnSelectionEvent(INSERTION_DRAG_STOPPED, handle.position());
274 else
275 client_->OnSelectionEvent(SELECTION_DRAG_STOPPED, handle.position());
278 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) {
279 if (insertion_handle_ && &handle == insertion_handle_.get())
280 client_->OnSelectionEvent(INSERTION_TAPPED, handle.position());
283 void TouchSelectionController::SetNeedsAnimate() {
284 client_->SetNeedsAnimate();
287 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() {
288 return client_->CreateDrawable();
291 base::TimeDelta TouchSelectionController::GetTapTimeout() const {
292 return tap_timeout_;
295 float TouchSelectionController::GetTapSlop() const {
296 return tap_slop_;
299 void TouchSelectionController::ShowInsertionHandleAutomatically() {
300 if (activate_insertion_automatically_)
301 return;
302 activate_insertion_automatically_ = true;
303 ResetCachedValuesIfInactive();
306 void TouchSelectionController::ShowSelectionHandlesAutomatically() {
307 if (activate_selection_automatically_)
308 return;
309 activate_selection_automatically_ = true;
310 ResetCachedValuesIfInactive();
313 void TouchSelectionController::OnInsertionChanged() {
314 DeactivateSelection();
316 if (response_pending_input_event_ == TAP && selection_empty_ &&
317 !show_on_tap_for_empty_editable_) {
318 HideAndDisallowShowingAutomatically();
319 return;
322 if (!activate_insertion_automatically_)
323 return;
325 const bool was_active = is_insertion_active_;
326 const gfx::PointF position = GetStartPosition();
327 if (!is_insertion_active_)
328 ActivateInsertion();
329 else
330 client_->OnSelectionEvent(INSERTION_MOVED, position);
332 insertion_handle_->SetVisible(GetStartVisible(),
333 GetAnimationStyle(was_active));
334 insertion_handle_->SetPosition(position);
337 void TouchSelectionController::OnSelectionChanged() {
338 DeactivateInsertion();
340 if (!activate_selection_automatically_)
341 return;
343 const bool was_active = is_selection_active_;
344 ActivateSelection();
346 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active);
347 start_selection_handle_->SetVisible(GetStartVisible(), animation);
348 end_selection_handle_->SetVisible(GetEndVisible(), animation);
350 start_selection_handle_->SetPosition(GetStartPosition());
351 end_selection_handle_->SetPosition(GetEndPosition());
354 void TouchSelectionController::ActivateInsertion() {
355 DCHECK(!is_selection_active_);
357 if (!insertion_handle_)
358 insertion_handle_.reset(
359 new TouchHandle(this, TouchHandleOrientation::CENTER));
361 if (!is_insertion_active_) {
362 is_insertion_active_ = true;
363 insertion_handle_->SetEnabled(true);
364 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition());
368 void TouchSelectionController::DeactivateInsertion() {
369 if (!is_insertion_active_)
370 return;
371 DCHECK(insertion_handle_);
372 is_insertion_active_ = false;
373 insertion_handle_->SetEnabled(false);
374 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF());
377 void TouchSelectionController::ActivateSelection() {
378 DCHECK(!is_insertion_active_);
380 if (!start_selection_handle_) {
381 start_selection_handle_.reset(new TouchHandle(this, start_orientation_));
382 } else {
383 start_selection_handle_->SetEnabled(true);
384 start_selection_handle_->SetOrientation(start_orientation_);
387 if (!end_selection_handle_) {
388 end_selection_handle_.reset(new TouchHandle(this, end_orientation_));
389 } else {
390 end_selection_handle_->SetEnabled(true);
391 end_selection_handle_->SetOrientation(end_orientation_);
394 // As a long press received while a selection is already active may trigger
395 // an entirely new selection, notify the client but avoid sending an
396 // intervening SELECTION_CLEARED update to avoid unnecessary state changes.
397 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) {
398 if (is_selection_active_) {
399 // The active selection session finishes with the start of the new one.
400 LogSelectionEnd();
402 is_selection_active_ = true;
403 selection_handle_dragged_ = false;
404 selection_start_time_ = base::TimeTicks::Now();
405 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE;
406 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition());
410 void TouchSelectionController::DeactivateSelection() {
411 if (!is_selection_active_)
412 return;
413 DCHECK(start_selection_handle_);
414 DCHECK(end_selection_handle_);
415 LogSelectionEnd();
416 start_selection_handle_->SetEnabled(false);
417 end_selection_handle_->SetEnabled(false);
418 is_selection_active_ = false;
419 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF());
422 void TouchSelectionController::ResetCachedValuesIfInactive() {
423 if (is_selection_active_ || is_insertion_active_)
424 return;
425 start_ = SelectionBound();
426 end_ = SelectionBound();
427 start_orientation_ = TouchHandleOrientation::UNDEFINED;
428 end_orientation_ = TouchHandleOrientation::UNDEFINED;
431 const gfx::PointF& TouchSelectionController::GetStartPosition() const {
432 return start_.edge_bottom();
435 const gfx::PointF& TouchSelectionController::GetEndPosition() const {
436 return end_.edge_bottom();
439 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const {
440 return ComputeLineOffsetFromBottom(start_);
443 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const {
444 return ComputeLineOffsetFromBottom(end_);
447 bool TouchSelectionController::GetStartVisible() const {
448 return start_.visible() && !temporarily_hidden_;
451 bool TouchSelectionController::GetEndVisible() const {
452 return end_.visible() && !temporarily_hidden_;
455 TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle(
456 bool was_active) const {
457 return was_active && client_->SupportsAnimation()
458 ? TouchHandle::ANIMATION_SMOOTH
459 : TouchHandle::ANIMATION_NONE;
462 void TouchSelectionController::LogSelectionEnd() {
463 // TODO(mfomitchev): Once we are able to tell the difference between
464 // 'successful' and 'unsuccessful' selections - log
465 // Event.TouchSelection.Duration instead and get rid of
466 // Event.TouchSelectionD.WasDraggeduration.
467 if (selection_handle_dragged_) {
468 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_;
469 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration",
470 duration,
471 base::TimeDelta::FromMilliseconds(500),
472 base::TimeDelta::FromSeconds(60),
473 60);
477 } // namespace ui