| 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_impl.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 #include "ui/gfx/geometry/rect.h" |
| 10 | 11 |
| 11 namespace ui { | 12 namespace ui { |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 15 // Delay before showing the quick menu, in milliseconds. |
| 16 //const int kQuickMenuDelayInMs = 100; |
| 17 |
| 14 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) { | 18 gfx::Vector2dF ComputeLineOffsetFromBottom(const SelectionBound& bound) { |
| 15 gfx::Vector2dF line_offset = | 19 gfx::Vector2dF line_offset = |
| 16 gfx::ScaleVector2d(bound.edge_top() - bound.edge_bottom(), 0.5f); | 20 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, | 21 // 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 | 22 // using half the line height avoids synthesizing a point on a line above |
| 19 // (or below) the intended line. | 23 // (or below) the intended line. |
| 20 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f); | 24 const gfx::Vector2dF kMaxLineOffset(5.f, 5.f); |
| 21 line_offset.SetToMin(kMaxLineOffset); | 25 line_offset.SetToMin(kMaxLineOffset); |
| 22 line_offset.SetToMax(-kMaxLineOffset); | 26 line_offset.SetToMax(-kMaxLineOffset); |
| 23 return line_offset; | 27 return line_offset; |
| 24 } | 28 } |
| 25 | 29 |
| 26 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { | 30 TouchHandleOrientation ToTouchHandleOrientation(SelectionBound::Type type) { |
| 27 switch (type) { | 31 switch (type) { |
| 28 case SelectionBound::LEFT: | 32 case SelectionBound::LEFT: |
| 29 return TouchHandleOrientation::LEFT; | 33 return TouchHandleOrientation::LEFT; |
| 30 case SelectionBound::RIGHT: | 34 case SelectionBound::RIGHT: |
| 31 return TouchHandleOrientation::RIGHT; | 35 return TouchHandleOrientation::RIGHT; |
| 32 case SelectionBound::CENTER: | 36 case SelectionBound::CENTER: |
| 33 return TouchHandleOrientation::CENTER; | 37 return TouchHandleOrientation::CENTER; |
| 34 case SelectionBound::EMPTY: | 38 case SelectionBound::EMPTY: |
| 35 return TouchHandleOrientation::UNDEFINED; | 39 return TouchHandleOrientation::UNDEFINED; |
| 36 } | 40 } |
| 37 NOTREACHED() << "Invalid selection bound type: " << type; | 41 NOTREACHED() << "Invalid selection bound type: " << type; |
| 38 return TouchHandleOrientation::UNDEFINED; | 42 return TouchHandleOrientation::UNDEFINED; |
| 39 } | 43 } |
| 40 | 44 |
| 45 void ClipPoint(gfx::PointF* point, const gfx::Rect& clip_rect) { |
| 46 point->SetToMax(clip_rect.origin()); |
| 47 point->SetToMin(clip_rect.bottom_right()); |
| 48 } |
| 49 |
| 50 void ClipSelectionBound(SelectionBound* bound, const gfx::Rect& clip_rect) { |
| 51 gfx::PointF edge_top = bound->edge_top(); |
| 52 gfx::PointF edge_bottom = bound->edge_bottom(); |
| 53 ClipPoint(&edge_top, clip_rect); |
| 54 ClipPoint(&edge_bottom, clip_rect); |
| 55 bound->SetEdge(edge_top, edge_bottom); |
| 56 } |
| 41 } // namespace | 57 } // namespace |
| 42 | 58 |
| 43 TouchSelectionController::TouchSelectionController( | 59 TouchSelectionControllerImpl::TouchSelectionControllerImpl( |
| 44 TouchSelectionControllerClient* client, | 60 TouchSelectionControllerClient* client, |
| 45 base::TimeDelta tap_timeout, | 61 base::TimeDelta tap_timeout, |
| 46 float tap_slop, | 62 float tap_slop, |
| 47 bool show_on_tap_for_empty_editable) | 63 bool show_on_tap_for_empty_editable) |
| 48 : client_(client), | 64 : client_(client), |
| 49 tap_timeout_(tap_timeout), | 65 tap_timeout_(tap_timeout), |
| 50 tap_slop_(tap_slop), | 66 tap_slop_(tap_slop), |
| 51 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), | 67 show_on_tap_for_empty_editable_(show_on_tap_for_empty_editable), |
| 52 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), | 68 response_pending_input_event_(INPUT_EVENT_TYPE_NONE), |
| 53 start_orientation_(TouchHandleOrientation::UNDEFINED), | 69 start_orientation_(TouchHandleOrientation::UNDEFINED), |
| 54 end_orientation_(TouchHandleOrientation::UNDEFINED), | 70 end_orientation_(TouchHandleOrientation::UNDEFINED), |
| 55 is_insertion_active_(false), | 71 is_insertion_active_(false), |
| 56 activate_insertion_automatically_(false), | 72 activate_insertion_automatically_(false), |
| 57 is_selection_active_(false), | 73 is_selection_active_(false), |
| 58 activate_selection_automatically_(false), | 74 activate_selection_automatically_(false), |
| 59 selection_empty_(false), | 75 selection_empty_(false), |
| 60 selection_editable_(false), | 76 selection_editable_(false), |
| 61 temporarily_hidden_(false), | 77 temporarily_hidden_(false), |
| 62 selection_handle_dragged_(false) { | 78 selection_handle_was_dragged_(false) { |
| 63 DCHECK(client_); | 79 DCHECK(client_); |
| 64 } | 80 } |
| 65 | 81 |
| 66 TouchSelectionController::~TouchSelectionController() { | 82 TouchSelectionControllerImpl::~TouchSelectionControllerImpl() { |
| 67 } | 83 } |
| 68 | 84 |
| 69 void TouchSelectionController::OnSelectionBoundsChanged( | 85 bool TouchSelectionControllerImpl::OnSelectionBoundsUpdated( |
| 70 const SelectionBound& start, | 86 const SelectionBound& start, |
| 71 const SelectionBound& end) { | 87 const SelectionBound& end) { |
| 72 if (start == start_ && end_ == end) | 88 if (start == start_ && end_ == end) |
| 73 return; | 89 return false; |
| 74 | 90 |
| 75 start_ = start; | 91 start_ = start; |
| 76 end_ = end; | 92 end_ = end; |
| 77 start_orientation_ = ToTouchHandleOrientation(start_.type()); | 93 start_orientation_ = ToTouchHandleOrientation(start_.type()); |
| 78 end_orientation_ = ToTouchHandleOrientation(end_.type()); | 94 end_orientation_ = ToTouchHandleOrientation(end_.type()); |
| 79 | 95 |
| 80 if (!activate_selection_automatically_ && | 96 if (!activate_selection_automatically_ && |
| 81 !activate_insertion_automatically_) { | 97 !activate_insertion_automatically_) { |
| 82 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); | 98 DCHECK_EQ(INPUT_EVENT_TYPE_NONE, response_pending_input_event_); |
| 83 return; | 99 return false; |
| 84 } | 100 } |
| 85 | 101 |
| 86 // Ensure that |response_pending_input_event_| is cleared after the method | 102 // Ensure that |response_pending_input_event_| is cleared after the method |
| 87 // completes, while also making its current value available for the duration | 103 // completes, while also making its current value available for the duration |
| 88 // of the call. | 104 // of the call. |
| 89 InputEventType causal_input_event = response_pending_input_event_; | 105 InputEventType causal_input_event = response_pending_input_event_; |
| 90 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | 106 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| 91 base::AutoReset<InputEventType> auto_reset_response_pending_input_event( | 107 base::AutoReset<InputEventType> auto_reset_response_pending_input_event( |
| 92 &response_pending_input_event_, causal_input_event); | 108 &response_pending_input_event_, causal_input_event); |
| 93 | 109 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 106 start_orientation_ = start_selection_handle_->orientation(); | 122 start_orientation_ = start_selection_handle_->orientation(); |
| 107 if (end_orientation_ == TouchHandleOrientation::CENTER) | 123 if (end_orientation_ == TouchHandleOrientation::CENTER) |
| 108 end_orientation_ = end_selection_handle_->orientation(); | 124 end_orientation_ = end_selection_handle_->orientation(); |
| 109 } | 125 } |
| 110 | 126 |
| 111 if (GetStartPosition() != GetEndPosition() || | 127 if (GetStartPosition() != GetEndPosition() || |
| 112 (is_selection_dragging && | 128 (is_selection_dragging && |
| 113 start_orientation_ != TouchHandleOrientation::UNDEFINED && | 129 start_orientation_ != TouchHandleOrientation::UNDEFINED && |
| 114 end_orientation_ != TouchHandleOrientation::UNDEFINED)) { | 130 end_orientation_ != TouchHandleOrientation::UNDEFINED)) { |
| 115 OnSelectionChanged(); | 131 OnSelectionChanged(); |
| 116 return; | 132 return true; |
| 117 } | 133 } |
| 118 | 134 |
| 119 if (start_orientation_ == TouchHandleOrientation::CENTER && | 135 if (start_orientation_ == TouchHandleOrientation::CENTER && |
| 120 selection_editable_) { | 136 selection_editable_) { |
| 121 OnInsertionChanged(); | 137 OnInsertionChanged(); |
| 122 return; | 138 return true; |
| 123 } | 139 } |
| 124 | 140 |
| 125 HideAndDisallowShowingAutomatically(); | 141 HideAndDisallowShowingAutomatically(); |
| 142 return true; |
| 126 } | 143 } |
| 127 | 144 |
| 128 bool TouchSelectionController::WillHandleTouchEvent(const MotionEvent& event) { | 145 bool TouchSelectionControllerImpl::WillHandleTouchEvent(const MotionEvent& event
) { |
| 129 if (is_insertion_active_) { | 146 if (is_insertion_active_) { |
| 130 DCHECK(insertion_handle_); | 147 DCHECK(insertion_handle_); |
| 131 return insertion_handle_->WillHandleTouchEvent(event); | 148 return insertion_handle_->WillHandleTouchEvent(event); |
| 132 } | 149 } |
| 133 | 150 |
| 134 if (is_selection_active_) { | 151 if (is_selection_active_) { |
| 135 DCHECK(start_selection_handle_); | 152 DCHECK(start_selection_handle_); |
| 136 DCHECK(end_selection_handle_); | 153 DCHECK(end_selection_handle_); |
| 137 if (start_selection_handle_->is_dragging()) | 154 if (start_selection_handle_->is_dragging()) |
| 138 return start_selection_handle_->WillHandleTouchEvent(event); | 155 return start_selection_handle_->WillHandleTouchEvent(event); |
| 139 | 156 |
| 140 if (end_selection_handle_->is_dragging()) | 157 if (end_selection_handle_->is_dragging()) |
| 141 return end_selection_handle_->WillHandleTouchEvent(event); | 158 return end_selection_handle_->WillHandleTouchEvent(event); |
| 142 | 159 |
| 143 const gfx::PointF event_pos(event.GetX(), event.GetY()); | 160 const gfx::PointF event_pos(event.GetX(), event.GetY()); |
| 144 if ((event_pos - GetStartPosition()).LengthSquared() <= | 161 if ((event_pos - GetStartPosition()).LengthSquared() <= |
| 145 (event_pos - GetEndPosition()).LengthSquared()) | 162 (event_pos - GetEndPosition()).LengthSquared()) |
| 146 return start_selection_handle_->WillHandleTouchEvent(event); | 163 return start_selection_handle_->WillHandleTouchEvent(event); |
| 147 else | 164 else |
| 148 return end_selection_handle_->WillHandleTouchEvent(event); | 165 return end_selection_handle_->WillHandleTouchEvent(event); |
| 149 } | 166 } |
| 150 | 167 |
| 151 return false; | 168 return false; |
| 152 } | 169 } |
| 153 | 170 |
| 154 void TouchSelectionController::OnLongPressEvent() { | 171 void TouchSelectionControllerImpl::OnLongPressEvent() { |
| 155 response_pending_input_event_ = LONG_PRESS; | 172 response_pending_input_event_ = LONG_PRESS; |
| 156 ShowSelectionHandlesAutomatically(); | 173 ShowSelectionHandlesAutomatically(); |
| 157 ShowInsertionHandleAutomatically(); | 174 ShowInsertionHandleAutomatically(); |
| 158 ResetCachedValuesIfInactive(); | 175 ResetCachedValuesIfInactive(); |
| 159 } | 176 } |
| 160 | 177 |
| 161 void TouchSelectionController::AllowShowingFromCurrentSelection() { | 178 void TouchSelectionControllerImpl::AllowShowingFromCurrentSelection() { |
| 162 if (is_selection_active_ || is_insertion_active_) | 179 if (is_selection_active_ || is_insertion_active_) |
| 163 return; | 180 return; |
| 164 | 181 |
| 165 activate_selection_automatically_ = true; | 182 activate_selection_automatically_ = true; |
| 166 activate_insertion_automatically_ = true; | 183 activate_insertion_automatically_ = true; |
| 167 if (GetStartPosition() != GetEndPosition()) | 184 if (GetStartPosition() != GetEndPosition()) |
| 168 OnSelectionChanged(); | 185 OnSelectionChanged(); |
| 169 else if (start_orientation_ == TouchHandleOrientation::CENTER && | 186 else if (start_orientation_ == TouchHandleOrientation::CENTER && |
| 170 selection_editable_) | 187 selection_editable_) |
| 171 OnInsertionChanged(); | 188 OnInsertionChanged(); |
| 172 } | 189 } |
| 173 | 190 |
| 174 void TouchSelectionController::OnTapEvent() { | 191 void TouchSelectionControllerImpl::OnTapEvent() { |
| 175 response_pending_input_event_ = TAP; | 192 response_pending_input_event_ = TAP; |
| 176 ShowInsertionHandleAutomatically(); | 193 ShowInsertionHandleAutomatically(); |
| 177 if (selection_empty_ && !show_on_tap_for_empty_editable_) | 194 if (selection_empty_ && !show_on_tap_for_empty_editable_) |
| 178 DeactivateInsertion(); | 195 DeactivateInsertion(); |
| 179 ResetCachedValuesIfInactive(); | 196 ResetCachedValuesIfInactive(); |
| 180 } | 197 } |
| 181 | 198 |
| 182 void TouchSelectionController::HideAndDisallowShowingAutomatically() { | 199 void TouchSelectionControllerImpl::HideAndDisallowShowingAutomatically() { |
| 183 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | 200 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| 184 DeactivateInsertion(); | 201 DeactivateInsertion(); |
| 185 DeactivateSelection(); | 202 DeactivateSelection(); |
| 186 activate_insertion_automatically_ = false; | 203 activate_insertion_automatically_ = false; |
| 187 activate_selection_automatically_ = false; | 204 activate_selection_automatically_ = false; |
| 205 //UpdateQuickMenu(); - DOn't think this is needed |
| 188 } | 206 } |
| 189 | 207 |
| 190 void TouchSelectionController::SetTemporarilyHidden(bool hidden) { | 208 void TouchSelectionControllerImpl::SetTemporarilyHidden(bool hidden) { |
| 191 if (temporarily_hidden_ == hidden) | 209 if (temporarily_hidden_ == hidden) |
| 192 return; | 210 return; |
| 193 temporarily_hidden_ = hidden; | 211 temporarily_hidden_ = hidden; |
| 194 | 212 |
| 195 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); | 213 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true); |
| 196 if (is_selection_active_) { | 214 if (is_selection_active_) { |
| 197 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); | 215 start_selection_handle_->SetVisible(GetStartVisible(), animation_style); |
| 198 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); | 216 end_selection_handle_->SetVisible(GetEndVisible(), animation_style); |
| 199 } | 217 } |
| 200 if (is_insertion_active_) | 218 if (is_insertion_active_) |
| 201 insertion_handle_->SetVisible(GetStartVisible(), animation_style); | 219 insertion_handle_->SetVisible(GetStartVisible(), animation_style); |
| 202 } | 220 } |
| 203 | 221 |
| 204 void TouchSelectionController::OnSelectionEditable(bool editable) { | 222 void TouchSelectionControllerImpl::OnSelectionEditable(bool editable) { |
| 205 if (selection_editable_ == editable) | 223 if (selection_editable_ == editable) |
| 206 return; | 224 return; |
| 207 selection_editable_ = editable; | 225 selection_editable_ = editable; |
| 208 ResetCachedValuesIfInactive(); | 226 ResetCachedValuesIfInactive(); |
| 209 if (!selection_editable_) | 227 if (!selection_editable_) |
| 210 DeactivateInsertion(); | 228 DeactivateInsertion(); |
| 211 } | 229 } |
| 212 | 230 |
| 213 void TouchSelectionController::OnSelectionEmpty(bool empty) { | 231 void TouchSelectionControllerImpl::OnSelectionEmpty(bool empty) { |
| 214 if (selection_empty_ == empty) | 232 if (selection_empty_ == empty) |
| 215 return; | 233 return; |
| 216 selection_empty_ = empty; | 234 selection_empty_ = empty; |
| 217 ResetCachedValuesIfInactive(); | 235 ResetCachedValuesIfInactive(); |
| 218 } | 236 } |
| 219 | 237 |
| 220 bool TouchSelectionController::Animate(base::TimeTicks frame_time) { | 238 bool TouchSelectionControllerImpl::Animate(base::TimeTicks frame_time) { |
| 221 if (is_insertion_active_) | 239 if (is_insertion_active_) |
| 222 return insertion_handle_->Animate(frame_time); | 240 return insertion_handle_->Animate(frame_time); |
| 223 | 241 |
| 224 if (is_selection_active_) { | 242 if (is_selection_active_) { |
| 225 bool needs_animate = start_selection_handle_->Animate(frame_time); | 243 bool needs_animate = start_selection_handle_->Animate(frame_time); |
| 226 needs_animate |= end_selection_handle_->Animate(frame_time); | 244 needs_animate |= end_selection_handle_->Animate(frame_time); |
| 227 return needs_animate; | 245 return needs_animate; |
| 228 } | 246 } |
| 229 | 247 |
| 230 return false; | 248 return false; |
| 231 } | 249 } |
| 232 | 250 |
| 233 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) { | 251 gfx::Rect TouchSelectionControllerImpl::GetRectBetweenBounds() const { |
| 252 // Short-circuit for efficiency |
| 253 if (!is_insertion_active_ && !is_selection_active_) |
| 254 return gfx::Rect(); |
| 255 |
| 256 SelectionBound clipped_start = start(); |
| 257 SelectionBound clipped_end = end(); |
| 258 const gfx::Rect& client_size = client_->GetClientSize(); |
| 259 if (clipped_start.visible()) |
| 260 ClipSelectionBound(&clipped_start, client_size); |
| 261 if (clipped_end.visible()) |
| 262 ClipSelectionBound(&clipped_end, client_size); |
| 263 |
| 264 if (clipped_start.visible() && clipped_end.visible()) |
| 265 return RectBetweenSelectionBounds(clipped_start, clipped_end); |
| 266 if (clipped_start.visible()) { |
| 267 return gfx::BoundingRect(clipped_start.edge_top_rounded(), |
| 268 clipped_start.edge_bottom_rounded()); |
| 269 } |
| 270 if (clipped_end.visible()) { |
| 271 return gfx::BoundingRect(clipped_end.edge_top_rounded(), |
| 272 clipped_end.edge_bottom_rounded()); |
| 273 } |
| 274 return gfx::Rect(); |
| 275 } |
| 276 |
| 277 void TouchSelectionControllerImpl::OnNativeViewMoved() { |
| 278 if (is_selection_active_) |
| 279 client_->OnSelectionEvent(SELECTION_MOVED); |
| 280 else if (is_insertion_active_) |
| 281 client_->OnSelectionEvent(INSERTION_MOVED); |
| 282 } |
| 283 |
| 284 void TouchSelectionControllerImpl::OnOverscrollStarted() { |
| 285 client_->OnSelectionEvent(NATIVE_VIEW_OVERSCROLL_STARTED); |
| 286 } |
| 287 |
| 288 void TouchSelectionControllerImpl::OnOverscrollCompleted() { |
| 289 client_->OnSelectionEvent(NATIVE_VIEW_OVERSCROLL_STOPPED); |
| 290 } |
| 291 |
| 292 void TouchSelectionControllerImpl::OnHandleDragBegin(const TouchHandle& handle)
{ |
| 234 if (&handle == insertion_handle_.get()) { | 293 if (&handle == insertion_handle_.get()) { |
| 235 client_->OnSelectionEvent(INSERTION_DRAG_STARTED, handle.position()); | 294 client_->OnSelectionEvent(INSERTION_DRAG_STARTED); |
| 236 return; | 295 return; |
| 237 } | 296 } |
| 238 | 297 |
| 239 gfx::PointF base, extent; | 298 gfx::PointF base, extent; |
| 240 if (&handle == start_selection_handle_.get()) { | 299 if (&handle == start_selection_handle_.get()) { |
| 241 base = end_selection_handle_->position() + GetEndLineOffset(); | 300 base = end_selection_handle_->position() + GetEndLineOffset(); |
| 242 extent = start_selection_handle_->position() + GetStartLineOffset(); | 301 extent = start_selection_handle_->position() + GetStartLineOffset(); |
| 243 } else { | 302 } else { |
| 244 base = start_selection_handle_->position() + GetStartLineOffset(); | 303 base = start_selection_handle_->position() + GetStartLineOffset(); |
| 245 extent = end_selection_handle_->position() + GetEndLineOffset(); | 304 extent = end_selection_handle_->position() + GetEndLineOffset(); |
| 246 } | 305 } |
| 247 selection_handle_dragged_ = true; | 306 selection_handle_was_dragged_ = true; |
| 248 | 307 |
| 249 // When moving the handle we want to move only the extent point. Before doing | 308 // 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. | 309 // so we must make sure that the base point is set correctly. |
| 251 client_->SelectBetweenCoordinates(base, extent); | 310 client_->SelectBetweenCoordinates(base, extent); |
| 252 | 311 |
| 253 client_->OnSelectionEvent(SELECTION_DRAG_STARTED, handle.position()); | 312 client_->OnSelectionEvent(SELECTION_DRAG_STARTED); |
| 254 } | 313 } |
| 255 | 314 |
| 256 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle, | 315 void TouchSelectionControllerImpl::OnHandleDragUpdate(const TouchHandle& handle, |
| 257 const gfx::PointF& position) { | 316 const gfx::PointF& position) { |
| 258 // As the position corresponds to the bottom left point of the selection | 317 // As the position corresponds to the bottom left point of the selection |
| 259 // bound, offset it by half the corresponding line height. | 318 // bound, offset it by half the corresponding line height. |
| 260 gfx::Vector2dF line_offset = &handle == start_selection_handle_.get() | 319 gfx::Vector2dF line_offset = &handle == start_selection_handle_.get() |
| 261 ? GetStartLineOffset() | 320 ? GetStartLineOffset() |
| 262 : GetEndLineOffset(); | 321 : GetEndLineOffset(); |
| 263 gfx::PointF line_position = position + line_offset; | 322 gfx::PointF line_position = position + line_offset; |
| 264 if (&handle == insertion_handle_.get()) { | 323 if (&handle == insertion_handle_.get()) |
| 265 client_->MoveCaret(line_position); | 324 client_->MoveCaret(line_position); |
| 266 } else { | 325 else |
| 267 client_->MoveRangeSelectionExtent(line_position); | 326 client_->MoveRangeSelectionExtent(line_position); |
| 268 } | |
| 269 } | 327 } |
| 270 | 328 |
| 271 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) { | 329 void TouchSelectionControllerImpl::OnHandleDragEnd(const TouchHandle& handle) { |
| 272 if (&handle == insertion_handle_.get()) | 330 if (&handle == insertion_handle_.get()) |
| 273 client_->OnSelectionEvent(INSERTION_DRAG_STOPPED, handle.position()); | 331 client_->OnSelectionEvent(INSERTION_DRAG_STOPPED); |
| 274 else | 332 else |
| 275 client_->OnSelectionEvent(SELECTION_DRAG_STOPPED, handle.position()); | 333 client_->OnSelectionEvent(SELECTION_DRAG_STOPPED); |
| 276 } | 334 } |
| 277 | 335 |
| 278 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) { | 336 void TouchSelectionControllerImpl::OnHandleTapped(const TouchHandle& handle) { |
| 279 if (insertion_handle_ && &handle == insertion_handle_.get()) | 337 if (insertion_handle_ && &handle == insertion_handle_.get()) |
| 280 client_->OnSelectionEvent(INSERTION_TAPPED, handle.position()); | 338 client_->OnSelectionEvent(INSERTION_TAPPED); |
| 281 } | 339 } |
| 282 | 340 |
| 283 void TouchSelectionController::SetNeedsAnimate() { | 341 void TouchSelectionControllerImpl::SetNeedsAnimate() { |
| 284 client_->SetNeedsAnimate(); | 342 client_->SetNeedsAnimate(); |
| 285 } | 343 } |
| 286 | 344 |
| 287 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() { | 345 scoped_ptr<TouchHandleDrawable> TouchSelectionControllerImpl::CreateDrawable() { |
| 288 return client_->CreateDrawable(); | 346 return client_->CreateDrawable(); |
| 289 } | 347 } |
| 290 | 348 |
| 291 base::TimeDelta TouchSelectionController::GetTapTimeout() const { | 349 base::TimeDelta TouchSelectionControllerImpl::GetTapTimeout() const { |
| 292 return tap_timeout_; | 350 return tap_timeout_; |
| 293 } | 351 } |
| 294 | 352 |
| 295 float TouchSelectionController::GetTapSlop() const { | 353 float TouchSelectionControllerImpl::GetTapSlop() const { |
| 296 return tap_slop_; | 354 return tap_slop_; |
| 297 } | 355 } |
| 298 | 356 |
| 299 void TouchSelectionController::ShowInsertionHandleAutomatically() { | 357 void TouchSelectionControllerImpl::ShowInsertionHandleAutomatically() { |
| 300 if (activate_insertion_automatically_) | 358 if (activate_insertion_automatically_) |
| 301 return; | 359 return; |
| 302 activate_insertion_automatically_ = true; | 360 activate_insertion_automatically_ = true; |
| 303 ResetCachedValuesIfInactive(); | 361 ResetCachedValuesIfInactive(); |
| 304 } | 362 } |
| 305 | 363 |
| 306 void TouchSelectionController::ShowSelectionHandlesAutomatically() { | 364 void TouchSelectionControllerImpl::ShowSelectionHandlesAutomatically() { |
| 307 if (activate_selection_automatically_) | 365 if (activate_selection_automatically_) |
| 308 return; | 366 return; |
| 309 activate_selection_automatically_ = true; | 367 activate_selection_automatically_ = true; |
| 310 ResetCachedValuesIfInactive(); | 368 ResetCachedValuesIfInactive(); |
| 311 } | 369 } |
| 312 | 370 |
| 313 void TouchSelectionController::OnInsertionChanged() { | 371 void TouchSelectionControllerImpl::OnInsertionChanged() { |
| 314 DeactivateSelection(); | 372 DeactivateSelection(); |
| 315 | 373 |
| 316 if (response_pending_input_event_ == TAP && selection_empty_ && | 374 if (response_pending_input_event_ == TAP && selection_empty_ && |
| 317 !show_on_tap_for_empty_editable_) { | 375 !show_on_tap_for_empty_editable_) { |
| 318 HideAndDisallowShowingAutomatically(); | 376 HideAndDisallowShowingAutomatically(); |
| 319 return; | 377 return; |
| 320 } | 378 } |
| 321 | 379 |
| 322 if (!activate_insertion_automatically_) | 380 if (!activate_insertion_automatically_) |
| 323 return; | 381 return; |
| 324 | 382 |
| 325 const bool was_active = is_insertion_active_; | 383 const bool was_active = is_insertion_active_; |
| 326 const gfx::PointF position = GetStartPosition(); | 384 const gfx::PointF position = GetStartPosition(); |
| 327 if (!is_insertion_active_) | 385 if (!is_insertion_active_) |
| 328 ActivateInsertion(); | 386 ActivateInsertion(); |
| 329 else | 387 else |
| 330 client_->OnSelectionEvent(INSERTION_MOVED, position); | 388 client_->OnSelectionEvent(INSERTION_MOVED); |
| 331 | 389 |
| 332 insertion_handle_->SetVisible(GetStartVisible(), | 390 insertion_handle_->SetVisible(GetStartVisible(), |
| 333 GetAnimationStyle(was_active)); | 391 GetAnimationStyle(was_active)); |
| 334 insertion_handle_->SetPosition(position); | 392 insertion_handle_->SetPosition(position); |
| 335 } | 393 } |
| 336 | 394 |
| 337 void TouchSelectionController::OnSelectionChanged() { | 395 void TouchSelectionControllerImpl::OnSelectionChanged() { |
| 338 DeactivateInsertion(); | 396 DeactivateInsertion(); |
| 339 | 397 |
| 340 if (!activate_selection_automatically_) | 398 if (!activate_selection_automatically_) |
| 341 return; | 399 return; |
| 342 | 400 |
| 343 const bool was_active = is_selection_active_; | 401 const bool was_active = is_selection_active_; |
| 344 ActivateSelection(); | 402 if (!was_active) |
| 403 ActivateSelection(); |
| 345 | 404 |
| 346 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active); | 405 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active); |
| 347 start_selection_handle_->SetVisible(GetStartVisible(), animation); | 406 start_selection_handle_->SetVisible(GetStartVisible(), animation); |
| 348 end_selection_handle_->SetVisible(GetEndVisible(), animation); | 407 end_selection_handle_->SetVisible(GetEndVisible(), animation); |
| 349 | 408 |
| 350 start_selection_handle_->SetPosition(GetStartPosition()); | 409 start_selection_handle_->SetPosition(GetStartPosition()); |
| 351 end_selection_handle_->SetPosition(GetEndPosition()); | 410 end_selection_handle_->SetPosition(GetEndPosition()); |
| 411 if (was_active) |
| 412 client_->OnSelectionEvent(SELECTION_MOVED); |
| 352 } | 413 } |
| 353 | 414 |
| 354 void TouchSelectionController::ActivateInsertion() { | 415 void TouchSelectionControllerImpl::ActivateInsertion() { |
| 355 DCHECK(!is_selection_active_); | 416 DCHECK(!is_selection_active_); |
| 356 | 417 |
| 357 if (!insertion_handle_) | 418 if (!insertion_handle_) |
| 358 insertion_handle_.reset( | 419 insertion_handle_.reset( |
| 359 new TouchHandle(this, TouchHandleOrientation::CENTER)); | 420 new TouchHandle(this, TouchHandleOrientation::CENTER)); |
| 360 | 421 |
| 361 if (!is_insertion_active_) { | 422 if (!is_insertion_active_) { |
| 362 is_insertion_active_ = true; | 423 is_insertion_active_ = true; |
| 363 insertion_handle_->SetEnabled(true); | 424 insertion_handle_->SetEnabled(true); |
| 364 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition()); | 425 client_->OnSelectionEvent(INSERTION_SHOWN); |
| 365 } | 426 } |
| 366 } | 427 } |
| 367 | 428 |
| 368 void TouchSelectionController::DeactivateInsertion() { | 429 void TouchSelectionControllerImpl::DeactivateInsertion() { |
| 369 if (!is_insertion_active_) | 430 if (!is_insertion_active_) |
| 370 return; | 431 return; |
| 371 DCHECK(insertion_handle_); | 432 DCHECK(insertion_handle_); |
| 372 is_insertion_active_ = false; | 433 is_insertion_active_ = false; |
| 373 insertion_handle_->SetEnabled(false); | 434 insertion_handle_->SetEnabled(false); |
| 374 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF()); | 435 client_->OnSelectionEvent(INSERTION_CLEARED); |
| 375 } | 436 } |
| 376 | 437 |
| 377 void TouchSelectionController::ActivateSelection() { | 438 void TouchSelectionControllerImpl::ActivateSelection() { |
| 378 DCHECK(!is_insertion_active_); | 439 DCHECK(!is_insertion_active_); |
| 379 | 440 |
| 380 if (!start_selection_handle_) { | 441 if (!start_selection_handle_) { |
| 381 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); | 442 start_selection_handle_.reset(new TouchHandle(this, start_orientation_)); |
| 382 } else { | 443 } else { |
| 383 start_selection_handle_->SetEnabled(true); | 444 start_selection_handle_->SetEnabled(true); |
| 384 start_selection_handle_->SetOrientation(start_orientation_); | 445 start_selection_handle_->SetOrientation(start_orientation_); |
| 385 } | 446 } |
| 386 | 447 |
| 387 if (!end_selection_handle_) { | 448 if (!end_selection_handle_) { |
| 388 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); | 449 end_selection_handle_.reset(new TouchHandle(this, end_orientation_)); |
| 389 } else { | 450 } else { |
| 390 end_selection_handle_->SetEnabled(true); | 451 end_selection_handle_->SetEnabled(true); |
| 391 end_selection_handle_->SetOrientation(end_orientation_); | 452 end_selection_handle_->SetOrientation(end_orientation_); |
| 392 } | 453 } |
| 393 | 454 |
| 394 // As a long press received while a selection is already active may trigger | 455 // 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 | 456 // an entirely new selection, notify the client but avoid sending an |
| 396 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. | 457 // intervening SELECTION_CLEARED update to avoid unnecessary state changes. |
| 397 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) { | 458 if (!is_selection_active_ || response_pending_input_event_ == LONG_PRESS) { |
| 398 if (is_selection_active_) { | 459 if (is_selection_active_) { |
| 399 // The active selection session finishes with the start of the new one. | 460 // The active selection session finishes with the start of the new one. |
| 400 LogSelectionEnd(); | 461 LogSelectionEnd(); |
| 401 } | 462 } |
| 402 is_selection_active_ = true; | 463 is_selection_active_ = true; |
| 403 selection_handle_dragged_ = false; | 464 selection_handle_was_dragged_ = false; |
| 404 selection_start_time_ = base::TimeTicks::Now(); | 465 selection_start_time_ = base::TimeTicks::Now(); |
| 405 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; | 466 response_pending_input_event_ = INPUT_EVENT_TYPE_NONE; |
| 406 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition()); | 467 client_->OnSelectionEvent(SELECTION_SHOWN); |
| 407 } | 468 } |
| 408 } | 469 } |
| 409 | 470 |
| 410 void TouchSelectionController::DeactivateSelection() { | 471 void TouchSelectionControllerImpl::DeactivateSelection() { |
| 411 if (!is_selection_active_) | 472 if (!is_selection_active_) |
| 412 return; | 473 return; |
| 413 DCHECK(start_selection_handle_); | 474 DCHECK(start_selection_handle_); |
| 414 DCHECK(end_selection_handle_); | 475 DCHECK(end_selection_handle_); |
| 415 LogSelectionEnd(); | 476 LogSelectionEnd(); |
| 416 start_selection_handle_->SetEnabled(false); | 477 start_selection_handle_->SetEnabled(false); |
| 417 end_selection_handle_->SetEnabled(false); | 478 end_selection_handle_->SetEnabled(false); |
| 418 is_selection_active_ = false; | 479 is_selection_active_ = false; |
| 419 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF()); | 480 client_->OnSelectionEvent(SELECTION_CLEARED); |
| 420 } | 481 } |
| 421 | 482 |
| 422 void TouchSelectionController::ResetCachedValuesIfInactive() { | 483 void TouchSelectionControllerImpl::ResetCachedValuesIfInactive() { |
| 423 if (is_selection_active_ || is_insertion_active_) | 484 if (is_selection_active_ || is_insertion_active_) |
| 424 return; | 485 return; |
| 425 start_ = SelectionBound(); | 486 start_ = SelectionBound(); |
| 426 end_ = SelectionBound(); | 487 end_ = SelectionBound(); |
| 427 start_orientation_ = TouchHandleOrientation::UNDEFINED; | 488 start_orientation_ = TouchHandleOrientation::UNDEFINED; |
| 428 end_orientation_ = TouchHandleOrientation::UNDEFINED; | 489 end_orientation_ = TouchHandleOrientation::UNDEFINED; |
| 429 } | 490 } |
| 430 | 491 |
| 431 const gfx::PointF& TouchSelectionController::GetStartPosition() const { | 492 const gfx::PointF& TouchSelectionControllerImpl::GetStartPosition() const { |
| 432 return start_.edge_bottom(); | 493 return start_.edge_bottom(); |
| 433 } | 494 } |
| 434 | 495 |
| 435 const gfx::PointF& TouchSelectionController::GetEndPosition() const { | 496 const gfx::PointF& TouchSelectionControllerImpl::GetEndPosition() const { |
| 436 return end_.edge_bottom(); | 497 return end_.edge_bottom(); |
| 437 } | 498 } |
| 438 | 499 |
| 439 gfx::Vector2dF TouchSelectionController::GetStartLineOffset() const { | 500 gfx::Vector2dF TouchSelectionControllerImpl::GetStartLineOffset() const { |
| 440 return ComputeLineOffsetFromBottom(start_); | 501 return ComputeLineOffsetFromBottom(start_); |
| 441 } | 502 } |
| 442 | 503 |
| 443 gfx::Vector2dF TouchSelectionController::GetEndLineOffset() const { | 504 gfx::Vector2dF TouchSelectionControllerImpl::GetEndLineOffset() const { |
| 444 return ComputeLineOffsetFromBottom(end_); | 505 return ComputeLineOffsetFromBottom(end_); |
| 445 } | 506 } |
| 446 | 507 |
| 447 bool TouchSelectionController::GetStartVisible() const { | 508 bool TouchSelectionControllerImpl::GetStartVisible() const { |
| 448 return start_.visible() && !temporarily_hidden_; | 509 return start_.visible() && !temporarily_hidden_; |
| 449 } | 510 } |
| 450 | 511 |
| 451 bool TouchSelectionController::GetEndVisible() const { | 512 bool TouchSelectionControllerImpl::GetEndVisible() const { |
| 452 return end_.visible() && !temporarily_hidden_; | 513 return end_.visible() && !temporarily_hidden_; |
| 453 } | 514 } |
| 454 | 515 |
| 455 TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle( | 516 TouchHandle::AnimationStyle TouchSelectionControllerImpl::GetAnimationStyle( |
| 456 bool was_active) const { | 517 bool was_active) const { |
| 457 return was_active && client_->SupportsAnimation() | 518 return was_active && client_->SupportsAnimation() |
| 458 ? TouchHandle::ANIMATION_SMOOTH | 519 ? TouchHandle::ANIMATION_SMOOTH |
| 459 : TouchHandle::ANIMATION_NONE; | 520 : TouchHandle::ANIMATION_NONE; |
| 460 } | 521 } |
| 461 | 522 |
| 462 void TouchSelectionController::LogSelectionEnd() { | 523 void TouchSelectionControllerImpl::LogSelectionEnd() { |
| 463 // TODO(mfomitchev): Once we are able to tell the difference between | 524 // TODO(mfomitchev): Once we are able to tell the difference between |
| 464 // 'successful' and 'unsuccessful' selections - log | 525 // 'successful' and 'unsuccessful' selections - log |
| 465 // Event.TouchSelection.Duration instead and get rid of | 526 // Event.TouchSelection.Duration instead and get rid of |
| 466 // Event.TouchSelectionD.WasDraggeduration. | 527 // Event.TouchSelectionD.WasDraggeduration. |
| 467 if (selection_handle_dragged_) { | 528 if (selection_handle_was_dragged_) { |
| 468 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; | 529 base::TimeDelta duration = base::TimeTicks::Now() - selection_start_time_; |
| 469 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", | 530 UMA_HISTOGRAM_CUSTOM_TIMES("Event.TouchSelection.WasDraggedDuration", |
| 470 duration, | 531 duration, |
| 471 base::TimeDelta::FromMilliseconds(500), | 532 base::TimeDelta::FromMilliseconds(500), |
| 472 base::TimeDelta::FromSeconds(60), | 533 base::TimeDelta::FromSeconds(60), |
| 473 60); | 534 60); |
| 474 } | 535 } |
| 475 } | 536 } |
| 476 | 537 |
| 477 } // namespace ui | 538 } // namespace ui |
| OLD | NEW |