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

Side by Side Diff: ui/views/touchui/touch_selection_controller_impl.cc

Issue 759433002: Reland: Move TouchSelectionController from content to ui (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Excluded ui/touch_selection from Windows GN build Created 6 years 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/views/touchui/touch_selection_controller_impl.h" 5 #include "ui/views/touchui/touch_selection_controller_impl.h"
6 6
7 #include "base/time/time.h" 7 #include "base/time/time.h"
8 #include "ui/aura/client/cursor_client.h" 8 #include "ui/aura/client/cursor_client.h"
9 #include "ui/aura/env.h" 9 #include "ui/aura/env.h"
10 #include "ui/aura/window.h" 10 #include "ui/aura/window.h"
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 return ALIGN_CENTER; 143 return ALIGN_CENTER;
144 default: 144 default:
145 NOTREACHED() << "Undefined bound type for cursor alignment."; 145 NOTREACHED() << "Undefined bound type for cursor alignment.";
146 return ALIGN_LEFT; 146 return ALIGN_LEFT;
147 }; 147 };
148 } 148 }
149 149
150 // Calculates the bounds of the widget containing the selection handle based 150 // Calculates the bounds of the widget containing the selection handle based
151 // on the SelectionBound's type and location 151 // on the SelectionBound's type and location
152 gfx::Rect GetSelectionWidgetBounds(const ui::SelectionBound& bound) { 152 gfx::Rect GetSelectionWidgetBounds(const ui::SelectionBound& bound) {
153 Alignment cursor_alignment = GetCursorAlignment(bound.type); 153 Alignment cursor_alignment = GetCursorAlignment(bound.type());
154 gfx::Size image_size = GetHandleImage(bound.type)->Size(); 154 gfx::Size image_size = GetHandleImage(bound.type())->Size();
155 int widget_width = image_size.width() + 2 * kSelectionHandleHorizPadding; 155 int widget_width = image_size.width() + 2 * kSelectionHandleHorizPadding;
156 int widget_height = bound.GetHeight() + image_size.height() + 156 int widget_height = bound.GetHeight() + image_size.height() +
157 kSelectionHandleVertPadding; 157 kSelectionHandleVertPadding;
158 int widget_left = 0; 158 int widget_left = 0;
159 switch (cursor_alignment) { 159 switch (cursor_alignment) {
160 case ALIGN_LEFT: 160 case ALIGN_LEFT:
161 widget_left = bound.edge_top.x() - kSelectionHandleHorizPadding; 161 widget_left = bound.edge_top_rounded().x() - kSelectionHandleHorizPadding;
162 break; 162 break;
163 case ALIGN_RIGHT: 163 case ALIGN_RIGHT:
164 widget_left = bound.edge_top.x() - image_size.width() - 164 widget_left = bound.edge_top_rounded().x() - image_size.width() -
165 kSelectionHandleHorizPadding; 165 kSelectionHandleHorizPadding;
166 break; 166 break;
167 case ALIGN_CENTER: 167 case ALIGN_CENTER:
168 widget_left = bound.edge_top.x() - widget_width / 2; 168 widget_left = bound.edge_top_rounded().x() - widget_width / 2;
169 break; 169 break;
170 }; 170 };
171 return gfx::Rect( 171 return gfx::Rect(
172 widget_left, bound.edge_top.y(), widget_width, widget_height); 172 widget_left, bound.edge_top_rounded().y(), widget_width, widget_height);
173 } 173 }
174 174
175 gfx::Size GetMaxHandleImageSize() { 175 gfx::Size GetMaxHandleImageSize() {
176 gfx::Rect center_rect = gfx::Rect(GetCenterHandleImage()->Size()); 176 gfx::Rect center_rect = gfx::Rect(GetCenterHandleImage()->Size());
177 gfx::Rect left_rect = gfx::Rect(GetLeftHandleImage()->Size()); 177 gfx::Rect left_rect = gfx::Rect(GetLeftHandleImage()->Size());
178 gfx::Rect right_rect = gfx::Rect(GetRightHandleImage()->Size()); 178 gfx::Rect right_rect = gfx::Rect(GetRightHandleImage()->Size());
179 gfx::Rect union_rect = center_rect; 179 gfx::Rect union_rect = center_rect;
180 union_rect.Union(left_rect); 180 union_rect.Union(left_rect);
181 union_rect.Union(right_rect); 181 union_rect.Union(right_rect);
182 return union_rect.size(); 182 return union_rect.size();
183 } 183 }
184 184
185 // Convenience methods to convert a |bound| from screen to the |client|'s 185 // Convenience methods to convert a |bound| from screen to the |client|'s
186 // coordinate system and vice versa. 186 // coordinate system and vice versa.
187 // Note that this is not quite correct because it does not take into account 187 // Note that this is not quite correct because it does not take into account
188 // transforms such as rotation and scaling. This should be in TouchEditable. 188 // transforms such as rotation and scaling. This should be in TouchEditable.
189 // TODO(varunjain): Fix this. 189 // TODO(varunjain): Fix this.
190 ui::SelectionBound ConvertFromScreen(ui::TouchEditable* client, 190 ui::SelectionBound ConvertFromScreen(ui::TouchEditable* client,
191 const ui::SelectionBound& bound) { 191 const ui::SelectionBound& bound) {
192 ui::SelectionBound result = bound; 192 ui::SelectionBound result = bound;
193 gfx::Point edge_bottom = bound.edge_bottom; 193 gfx::Point edge_bottom = bound.edge_bottom_rounded();
194 gfx::Point edge_top = bound.edge_top; 194 gfx::Point edge_top = bound.edge_top_rounded();
195 client->ConvertPointFromScreen(&edge_bottom); 195 client->ConvertPointFromScreen(&edge_bottom);
196 client->ConvertPointFromScreen(&edge_top); 196 client->ConvertPointFromScreen(&edge_top);
197 result.edge_bottom = edge_bottom; 197 result.SetEdge(edge_top, edge_bottom);
198 result.edge_top = edge_top;
199 return result; 198 return result;
200 } 199 }
201 200
202 ui::SelectionBound ConvertToScreen(ui::TouchEditable* client, 201 ui::SelectionBound ConvertToScreen(ui::TouchEditable* client,
203 const ui::SelectionBound& bound) { 202 const ui::SelectionBound& bound) {
204 ui::SelectionBound result = bound; 203 ui::SelectionBound result = bound;
205 gfx::Point edge_bottom = bound.edge_bottom; 204 gfx::Point edge_bottom = bound.edge_bottom_rounded();
206 gfx::Point edge_top = bound.edge_top; 205 gfx::Point edge_top = bound.edge_top_rounded();
207 client->ConvertPointToScreen(&edge_bottom); 206 client->ConvertPointToScreen(&edge_bottom);
208 client->ConvertPointToScreen(&edge_top); 207 client->ConvertPointToScreen(&edge_top);
209 result.edge_bottom = edge_bottom; 208 result.SetEdge(edge_top, edge_bottom);
210 result.edge_top = edge_top;
211 return result; 209 return result;
212 } 210 }
213 211
214 gfx::Rect BoundToRect(const ui::SelectionBound& bound) { 212 gfx::Rect BoundToRect(const ui::SelectionBound& bound) {
215 return gfx::BoundingRect(bound.edge_top, bound.edge_bottom); 213 return gfx::BoundingRect(bound.edge_top_rounded(),
214 bound.edge_bottom_rounded());
216 } 215 }
217 216
218 } // namespace 217 } // namespace
219 218
220 namespace views { 219 namespace views {
221 220
222 typedef TouchSelectionControllerImpl::EditingHandleView EditingHandleView; 221 typedef TouchSelectionControllerImpl::EditingHandleView EditingHandleView;
223 222
224 class TouchHandleWindowTargeter : public wm::MaskedWindowTargeter { 223 class TouchHandleWindowTargeter : public wm::MaskedWindowTargeter {
225 public: 224 public:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 275
277 void DeleteDelegate() override { 276 void DeleteDelegate() override {
278 // We are owned and deleted by TouchSelectionController. 277 // We are owned and deleted by TouchSelectionController.
279 } 278 }
280 279
281 // Overridden from views::View: 280 // Overridden from views::View:
282 void OnPaint(gfx::Canvas* canvas) override { 281 void OnPaint(gfx::Canvas* canvas) override {
283 if (draw_invisible_) 282 if (draw_invisible_)
284 return; 283 return;
285 284
286 Alignment cursor_alignment = GetCursorAlignment(selection_bound_.type); 285 Alignment cursor_alignment = GetCursorAlignment(selection_bound_.type());
287 int cursor_x = 0; 286 int cursor_x = 0;
288 switch (cursor_alignment) { 287 switch (cursor_alignment) {
289 case ALIGN_RIGHT: 288 case ALIGN_RIGHT:
290 cursor_x = 289 cursor_x = selection_bound_.edge_top_rounded().x() -
291 selection_bound_.edge_top.x() - kSelectionHandleLineWidth + 1; 290 kSelectionHandleLineWidth + 1;
292 break; 291 break;
293 case ALIGN_LEFT: 292 case ALIGN_LEFT:
294 cursor_x = selection_bound_.edge_top.x() - 1; 293 cursor_x = selection_bound_.edge_top_rounded().x() - 1;
295 break; 294 break;
296 case ALIGN_CENTER: 295 case ALIGN_CENTER:
297 cursor_x = 296 cursor_x = selection_bound_.edge_top_rounded().x() -
298 selection_bound_.edge_top.x() - kSelectionHandleLineWidth / 2; 297 kSelectionHandleLineWidth / 2;
299 break; 298 break;
300 }; 299 };
301 // Draw the cursor line. 300 // Draw the cursor line.
302 canvas->FillRect(gfx::Rect(cursor_x, 301 canvas->FillRect(gfx::Rect(cursor_x,
303 0, 302 0,
304 kSelectionHandleLineWidth, 303 kSelectionHandleLineWidth,
305 selection_bound_.GetHeight()), 304 selection_bound_.GetHeight()),
306 kSelectionHandleLineColor); 305 kSelectionHandleLineColor);
307 // Draw the handle image. 306 // Draw the handle image.
308 canvas->DrawImageInt(*image_->ToImageSkia(), 307 canvas->DrawImageInt(*image_->ToImageSkia(),
309 kSelectionHandleHorizPadding, selection_bound_.GetHeight()); 308 kSelectionHandleHorizPadding, selection_bound_.GetHeight());
310 } 309 }
311 310
312 void OnGestureEvent(ui::GestureEvent* event) override { 311 void OnGestureEvent(ui::GestureEvent* event) override {
313 event->SetHandled(); 312 event->SetHandled();
314 switch (event->type()) { 313 switch (event->type()) {
315 case ui::ET_GESTURE_SCROLL_BEGIN: { 314 case ui::ET_GESTURE_SCROLL_BEGIN: {
316 widget_->SetCapture(this); 315 widget_->SetCapture(this);
317 controller_->SetDraggingHandle(this); 316 controller_->SetDraggingHandle(this);
318 // Distance from the point which is |kSelectionHandleVerticalDragOffset| 317 // Distance from the point which is |kSelectionHandleVerticalDragOffset|
319 // pixels above the bottom of the handle's cursor line to the event 318 // pixels above the bottom of the handle's cursor line to the event
320 // location (aka the touch-drag point). 319 // location (aka the touch-drag point).
321 drag_offset_ = selection_bound_.edge_bottom - 320 drag_offset_ = selection_bound_.edge_bottom_rounded() -
322 gfx::Vector2d(0, kSelectionHandleVerticalDragOffset) - 321 gfx::Vector2d(0, kSelectionHandleVerticalDragOffset) -
323 event->location(); 322 event->location();
324 break; 323 break;
325 } 324 }
326 case ui::ET_GESTURE_SCROLL_UPDATE: { 325 case ui::ET_GESTURE_SCROLL_UPDATE: {
327 controller_->SelectionHandleDragged(event->location() + drag_offset_); 326 controller_->SelectionHandleDragged(event->location() + drag_offset_);
328 break; 327 break;
329 } 328 }
330 case ui::ET_GESTURE_SCROLL_END: 329 case ui::ET_GESTURE_SCROLL_END:
331 case ui::ET_SCROLL_FLING_START: 330 case ui::ET_SCROLL_FLING_START:
(...skipping 24 matching lines...) Expand all
356 quick ? kSelectionHandleQuickFadeDurationMs : 0)); 355 quick ? kSelectionHandleQuickFadeDurationMs : 0));
357 if (visible) 356 if (visible)
358 widget_->Show(); 357 widget_->Show();
359 else 358 else
360 widget_->Hide(); 359 widget_->Hide();
361 } 360 }
362 361
363 void SetBoundInScreen(const ui::SelectionBound& bound) { 362 void SetBoundInScreen(const ui::SelectionBound& bound) {
364 bool update_bound_type = false; 363 bool update_bound_type = false;
365 // Cursor handle should always have the bound type CENTER 364 // Cursor handle should always have the bound type CENTER
366 DCHECK(!is_cursor_handle_ || bound.type == ui::SelectionBound::CENTER); 365 DCHECK(!is_cursor_handle_ || bound.type() == ui::SelectionBound::CENTER);
367 366
368 if (bound.type != selection_bound_.type) { 367 if (bound.type() != selection_bound_.type()) {
369 // Unless this is a cursor handle, do not set the type to CENTER - 368 // Unless this is a cursor handle, do not set the type to CENTER -
370 // selection handles corresponding to a selection should always use left 369 // selection handles corresponding to a selection should always use left
371 // or right handle image. If selection handles are dragged to be located 370 // or right handle image. If selection handles are dragged to be located
372 // at the same spot, the |bound|'s type here will be CENTER for both of 371 // at the same spot, the |bound|'s type here will be CENTER for both of
373 // them. In this case do not update the type of the |selection_bound_|. 372 // them. In this case do not update the type of the |selection_bound_|.
374 if (bound.type != ui::SelectionBound::CENTER || is_cursor_handle_) 373 if (bound.type() != ui::SelectionBound::CENTER || is_cursor_handle_)
375 update_bound_type = true; 374 update_bound_type = true;
376 } 375 }
377 if (update_bound_type) { 376 if (update_bound_type) {
378 selection_bound_ = bound; 377 selection_bound_.set_type(bound.type());
379 image_ = GetHandleImage(bound.type); 378 image_ = GetHandleImage(bound.type());
380 SchedulePaint(); 379 SchedulePaint();
381 } else {
382 selection_bound_.edge_top = bound.edge_top;
383 selection_bound_.edge_bottom = bound.edge_bottom;
384 } 380 }
381 selection_bound_.SetEdge(bound.edge_top(), bound.edge_bottom());
385 382
386 widget_->SetBounds(GetSelectionWidgetBounds(selection_bound_)); 383 widget_->SetBounds(GetSelectionWidgetBounds(selection_bound_));
387 384
388 aura::Window* window = widget_->GetNativeView(); 385 aura::Window* window = widget_->GetNativeView();
389 wm::ConvertPointFromScreen(window, &selection_bound_.edge_top); 386 gfx::Point edge_top = selection_bound_.edge_top_rounded();
390 wm::ConvertPointFromScreen(window, &selection_bound_.edge_bottom); 387 gfx::Point edge_bottom = selection_bound_.edge_bottom_rounded();
388 wm::ConvertPointFromScreen(window, &edge_top);
389 wm::ConvertPointFromScreen(window, &edge_bottom);
390 selection_bound_.SetEdge(edge_top, edge_bottom);
391 } 391 }
392 392
393 void SetDrawInvisible(bool draw_invisible) { 393 void SetDrawInvisible(bool draw_invisible) {
394 if (draw_invisible_ == draw_invisible) 394 if (draw_invisible_ == draw_invisible)
395 return; 395 return;
396 draw_invisible_ = draw_invisible; 396 draw_invisible_ = draw_invisible;
397 SchedulePaint(); 397 SchedulePaint();
398 } 398 }
399 399
400 private: 400 private:
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 client_view_->GetNativeView()->RemoveObserver(this); 467 client_view_->GetNativeView()->RemoveObserver(this);
468 } 468 }
469 469
470 void TouchSelectionControllerImpl::SelectionChanged() { 470 void TouchSelectionControllerImpl::SelectionChanged() {
471 ui::SelectionBound anchor, focus; 471 ui::SelectionBound anchor, focus;
472 client_view_->GetSelectionEndPoints(&anchor, &focus); 472 client_view_->GetSelectionEndPoints(&anchor, &focus);
473 ui::SelectionBound screen_bound_anchor = 473 ui::SelectionBound screen_bound_anchor =
474 ConvertToScreen(client_view_, anchor); 474 ConvertToScreen(client_view_, anchor);
475 ui::SelectionBound screen_bound_focus = ConvertToScreen(client_view_, focus); 475 ui::SelectionBound screen_bound_focus = ConvertToScreen(client_view_, focus);
476 gfx::Rect client_bounds = client_view_->GetBounds(); 476 gfx::Rect client_bounds = client_view_->GetBounds();
477 if (anchor.edge_top.y() < client_bounds.y()) 477 if (anchor.edge_top().y() < client_bounds.y()) {
478 anchor.edge_top.set_y(client_bounds.y()); 478 gfx::Point anchor_edge_top = anchor.edge_top_rounded();
479 if (focus.edge_top.y() < client_bounds.y()) 479 anchor_edge_top.set_y(client_bounds.y());
480 focus.edge_top.set_y(client_bounds.y()); 480 anchor.SetEdgeTop(anchor_edge_top);
481 }
482 if (focus.edge_top().y() < client_bounds.y()) {
483 gfx::Point focus_edge_top = focus.edge_top_rounded();
484 focus_edge_top.set_y(client_bounds.y());
485 focus.SetEdgeTop(focus_edge_top);
486 }
481 ui::SelectionBound screen_bound_anchor_clipped = 487 ui::SelectionBound screen_bound_anchor_clipped =
482 ConvertToScreen(client_view_, anchor); 488 ConvertToScreen(client_view_, anchor);
483 ui::SelectionBound screen_bound_focus_clipped = 489 ui::SelectionBound screen_bound_focus_clipped =
484 ConvertToScreen(client_view_, focus); 490 ConvertToScreen(client_view_, focus);
485 if (screen_bound_anchor_clipped == selection_bound_1_clipped_ && 491 if (screen_bound_anchor_clipped == selection_bound_1_clipped_ &&
486 screen_bound_focus_clipped == selection_bound_2_clipped_) 492 screen_bound_focus_clipped == selection_bound_2_clipped_)
487 return; 493 return;
488 494
489 selection_bound_1_ = screen_bound_anchor; 495 selection_bound_1_ = screen_bound_anchor;
490 selection_bound_2_ = screen_bound_focus; 496 selection_bound_2_ = screen_bound_focus;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 selection_bound_2_ = screen_bound_anchor; 530 selection_bound_2_ = screen_bound_anchor;
525 selection_bound_1_clipped_ = screen_bound_focus_clipped; 531 selection_bound_1_clipped_ = screen_bound_focus_clipped;
526 selection_bound_2_clipped_ = screen_bound_anchor_clipped; 532 selection_bound_2_clipped_ = screen_bound_anchor_clipped;
527 } 533 }
528 SetHandleBound(non_dragging_handle, anchor, screen_bound_anchor_clipped); 534 SetHandleBound(non_dragging_handle, anchor, screen_bound_anchor_clipped);
529 } 535 }
530 } else { 536 } else {
531 UpdateContextMenu(); 537 UpdateContextMenu();
532 538
533 // Check if there is any selection at all. 539 // Check if there is any selection at all.
534 if (screen_bound_anchor.edge_top == screen_bound_focus.edge_top && 540 if (screen_bound_anchor.edge_top() == screen_bound_focus.edge_top() &&
535 screen_bound_anchor.edge_bottom == screen_bound_focus.edge_bottom) { 541 screen_bound_anchor.edge_bottom() == screen_bound_focus.edge_bottom()) {
536 selection_handle_1_->SetWidgetVisible(false, false); 542 selection_handle_1_->SetWidgetVisible(false, false);
537 selection_handle_2_->SetWidgetVisible(false, false); 543 selection_handle_2_->SetWidgetVisible(false, false);
538 SetHandleBound(cursor_handle_.get(), anchor, screen_bound_anchor_clipped); 544 SetHandleBound(cursor_handle_.get(), anchor, screen_bound_anchor_clipped);
539 return; 545 return;
540 } 546 }
541 547
542 cursor_handle_->SetWidgetVisible(false, false); 548 cursor_handle_->SetWidgetVisible(false, false);
543 SetHandleBound( 549 SetHandleBound(
544 selection_handle_1_.get(), anchor, screen_bound_anchor_clipped); 550 selection_handle_1_.get(), anchor, screen_bound_anchor_clipped);
545 SetHandleBound( 551 SetHandleBound(
(...skipping 30 matching lines...) Expand all
576 client_view_->MoveCaretTo(drag_pos_in_client); 582 client_view_->MoveCaretTo(drag_pos_in_client);
577 return; 583 return;
578 } 584 }
579 585
580 // Find the stationary selection handle. 586 // Find the stationary selection handle.
581 ui::SelectionBound anchor_bound = 587 ui::SelectionBound anchor_bound =
582 selection_handle_1_ == dragging_handle_ ? selection_bound_2_ 588 selection_handle_1_ == dragging_handle_ ? selection_bound_2_
583 : selection_bound_1_; 589 : selection_bound_1_;
584 590
585 // Find selection end points in client_view's coordinate system. 591 // Find selection end points in client_view's coordinate system.
586 gfx::Point p2 = anchor_bound.edge_top; 592 gfx::Point p2 = anchor_bound.edge_top_rounded();
587 p2.Offset(0, anchor_bound.GetHeight() / 2); 593 p2.Offset(0, anchor_bound.GetHeight() / 2);
588 client_view_->ConvertPointFromScreen(&p2); 594 client_view_->ConvertPointFromScreen(&p2);
589 595
590 // Instruct client_view to select the region between p1 and p2. The position 596 // Instruct client_view to select the region between p1 and p2. The position
591 // of |fixed_handle| is the start and that of |dragging_handle| is the end 597 // of |fixed_handle| is the start and that of |dragging_handle| is the end
592 // of selection. 598 // of selection.
593 client_view_->SelectRect(p2, drag_pos_in_client); 599 client_view_->SelectRect(p2, drag_pos_in_client);
594 } 600 }
595 601
596 void TouchSelectionControllerImpl::ConvertPointToClientView( 602 void TouchSelectionControllerImpl::ConvertPointToClientView(
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 766
761 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle1View() { 767 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle1View() {
762 return selection_handle_1_.get(); 768 return selection_handle_1_.get();
763 } 769 }
764 770
765 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle2View() { 771 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle2View() {
766 return selection_handle_2_.get(); 772 return selection_handle_2_.get();
767 } 773 }
768 774
769 } // namespace views 775 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/touchui/touch_selection_controller_impl.h ('k') | ui/views/touchui/touch_selection_controller_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698