OLD | NEW |
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 "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
9 #include "grit/ui_strings.h" | 9 #include "grit/ui_strings.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/base/ui_base_switches_util.h" | 11 #include "ui/base/ui_base_switches_util.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
14 #include "ui/gfx/path.h" | 14 #include "ui/gfx/path.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/screen.h" | 16 #include "ui/gfx/screen.h" |
17 #include "ui/gfx/size.h" | 17 #include "ui/gfx/size.h" |
18 #include "ui/views/corewm/shadow_types.h" | 18 #include "ui/views/corewm/shadow_types.h" |
19 #include "ui/views/corewm/window_animations.h" | |
20 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
21 #include "ui/wm/public/masked_window_targeter.h" | 20 #include "ui/wm/public/masked_window_targeter.h" |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 // Constants defining the visual attributes of selection handles | 24 // Constants defining the visual attributes of selection handles |
26 const int kSelectionHandleLineWidth = 1; | 25 const int kSelectionHandleLineWidth = 1; |
27 const SkColor kSelectionHandleLineColor = | 26 const SkColor kSelectionHandleLineColor = |
28 SkColorSetRGB(0x42, 0x81, 0xf4); | 27 SkColorSetRGB(0x42, 0x81, 0xf4); |
29 | 28 |
(...skipping 21 matching lines...) Expand all Loading... |
51 // | 50 // |
52 const int kSelectionHandleVerticalDragOffset = 5; | 51 const int kSelectionHandleVerticalDragOffset = 5; |
53 | 52 |
54 // Padding around the selection handle defining the area that will be included | 53 // Padding around the selection handle defining the area that will be included |
55 // in the touch target to make dragging the handle easier (see pic above). | 54 // in the touch target to make dragging the handle easier (see pic above). |
56 const int kSelectionHandleHorizPadding = 10; | 55 const int kSelectionHandleHorizPadding = 10; |
57 const int kSelectionHandleVertPadding = 20; | 56 const int kSelectionHandleVertPadding = 20; |
58 | 57 |
59 const int kContextMenuTimoutMs = 200; | 58 const int kContextMenuTimoutMs = 200; |
60 | 59 |
61 const int kSelectionHandleQuickFadeDurationMs = 50; | |
62 | |
63 // Creates a widget to host SelectionHandleView. | 60 // Creates a widget to host SelectionHandleView. |
64 views::Widget* CreateTouchSelectionPopupWidget( | 61 views::Widget* CreateTouchSelectionPopupWidget( |
65 gfx::NativeView context, | 62 gfx::NativeView context, |
66 views::WidgetDelegate* widget_delegate) { | 63 views::WidgetDelegate* widget_delegate) { |
67 views::Widget* widget = new views::Widget; | 64 views::Widget* widget = new views::Widget; |
68 views::Widget::InitParams params(views::Widget::InitParams::TYPE_TOOLTIP); | 65 views::Widget::InitParams params(views::Widget::InitParams::TYPE_TOOLTIP); |
69 params.can_activate = false; | 66 params.can_activate = false; |
70 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 67 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
71 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 68 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
72 params.context = context; | 69 params.context = context; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 147 |
151 aura::Window* window = widget_->GetNativeWindow(); | 148 aura::Window* window = widget_->GetNativeWindow(); |
152 window->set_event_targeter(scoped_ptr<ui::EventTargeter>( | 149 window->set_event_targeter(scoped_ptr<ui::EventTargeter>( |
153 new TouchHandleWindowTargeter(window, this))); | 150 new TouchHandleWindowTargeter(window, this))); |
154 | 151 |
155 // We are owned by the TouchSelectionController. | 152 // We are owned by the TouchSelectionController. |
156 set_owned_by_client(); | 153 set_owned_by_client(); |
157 } | 154 } |
158 | 155 |
159 virtual ~EditingHandleView() { | 156 virtual ~EditingHandleView() { |
160 SetWidgetVisible(false, false); | |
161 } | 157 } |
162 | 158 |
163 // Overridden from views::WidgetDelegateView: | 159 // Overridden from views::WidgetDelegateView: |
164 virtual bool WidgetHasHitTestMask() const OVERRIDE { | 160 virtual bool WidgetHasHitTestMask() const OVERRIDE { |
165 return true; | 161 return true; |
166 } | 162 } |
167 | 163 |
168 virtual void GetWidgetHitTestMask(gfx::Path* mask) const OVERRIDE { | 164 virtual void GetWidgetHitTestMask(gfx::Path* mask) const OVERRIDE { |
169 gfx::Size image_size = GetHandleImageSize(); | 165 gfx::Size image_size = GetHandleImageSize(); |
170 mask->addRect(SkIntToScalar(0), SkIntToScalar(selection_rect_.height()), | 166 mask->addRect(SkIntToScalar(0), SkIntToScalar(selection_rect_.height()), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 gfx::Size image_size = GetHandleImageSize(); | 221 gfx::Size image_size = GetHandleImageSize(); |
226 return gfx::Size(image_size.width() + 2 * kSelectionHandleHorizPadding, | 222 return gfx::Size(image_size.width() + 2 * kSelectionHandleHorizPadding, |
227 image_size.height() + selection_rect_.height() + | 223 image_size.height() + selection_rect_.height() + |
228 kSelectionHandleVertPadding); | 224 kSelectionHandleVertPadding); |
229 } | 225 } |
230 | 226 |
231 bool IsWidgetVisible() const { | 227 bool IsWidgetVisible() const { |
232 return widget_->IsVisible(); | 228 return widget_->IsVisible(); |
233 } | 229 } |
234 | 230 |
235 void SetWidgetVisible(bool visible, bool quick) { | 231 void SetWidgetVisible(bool visible) { |
236 if (widget_->IsVisible() == visible) | 232 if (widget_->IsVisible() == visible) |
237 return; | 233 return; |
238 if (visible) { | 234 if (visible) |
239 corewm::SetWindowShowAnimationDuration( | |
240 widget_->GetNativeView(), | |
241 base::TimeDelta::FromMilliseconds( | |
242 quick ? kSelectionHandleQuickFadeDurationMs : 0)); | |
243 widget_->Show(); | 235 widget_->Show(); |
244 } else { | 236 else |
245 corewm::SetWindowHideAnimationDuration( | |
246 widget_->GetNativeView(), | |
247 base::TimeDelta::FromMilliseconds( | |
248 quick ? kSelectionHandleQuickFadeDurationMs : 0)); | |
249 widget_->Hide(); | 237 widget_->Hide(); |
250 } | |
251 } | 238 } |
252 | 239 |
253 void SetSelectionRectInScreen(const gfx::Rect& rect) { | 240 void SetSelectionRectInScreen(const gfx::Rect& rect) { |
254 gfx::Size image_size = GetHandleImageSize(); | 241 gfx::Size image_size = GetHandleImageSize(); |
255 selection_rect_ = rect; | 242 selection_rect_ = rect; |
256 gfx::Rect widget_bounds( | 243 gfx::Rect widget_bounds( |
257 rect.x() - image_size.width() / 2 - kSelectionHandleHorizPadding, | 244 rect.x() - image_size.width() / 2 - kSelectionHandleHorizPadding, |
258 rect.y(), | 245 rect.y(), |
259 image_size.width() + 2 * kSelectionHandleHorizPadding, | 246 image_size.width() + 2 * kSelectionHandleHorizPadding, |
260 rect.height() + image_size.height() + kSelectionHandleVertPadding); | 247 rect.height() + image_size.height() + kSelectionHandleVertPadding); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 selection_end_point_1_ = screen_rect_2; | 369 selection_end_point_1_ = screen_rect_2; |
383 selection_end_point_2_ = screen_rect_1; | 370 selection_end_point_2_ = screen_rect_1; |
384 } | 371 } |
385 SetHandleSelectionRect(non_dragging_handle, r1, screen_rect_1); | 372 SetHandleSelectionRect(non_dragging_handle, r1, screen_rect_1); |
386 } | 373 } |
387 } else { | 374 } else { |
388 UpdateContextMenu(r1.origin(), r2.origin()); | 375 UpdateContextMenu(r1.origin(), r2.origin()); |
389 | 376 |
390 // Check if there is any selection at all. | 377 // Check if there is any selection at all. |
391 if (screen_pos_1 == screen_pos_2) { | 378 if (screen_pos_1 == screen_pos_2) { |
392 selection_handle_1_->SetWidgetVisible(false, false); | 379 selection_handle_1_->SetWidgetVisible(false); |
393 selection_handle_2_->SetWidgetVisible(false, false); | 380 selection_handle_2_->SetWidgetVisible(false); |
394 SetHandleSelectionRect(cursor_handle_.get(), r1, screen_rect_1); | 381 SetHandleSelectionRect(cursor_handle_.get(), r1, screen_rect_1); |
395 return; | 382 return; |
396 } | 383 } |
397 | 384 |
398 cursor_handle_->SetWidgetVisible(false, false); | 385 cursor_handle_->SetWidgetVisible(false); |
399 SetHandleSelectionRect(selection_handle_1_.get(), r1, screen_rect_1); | 386 SetHandleSelectionRect(selection_handle_1_.get(), r1, screen_rect_1); |
400 SetHandleSelectionRect(selection_handle_2_.get(), r2, screen_rect_2); | 387 SetHandleSelectionRect(selection_handle_2_.get(), r2, screen_rect_2); |
401 } | 388 } |
402 } | 389 } |
403 | 390 |
404 bool TouchSelectionControllerImpl::IsHandleDragInProgress() { | 391 bool TouchSelectionControllerImpl::IsHandleDragInProgress() { |
405 return !!dragging_handle_; | 392 return !!dragging_handle_; |
406 } | 393 } |
407 | 394 |
408 void TouchSelectionControllerImpl::HideHandles(bool quick) { | |
409 selection_handle_1_->SetWidgetVisible(false, quick); | |
410 selection_handle_2_->SetWidgetVisible(false, quick); | |
411 cursor_handle_->SetWidgetVisible(false, quick); | |
412 } | |
413 | |
414 void TouchSelectionControllerImpl::SetDraggingHandle( | 395 void TouchSelectionControllerImpl::SetDraggingHandle( |
415 EditingHandleView* handle) { | 396 EditingHandleView* handle) { |
416 dragging_handle_ = handle; | 397 dragging_handle_ = handle; |
417 if (dragging_handle_) | 398 if (dragging_handle_) |
418 HideContextMenu(); | 399 HideContextMenu(); |
419 else | 400 else |
420 StartContextMenuTimer(); | 401 StartContextMenuTimer(); |
421 } | 402 } |
422 | 403 |
423 void TouchSelectionControllerImpl::SelectionHandleDragged( | 404 void TouchSelectionControllerImpl::SelectionHandleDragged( |
(...skipping 29 matching lines...) Expand all Loading... |
453 void TouchSelectionControllerImpl::ConvertPointToClientView( | 434 void TouchSelectionControllerImpl::ConvertPointToClientView( |
454 EditingHandleView* source, gfx::Point* point) { | 435 EditingHandleView* source, gfx::Point* point) { |
455 View::ConvertPointToScreen(source, point); | 436 View::ConvertPointToScreen(source, point); |
456 client_view_->ConvertPointFromScreen(point); | 437 client_view_->ConvertPointFromScreen(point); |
457 } | 438 } |
458 | 439 |
459 void TouchSelectionControllerImpl::SetHandleSelectionRect( | 440 void TouchSelectionControllerImpl::SetHandleSelectionRect( |
460 EditingHandleView* handle, | 441 EditingHandleView* handle, |
461 const gfx::Rect& rect, | 442 const gfx::Rect& rect, |
462 const gfx::Rect& rect_in_screen) { | 443 const gfx::Rect& rect_in_screen) { |
463 handle->SetWidgetVisible(client_view_->GetBounds().Contains(rect), false); | 444 handle->SetWidgetVisible(client_view_->GetBounds().Contains(rect)); |
464 if (handle->IsWidgetVisible()) | 445 if (handle->IsWidgetVisible()) |
465 handle->SetSelectionRectInScreen(rect_in_screen); | 446 handle->SetSelectionRectInScreen(rect_in_screen); |
466 } | 447 } |
467 | 448 |
468 bool TouchSelectionControllerImpl::IsCommandIdEnabled(int command_id) const { | 449 bool TouchSelectionControllerImpl::IsCommandIdEnabled(int command_id) const { |
469 return client_view_->IsCommandIdEnabled(command_id); | 450 return client_view_->IsCommandIdEnabled(command_id); |
470 } | 451 } |
471 | 452 |
472 void TouchSelectionControllerImpl::ExecuteCommand(int command_id, | 453 void TouchSelectionControllerImpl::ExecuteCommand(int command_id, |
473 int event_flags) { | 454 int event_flags) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 } | 570 } |
590 | 571 |
591 ui::TouchSelectionController* ViewsTouchSelectionControllerFactory::create( | 572 ui::TouchSelectionController* ViewsTouchSelectionControllerFactory::create( |
592 ui::TouchEditable* client_view) { | 573 ui::TouchEditable* client_view) { |
593 if (switches::IsTouchEditingEnabled()) | 574 if (switches::IsTouchEditingEnabled()) |
594 return new views::TouchSelectionControllerImpl(client_view); | 575 return new views::TouchSelectionControllerImpl(client_view); |
595 return NULL; | 576 return NULL; |
596 } | 577 } |
597 | 578 |
598 } // namespace views | 579 } // namespace views |
OLD | NEW |