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

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: Fix for ios build and some typos 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.SetEdgeBottom(edge_bottom);
198 result.edge_top = edge_top; 198 result.SetEdgeTop(edge_top);
199 return result; 199 return result;
200 } 200 }
201 201
202 ui::SelectionBound ConvertToScreen(ui::TouchEditable* client, 202 ui::SelectionBound ConvertToScreen(ui::TouchEditable* client,
203 const ui::SelectionBound& bound) { 203 const ui::SelectionBound& bound) {
204 ui::SelectionBound result = bound; 204 ui::SelectionBound result = bound;
205 gfx::Point edge_bottom = bound.edge_bottom; 205 gfx::Point edge_bottom = bound.edge_bottom_rounded();
206 gfx::Point edge_top = bound.edge_top; 206 gfx::Point edge_top = bound.edge_top_rounded();
207 client->ConvertPointToScreen(&edge_bottom); 207 client->ConvertPointToScreen(&edge_bottom);
208 client->ConvertPointToScreen(&edge_top); 208 client->ConvertPointToScreen(&edge_top);
209 result.edge_bottom = edge_bottom; 209 result.SetEdgeBottom(edge_bottom);
210 result.edge_top = edge_top; 210 result.SetEdgeTop(edge_top);
211 return result; 211 return result;
212 } 212 }
213 213
214 gfx::Rect BoundToRect(const ui::SelectionBound& bound) { 214 gfx::Rect BoundToRect(const ui::SelectionBound& bound) {
215 return gfx::BoundingRect(bound.edge_top, bound.edge_bottom); 215 return gfx::BoundingRect(bound.edge_top_rounded(),
216 bound.edge_bottom_rounded());
216 } 217 }
217 218
218 } // namespace 219 } // namespace
219 220
220 namespace views { 221 namespace views {
221 222
222 typedef TouchSelectionControllerImpl::EditingHandleView EditingHandleView; 223 typedef TouchSelectionControllerImpl::EditingHandleView EditingHandleView;
223 224
224 class TouchHandleWindowTargeter : public wm::MaskedWindowTargeter { 225 class TouchHandleWindowTargeter : public wm::MaskedWindowTargeter {
225 public: 226 public:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 277
277 void DeleteDelegate() override { 278 void DeleteDelegate() override {
278 // We are owned and deleted by TouchSelectionController. 279 // We are owned and deleted by TouchSelectionController.
279 } 280 }
280 281
281 // Overridden from views::View: 282 // Overridden from views::View:
282 void OnPaint(gfx::Canvas* canvas) override { 283 void OnPaint(gfx::Canvas* canvas) override {
283 if (draw_invisible_) 284 if (draw_invisible_)
284 return; 285 return;
285 286
286 Alignment cursor_alignment = GetCursorAlignment(selection_bound_.type); 287 Alignment cursor_alignment = GetCursorAlignment(selection_bound_.type());
287 int cursor_x = 0; 288 int cursor_x = 0;
288 switch (cursor_alignment) { 289 switch (cursor_alignment) {
289 case ALIGN_RIGHT: 290 case ALIGN_RIGHT:
290 cursor_x = 291 cursor_x = selection_bound_.edge_top_rounded().x() -
291 selection_bound_.edge_top.x() - kSelectionHandleLineWidth + 1; 292 kSelectionHandleLineWidth + 1;
292 break; 293 break;
293 case ALIGN_LEFT: 294 case ALIGN_LEFT:
294 cursor_x = selection_bound_.edge_top.x() - 1; 295 cursor_x = selection_bound_.edge_top_rounded().x() - 1;
295 break; 296 break;
296 case ALIGN_CENTER: 297 case ALIGN_CENTER:
297 cursor_x = 298 cursor_x = selection_bound_.edge_top_rounded().x() -
298 selection_bound_.edge_top.x() - kSelectionHandleLineWidth / 2; 299 kSelectionHandleLineWidth / 2;
299 break; 300 break;
300 }; 301 };
301 // Draw the cursor line. 302 // Draw the cursor line.
302 canvas->FillRect(gfx::Rect(cursor_x, 303 canvas->FillRect(gfx::Rect(cursor_x,
303 0, 304 0,
304 kSelectionHandleLineWidth, 305 kSelectionHandleLineWidth,
305 selection_bound_.GetHeight()), 306 selection_bound_.GetHeight()),
306 kSelectionHandleLineColor); 307 kSelectionHandleLineColor);
307 // Draw the handle image. 308 // Draw the handle image.
308 canvas->DrawImageInt(*image_->ToImageSkia(), 309 canvas->DrawImageInt(*image_->ToImageSkia(),
309 kSelectionHandleHorizPadding, selection_bound_.GetHeight()); 310 kSelectionHandleHorizPadding, selection_bound_.GetHeight());
310 } 311 }
311 312
312 void OnGestureEvent(ui::GestureEvent* event) override { 313 void OnGestureEvent(ui::GestureEvent* event) override {
313 event->SetHandled(); 314 event->SetHandled();
314 switch (event->type()) { 315 switch (event->type()) {
315 case ui::ET_GESTURE_SCROLL_BEGIN: { 316 case ui::ET_GESTURE_SCROLL_BEGIN: {
316 widget_->SetCapture(this); 317 widget_->SetCapture(this);
317 controller_->SetDraggingHandle(this); 318 controller_->SetDraggingHandle(this);
318 // Distance from the point which is |kSelectionHandleVerticalDragOffset| 319 // Distance from the point which is |kSelectionHandleVerticalDragOffset|
319 // pixels above the bottom of the handle's cursor line to the event 320 // pixels above the bottom of the handle's cursor line to the event
320 // location (aka the touch-drag point). 321 // location (aka the touch-drag point).
321 drag_offset_ = selection_bound_.edge_bottom - 322 drag_offset_ = selection_bound_.edge_bottom_rounded() -
322 gfx::Vector2d(0, kSelectionHandleVerticalDragOffset) - 323 gfx::Vector2d(0, kSelectionHandleVerticalDragOffset) -
323 event->location(); 324 event->location();
324 break; 325 break;
325 } 326 }
326 case ui::ET_GESTURE_SCROLL_UPDATE: { 327 case ui::ET_GESTURE_SCROLL_UPDATE: {
327 controller_->SelectionHandleDragged(event->location() + drag_offset_); 328 controller_->SelectionHandleDragged(event->location() + drag_offset_);
328 break; 329 break;
329 } 330 }
330 case ui::ET_GESTURE_SCROLL_END: 331 case ui::ET_GESTURE_SCROLL_END:
331 case ui::ET_SCROLL_FLING_START: 332 case ui::ET_SCROLL_FLING_START:
(...skipping 24 matching lines...) Expand all
356 quick ? kSelectionHandleQuickFadeDurationMs : 0)); 357 quick ? kSelectionHandleQuickFadeDurationMs : 0));
357 if (visible) 358 if (visible)
358 widget_->Show(); 359 widget_->Show();
359 else 360 else
360 widget_->Hide(); 361 widget_->Hide();
361 } 362 }
362 363
363 void SetBoundInScreen(const ui::SelectionBound& bound) { 364 void SetBoundInScreen(const ui::SelectionBound& bound) {
364 bool update_bound_type = false; 365 bool update_bound_type = false;
365 // Cursor handle should always have the bound type CENTER 366 // Cursor handle should always have the bound type CENTER
366 DCHECK(!is_cursor_handle_ || bound.type == ui::SelectionBound::CENTER); 367 DCHECK(!is_cursor_handle_ || bound.type() == ui::SelectionBound::CENTER);
367 368
368 if (bound.type != selection_bound_.type) { 369 if (bound.type() != selection_bound_.type()) {
369 // Unless this is a cursor handle, do not set the type to CENTER - 370 // Unless this is a cursor handle, do not set the type to CENTER -
370 // selection handles corresponding to a selection should always use left 371 // selection handles corresponding to a selection should always use left
371 // or right handle image. If selection handles are dragged to be located 372 // 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 373 // 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_|. 374 // them. In this case do not update the type of the |selection_bound_|.
374 if (bound.type != ui::SelectionBound::CENTER || is_cursor_handle_) 375 if (bound.type() != ui::SelectionBound::CENTER || is_cursor_handle_)
375 update_bound_type = true; 376 update_bound_type = true;
376 } 377 }
377 if (update_bound_type) { 378 if (update_bound_type) {
378 selection_bound_ = bound; 379 selection_bound_.set_type(bound.type());
379 image_ = GetHandleImage(bound.type); 380 image_ = GetHandleImage(bound.type());
380 SchedulePaint(); 381 SchedulePaint();
381 } else {
382 selection_bound_.edge_top = bound.edge_top;
383 selection_bound_.edge_bottom = bound.edge_bottom;
384 } 382 }
383 selection_bound_.SetEdgeTop(bound.edge_top());
384 selection_bound_.SetEdgeBottom(bound.edge_bottom());
385 385
386 widget_->SetBounds(GetSelectionWidgetBounds(selection_bound_)); 386 widget_->SetBounds(GetSelectionWidgetBounds(selection_bound_));
387 387
388 aura::Window* window = widget_->GetNativeView(); 388 aura::Window* window = widget_->GetNativeView();
389 wm::ConvertPointFromScreen(window, &selection_bound_.edge_top); 389 gfx::Point edge_top = selection_bound_.edge_top_rounded();
390 wm::ConvertPointFromScreen(window, &selection_bound_.edge_bottom); 390 gfx::Point edge_bottom = selection_bound_.edge_bottom_rounded();
391 wm::ConvertPointFromScreen(window, &edge_top);
392 wm::ConvertPointFromScreen(window, &edge_bottom);
393 selection_bound_.SetEdgeTop(edge_top);
394 selection_bound_.SetEdgeBottom(edge_bottom);
391 } 395 }
392 396
393 void SetDrawInvisible(bool draw_invisible) { 397 void SetDrawInvisible(bool draw_invisible) {
394 if (draw_invisible_ == draw_invisible) 398 if (draw_invisible_ == draw_invisible)
395 return; 399 return;
396 draw_invisible_ = draw_invisible; 400 draw_invisible_ = draw_invisible;
397 SchedulePaint(); 401 SchedulePaint();
398 } 402 }
399 403
400 private: 404 private:
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 client_view_->GetNativeView()->RemoveObserver(this); 471 client_view_->GetNativeView()->RemoveObserver(this);
468 } 472 }
469 473
470 void TouchSelectionControllerImpl::SelectionChanged() { 474 void TouchSelectionControllerImpl::SelectionChanged() {
471 ui::SelectionBound anchor, focus; 475 ui::SelectionBound anchor, focus;
472 client_view_->GetSelectionEndPoints(&anchor, &focus); 476 client_view_->GetSelectionEndPoints(&anchor, &focus);
473 ui::SelectionBound screen_bound_anchor = 477 ui::SelectionBound screen_bound_anchor =
474 ConvertToScreen(client_view_, anchor); 478 ConvertToScreen(client_view_, anchor);
475 ui::SelectionBound screen_bound_focus = ConvertToScreen(client_view_, focus); 479 ui::SelectionBound screen_bound_focus = ConvertToScreen(client_view_, focus);
476 gfx::Rect client_bounds = client_view_->GetBounds(); 480 gfx::Rect client_bounds = client_view_->GetBounds();
477 if (anchor.edge_top.y() < client_bounds.y()) 481 if (anchor.edge_top().y() < client_bounds.y()) {
478 anchor.edge_top.set_y(client_bounds.y()); 482 gfx::Point anchor_edge_top = anchor.edge_top_rounded();
479 if (focus.edge_top.y() < client_bounds.y()) 483 anchor_edge_top.set_y(client_bounds.y());
480 focus.edge_top.set_y(client_bounds.y()); 484 anchor.SetEdgeTop(anchor_edge_top);
485 }
486 if (focus.edge_top().y() < client_bounds.y()) {
487 gfx::Point focus_edge_top = focus.edge_top_rounded();
488 focus_edge_top.set_y(client_bounds.y());
489 focus.SetEdgeTop(focus_edge_top);
490 }
481 ui::SelectionBound screen_bound_anchor_clipped = 491 ui::SelectionBound screen_bound_anchor_clipped =
482 ConvertToScreen(client_view_, anchor); 492 ConvertToScreen(client_view_, anchor);
483 ui::SelectionBound screen_bound_focus_clipped = 493 ui::SelectionBound screen_bound_focus_clipped =
484 ConvertToScreen(client_view_, focus); 494 ConvertToScreen(client_view_, focus);
485 if (screen_bound_anchor_clipped == selection_bound_1_clipped_ && 495 if (screen_bound_anchor_clipped == selection_bound_1_clipped_ &&
486 screen_bound_focus_clipped == selection_bound_2_clipped_) 496 screen_bound_focus_clipped == selection_bound_2_clipped_)
487 return; 497 return;
488 498
489 selection_bound_1_ = screen_bound_anchor; 499 selection_bound_1_ = screen_bound_anchor;
490 selection_bound_2_ = screen_bound_focus; 500 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; 534 selection_bound_2_ = screen_bound_anchor;
525 selection_bound_1_clipped_ = screen_bound_focus_clipped; 535 selection_bound_1_clipped_ = screen_bound_focus_clipped;
526 selection_bound_2_clipped_ = screen_bound_anchor_clipped; 536 selection_bound_2_clipped_ = screen_bound_anchor_clipped;
527 } 537 }
528 SetHandleBound(non_dragging_handle, anchor, screen_bound_anchor_clipped); 538 SetHandleBound(non_dragging_handle, anchor, screen_bound_anchor_clipped);
529 } 539 }
530 } else { 540 } else {
531 UpdateContextMenu(); 541 UpdateContextMenu();
532 542
533 // Check if there is any selection at all. 543 // Check if there is any selection at all.
534 if (screen_bound_anchor.edge_top == screen_bound_focus.edge_top && 544 if (screen_bound_anchor.edge_top() == screen_bound_focus.edge_top() &&
535 screen_bound_anchor.edge_bottom == screen_bound_focus.edge_bottom) { 545 screen_bound_anchor.edge_bottom() == screen_bound_focus.edge_bottom()) {
536 selection_handle_1_->SetWidgetVisible(false, false); 546 selection_handle_1_->SetWidgetVisible(false, false);
537 selection_handle_2_->SetWidgetVisible(false, false); 547 selection_handle_2_->SetWidgetVisible(false, false);
538 SetHandleBound(cursor_handle_.get(), anchor, screen_bound_anchor_clipped); 548 SetHandleBound(cursor_handle_.get(), anchor, screen_bound_anchor_clipped);
539 return; 549 return;
540 } 550 }
541 551
542 cursor_handle_->SetWidgetVisible(false, false); 552 cursor_handle_->SetWidgetVisible(false, false);
543 SetHandleBound( 553 SetHandleBound(
544 selection_handle_1_.get(), anchor, screen_bound_anchor_clipped); 554 selection_handle_1_.get(), anchor, screen_bound_anchor_clipped);
545 SetHandleBound( 555 SetHandleBound(
(...skipping 30 matching lines...) Expand all
576 client_view_->MoveCaretTo(drag_pos_in_client); 586 client_view_->MoveCaretTo(drag_pos_in_client);
577 return; 587 return;
578 } 588 }
579 589
580 // Find the stationary selection handle. 590 // Find the stationary selection handle.
581 ui::SelectionBound anchor_bound = 591 ui::SelectionBound anchor_bound =
582 selection_handle_1_ == dragging_handle_ ? selection_bound_2_ 592 selection_handle_1_ == dragging_handle_ ? selection_bound_2_
583 : selection_bound_1_; 593 : selection_bound_1_;
584 594
585 // Find selection end points in client_view's coordinate system. 595 // Find selection end points in client_view's coordinate system.
586 gfx::Point p2 = anchor_bound.edge_top; 596 gfx::Point p2 = anchor_bound.edge_top_rounded();
587 p2.Offset(0, anchor_bound.GetHeight() / 2); 597 p2.Offset(0, anchor_bound.GetHeight() / 2);
588 client_view_->ConvertPointFromScreen(&p2); 598 client_view_->ConvertPointFromScreen(&p2);
589 599
590 // Instruct client_view to select the region between p1 and p2. The position 600 // 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 601 // of |fixed_handle| is the start and that of |dragging_handle| is the end
592 // of selection. 602 // of selection.
593 client_view_->SelectRect(p2, drag_pos_in_client); 603 client_view_->SelectRect(p2, drag_pos_in_client);
594 } 604 }
595 605
596 void TouchSelectionControllerImpl::ConvertPointToClientView( 606 void TouchSelectionControllerImpl::ConvertPointToClientView(
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 770
761 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle1View() { 771 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle1View() {
762 return selection_handle_1_.get(); 772 return selection_handle_1_.get();
763 } 773 }
764 774
765 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle2View() { 775 views::WidgetDelegateView* TouchSelectionControllerImpl::GetHandle2View() {
766 return selection_handle_2_.get(); 776 return selection_handle_2_.get();
767 } 777 }
768 778
769 } // namespace views 779 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698