| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/touch_selection/touch_selection_controller.h" | 5 #include "ui/touch_selection/touch_selection_controller.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 TouchSelectionController::TouchSelectionController( | 43 TouchSelectionController::TouchSelectionController( |
| 44 TouchSelectionControllerClient* client, | 44 TouchSelectionControllerClient* client, |
| 45 base::TimeDelta tap_timeout, | 45 base::TimeDelta tap_timeout, |
| 46 float tap_slop, | 46 float tap_slop, |
| 47 bool show_on_tap_for_empty_editable) | 47 bool show_on_tap_for_empty_editable) |
| 48 : client_(client), | 48 : client_(client), |
| 49 tap_timeout_(tap_timeout), | 49 tap_timeout_(tap_timeout), |
| 50 tap_slop_(tap_slop), | 50 tap_slop_(tap_slop), |
| 51 force_next_update_(false), |
| 51 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), | 52 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), |
| 52 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), | 53 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), |
| 53 start_orientation_(TouchHandleOrientation::UNDEFINED), | 54 start_orientation_(TouchHandleOrientation::UNDEFINED), |
| 54 end_orientation_(TouchHandleOrientation::UNDEFINED), | 55 end_orientation_(TouchHandleOrientation::UNDEFINED), |
| 55 active_status_(INACTIVE), | 56 active_status_(INACTIVE), |
| 56 activate_insertion_automatically_(false), | 57 activate_insertion_automatically_(false), |
| 57 activate_selection_automatically_(false), | 58 activate_selection_automatically_(false), |
| 58 selection_empty_(false), | 59 selection_empty_(false), |
| 59 selection_editable_(false), | 60 selection_editable_(false), |
| 60 temporarily_hidden_(false), | 61 temporarily_hidden_(false), |
| 61 selection_handle_dragged_(false) { | 62 selection_handle_dragged_(false) { |
| 62 DCHECK(client_); | 63 DCHECK(client_); |
| 63 } | 64 } |
| 64 | 65 |
| 65 TouchSelectionController::~TouchSelectionController() { | 66 TouchSelectionController::~TouchSelectionController() { |
| 66 } | 67 } |
| 67 | 68 |
| 68 void TouchSelectionController::OnSelectionBoundsChanged( | 69 void TouchSelectionController::OnSelectionBoundsChanged( |
| 69 const SelectionBound& start, | 70 const SelectionBound& start, |
| 70 const SelectionBound& end) { | 71 const SelectionBound& end) { |
| 71 if (start == start_ && end_ == end) | 72 if (!force_next_update_ && start == start_ && end_ == end) |
| 72 return; | 73 return; |
| 73 | 74 |
| 74 start_ = start; | 75 start_ = start; |
| 75 end_ = end; | 76 end_ = end; |
| 76 start_orientation_ = ToTouchHandleOrientation(start_.type()); | 77 start_orientation_ = ToTouchHandleOrientation(start_.type()); |
| 77 end_orientation_ = ToTouchHandleOrientation(end_.type()); | 78 end_orientation_ = ToTouchHandleOrientation(end_.type()); |
| 79 force_next_update_ = false; |
| 78 | 80 |
| 79 if (!activate_selection_automatically_ && | 81 if (!activate_selection_automatically_ && |
| 80 !activate_insertion_automatically_) { | 82 !activate_insertion_automatically_) { |
| 81 DCHECK_EQ(INACTIVE, active_status_); | 83 DCHECK_EQ(INACTIVE, active_status_); |
| 82 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); | 84 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); |
| 83 return; | 85 return; |
| 84 } | 86 } |
| 85 | 87 |
| 86 // Ensure that |response_pending_input_event_| is cleared after the method | 88 // Ensure that |response_pending_input_event_| is cleared after the method |
| 87 // completes, while also making its current value available for the duration | 89 // completes, while also making its current value available for the duration |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 return end_selection_handle_->WillHandleTouchEvent(event); | 150 return end_selection_handle_->WillHandleTouchEvent(event); |
| 149 } | 151 } |
| 150 | 152 |
| 151 return false; | 153 return false; |
| 152 } | 154 } |
| 153 | 155 |
| 154 void TouchSelectionController::OnLongPressEvent() { | 156 void TouchSelectionController::OnLongPressEvent() { |
| 155 response_pending_input_event_ = LONG_PRESS; | 157 response_pending_input_event_ = LONG_PRESS; |
| 156 ShowSelectionHandlesAutomatically(); | 158 ShowSelectionHandlesAutomatically(); |
| 157 ShowInsertionHandleAutomatically(); | 159 ShowInsertionHandleAutomatically(); |
| 158 ResetCachedValuesIfInactive(); | 160 ForceNextUpdateIfInactive(); |
| 159 } | 161 } |
| 160 | 162 |
| 161 void TouchSelectionController::AllowShowingFromCurrentSelection() { | 163 void TouchSelectionController::AllowShowingFromCurrentSelection() { |
| 162 if (active_status_ != INACTIVE) | 164 if (active_status_ != INACTIVE) |
| 163 return; | 165 return; |
| 164 | 166 |
| 165 activate_selection_automatically_ = true; | 167 activate_selection_automatically_ = true; |
| 166 activate_insertion_automatically_ = true; | 168 activate_insertion_automatically_ = true; |
| 167 if (GetStartPosition() != GetEndPosition()) { | 169 if (GetStartPosition() != GetEndPosition()) { |
| 168 OnSelectionChanged(); | 170 OnSelectionChanged(); |
| 169 } else if (start_orientation_ == TouchHandleOrientation::CENTER && | 171 } else if (start_orientation_ == TouchHandleOrientation::CENTER && |
| 170 selection_editable_) { | 172 selection_editable_) { |
| 171 OnInsertionChanged(); | 173 OnInsertionChanged(); |
| 172 } | 174 } |
| 173 } | 175 } |
| 174 | 176 |
| 175 void TouchSelectionController::OnTapEvent() { | 177 void TouchSelectionController::OnTapEvent() { |
| 176 response_pending_input_event_ = TAP; | 178 response_pending_input_event_ = TAP; |
| 177 if (active_status_ != SELECTION_ACTIVE) | 179 if (active_status_ != SELECTION_ACTIVE) |
| 178 activate_selection_automatically_ = false; | 180 activate_selection_automatically_ = false; |
| 179 ShowInsertionHandleAutomatically(); | 181 ShowInsertionHandleAutomatically(); |
| 180 if (selection_empty_ && !show_on_tap_for_empty_editable_) | 182 if (selection_empty_ && !show_on_tap_for_empty_editable_) |
| 181 DeactivateInsertion(); | 183 DeactivateInsertion(); |
| 182 ResetCachedValuesIfInactive(); | 184 ForceNextUpdateIfInactive(); |
| 183 } | 185 } |
| 184 | 186 |
| 185 void TouchSelectionController::HideAndDisallowShowingAutomatically() { | 187 void TouchSelectionController::HideAndDisallowShowingAutomatically() { |
| 186 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | 188 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| 187 DeactivateInsertion(); | 189 DeactivateInsertion(); |
| 188 DeactivateSelection(); | 190 DeactivateSelection(); |
| 189 activate_insertion_automatically_ = false; | 191 activate_insertion_automatically_ = false; |
| 190 activate_selection_automatically_ = false; | 192 activate_selection_automatically_ = false; |
| 191 } | 193 } |
| 192 | 194 |
| 193 void TouchSelectionController::SetTemporarilyHidden(bool hidden) { | 195 void TouchSelectionController::SetTemporarilyHidden(bool hidden) { |
| 194 if (temporarily_hidden_ == hidden) | 196 if (temporarily_hidden_ == hidden) |
| 195 return; | 197 return; |
| 196 temporarily_hidden_ = hidden; | 198 temporarily_hidden_ = hidden; |
| 197 | 199 |
| 198 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); | 200 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
| 199 if (active_status_ == SELECTION_ACTIVE) { | 201 if (active_status_ == SELECTION_ACTIVE) { |
| 200 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); | 202 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
| 201 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); | 203 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
| 202 } else if (active_status_ == INSERTION_ACTIVE) { | 204 } else if (active_status_ == INSERTION_ACTIVE) { |
| 203 insertion_handle_->SetVisible(GetStartVisible(), animation_style); | 205 insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
| 204 } | 206 } |
| 205 } | 207 } |
| 206 | 208 |
| 207 void TouchSelectionController::OnSelectionEditable(bool editable) { | 209 void TouchSelectionController::OnSelectionEditable(bool editable) { |
| 208 if (selection_editable_ == editable) | 210 if (selection_editable_ == editable) |
| 209 return; | 211 return; |
| 210 selection_editable_ = editable; | 212 selection_editable_ = editable; |
| 211 ResetCachedValuesIfInactive(); | 213 ForceNextUpdateIfInactive(); |
| 212 if (!selection_editable_) | 214 if (!selection_editable_) |
| 213 DeactivateInsertion(); | 215 DeactivateInsertion(); |
| 214 } | 216 } |
| 215 | 217 |
| 216 void TouchSelectionController::OnSelectionEmpty(bool empty) { | 218 void TouchSelectionController::OnSelectionEmpty(bool empty) { |
| 217 if (selection_empty_ == empty) | 219 if (selection_empty_ == empty) |
| 218 return; | 220 return; |
| 219 selection_empty_ = empty; | 221 selection_empty_ = empty; |
| 220 ResetCachedValuesIfInactive(); | 222 ForceNextUpdateIfInactive(); |
| 221 } | 223 } |
| 222 | 224 |
| 223 bool TouchSelectionController::Animate(base::TimeTicks frame_time) { | 225 bool TouchSelectionController::Animate(base::TimeTicks frame_time) { |
| 224 if (active_status_ == INSERTION_ACTIVE) | 226 if (active_status_ == INSERTION_ACTIVE) |
| 225 return insertion_handle_->Animate(frame_time); | 227 return insertion_handle_->Animate(frame_time); |
| 226 | 228 |
| 227 if (active_status_ == SELECTION_ACTIVE) { | 229 if (active_status_ == SELECTION_ACTIVE) { |
| 228 bool needs_animate = start_selection_handle_->Animate(frame_time); | 230 bool needs_animate = start_selection_handle_->Animate(frame_time); |
| 229 needs_animate |= end_selection_handle_->Animate(frame_time); | 231 needs_animate |= end_selection_handle_->Animate(frame_time); |
| 230 return needs_animate; | 232 return needs_animate; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 } | 335 } |
| 334 | 336 |
| 335 float TouchSelectionController::GetTapSlop() const { | 337 float TouchSelectionController::GetTapSlop() const { |
| 336 return tap_slop_; | 338 return tap_slop_; |
| 337 } | 339 } |
| 338 | 340 |
| 339 void TouchSelectionController::ShowInsertionHandleAutomatically() { | 341 void TouchSelectionController::ShowInsertionHandleAutomatically() { |
| 340 if (activate_insertion_automatically_) | 342 if (activate_insertion_automatically_) |
| 341 return; | 343 return; |
| 342 activate_insertion_automatically_ = true; | 344 activate_insertion_automatically_ = true; |
| 343 ResetCachedValuesIfInactive(); | 345 ForceNextUpdateIfInactive(); |
| 344 } | 346 } |
| 345 | 347 |
| 346 void TouchSelectionController::ShowSelectionHandlesAutomatically() { | 348 void TouchSelectionController::ShowSelectionHandlesAutomatically() { |
| 347 if (activate_selection_automatically_) | 349 if (activate_selection_automatically_) |
| 348 return; | 350 return; |
| 349 activate_selection_automatically_ = true; | 351 activate_selection_automatically_ = true; |
| 350 ResetCachedValuesIfInactive(); | 352 ForceNextUpdateIfInactive(); |
| 351 } | 353 } |
| 352 | 354 |
| 353 void TouchSelectionController::OnInsertionChanged() { | 355 void TouchSelectionController::OnInsertionChanged() { |
| 354 DeactivateSelection(); | 356 DeactivateSelection(); |
| 355 | 357 |
| 356 if (response_pending_input_event_ == TAP && selection_empty_ && | 358 if (response_pending_input_event_ == TAP && selection_empty_ && |
| 357 !show_on_tap_for_empty_editable_) { | 359 !show_on_tap_for_empty_editable_) { |
| 358 HideAndDisallowShowingAutomatically(); | 360 HideAndDisallowShowingAutomatically(); |
| 359 return; | 361 return; |
| 360 } | 362 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 return; | 458 return; |
| 457 DCHECK(start_selection_handle_); | 459 DCHECK(start_selection_handle_); |
| 458 DCHECK(end_selection_handle_); | 460 DCHECK(end_selection_handle_); |
| 459 LogSelectionEnd(); | 461 LogSelectionEnd(); |
| 460 start_selection_handle_->SetEnabled(false); | 462 start_selection_handle_->SetEnabled(false); |
| 461 end_selection_handle_->SetEnabled(false); | 463 end_selection_handle_->SetEnabled(false); |
| 462 active_status_ = INACTIVE; | 464 active_status_ = INACTIVE; |
| 463 client_->OnSelectionEvent(SELECTION_CLEARED); | 465 client_->OnSelectionEvent(SELECTION_CLEARED); |
| 464 } | 466 } |
| 465 | 467 |
| 466 void TouchSelectionController::ResetCachedValuesIfInactive() { | 468 void TouchSelectionController::ForceNextUpdateIfInactive() { |
| 467 if (active_status_ != INACTIVE) | 469 if (active_status_ == INACTIVE) |
| 468 return; | 470 force_next_update_ = true; |
| 469 start_ = SelectionBound(); | |
| 470 end_ = SelectionBound(); | |
| 471 start_orientation_ = TouchHandleOrientation::UNDEFINED; | |
| 472 end_orientation_ = TouchHandleOrientation::UNDEFINED; | |
| 473 } | 471 } |
| 474 | 472 |
| 475 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { | 473 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { |
| 476 return ComputeLineOffsetFromBottom(start_); | 474 return ComputeLineOffsetFromBottom(start_); |
| 477 } | 475 } |
| 478 | 476 |
| 479 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { | 477 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { |
| 480 return ComputeLineOffsetFromBottom(end_); | 478 return ComputeLineOffsetFromBottom(end_); |
| 481 } | 479 } |
| 482 | 480 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 504 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 502 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
| 505 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 503 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
| 506 duration, | 504 duration, |
| 507 base::TimeDelta::FromMilliseconds(500), | 505 base::TimeDelta::FromMilliseconds(500), |
| 508 base::TimeDelta::FromSeconds(60), | 506 base::TimeDelta::FromSeconds(60), |
| 509 60); | 507 60); |
| 510 } | 508 } |
| 511 } | 509 } |
| 512 | 510 |
| 513 } // namespace ui | 511 } // namespace ui |
| OLD | NEW |