1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/tabs/tab_drag_controller.h"
10 #include "base/auto_reset.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/i18n/rtl.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_function_dispatcher.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
21 #include "chrome/browser/ui/views/frame/browser_view.h"
22 #include "chrome/browser/ui/views/tabs/browser_tab_strip_controller.h"
23 #include "chrome/browser/ui/views/tabs/dragged_tab_view.h"
24 #include "chrome/browser/ui/views/tabs/native_view_photobooth.h"
25 #include "chrome/browser/ui/views/tabs/stacked_tab_strip_layout.h"
26 #include "chrome/browser/ui/views/tabs/tab.h"
27 #include "chrome/browser/ui/views/tabs/tab_strip.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_source.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/user_metrics.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/browser/web_contents_view.h"
36 #include "grit/theme_resources.h"
37 #include "ui/base/animation/animation.h"
38 #include "ui/base/animation/animation_delegate.h"
39 #include "ui/base/animation/slide_animation.h"
40 #include "ui/base/events/event_constants.h"
41 #include "ui/base/events/event_utils.h"
42 #include "ui/base/resource/resource_bundle.h"
43 #include "ui/gfx/canvas.h"
44 #include "ui/gfx/image/image_skia.h"
45 #include "ui/gfx/screen.h"
46 #include "ui/views/focus/view_storage.h"
47 #include "ui/views/widget/root_view.h"
48 #include "ui/views/widget/widget.h"
51 #include "ash/shell.h"
52 #include "ash/wm/coordinate_conversion.h"
53 #include "ash/wm/property_util.h"
54 #include "ash/wm/window_util.h"
55 #include "ui/aura/env.h"
56 #include "ui/aura/root_window.h"
57 #include "ui/base/gestures/gesture_recognizer.h"
60 using content::OpenURLParams
;
61 using content::UserMetricsAction
;
62 using content::WebContents
;
64 static const int kHorizontalMoveThreshold
= 16; // Pixels.
66 // Distance from the next/previous stacked before before we consider the tab
67 // close enough to trigger moving.
68 static const int kStackedDistance
= 36;
70 // If non-null there is a drag underway.
71 static TabDragController
* instance_
= NULL
;
75 // Delay, in ms, during dragging before we bring a window to front.
76 const int kBringToFrontDelay
= 750;
78 // Initial delay before moving tabs when the dragged tab is close to the edge of
80 const int kMoveAttachedInitialDelay
= 600;
82 // Delay for moving tabs after the initial delay has passed.
83 const int kMoveAttachedSubsequentDelay
= 300;
85 // Radius of the rect drawn by DockView.
86 const int kRoundedRectRadius
= 4;
88 // Spacing between tab icons when DockView is showing a docking location that
89 // contains more than one tab.
90 const int kTabSpacing
= 4;
92 // DockView is the view responsible for giving a visual indicator of where a
93 // dock is going to occur.
95 class DockView
: public views::View
{
97 explicit DockView(DockInfo::Type type
) : type_(type
) {}
99 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
100 return gfx::Size(DockInfo::popup_width(), DockInfo::popup_height());
103 virtual void OnPaintBackground(gfx::Canvas
* canvas
) OVERRIDE
{
104 // Fill the background rect.
106 paint
.setColor(SkColorSetRGB(108, 108, 108));
107 paint
.setStyle(SkPaint::kFill_Style
);
108 canvas
->DrawRoundRect(GetLocalBounds(), kRoundedRectRadius
, paint
);
110 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
112 gfx::ImageSkia
* high_icon
= rb
.GetImageSkiaNamed(IDR_DOCK_HIGH
);
113 gfx::ImageSkia
* wide_icon
= rb
.GetImageSkiaNamed(IDR_DOCK_WIDE
);
116 bool rtl_ui
= base::i18n::IsRTL();
118 // Flip canvas to draw the mirrored tab images for RTL UI.
119 canvas
->Translate(gfx::Vector2d(width(), 0));
120 canvas
->Scale(-1, 1);
122 int x_of_active_tab
= width() / 2 + kTabSpacing
/ 2;
123 int x_of_inactive_tab
= width() / 2 - high_icon
->width() - kTabSpacing
/ 2;
125 case DockInfo::LEFT_OF_WINDOW
:
126 case DockInfo::LEFT_HALF
:
128 std::swap(x_of_active_tab
, x_of_inactive_tab
);
129 canvas
->DrawImageInt(*high_icon
, x_of_active_tab
,
130 (height() - high_icon
->height()) / 2);
131 if (type_
== DockInfo::LEFT_OF_WINDOW
) {
132 DrawImageWithAlpha(canvas
, *high_icon
, x_of_inactive_tab
,
133 (height() - high_icon
->height()) / 2);
138 case DockInfo::RIGHT_OF_WINDOW
:
139 case DockInfo::RIGHT_HALF
:
141 std::swap(x_of_active_tab
, x_of_inactive_tab
);
142 canvas
->DrawImageInt(*high_icon
, x_of_active_tab
,
143 (height() - high_icon
->height()) / 2);
144 if (type_
== DockInfo::RIGHT_OF_WINDOW
) {
145 DrawImageWithAlpha(canvas
, *high_icon
, x_of_inactive_tab
,
146 (height() - high_icon
->height()) / 2);
150 case DockInfo::TOP_OF_WINDOW
:
151 canvas
->DrawImageInt(*wide_icon
, (width() - wide_icon
->width()) / 2,
152 height() / 2 - high_icon
->height());
155 case DockInfo::MAXIMIZE
: {
156 gfx::ImageSkia
* max_icon
= rb
.GetImageSkiaNamed(IDR_DOCK_MAX
);
157 canvas
->DrawImageInt(*max_icon
, (width() - max_icon
->width()) / 2,
158 (height() - max_icon
->height()) / 2);
162 case DockInfo::BOTTOM_HALF
:
163 case DockInfo::BOTTOM_OF_WINDOW
:
164 canvas
->DrawImageInt(*wide_icon
, (width() - wide_icon
->width()) / 2,
165 height() / 2 + kTabSpacing
/ 2);
166 if (type_
== DockInfo::BOTTOM_OF_WINDOW
) {
167 DrawImageWithAlpha(canvas
, *wide_icon
,
168 (width() - wide_icon
->width()) / 2,
169 height() / 2 - kTabSpacing
/ 2 - wide_icon
->height());
181 void DrawImageWithAlpha(gfx::Canvas
* canvas
, const gfx::ImageSkia
& image
,
185 canvas
->DrawImageInt(image
, x
, y
, paint
);
188 DockInfo::Type type_
;
190 DISALLOW_COPY_AND_ASSIGN(DockView
);
193 void SetTrackedByWorkspace(gfx::NativeWindow window
, bool value
) {
195 ash::SetTrackedByWorkspace(window
, value
);
199 void SetWindowPositionManaged(gfx::NativeWindow window
, bool value
) {
201 ash::wm::SetWindowPositionManaged(window
, value
);
205 // Returns true if |bounds| contains the y-coordinate |y|. The y-coordinate
206 // of |bounds| is adjusted by |vertical_adjustment|.
207 bool DoesRectContainVerticalPointExpanded(
208 const gfx::Rect
& bounds
,
209 int vertical_adjustment
,
211 int upper_threshold
= bounds
.bottom() + vertical_adjustment
;
212 int lower_threshold
= bounds
.y() - vertical_adjustment
;
213 return y
>= lower_threshold
&& y
<= upper_threshold
;
216 // WidgetObserver implementation that resets the window position managed
218 // We're forced to do this here since BrowserFrameAura resets the 'window
219 // position managed' property during a show and we need the property set to
220 // false before WorkspaceLayoutManager2 sees the visibility change.
221 class WindowPositionManagedUpdater
: public views::WidgetObserver
{
223 virtual void OnWidgetVisibilityChanged(views::Widget
* widget
,
224 bool visible
) OVERRIDE
{
225 SetWindowPositionManaged(widget
->GetNativeView(), false);
231 ///////////////////////////////////////////////////////////////////////////////
234 // DockDisplayer is responsible for giving the user a visual indication of a
235 // possible dock position (as represented by DockInfo). DockDisplayer shows
236 // a window with a DockView in it. Two animations are used that correspond to
237 // the state of DockInfo::in_enable_area.
238 class TabDragController::DockDisplayer
: public ui::AnimationDelegate
{
240 DockDisplayer(TabDragController
* controller
, const DockInfo
& info
)
241 : controller_(controller
),
246 in_enable_area_(info
.in_enable_area()) {
247 popup_
= new views::Widget
;
248 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
249 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
250 params
.keep_on_top
= true;
251 params
.bounds
= info
.GetPopupRect();
252 popup_
->Init(params
);
253 popup_
->SetContentsView(new DockView(info
.type()));
254 popup_
->SetOpacity(0x00);
255 if (info
.in_enable_area())
260 popup_view_
= popup_
->GetNativeView();
263 virtual ~DockDisplayer() {
265 controller_
->DockDisplayerDestroyed(this);
268 // Updates the state based on |in_enable_area|.
269 void UpdateInEnabledArea(bool in_enable_area
) {
270 if (in_enable_area
!= in_enable_area_
) {
271 in_enable_area_
= in_enable_area
;
272 UpdateLayeredAlpha();
276 // Resets the reference to the hosting TabDragController. This is
277 // invoked when the TabDragController is destroyed.
278 void clear_controller() { controller_
= NULL
; }
280 // NativeView of the window we create.
281 gfx::NativeView
popup_view() { return popup_view_
; }
283 // Starts the hide animation. When the window is closed the
284 // TabDragController is notified by way of the DockDisplayerDestroyed
298 virtual void AnimationProgressed(const ui::Animation
* animation
) OVERRIDE
{
299 UpdateLayeredAlpha();
302 virtual void AnimationEnded(const ui::Animation
* animation
) OVERRIDE
{
310 void UpdateLayeredAlpha() {
311 double scale
= in_enable_area_
? 1 : .5;
312 popup_
->SetOpacity(static_cast<unsigned char>(animation_
.GetCurrentValue() *
316 // TabDragController that created us.
317 TabDragController
* controller_
;
319 // Window we're showing.
320 views::Widget
* popup_
;
322 // NativeView of |popup_|. We cache this to avoid the possibility of
323 // invoking a method on popup_ after we close it.
324 gfx::NativeView popup_view_
;
326 // Animation for when first made visible.
327 ui::SlideAnimation animation_
;
329 // Have we been hidden?
332 // Value of DockInfo::in_enable_area.
333 bool in_enable_area_
;
336 TabDragController::TabDragData::TabDragData()
338 original_delegate(NULL
),
339 source_model_index(-1),
344 TabDragController::TabDragData::~TabDragData() {
347 ///////////////////////////////////////////////////////////////////////////////
348 // TabDragController, public:
351 const int TabDragController::kTouchVerticalDetachMagnetism
= 50;
354 const int TabDragController::kVerticalDetachMagnetism
= 15;
356 TabDragController::TabDragController()
357 : detach_into_browser_(ShouldDetachIntoNewBrowser()),
358 event_source_(EVENT_SOURCE_MOUSE
),
359 source_tabstrip_(NULL
),
360 attached_tabstrip_(NULL
),
362 host_desktop_type_(chrome::HOST_DESKTOP_TYPE_NATIVE
),
363 offset_to_width_ratio_(0),
364 old_focused_view_id_(
365 views::ViewStorage::GetInstance()->CreateStorageID()),
366 last_move_screen_loc_(0),
367 started_drag_(false),
369 source_tab_index_(std::numeric_limits
<size_t>::max()),
371 detach_behavior_(DETACHABLE
),
372 move_behavior_(REORDER
),
373 mouse_move_direction_(0),
374 is_dragging_window_(false),
375 end_run_loop_behavior_(END_RUN_LOOP_STOP_DRAGGING
),
376 waiting_for_run_loop_to_exit_(false),
377 tab_strip_to_attach_to_after_exit_(NULL
),
378 move_loop_widget_(NULL
),
380 is_mutating_(false) {
384 TabDragController::~TabDragController() {
385 views::ViewStorage::GetInstance()->RemoveView(old_focused_view_id_
);
387 if (instance_
== this)
393 if (move_loop_widget_
) {
394 move_loop_widget_
->RemoveObserver(this);
395 SetTrackedByWorkspace(move_loop_widget_
->GetNativeView(), true);
396 SetWindowPositionManaged(move_loop_widget_
->GetNativeView(), true);
399 if (source_tabstrip_
&& detach_into_browser_
)
400 GetModel(source_tabstrip_
)->RemoveObserver(this);
402 base::MessageLoopForUI::current()->RemoveObserver(this);
404 // Need to delete the view here manually _before_ we reset the dragged
405 // contents to NULL, otherwise if the view is animating to its destination
406 // bounds, it won't be able to clean up properly since its cleanup routine
407 // uses GetIndexForDraggedContents, which will be invalid.
410 // Reset the delegate of the dragged WebContents. This ends up doing nothing
411 // if the drag was completed.
412 if (!detach_into_browser_
)
416 void TabDragController::Init(
417 TabStrip
* source_tabstrip
,
419 const std::vector
<Tab
*>& tabs
,
420 const gfx::Point
& mouse_offset
,
421 int source_tab_offset
,
422 const ui::ListSelectionModel
& initial_selection_model
,
423 DetachBehavior detach_behavior
,
424 MoveBehavior move_behavior
,
425 EventSource event_source
) {
426 DCHECK(!tabs
.empty());
427 DCHECK(std::find(tabs
.begin(), tabs
.end(), source_tab
) != tabs
.end());
428 source_tabstrip_
= source_tabstrip
;
429 screen_
= gfx::Screen::GetScreenFor(
430 source_tabstrip
->GetWidget()->GetNativeView());
431 host_desktop_type_
= chrome::GetHostDesktopTypeForNativeView(
432 source_tabstrip
->GetWidget()->GetNativeView());
433 start_point_in_screen_
= gfx::Point(source_tab_offset
, mouse_offset
.y());
434 views::View::ConvertPointToScreen(source_tab
, &start_point_in_screen_
);
435 event_source_
= event_source
;
436 mouse_offset_
= mouse_offset
;
437 detach_behavior_
= detach_behavior
;
438 move_behavior_
= move_behavior
;
439 last_point_in_screen_
= start_point_in_screen_
;
440 last_move_screen_loc_
= start_point_in_screen_
.x();
441 initial_tab_positions_
= source_tabstrip
->GetTabXCoordinates();
442 if (detach_behavior
== NOT_DETACHABLE
)
443 detach_into_browser_
= false;
445 if (detach_into_browser_
)
446 GetModel(source_tabstrip_
)->AddObserver(this);
448 drag_data_
.resize(tabs
.size());
449 for (size_t i
= 0; i
< tabs
.size(); ++i
)
450 InitTabDragData(tabs
[i
], &(drag_data_
[i
]));
452 std::find(tabs
.begin(), tabs
.end(), source_tab
) - tabs
.begin();
454 // Listen for Esc key presses.
455 base::MessageLoopForUI::current()->AddObserver(this);
457 if (source_tab
->width() > 0) {
458 offset_to_width_ratio_
= static_cast<float>(
459 source_tab
->GetMirroredXInView(source_tab_offset
)) /
460 static_cast<float>(source_tab
->width());
462 InitWindowCreatePoint();
463 initial_selection_model_
.Copy(initial_selection_model
);
467 bool TabDragController::IsAttachedTo(TabStrip
* tab_strip
) {
468 return (instance_
&& instance_
->active() &&
469 instance_
->attached_tabstrip() == tab_strip
);
473 bool TabDragController::IsActive() {
474 return instance_
&& instance_
->active();
478 bool TabDragController::ShouldDetachIntoNewBrowser() {
479 #if defined(USE_AURA)
482 return CommandLine::ForCurrentProcess()->HasSwitch(
483 switches::kTabBrowserDragging
);
487 void TabDragController::SetMoveBehavior(MoveBehavior behavior
) {
491 move_behavior_
= behavior
;
494 void TabDragController::Drag(const gfx::Point
& point_in_screen
) {
495 bring_to_front_timer_
.Stop();
496 move_stacked_timer_
.Stop();
498 if (waiting_for_run_loop_to_exit_
)
501 if (!started_drag_
) {
502 if (!CanStartDrag(point_in_screen
))
503 return; // User hasn't dragged far enough yet.
505 // On windows SaveFocus() may trigger a capture lost, which destroys us.
507 bool destroyed
= false;
508 destroyed_
= &destroyed
;
514 started_drag_
= true;
515 Attach(source_tabstrip_
, gfx::Point());
516 if (detach_into_browser_
&& static_cast<int>(drag_data_
.size()) ==
517 GetModel(source_tabstrip_
)->count()) {
518 RunMoveLoop(GetWindowOffset(point_in_screen
));
523 ContinueDragging(point_in_screen
);
526 void TabDragController::EndDrag(EndDragReason reason
) {
527 // If we're dragging a window ignore capture lost since it'll ultimately
528 // trigger the move loop to end and we'll revert the drag when RunMoveLoop()
530 if (reason
== END_DRAG_CAPTURE_LOST
&& is_dragging_window_
)
532 EndDragImpl(reason
!= END_DRAG_COMPLETE
&& source_tabstrip_
?
536 void TabDragController::InitTabDragData(Tab
* tab
,
537 TabDragData
* drag_data
) {
538 drag_data
->source_model_index
=
539 source_tabstrip_
->GetModelIndexOfTab(tab
);
540 drag_data
->contents
= GetModel(source_tabstrip_
)->GetWebContentsAt(
541 drag_data
->source_model_index
);
542 drag_data
->pinned
= source_tabstrip_
->IsTabPinned(tab
);
545 content::NOTIFICATION_WEB_CONTENTS_DESTROYED
,
546 content::Source
<WebContents
>(drag_data
->contents
));
548 if (!detach_into_browser_
) {
549 drag_data
->original_delegate
= drag_data
->contents
->GetDelegate();
550 drag_data
->contents
->SetDelegate(this);
554 ///////////////////////////////////////////////////////////////////////////////
555 // TabDragController, PageNavigator implementation:
557 WebContents
* TabDragController::OpenURLFromTab(
559 const OpenURLParams
& params
) {
560 if (source_tab_drag_data()->original_delegate
) {
561 OpenURLParams forward_params
= params
;
562 if (params
.disposition
== CURRENT_TAB
)
563 forward_params
.disposition
= NEW_WINDOW
;
565 return source_tab_drag_data()->original_delegate
->OpenURLFromTab(
566 source
, forward_params
);
571 ///////////////////////////////////////////////////////////////////////////////
572 // TabDragController, content::WebContentsDelegate implementation:
574 void TabDragController::NavigationStateChanged(const WebContents
* source
,
575 unsigned changed_flags
) {
576 if (attached_tabstrip_
) {
577 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
578 if (drag_data_
[i
].contents
== source
) {
579 // Pass the NavigationStateChanged call to the original delegate so
580 // that the title is updated. Do this only when we are attached as
581 // otherwise the Tab isn't in the TabStrip.
582 drag_data_
[i
].original_delegate
->NavigationStateChanged(source
,
592 void TabDragController::AddNewContents(WebContents
* source
,
593 WebContents
* new_contents
,
594 WindowOpenDisposition disposition
,
595 const gfx::Rect
& initial_pos
,
598 DCHECK_NE(CURRENT_TAB
, disposition
);
600 // Theoretically could be called while dragging if the page tries to
601 // spawn a window. Route this message back to the browser in most cases.
602 if (source_tab_drag_data()->original_delegate
) {
603 source_tab_drag_data()->original_delegate
->AddNewContents(
604 source
, new_contents
, disposition
, initial_pos
, user_gesture
,
609 void TabDragController::LoadingStateChanged(WebContents
* source
) {
610 // It would be nice to respond to this message by changing the
611 // screen shot in the dragged tab.
616 bool TabDragController::ShouldSuppressDialogs() {
617 // When a dialog is about to be shown we revert the drag. Otherwise a modal
618 // dialog might appear and attempt to parent itself to a hidden tabcontents.
619 EndDragImpl(CANCELED
);
623 content::JavaScriptDialogManager
*
624 TabDragController::GetJavaScriptDialogManager() {
625 return GetJavaScriptDialogManagerInstance();
628 ///////////////////////////////////////////////////////////////////////////////
629 // TabDragController, content::NotificationObserver implementation:
631 void TabDragController::Observe(
633 const content::NotificationSource
& source
,
634 const content::NotificationDetails
& details
) {
635 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED
, type
);
636 WebContents
* destroyed_web_contents
=
637 content::Source
<WebContents
>(source
).ptr();
638 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
639 if (drag_data_
[i
].contents
== destroyed_web_contents
) {
640 // One of the tabs we're dragging has been destroyed. Cancel the drag.
641 if (destroyed_web_contents
->GetDelegate() == this)
642 destroyed_web_contents
->SetDelegate(NULL
);
643 drag_data_
[i
].contents
= NULL
;
644 drag_data_
[i
].original_delegate
= NULL
;
645 EndDragImpl(TAB_DESTROYED
);
649 // If we get here it means we got notification for a tab we don't know about.
653 ///////////////////////////////////////////////////////////////////////////////
654 // TabDragController, MessageLoop::Observer implementation:
656 base::EventStatus
TabDragController::WillProcessEvent(
657 const base::NativeEvent
& event
) {
658 return base::EVENT_CONTINUE
;
661 void TabDragController::DidProcessEvent(const base::NativeEvent
& event
) {
662 // If the user presses ESC during a drag, we need to abort and revert things
663 // to the way they were. This is the most reliable way to do this since no
664 // single view or window reliably receives events throughout all the various
665 // kinds of tab dragging.
666 if (ui::EventTypeFromNative(event
) == ui::ET_KEY_PRESSED
&&
667 ui::KeyboardCodeFromNative(event
) == ui::VKEY_ESCAPE
) {
668 EndDrag(END_DRAG_CANCEL
);
672 void TabDragController::OnWidgetBoundsChanged(views::Widget
* widget
,
673 const gfx::Rect
& new_bounds
) {
674 Drag(GetCursorScreenPoint());
677 void TabDragController::TabStripEmpty() {
678 DCHECK(detach_into_browser_
);
679 GetModel(source_tabstrip_
)->RemoveObserver(this);
680 // NULL out source_tabstrip_ so that we don't attempt to add back to it (in
681 // the case of a revert).
682 source_tabstrip_
= NULL
;
685 ///////////////////////////////////////////////////////////////////////////////
686 // TabDragController, private:
688 void TabDragController::InitWindowCreatePoint() {
689 // window_create_point_ is only used in CompleteDrag() (through
690 // GetWindowCreatePoint() to get the start point of the docked window) when
691 // the attached_tabstrip_ is NULL and all the window's related bound
692 // information are obtained from source_tabstrip_. So, we need to get the
693 // first_tab based on source_tabstrip_, not attached_tabstrip_. Otherwise,
694 // the window_create_point_ is not in the correct coordinate system. Please
695 // refer to http://crbug.com/6223 comment #15 for detailed information.
696 views::View
* first_tab
= source_tabstrip_
->tab_at(0);
697 views::View::ConvertPointToWidget(first_tab
, &first_source_tab_point_
);
698 window_create_point_
= first_source_tab_point_
;
699 window_create_point_
.Offset(mouse_offset_
.x(), mouse_offset_
.y());
702 gfx::Point
TabDragController::GetWindowCreatePoint(
703 const gfx::Point
& origin
) const {
704 if (dock_info_
.type() != DockInfo::NONE
&& dock_info_
.in_enable_area()) {
705 // If we're going to dock, we need to return the exact coordinate,
706 // otherwise we may attempt to maximize on the wrong monitor.
710 // If the cursor is outside the monitor area, move it inside. For example,
711 // dropping a tab onto the task bar on Windows produces this situation.
712 gfx::Rect work_area
= screen_
->GetDisplayNearestPoint(origin
).work_area();
713 gfx::Point
create_point(origin
);
714 if (!work_area
.IsEmpty()) {
715 if (create_point
.x() < work_area
.x())
716 create_point
.set_x(work_area
.x());
717 else if (create_point
.x() > work_area
.right())
718 create_point
.set_x(work_area
.right());
719 if (create_point
.y() < work_area
.y())
720 create_point
.set_y(work_area
.y());
721 else if (create_point
.y() > work_area
.bottom())
722 create_point
.set_y(work_area
.bottom());
724 return gfx::Point(create_point
.x() - window_create_point_
.x(),
725 create_point
.y() - window_create_point_
.y());
728 void TabDragController::UpdateDockInfo(const gfx::Point
& point_in_screen
) {
729 // Update the DockInfo for the current mouse coordinates.
730 DockInfo dock_info
= GetDockInfoAtPoint(point_in_screen
);
731 if (!dock_info
.equals(dock_info_
)) {
732 // DockInfo for current position differs.
733 if (dock_info_
.type() != DockInfo::NONE
&&
734 !dock_controllers_
.empty()) {
735 // Hide old visual indicator.
736 dock_controllers_
.back()->Hide();
738 dock_info_
= dock_info
;
739 if (dock_info_
.type() != DockInfo::NONE
) {
740 // Show new docking position.
741 DockDisplayer
* controller
= new DockDisplayer(this, dock_info_
);
742 if (controller
->popup_view()) {
743 dock_controllers_
.push_back(controller
);
744 dock_windows_
.insert(controller
->popup_view());
749 } else if (dock_info_
.type() != DockInfo::NONE
&&
750 !dock_controllers_
.empty()) {
751 // Current dock position is the same as last, update the controller's
752 // in_enable_area state as it may have changed.
753 dock_controllers_
.back()->UpdateInEnabledArea(dock_info_
.in_enable_area());
757 void TabDragController::SaveFocus() {
758 DCHECK(source_tabstrip_
);
759 views::View
* focused_view
=
760 source_tabstrip_
->GetFocusManager()->GetFocusedView();
762 views::ViewStorage::GetInstance()->StoreView(old_focused_view_id_
,
764 source_tabstrip_
->GetFocusManager()->SetFocusedView(source_tabstrip_
);
765 // WARNING: we may have been deleted.
768 void TabDragController::RestoreFocus() {
769 if (attached_tabstrip_
!= source_tabstrip_
)
771 views::View
* old_focused_view
=
772 views::ViewStorage::GetInstance()->RetrieveView(
773 old_focused_view_id_
);
774 if (!old_focused_view
)
776 old_focused_view
->GetFocusManager()->SetFocusedView(old_focused_view
);
779 bool TabDragController::CanStartDrag(const gfx::Point
& point_in_screen
) const {
780 // Determine if the mouse has moved beyond a minimum elasticity distance in
781 // any direction from the starting point.
782 static const int kMinimumDragDistance
= 10;
783 int x_offset
= abs(point_in_screen
.x() - start_point_in_screen_
.x());
784 int y_offset
= abs(point_in_screen
.y() - start_point_in_screen_
.y());
785 return sqrt(pow(static_cast<float>(x_offset
), 2) +
786 pow(static_cast<float>(y_offset
), 2)) > kMinimumDragDistance
;
789 void TabDragController::ContinueDragging(const gfx::Point
& point_in_screen
) {
790 DCHECK(!detach_into_browser_
|| attached_tabstrip_
);
792 TabStrip
* target_tabstrip
= detach_behavior_
== DETACHABLE
?
793 GetTargetTabStripForPoint(point_in_screen
) : source_tabstrip_
;
794 bool tab_strip_changed
= (target_tabstrip
!= attached_tabstrip_
);
796 if (attached_tabstrip_
) {
797 int move_delta
= point_in_screen
.x() - last_point_in_screen_
.x();
799 mouse_move_direction_
|= kMovedMouseRight
;
800 else if (move_delta
< 0)
801 mouse_move_direction_
|= kMovedMouseLeft
;
803 last_point_in_screen_
= point_in_screen
;
805 if (tab_strip_changed
) {
806 if (detach_into_browser_
&&
807 DragBrowserToNewTabStrip(target_tabstrip
, point_in_screen
) ==
808 DRAG_BROWSER_RESULT_STOP
) {
810 } else if (!detach_into_browser_
) {
811 if (attached_tabstrip_
)
812 Detach(RELEASE_CAPTURE
);
814 Attach(target_tabstrip
, point_in_screen
);
817 if (view_
.get() || is_dragging_window_
) {
818 static_cast<base::Timer
*>(&bring_to_front_timer_
)->Start(FROM_HERE
,
819 base::TimeDelta::FromMilliseconds(kBringToFrontDelay
),
820 base::Bind(&TabDragController::BringWindowUnderPointToFront
,
821 base::Unretained(this), point_in_screen
));
824 UpdateDockInfo(point_in_screen
);
826 if (!is_dragging_window_
) {
827 if (attached_tabstrip_
) {
829 DragActiveTabStacked(point_in_screen
);
831 MoveAttached(point_in_screen
);
832 if (tab_strip_changed
) {
833 // Move the corresponding window to the front. We do this after the
834 // move as on windows activate triggers a synchronous paint.
835 attached_tabstrip_
->GetWidget()->Activate();
839 MoveDetached(point_in_screen
);
844 TabDragController::DragBrowserResultType
845 TabDragController::DragBrowserToNewTabStrip(
846 TabStrip
* target_tabstrip
,
847 const gfx::Point
& point_in_screen
) {
848 if (!target_tabstrip
) {
849 DetachIntoNewBrowserAndRunMoveLoop(point_in_screen
);
850 return DRAG_BROWSER_RESULT_STOP
;
852 if (is_dragging_window_
) {
853 // ReleaseCapture() is going to result in calling back to us (because it
854 // results in a move). That'll cause all sorts of problems. Reset the
855 // observer so we don't get notified and process the event.
856 if (host_desktop_type_
== chrome::HOST_DESKTOP_TYPE_ASH
) {
857 move_loop_widget_
->RemoveObserver(this);
858 move_loop_widget_
= NULL
;
860 views::Widget
* browser_widget
= GetAttachedBrowserWidget();
861 // Need to release the drag controller before starting the move loop as it's
862 // going to trigger capture lost, which cancels drag.
863 attached_tabstrip_
->ReleaseDragController();
864 target_tabstrip
->OwnDragController(this);
865 // Disable animations so that we don't see a close animation on aero.
866 browser_widget
->SetVisibilityChangedAnimationsEnabled(false);
867 // For aura we can't release capture, otherwise it'll cancel a gesture.
868 // Instead we have to directly change capture.
869 if (host_desktop_type_
== chrome::HOST_DESKTOP_TYPE_ASH
)
870 target_tabstrip
->GetWidget()->SetCapture(attached_tabstrip_
);
872 browser_widget
->ReleaseCapture();
873 // The window is going away. Since the drag is still on going we don't want
874 // that to effect the position of any windows.
875 SetWindowPositionManaged(browser_widget
->GetNativeView(), false);
877 // EndMoveLoop is going to snap the window back to its original location.
878 // Hide it so users don't see this.
879 browser_widget
->Hide();
880 browser_widget
->EndMoveLoop();
882 // Ideally we would always swap the tabs now, but on non-ash it seems that
883 // running the move loop implicitly activates the window when done, leading
884 // to all sorts of flicker. So, on non-ash, instead we process the move
885 // after the loop completes. But on chromeos, we can do tab swapping now to
886 // avoid the tab flashing issue(crbug.com/116329).
887 if (host_desktop_type_
== chrome::HOST_DESKTOP_TYPE_ASH
) {
888 is_dragging_window_
= false;
889 Detach(DONT_RELEASE_CAPTURE
);
890 Attach(target_tabstrip
, point_in_screen
);
891 // Move the tabs into position.
892 MoveAttached(point_in_screen
);
893 attached_tabstrip_
->GetWidget()->Activate();
895 tab_strip_to_attach_to_after_exit_
= target_tabstrip
;
898 waiting_for_run_loop_to_exit_
= true;
899 end_run_loop_behavior_
= END_RUN_LOOP_CONTINUE_DRAGGING
;
900 return DRAG_BROWSER_RESULT_STOP
;
902 Detach(DONT_RELEASE_CAPTURE
);
903 Attach(target_tabstrip
, point_in_screen
);
904 return DRAG_BROWSER_RESULT_CONTINUE
;
907 void TabDragController::DragActiveTabStacked(
908 const gfx::Point
& point_in_screen
) {
909 if (attached_tabstrip_
->tab_count() !=
910 static_cast<int>(initial_tab_positions_
.size()))
911 return; // TODO: should cancel drag if this happens.
913 int delta
= point_in_screen
.x() - start_point_in_screen_
.x();
914 attached_tabstrip_
->DragActiveTab(initial_tab_positions_
, delta
);
917 void TabDragController::MoveAttachedToNextStackedIndex(
918 const gfx::Point
& point_in_screen
) {
919 int index
= attached_tabstrip_
->touch_layout_
->active_index();
920 if (index
+ 1 >= attached_tabstrip_
->tab_count())
923 GetModel(attached_tabstrip_
)->MoveSelectedTabsTo(index
+ 1);
924 StartMoveStackedTimerIfNecessary(point_in_screen
,
925 kMoveAttachedSubsequentDelay
);
928 void TabDragController::MoveAttachedToPreviousStackedIndex(
929 const gfx::Point
& point_in_screen
) {
930 int index
= attached_tabstrip_
->touch_layout_
->active_index();
931 if (index
<= attached_tabstrip_
->GetMiniTabCount())
934 GetModel(attached_tabstrip_
)->MoveSelectedTabsTo(index
- 1);
935 StartMoveStackedTimerIfNecessary(point_in_screen
,
936 kMoveAttachedSubsequentDelay
);
939 void TabDragController::MoveAttached(const gfx::Point
& point_in_screen
) {
940 DCHECK(attached_tabstrip_
);
941 DCHECK(!view_
.get());
942 DCHECK(!is_dragging_window_
);
944 gfx::Point dragged_view_point
= GetAttachedDragPoint(point_in_screen
);
946 // Determine the horizontal move threshold. This is dependent on the width
947 // of tabs. The smaller the tabs compared to the standard size, the smaller
949 int threshold
= kHorizontalMoveThreshold
;
950 if (!attached_tabstrip_
->touch_layout_
.get()) {
951 double unselected
, selected
;
952 attached_tabstrip_
->GetCurrentTabWidths(&unselected
, &selected
);
953 double ratio
= unselected
/ Tab::GetStandardSize().width();
954 threshold
= static_cast<int>(ratio
* kHorizontalMoveThreshold
);
956 // else case: touch tabs never shrink.
958 std::vector
<Tab
*> tabs(drag_data_
.size());
959 for (size_t i
= 0; i
< drag_data_
.size(); ++i
)
960 tabs
[i
] = drag_data_
[i
].attached_tab
;
962 bool did_layout
= false;
963 // Update the model, moving the WebContents from one index to another. Do this
964 // only if we have moved a minimum distance since the last reorder (to prevent
965 // jitter) or if this the first move and the tabs are not consecutive.
966 if ((abs(point_in_screen
.x() - last_move_screen_loc_
) > threshold
||
967 (initial_move_
&& !AreTabsConsecutive()))) {
968 TabStripModel
* attached_model
= GetModel(attached_tabstrip_
);
969 gfx::Rect bounds
= GetDraggedViewTabStripBounds(dragged_view_point
);
970 int to_index
= GetInsertionIndexForDraggedBounds(bounds
);
971 WebContents
* last_contents
= drag_data_
[drag_data_
.size() - 1].contents
;
972 int index_of_last_item
=
973 attached_model
->GetIndexOfWebContents(last_contents
);
975 // TabStrip determines if the tabs needs to be animated based on model
976 // position. This means we need to invoke LayoutDraggedTabsAt before
977 // changing the model.
978 attached_tabstrip_
->LayoutDraggedTabsAt(
979 tabs
, source_tab_drag_data()->attached_tab
, dragged_view_point
,
983 attached_model
->MoveSelectedTabsTo(to_index
);
985 // Move may do nothing in certain situations (such as when dragging pinned
986 // tabs). Make sure the tabstrip actually changed before updating
987 // last_move_screen_loc_.
988 if (index_of_last_item
!=
989 attached_model
->GetIndexOfWebContents(last_contents
)) {
990 last_move_screen_loc_
= point_in_screen
.x();
995 attached_tabstrip_
->LayoutDraggedTabsAt(
996 tabs
, source_tab_drag_data()->attached_tab
, dragged_view_point
,
1000 StartMoveStackedTimerIfNecessary(point_in_screen
, kMoveAttachedInitialDelay
);
1002 initial_move_
= false;
1005 void TabDragController::MoveDetached(const gfx::Point
& point_in_screen
) {
1006 DCHECK(!attached_tabstrip_
);
1007 DCHECK(view_
.get());
1008 DCHECK(!is_dragging_window_
);
1010 // Move the View. There are no changes to the model if we're detached.
1011 view_
->MoveTo(point_in_screen
);
1014 void TabDragController::StartMoveStackedTimerIfNecessary(
1015 const gfx::Point
& point_in_screen
,
1017 DCHECK(attached_tabstrip_
);
1019 StackedTabStripLayout
* touch_layout
= attached_tabstrip_
->touch_layout_
.get();
1023 gfx::Point dragged_view_point
= GetAttachedDragPoint(point_in_screen
);
1024 gfx::Rect bounds
= GetDraggedViewTabStripBounds(dragged_view_point
);
1025 int index
= touch_layout
->active_index();
1026 if (ShouldDragToNextStackedTab(bounds
, index
)) {
1027 static_cast<base::Timer
*>(&move_stacked_timer_
)->Start(
1029 base::TimeDelta::FromMilliseconds(delay_ms
),
1030 base::Bind(&TabDragController::MoveAttachedToNextStackedIndex
,
1031 base::Unretained(this), point_in_screen
));
1032 } else if (ShouldDragToPreviousStackedTab(bounds
, index
)) {
1033 static_cast<base::Timer
*>(&move_stacked_timer_
)->Start(
1035 base::TimeDelta::FromMilliseconds(delay_ms
),
1036 base::Bind(&TabDragController::MoveAttachedToPreviousStackedIndex
,
1037 base::Unretained(this), point_in_screen
));
1041 TabDragController::DetachPosition
TabDragController::GetDetachPosition(
1042 const gfx::Point
& point_in_screen
) {
1043 DCHECK(attached_tabstrip_
);
1044 gfx::Point
attached_point(point_in_screen
);
1045 views::View::ConvertPointToTarget(NULL
, attached_tabstrip_
, &attached_point
);
1046 if (attached_point
.x() < 0)
1047 return DETACH_BEFORE
;
1048 if (attached_point
.x() >= attached_tabstrip_
->width())
1049 return DETACH_AFTER
;
1050 return DETACH_ABOVE_OR_BELOW
;
1053 DockInfo
TabDragController::GetDockInfoAtPoint(
1054 const gfx::Point
& point_in_screen
) {
1055 // TODO: add support for dock info when |detach_into_browser_| is true.
1056 if (attached_tabstrip_
|| detach_into_browser_
) {
1057 // If the mouse is over a tab strip, don't offer a dock position.
1061 if (dock_info_
.IsValidForPoint(point_in_screen
)) {
1062 // It's possible any given screen coordinate has multiple docking
1063 // positions. Check the current info first to avoid having the docking
1064 // position bounce around.
1068 gfx::NativeView dragged_view
= view_
->GetWidget()->GetNativeView();
1069 dock_windows_
.insert(dragged_view
);
1070 DockInfo info
= DockInfo::GetDockInfoAtPoint(
1074 dock_windows_
.erase(dragged_view
);
1078 TabStrip
* TabDragController::GetTargetTabStripForPoint(
1079 const gfx::Point
& point_in_screen
) {
1080 if (move_only() && attached_tabstrip_
) {
1081 DCHECK_EQ(DETACHABLE
, detach_behavior_
);
1082 // move_only() is intended for touch, in which case we only want to detach
1083 // if the touch point moves significantly in the vertical distance.
1084 gfx::Rect tabstrip_bounds
= GetViewScreenBounds(attached_tabstrip_
);
1085 if (DoesRectContainVerticalPointExpanded(tabstrip_bounds
,
1086 kTouchVerticalDetachMagnetism
,
1087 point_in_screen
.y()))
1088 return attached_tabstrip_
;
1090 gfx::NativeView dragged_view
= NULL
;
1092 dragged_view
= view_
->GetWidget()->GetNativeView();
1093 else if (is_dragging_window_
)
1094 dragged_view
= attached_tabstrip_
->GetWidget()->GetNativeView();
1096 dock_windows_
.insert(dragged_view
);
1097 gfx::NativeWindow local_window
=
1098 DockInfo::GetLocalProcessWindowAtPoint(
1103 dock_windows_
.erase(dragged_view
);
1104 TabStrip
* tab_strip
= GetTabStripForWindow(local_window
);
1105 if (tab_strip
&& DoesTabStripContain(tab_strip
, point_in_screen
))
1107 return is_dragging_window_
? attached_tabstrip_
: NULL
;
1110 TabStrip
* TabDragController::GetTabStripForWindow(gfx::NativeWindow window
) {
1113 BrowserView
* browser_view
=
1114 BrowserView::GetBrowserViewForNativeWindow(window
);
1115 // We don't allow drops on windows that don't have tabstrips.
1116 if (!browser_view
||
1117 !browser_view
->browser()->SupportsWindowFeature(
1118 Browser::FEATURE_TABSTRIP
))
1121 TabStrip
* other_tabstrip
= browser_view
->tabstrip();
1122 TabStrip
* tab_strip
=
1123 attached_tabstrip_
? attached_tabstrip_
: source_tabstrip_
;
1126 return other_tabstrip
->controller()->IsCompatibleWith(tab_strip
) ?
1127 other_tabstrip
: NULL
;
1130 bool TabDragController::DoesTabStripContain(
1132 const gfx::Point
& point_in_screen
) const {
1133 // Make sure the specified screen point is actually within the bounds of the
1134 // specified tabstrip...
1135 gfx::Rect tabstrip_bounds
= GetViewScreenBounds(tabstrip
);
1136 return point_in_screen
.x() < tabstrip_bounds
.right() &&
1137 point_in_screen
.x() >= tabstrip_bounds
.x() &&
1138 DoesRectContainVerticalPointExpanded(tabstrip_bounds
,
1139 kVerticalDetachMagnetism
,
1140 point_in_screen
.y());
1143 void TabDragController::Attach(TabStrip
* attached_tabstrip
,
1144 const gfx::Point
& point_in_screen
) {
1145 DCHECK(!attached_tabstrip_
); // We should already have detached by the time
1148 attached_tabstrip_
= attached_tabstrip
;
1150 // And we don't need the dragged view.
1153 std::vector
<Tab
*> tabs
=
1154 GetTabsMatchingDraggedContents(attached_tabstrip_
);
1157 // Transitioning from detached to attached to a new tabstrip. Add tabs to
1160 selection_model_before_attach_
.Copy(attached_tabstrip
->GetSelectionModel());
1162 if (!detach_into_browser_
) {
1163 // Remove ourselves as the delegate now that the dragged WebContents is
1164 // being inserted back into a Browser.
1165 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1166 drag_data_
[i
].contents
->SetDelegate(NULL
);
1167 drag_data_
[i
].original_delegate
= NULL
;
1170 // Return the WebContents to normalcy.
1171 source_dragged_contents()->DecrementCapturerCount();
1174 // Inserting counts as a move. We don't want the tabs to jitter when the
1175 // user moves the tab immediately after attaching it.
1176 last_move_screen_loc_
= point_in_screen
.x();
1178 // Figure out where to insert the tab based on the bounds of the dragged
1179 // representation and the ideal bounds of the other Tabs already in the
1180 // strip. ("ideal bounds" are stable even if the Tabs' actual bounds are
1181 // changing due to animation).
1182 gfx::Point
tab_strip_point(point_in_screen
);
1183 views::View::ConvertPointToTarget(NULL
, attached_tabstrip_
,
1185 tab_strip_point
.set_x(
1186 attached_tabstrip_
->GetMirroredXInView(tab_strip_point
.x()));
1187 tab_strip_point
.Offset(-mouse_offset_
.x(), -mouse_offset_
.y());
1188 gfx::Rect bounds
= GetDraggedViewTabStripBounds(tab_strip_point
);
1189 int index
= GetInsertionIndexForDraggedBounds(bounds
);
1190 base::AutoReset
<bool> setter(&is_mutating_
, true);
1191 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1192 int add_types
= TabStripModel::ADD_NONE
;
1193 if (attached_tabstrip_
->touch_layout_
.get()) {
1194 // StackedTabStripLayout positions relative to the active tab, if we
1195 // don't add the tab as active things bounce around.
1196 DCHECK_EQ(1u, drag_data_
.size());
1197 add_types
|= TabStripModel::ADD_ACTIVE
;
1199 if (drag_data_
[i
].pinned
)
1200 add_types
|= TabStripModel::ADD_PINNED
;
1201 GetModel(attached_tabstrip_
)->InsertWebContentsAt(
1202 index
+ i
, drag_data_
[i
].contents
, add_types
);
1205 tabs
= GetTabsMatchingDraggedContents(attached_tabstrip_
);
1207 DCHECK_EQ(tabs
.size(), drag_data_
.size());
1208 for (size_t i
= 0; i
< drag_data_
.size(); ++i
)
1209 drag_data_
[i
].attached_tab
= tabs
[i
];
1211 attached_tabstrip_
->StartedDraggingTabs(tabs
);
1213 ResetSelection(GetModel(attached_tabstrip_
));
1215 // The size of the dragged tab may have changed. Adjust the x offset so that
1216 // ratio of mouse_offset_ to original width is maintained.
1217 std::vector
<Tab
*> tabs_to_source(tabs
);
1218 tabs_to_source
.erase(tabs_to_source
.begin() + source_tab_index_
+ 1,
1219 tabs_to_source
.end());
1220 int new_x
= attached_tabstrip_
->GetSizeNeededForTabs(tabs_to_source
) -
1221 tabs
[source_tab_index_
]->width() +
1222 static_cast<int>(offset_to_width_ratio_
*
1223 tabs
[source_tab_index_
]->width());
1224 mouse_offset_
.set_x(new_x
);
1226 // Transfer ownership of us to the new tabstrip as well as making sure the
1227 // window has capture. This is important so that if activation changes the
1228 // drag isn't prematurely canceled.
1229 if (detach_into_browser_
) {
1230 attached_tabstrip_
->GetWidget()->SetCapture(attached_tabstrip_
);
1231 attached_tabstrip_
->OwnDragController(this);
1234 // Redirect all mouse events to the TabStrip so that the tab that originated
1235 // the drag can safely be deleted.
1236 if (detach_into_browser_
|| attached_tabstrip_
== source_tabstrip_
) {
1237 static_cast<views::internal::RootView
*>(
1238 attached_tabstrip_
->GetWidget()->GetRootView())->SetMouseHandler(
1239 attached_tabstrip_
);
1243 void TabDragController::Detach(ReleaseCapture release_capture
) {
1244 // When the user detaches we assume they want to reorder.
1245 move_behavior_
= REORDER
;
1247 // Release ownership of the drag controller and mouse capture. When we
1248 // reattach ownership is transfered.
1249 if (detach_into_browser_
) {
1250 attached_tabstrip_
->ReleaseDragController();
1251 if (release_capture
== RELEASE_CAPTURE
)
1252 attached_tabstrip_
->GetWidget()->ReleaseCapture();
1255 mouse_move_direction_
= kMovedMouseLeft
| kMovedMouseRight
;
1257 // Prevent the WebContents HWND from being hidden by any of the model
1258 // operations performed during the drag.
1259 if (!detach_into_browser_
)
1260 source_dragged_contents()->IncrementCapturerCount();
1262 std::vector
<gfx::Rect
> drag_bounds
= CalculateBoundsForDraggedTabs(0);
1263 TabStripModel
* attached_model
= GetModel(attached_tabstrip_
);
1264 std::vector
<TabRendererData
> tab_data
;
1265 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1266 tab_data
.push_back(drag_data_
[i
].attached_tab
->data());
1267 int index
= attached_model
->GetIndexOfWebContents(drag_data_
[i
].contents
);
1268 DCHECK_NE(-1, index
);
1270 // Hide the tab so that the user doesn't see it animate closed.
1271 drag_data_
[i
].attached_tab
->SetVisible(false);
1273 attached_model
->DetachWebContentsAt(index
);
1275 // Detaching resets the delegate, but we still want to be the delegate.
1276 if (!detach_into_browser_
)
1277 drag_data_
[i
].contents
->SetDelegate(this);
1279 // Detaching may end up deleting the tab, drop references to it.
1280 drag_data_
[i
].attached_tab
= NULL
;
1283 // If we've removed the last Tab from the TabStrip, hide the frame now.
1284 if (!attached_model
->empty()) {
1285 if (!selection_model_before_attach_
.empty() &&
1286 selection_model_before_attach_
.active() >= 0 &&
1287 selection_model_before_attach_
.active() < attached_model
->count()) {
1288 // Restore the selection.
1289 attached_model
->SetSelectionFromModel(selection_model_before_attach_
);
1290 } else if (attached_tabstrip_
== source_tabstrip_
&&
1291 !initial_selection_model_
.empty()) {
1292 // First time detaching from the source tabstrip. Reset selection model to
1293 // initial_selection_model_. Before resetting though we have to remove all
1294 // the tabs from initial_selection_model_ as it was created with the tabs
1296 ui::ListSelectionModel selection_model
;
1297 selection_model
.Copy(initial_selection_model_
);
1298 for (DragData::const_reverse_iterator
i(drag_data_
.rbegin());
1299 i
!= drag_data_
.rend(); ++i
) {
1300 selection_model
.DecrementFrom(i
->source_model_index
);
1302 // We may have cleared out the selection model. Only reset it if it
1303 // contains something.
1304 if (!selection_model
.empty())
1305 attached_model
->SetSelectionFromModel(selection_model
);
1307 } else if (!detach_into_browser_
) {
1311 // Create the dragged view.
1312 if (!detach_into_browser_
)
1313 CreateDraggedView(tab_data
, drag_bounds
);
1315 attached_tabstrip_
->DraggedTabsDetached();
1316 attached_tabstrip_
= NULL
;
1319 void TabDragController::DetachIntoNewBrowserAndRunMoveLoop(
1320 const gfx::Point
& point_in_screen
) {
1321 if (GetModel(attached_tabstrip_
)->count() ==
1322 static_cast<int>(drag_data_
.size())) {
1323 // All the tabs in a browser are being dragged but all the tabs weren't
1324 // initially being dragged. For this to happen the user would have to
1325 // start dragging a set of tabs, the other tabs close, then detach.
1326 RunMoveLoop(GetWindowOffset(point_in_screen
));
1330 // Create a new browser to house the dragged tabs and have the OS run a move
1332 gfx::Point attached_point
= GetAttachedDragPoint(point_in_screen
);
1334 // Calculate the bounds for the tabs from the attached_tab_strip. We do this
1335 // so that the tabs don't change size when detached.
1336 std::vector
<gfx::Rect
> drag_bounds
=
1337 CalculateBoundsForDraggedTabs(attached_point
.x());
1339 // Stash the current window size and tab area width.
1340 gfx::Size source_size
=
1341 attached_tabstrip_
->GetWidget()->GetWindowBoundsInScreen().size();
1342 int available_source_width
= attached_tabstrip_
->tab_area_width();
1344 gfx::Vector2d drag_offset
;
1345 Browser
* browser
= CreateBrowserForDrag(
1346 attached_tabstrip_
, point_in_screen
, &drag_offset
, &drag_bounds
);
1347 Detach(DONT_RELEASE_CAPTURE
);
1348 BrowserView
* dragged_browser_view
=
1349 BrowserView::GetBrowserViewForBrowser(browser
);
1350 dragged_browser_view
->GetWidget()->SetVisibilityChangedAnimationsEnabled(
1352 Attach(dragged_browser_view
->tabstrip(), gfx::Point());
1354 // If the window size has changed, the tab positioning will be quite off.
1356 attached_tabstrip_
->GetWidget()->GetWindowBoundsInScreen().size()) {
1357 // First, scale the drag bounds such that they fit within the new window
1358 // while maintaining the same relative positions. This scales the tabs
1359 // down so that they occupy the same relative width on the new tab strip,
1360 // clamping to minimum tab width.
1361 int available_attached_width
= attached_tabstrip_
->tab_area_width();
1363 static_cast<float>(available_attached_width
) / available_source_width
;
1364 int x_offset
= std::ceil((1.0 - x_scale
) * drag_bounds
[0].x());
1365 int accumulated_width_offset
= 0;
1366 for (size_t i
= 0; i
< drag_bounds
.size(); ++i
) {
1367 gfx::Rect
& tab_bounds
= drag_bounds
[i
];
1368 tab_bounds
.Offset(-(x_offset
+ accumulated_width_offset
), 0);
1369 int old_width
= tab_bounds
.width();
1370 int min_width
= (i
== source_tab_index_
) ?
1371 drag_data_
[i
].attached_tab
->GetMinimumSelectedSize().width() :
1372 drag_data_
[i
].attached_tab
->GetMinimumUnselectedSize().width();
1374 std::max(min_width
, static_cast<int>(std::ceil(old_width
* x_scale
)));
1375 tab_bounds
.set_width(new_width
);
1376 accumulated_width_offset
+= (old_width
- tab_bounds
.width());
1379 // Next, re-position the restored window such that the tab that was dragged
1380 // remains centered under the mouse cursor. The two offsets needed here are
1381 // the offset of the dragged tab in widget coordinates, and half the dragged
1382 // tab width. The sum of these is the horizontal distance from the mouse
1383 // cursor to the window edge.
1384 gfx::Point
offset(drag_bounds
[source_tab_index_
].origin());
1385 views::View::ConvertPointToWidget(attached_tabstrip_
, &offset
);
1386 int half_tab_width
= drag_bounds
[source_tab_index_
].width() / 2;
1387 gfx::Rect new_bounds
= browser
->window()->GetBounds();
1388 new_bounds
.set_x(point_in_screen
.x() - offset
.x() - half_tab_width
);
1390 // To account for the extra vertical on restored windows that is absent
1391 // on maximized windows, add an additional vertical offset extracted from
1393 new_bounds
.Offset(0, -attached_tabstrip_
->button_v_offset());
1394 browser
->window()->SetBounds(new_bounds
);
1397 attached_tabstrip_
->SetTabBoundsForDrag(drag_bounds
);
1399 WindowPositionManagedUpdater updater
;
1400 dragged_browser_view
->GetWidget()->AddObserver(&updater
);
1401 browser
->window()->Show();
1402 dragged_browser_view
->GetWidget()->RemoveObserver(&updater
);
1404 browser
->window()->Activate();
1405 dragged_browser_view
->GetWidget()->SetVisibilityChangedAnimationsEnabled(
1407 RunMoveLoop(drag_offset
);
1410 void TabDragController::RunMoveLoop(const gfx::Vector2d
& drag_offset
) {
1411 // If the user drags the whole window we'll assume they are going to attach to
1412 // another window and therefore want to reorder.
1413 move_behavior_
= REORDER
;
1415 move_loop_widget_
= GetAttachedBrowserWidget();
1416 DCHECK(move_loop_widget_
);
1417 SetTrackedByWorkspace(move_loop_widget_
->GetNativeView(), false);
1418 move_loop_widget_
->AddObserver(this);
1419 is_dragging_window_
= true;
1420 bool destroyed
= false;
1421 destroyed_
= &destroyed
;
1422 // Running the move loop releases mouse capture on non-ash, which triggers
1423 // destroying the drag loop. Release mouse capture ourself before this while
1424 // the DragController isn't owned by the TabStrip.
1425 if (host_desktop_type_
!= chrome::HOST_DESKTOP_TYPE_ASH
) {
1426 attached_tabstrip_
->ReleaseDragController();
1427 attached_tabstrip_
->GetWidget()->ReleaseCapture();
1428 attached_tabstrip_
->OwnDragController(this);
1430 const views::Widget::MoveLoopSource move_loop_source
=
1431 event_source_
== EVENT_SOURCE_MOUSE
?
1432 views::Widget::MOVE_LOOP_SOURCE_MOUSE
:
1433 views::Widget::MOVE_LOOP_SOURCE_TOUCH
;
1434 views::Widget::MoveLoopResult result
=
1435 move_loop_widget_
->RunMoveLoop(drag_offset
, move_loop_source
);
1436 content::NotificationService::current()->Notify(
1437 chrome::NOTIFICATION_TAB_DRAG_LOOP_DONE
,
1438 content::NotificationService::AllBrowserContextsAndSources(),
1439 content::NotificationService::NoDetails());
1444 // Under chromeos we immediately set the |move_loop_widget_| to NULL.
1445 if (move_loop_widget_
) {
1446 move_loop_widget_
->RemoveObserver(this);
1447 move_loop_widget_
= NULL
;
1449 is_dragging_window_
= false;
1450 waiting_for_run_loop_to_exit_
= false;
1451 if (end_run_loop_behavior_
== END_RUN_LOOP_CONTINUE_DRAGGING
) {
1452 end_run_loop_behavior_
= END_RUN_LOOP_STOP_DRAGGING
;
1453 if (tab_strip_to_attach_to_after_exit_
) {
1454 gfx::Point
point_in_screen(GetCursorScreenPoint());
1455 Detach(DONT_RELEASE_CAPTURE
);
1456 Attach(tab_strip_to_attach_to_after_exit_
, point_in_screen
);
1457 // Move the tabs into position.
1458 MoveAttached(point_in_screen
);
1459 attached_tabstrip_
->GetWidget()->Activate();
1460 tab_strip_to_attach_to_after_exit_
= NULL
;
1462 DCHECK(attached_tabstrip_
);
1463 attached_tabstrip_
->GetWidget()->SetCapture(attached_tabstrip_
);
1464 } else if (active_
) {
1465 EndDrag(result
== views::Widget::MOVE_LOOP_CANCELED
?
1466 END_DRAG_CANCEL
: END_DRAG_COMPLETE
);
1470 int TabDragController::GetInsertionIndexFrom(const gfx::Rect
& dragged_bounds
,
1473 for (int i
= start
, tab_count
= attached_tabstrip_
->tab_count();
1474 i
>= 0 && i
< tab_count
; i
+= delta
) {
1475 const gfx::Rect
& ideal_bounds
= attached_tabstrip_
->ideal_bounds(i
);
1476 gfx::Rect left_half
, right_half
;
1477 ideal_bounds
.SplitVertically(&left_half
, &right_half
);
1478 if (dragged_bounds
.x() >= right_half
.x() &&
1479 dragged_bounds
.x() < right_half
.right()) {
1481 } else if (dragged_bounds
.x() >= left_half
.x() &&
1482 dragged_bounds
.x() < left_half
.right()) {
1489 int TabDragController::GetInsertionIndexForDraggedBounds(
1490 const gfx::Rect
& dragged_bounds
) const {
1492 if (attached_tabstrip_
->touch_layout_
.get()) {
1493 index
= GetInsertionIndexForDraggedBoundsStacked(dragged_bounds
);
1495 // Only move the tab to the left/right if the user actually moved the
1496 // mouse that way. This is necessary as tabs with stacked tabs
1497 // before/after them have multiple drag positions.
1498 int active_index
= attached_tabstrip_
->touch_layout_
->active_index();
1499 if ((index
< active_index
&&
1500 (mouse_move_direction_
& kMovedMouseLeft
) == 0) ||
1501 (index
> active_index
&&
1502 (mouse_move_direction_
& kMovedMouseRight
) == 0)) {
1503 index
= active_index
;
1507 index
= GetInsertionIndexFrom(dragged_bounds
, 0, 1);
1510 int tab_count
= attached_tabstrip_
->tab_count();
1511 int right_tab_x
= tab_count
== 0 ? 0 :
1512 attached_tabstrip_
->ideal_bounds(tab_count
- 1).right();
1513 if (dragged_bounds
.right() > right_tab_x
) {
1514 index
= GetModel(attached_tabstrip_
)->count();
1520 if (!drag_data_
[0].attached_tab
) {
1521 // If 'attached_tab' is NULL, it means we're in the process of attaching and
1522 // don't need to constrain the index.
1526 int max_index
= GetModel(attached_tabstrip_
)->count() -
1527 static_cast<int>(drag_data_
.size());
1528 return std::max(0, std::min(max_index
, index
));
1531 bool TabDragController::ShouldDragToNextStackedTab(
1532 const gfx::Rect
& dragged_bounds
,
1534 if (index
+ 1 >= attached_tabstrip_
->tab_count() ||
1535 !attached_tabstrip_
->touch_layout_
->IsStacked(index
+ 1) ||
1536 (mouse_move_direction_
& kMovedMouseRight
) == 0)
1539 int active_x
= attached_tabstrip_
->ideal_bounds(index
).x();
1540 int next_x
= attached_tabstrip_
->ideal_bounds(index
+ 1).x();
1541 int mid_x
= std::min(next_x
- kStackedDistance
,
1542 active_x
+ (next_x
- active_x
) / 4);
1543 return dragged_bounds
.x() >= mid_x
;
1546 bool TabDragController::ShouldDragToPreviousStackedTab(
1547 const gfx::Rect
& dragged_bounds
,
1549 if (index
- 1 < attached_tabstrip_
->GetMiniTabCount() ||
1550 !attached_tabstrip_
->touch_layout_
->IsStacked(index
- 1) ||
1551 (mouse_move_direction_
& kMovedMouseLeft
) == 0)
1554 int active_x
= attached_tabstrip_
->ideal_bounds(index
).x();
1555 int previous_x
= attached_tabstrip_
->ideal_bounds(index
- 1).x();
1556 int mid_x
= std::max(previous_x
+ kStackedDistance
,
1557 active_x
- (active_x
- previous_x
) / 4);
1558 return dragged_bounds
.x() <= mid_x
;
1561 int TabDragController::GetInsertionIndexForDraggedBoundsStacked(
1562 const gfx::Rect
& dragged_bounds
) const {
1563 StackedTabStripLayout
* touch_layout
= attached_tabstrip_
->touch_layout_
.get();
1564 int active_index
= touch_layout
->active_index();
1565 // Search from the active index to the front of the tabstrip. Do this as tabs
1566 // overlap each other from the active index.
1567 int index
= GetInsertionIndexFrom(dragged_bounds
, active_index
, -1);
1568 if (index
!= active_index
)
1571 return GetInsertionIndexFrom(dragged_bounds
, active_index
+ 1, 1);
1573 // The position to drag to corresponds to the active tab. If the next/previous
1574 // tab is stacked, then shorten the distance used to determine insertion
1575 // bounds. We do this as GetInsertionIndexFrom() uses the bounds of the
1576 // tabs. When tabs are stacked the next/previous tab is on top of the tab.
1577 if (active_index
+ 1 < attached_tabstrip_
->tab_count() &&
1578 touch_layout
->IsStacked(active_index
+ 1)) {
1579 index
= GetInsertionIndexFrom(dragged_bounds
, active_index
+ 1, 1);
1580 if (index
== -1 && ShouldDragToNextStackedTab(dragged_bounds
, active_index
))
1581 index
= active_index
+ 1;
1582 else if (index
== -1)
1583 index
= active_index
;
1584 } else if (ShouldDragToPreviousStackedTab(dragged_bounds
, active_index
)) {
1585 index
= active_index
- 1;
1590 gfx::Rect
TabDragController::GetDraggedViewTabStripBounds(
1591 const gfx::Point
& tab_strip_point
) {
1592 // attached_tab is NULL when inserting into a new tabstrip.
1593 if (source_tab_drag_data()->attached_tab
) {
1594 return gfx::Rect(tab_strip_point
.x(), tab_strip_point
.y(),
1595 source_tab_drag_data()->attached_tab
->width(),
1596 source_tab_drag_data()->attached_tab
->height());
1599 double sel_width
, unselected_width
;
1600 attached_tabstrip_
->GetCurrentTabWidths(&sel_width
, &unselected_width
);
1601 return gfx::Rect(tab_strip_point
.x(), tab_strip_point
.y(),
1602 static_cast<int>(sel_width
),
1603 Tab::GetStandardSize().height());
1606 gfx::Point
TabDragController::GetAttachedDragPoint(
1607 const gfx::Point
& point_in_screen
) {
1608 DCHECK(attached_tabstrip_
); // The tab must be attached.
1610 gfx::Point
tab_loc(point_in_screen
);
1611 views::View::ConvertPointToTarget(NULL
, attached_tabstrip_
, &tab_loc
);
1613 attached_tabstrip_
->GetMirroredXInView(tab_loc
.x()) - mouse_offset_
.x();
1615 // TODO: consider caching this.
1616 std::vector
<Tab
*> attached_tabs
;
1617 for (size_t i
= 0; i
< drag_data_
.size(); ++i
)
1618 attached_tabs
.push_back(drag_data_
[i
].attached_tab
);
1619 int size
= attached_tabstrip_
->GetSizeNeededForTabs(attached_tabs
);
1620 int max_x
= attached_tabstrip_
->width() - size
;
1621 return gfx::Point(std::min(std::max(x
, 0), max_x
), 0);
1624 std::vector
<Tab
*> TabDragController::GetTabsMatchingDraggedContents(
1625 TabStrip
* tabstrip
) {
1626 TabStripModel
* model
= GetModel(attached_tabstrip_
);
1627 std::vector
<Tab
*> tabs
;
1628 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1629 int model_index
= model
->GetIndexOfWebContents(drag_data_
[i
].contents
);
1630 if (model_index
== TabStripModel::kNoTab
)
1631 return std::vector
<Tab
*>();
1632 tabs
.push_back(tabstrip
->tab_at(model_index
));
1637 std::vector
<gfx::Rect
> TabDragController::CalculateBoundsForDraggedTabs(
1639 std::vector
<gfx::Rect
> drag_bounds
;
1640 std::vector
<Tab
*> attached_tabs
;
1641 for (size_t i
= 0; i
< drag_data_
.size(); ++i
)
1642 attached_tabs
.push_back(drag_data_
[i
].attached_tab
);
1643 attached_tabstrip_
->CalculateBoundsForDraggedTabs(attached_tabs
,
1645 if (x_offset
!= 0) {
1646 for (size_t i
= 0; i
< drag_bounds
.size(); ++i
)
1647 drag_bounds
[i
].set_x(drag_bounds
[i
].x() + x_offset
);
1652 void TabDragController::EndDragImpl(EndDragType type
) {
1656 bring_to_front_timer_
.Stop();
1657 move_stacked_timer_
.Stop();
1659 if (is_dragging_window_
) {
1660 // SetTrackedByWorkspace() may call us back (by way of the window bounds
1661 // changing). Set |waiting_for_run_loop_to_exit_| here so that if that
1662 // happens we ignore it.
1663 waiting_for_run_loop_to_exit_
= true;
1665 if (type
== NORMAL
|| (type
== TAB_DESTROYED
&& drag_data_
.size() > 1)) {
1666 SetTrackedByWorkspace(GetAttachedBrowserWidget()->GetNativeView(), true);
1667 SetWindowPositionManaged(GetAttachedBrowserWidget()->GetNativeView(),
1671 // End the nested drag loop.
1672 GetAttachedBrowserWidget()->EndMoveLoop();
1675 // Hide the current dock controllers.
1676 for (size_t i
= 0; i
< dock_controllers_
.size(); ++i
) {
1677 // Be sure and clear the controller first, that way if Hide ends up
1678 // deleting the controller it won't call us back.
1679 dock_controllers_
[i
]->clear_controller();
1680 dock_controllers_
[i
]->Hide();
1682 dock_controllers_
.clear();
1683 dock_windows_
.clear();
1685 if (type
!= TAB_DESTROYED
) {
1686 // We only finish up the drag if we were actually dragging. If start_drag_
1687 // is false, the user just clicked and released and didn't move the mouse
1688 // enough to trigger a drag.
1689 if (started_drag_
) {
1691 if (type
== CANCELED
)
1696 } else if (drag_data_
.size() > 1) {
1698 } // else case the only tab we were dragging was deleted. Nothing to do.
1700 if (!detach_into_browser_
)
1703 // Clear out drag data so we don't attempt to do anything with it.
1706 TabStrip
* owning_tabstrip
= (attached_tabstrip_
&& detach_into_browser_
) ?
1707 attached_tabstrip_
: source_tabstrip_
;
1708 owning_tabstrip
->DestroyDragController();
1711 void TabDragController::RevertDrag() {
1712 std::vector
<Tab
*> tabs
;
1713 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1714 if (drag_data_
[i
].contents
) {
1715 // Contents is NULL if a tab was destroyed while the drag was under way.
1716 tabs
.push_back(drag_data_
[i
].attached_tab
);
1721 bool restore_frame
= !detach_into_browser_
&&
1722 attached_tabstrip_
!= source_tabstrip_
;
1723 if (attached_tabstrip_
) {
1724 if (attached_tabstrip_
== source_tabstrip_
) {
1725 source_tabstrip_
->StoppedDraggingTabs(
1726 tabs
, initial_tab_positions_
, move_behavior_
== MOVE_VISIBILE_TABS
,
1729 attached_tabstrip_
->DraggedTabsDetached();
1733 if (initial_selection_model_
.empty())
1734 ResetSelection(GetModel(source_tabstrip_
));
1736 GetModel(source_tabstrip_
)->SetSelectionFromModel(initial_selection_model_
);
1738 // If we're not attached to any TabStrip, or attached to some other TabStrip,
1739 // we need to restore the bounds of the original TabStrip's frame, in case
1740 // it has been hidden.
1741 if (restore_frame
&& !restore_bounds_
.IsEmpty())
1742 source_tabstrip_
->GetWidget()->SetBounds(restore_bounds_
);
1744 if (detach_into_browser_
&& source_tabstrip_
)
1745 source_tabstrip_
->GetWidget()->Activate();
1747 // Return the WebContents to normalcy. If the tab was attached to a
1748 // TabStrip before the revert, the decrement has already occurred.
1749 // If the tab was destroyed, don't attempt to dereference the
1750 // WebContents pointer.
1751 if (!detach_into_browser_
&& !attached_tabstrip_
&& source_dragged_contents())
1752 source_dragged_contents()->DecrementCapturerCount();
1755 void TabDragController::ResetSelection(TabStripModel
* model
) {
1757 ui::ListSelectionModel selection_model
;
1758 bool has_one_valid_tab
= false;
1759 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1760 // |contents| is NULL if a tab was deleted out from under us.
1761 if (drag_data_
[i
].contents
) {
1762 int index
= model
->GetIndexOfWebContents(drag_data_
[i
].contents
);
1763 DCHECK_NE(-1, index
);
1764 selection_model
.AddIndexToSelection(index
);
1765 if (!has_one_valid_tab
|| i
== source_tab_index_
) {
1766 // Reset the active/lead to the first tab. If the source tab is still
1767 // valid we'll reset these again later on.
1768 selection_model
.set_active(index
);
1769 selection_model
.set_anchor(index
);
1770 has_one_valid_tab
= true;
1774 if (!has_one_valid_tab
)
1777 model
->SetSelectionFromModel(selection_model
);
1780 void TabDragController::RevertDragAt(size_t drag_index
) {
1781 DCHECK(started_drag_
);
1782 DCHECK(source_tabstrip_
);
1784 base::AutoReset
<bool> setter(&is_mutating_
, true);
1785 TabDragData
* data
= &(drag_data_
[drag_index
]);
1786 if (attached_tabstrip_
) {
1788 GetModel(attached_tabstrip_
)->GetIndexOfWebContents(data
->contents
);
1789 if (attached_tabstrip_
!= source_tabstrip_
) {
1790 // The Tab was inserted into another TabStrip. We need to put it back
1791 // into the original one.
1792 GetModel(attached_tabstrip_
)->DetachWebContentsAt(index
);
1793 // TODO(beng): (Cleanup) seems like we should use Attach() for this
1795 GetModel(source_tabstrip_
)->InsertWebContentsAt(
1796 data
->source_model_index
, data
->contents
,
1797 (data
->pinned
? TabStripModel::ADD_PINNED
: 0));
1799 // The Tab was moved within the TabStrip where the drag was initiated.
1800 // Move it back to the starting location.
1801 GetModel(source_tabstrip_
)->MoveWebContentsAt(
1802 index
, data
->source_model_index
, false);
1805 // The Tab was detached from the TabStrip where the drag began, and has not
1806 // been attached to any other TabStrip. We need to put it back into the
1808 GetModel(source_tabstrip_
)->InsertWebContentsAt(
1809 data
->source_model_index
, data
->contents
,
1810 (data
->pinned
? TabStripModel::ADD_PINNED
: 0));
1814 void TabDragController::CompleteDrag() {
1815 DCHECK(started_drag_
);
1817 if (attached_tabstrip_
) {
1818 attached_tabstrip_
->StoppedDraggingTabs(
1819 GetTabsMatchingDraggedContents(attached_tabstrip_
),
1820 initial_tab_positions_
,
1821 move_behavior_
== MOVE_VISIBILE_TABS
,
1824 if (dock_info_
.type() != DockInfo::NONE
) {
1825 switch (dock_info_
.type()) {
1826 case DockInfo::LEFT_OF_WINDOW
:
1827 content::RecordAction(UserMetricsAction("DockingWindow_Left"));
1830 case DockInfo::RIGHT_OF_WINDOW
:
1831 content::RecordAction(UserMetricsAction("DockingWindow_Right"));
1834 case DockInfo::BOTTOM_OF_WINDOW
:
1835 content::RecordAction(UserMetricsAction("DockingWindow_Bottom"));
1838 case DockInfo::TOP_OF_WINDOW
:
1839 content::RecordAction(UserMetricsAction("DockingWindow_Top"));
1842 case DockInfo::MAXIMIZE
:
1843 content::RecordAction(
1844 UserMetricsAction("DockingWindow_Maximize"));
1847 case DockInfo::LEFT_HALF
:
1848 content::RecordAction(
1849 UserMetricsAction("DockingWindow_LeftHalf"));
1852 case DockInfo::RIGHT_HALF
:
1853 content::RecordAction(
1854 UserMetricsAction("DockingWindow_RightHalf"));
1857 case DockInfo::BOTTOM_HALF
:
1858 content::RecordAction(
1859 UserMetricsAction("DockingWindow_BottomHalf"));
1867 // Compel the model to construct a new window for the detached
1869 views::Widget
* widget
= source_tabstrip_
->GetWidget();
1870 gfx::Rect
window_bounds(widget
->GetRestoredBounds());
1871 window_bounds
.set_origin(GetWindowCreatePoint(last_point_in_screen_
));
1873 // When modifying the following if statement, please make sure not to
1874 // introduce issue listed in http://crbug.com/6223 comment #11.
1875 bool rtl_ui
= base::i18n::IsRTL();
1876 bool has_dock_position
= (dock_info_
.type() != DockInfo::NONE
);
1877 if (rtl_ui
&& has_dock_position
) {
1878 // Mirror X axis so the docked tab is aligned using the mouse click as
1879 // the top-right corner.
1880 window_bounds
.set_x(window_bounds
.x() - window_bounds
.width());
1882 base::AutoReset
<bool> setter(&is_mutating_
, true);
1884 std::vector
<TabStripModelDelegate::NewStripContents
> contentses
;
1885 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1886 TabStripModelDelegate::NewStripContents item
;
1887 item
.web_contents
= drag_data_
[i
].contents
;
1888 item
.add_types
= drag_data_
[i
].pinned
? TabStripModel::ADD_PINNED
1889 : TabStripModel::ADD_NONE
;
1890 contentses
.push_back(item
);
1893 Browser
* new_browser
=
1894 GetModel(source_tabstrip_
)->delegate()->CreateNewStripWithContents(
1895 contentses
, window_bounds
, dock_info_
, widget
->IsMaximized());
1896 ResetSelection(new_browser
->tab_strip_model());
1897 new_browser
->window()->Show();
1899 // Return the WebContents to normalcy.
1900 if (!detach_into_browser_
)
1901 source_dragged_contents()->DecrementCapturerCount();
1904 CleanUpHiddenFrame();
1907 void TabDragController::ResetDelegates() {
1908 DCHECK(!detach_into_browser_
);
1909 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1910 if (drag_data_
[i
].contents
&&
1911 drag_data_
[i
].contents
->GetDelegate() == this) {
1912 drag_data_
[i
].contents
->SetDelegate(
1913 drag_data_
[i
].original_delegate
);
1918 void TabDragController::CreateDraggedView(
1919 const std::vector
<TabRendererData
>& data
,
1920 const std::vector
<gfx::Rect
>& renderer_bounds
) {
1921 #if !defined(USE_AURA)
1922 DCHECK(!view_
.get());
1923 DCHECK_EQ(data
.size(), drag_data_
.size());
1925 // Set up the photo booth to start capturing the contents of the dragged
1927 NativeViewPhotobooth
* photobooth
= NativeViewPhotobooth::Create(
1928 source_dragged_contents()->GetView()->GetNativeView());
1930 gfx::Rect content_bounds
;
1931 source_dragged_contents()->GetView()->GetContainerBounds(&content_bounds
);
1933 std::vector
<views::View
*> renderers
;
1934 for (size_t i
= 0; i
< drag_data_
.size(); ++i
) {
1935 Tab
* renderer
= source_tabstrip_
->CreateTabForDragging();
1936 renderer
->SetData(data
[i
]);
1937 renderers
.push_back(renderer
);
1939 // DraggedTabView takes ownership of the renderers.
1940 view_
.reset(new DraggedTabView(renderers
, renderer_bounds
, mouse_offset_
,
1941 content_bounds
.size(), photobooth
));
1943 // Aura always hits the |detach_into_browser_| path.
1948 gfx::Rect
TabDragController::GetViewScreenBounds(
1949 views::View
* view
) const {
1950 gfx::Point view_topleft
;
1951 views::View::ConvertPointToScreen(view
, &view_topleft
);
1952 gfx::Rect view_screen_bounds
= view
->GetLocalBounds();
1953 view_screen_bounds
.Offset(view_topleft
.x(), view_topleft
.y());
1954 return view_screen_bounds
;
1957 void TabDragController::HideFrame() {
1958 #if defined(OS_WIN) && !defined(USE_AURA)
1959 // We don't actually hide the window, rather we just move it way off-screen.
1960 // If we actually hide it, we stop receiving drag events.
1962 // Windows coordinates are 16 bit values. Additionally mouse events are
1963 // relative, this means if we move this window to the max position it is easy
1964 // to trigger overflow. To avoid this we don't move to the max position,
1965 // rather some where reasonably large. This should avoid common overflow
1967 // An alternative approach is to query the mouse pointer and ignore the
1968 // location on the mouse (early versions did this). This proves problematic as
1969 // if we happen to get behind in event processing it is all to easy to process
1970 // a release in the wrong location, triggering either an unexpected move or an
1971 // unexpected detach.
1972 HWND frame_hwnd
= source_tabstrip_
->GetWidget()->GetNativeView();
1974 GetWindowRect(frame_hwnd
, &wr
);
1975 MoveWindow(frame_hwnd
, 0x3FFF, 0x3FFF, wr
.right
- wr
.left
,
1976 wr
.bottom
- wr
.top
, TRUE
);
1978 // We also save the bounds of the window prior to it being moved, so that if
1979 // the drag session is aborted we can restore them.
1980 restore_bounds_
= gfx::Rect(wr
);
1982 // Shouldn't hit as aura triggers the |detach_into_browser_| path.
1987 void TabDragController::CleanUpHiddenFrame() {
1988 // If the model we started dragging from is now empty, we must ask the
1989 // delegate to close the frame.
1990 if (!detach_into_browser_
&& GetModel(source_tabstrip_
)->empty())
1991 GetModel(source_tabstrip_
)->delegate()->CloseFrameAfterDragSession();
1994 void TabDragController::DockDisplayerDestroyed(
1995 DockDisplayer
* controller
) {
1996 DockWindows::iterator dock_i
=
1997 dock_windows_
.find(controller
->popup_view());
1998 if (dock_i
!= dock_windows_
.end())
1999 dock_windows_
.erase(dock_i
);
2003 std::vector
<DockDisplayer
*>::iterator i
=
2004 std::find(dock_controllers_
.begin(), dock_controllers_
.end(),
2006 if (i
!= dock_controllers_
.end())
2007 dock_controllers_
.erase(i
);
2012 void TabDragController::BringWindowUnderPointToFront(
2013 const gfx::Point
& point_in_screen
) {
2014 // If we're going to dock to another window, bring it to the front.
2015 gfx::NativeWindow window
= dock_info_
.window();
2017 views::View
* dragged_view
;
2019 dragged_view
= view_
.get();
2021 dragged_view
= attached_tabstrip_
;
2022 gfx::NativeView dragged_native_view
=
2023 dragged_view
->GetWidget()->GetNativeView();
2024 dock_windows_
.insert(dragged_native_view
);
2025 window
= DockInfo::GetLocalProcessWindowAtPoint(
2029 dock_windows_
.erase(dragged_native_view
);
2030 // Only bring browser windows to front - only windows with a TabStrip can
2031 // be tab drag targets.
2032 if (!GetTabStripForWindow(window
))
2036 views::Widget
* widget_window
= views::Widget::GetWidgetForNativeView(
2041 #if defined(USE_ASH)
2042 // TODO(varkha): A better strategy would be for DragWindowController to
2043 // be able to observe stacking changes to the phantom drag widget's
2044 // siblings in order to keep it on top.
2045 // One way is to implement a notification that is sent to a window parent's
2046 // observers when a stacking order is changed among the children of that
2047 // same parent. Note that OnWindowStackingChanged is sent only to the
2048 // child that is the argument of one of the Window::StackChildX calls and
2049 // not to all its siblings affected by the stacking change.
2050 aura::Window
* browser_window
= widget_window
->GetNativeView();
2051 // Find a topmost non-popup window and stack the recipient browser window
2052 // above it in order to avoid stacking the browser window on top of the
2053 // phantom drag widget created by DragWindowController in a second display.
2054 for (aura::Window::Windows::const_reverse_iterator it
=
2055 browser_window
->parent()->children().rbegin();
2056 it
!= browser_window
->parent()->children().rend(); ++it
) {
2057 // If the iteration reached the recipient browser window then it is
2058 // already topmost and it is safe to return early with no stacking change.
2059 if (*it
== browser_window
)
2061 if ((*it
)->type() != aura::client::WINDOW_TYPE_POPUP
) {
2062 widget_window
->StackAbove(*it
);
2067 widget_window
->StackAtTop();
2070 // The previous call made the window appear on top of the dragged window,
2071 // move the dragged window to the front.
2073 view_
->GetWidget()->StackAtTop();
2074 else if (is_dragging_window_
)
2075 attached_tabstrip_
->GetWidget()->StackAtTop();
2079 TabStripModel
* TabDragController::GetModel(
2080 TabStrip
* tabstrip
) const {
2081 return static_cast<BrowserTabStripController
*>(tabstrip
->controller())->
2085 views::Widget
* TabDragController::GetAttachedBrowserWidget() {
2086 return attached_tabstrip_
->GetWidget();
2089 bool TabDragController::AreTabsConsecutive() {
2090 for (size_t i
= 1; i
< drag_data_
.size(); ++i
) {
2091 if (drag_data_
[i
- 1].source_model_index
+ 1 !=
2092 drag_data_
[i
].source_model_index
) {
2099 Browser
* TabDragController::CreateBrowserForDrag(
2101 const gfx::Point
& point_in_screen
,
2102 gfx::Vector2d
* drag_offset
,
2103 std::vector
<gfx::Rect
>* drag_bounds
) {
2104 gfx::Point
center(0, source
->height() / 2);
2105 views::View::ConvertPointToWidget(source
, ¢er
);
2106 gfx::Rect
new_bounds(source
->GetWidget()->GetRestoredBounds());
2107 new_bounds
.set_y(point_in_screen
.y() - center
.y());
2108 switch (GetDetachPosition(point_in_screen
)) {
2110 new_bounds
.set_x(point_in_screen
.x() - center
.x());
2111 new_bounds
.Offset(-mouse_offset_
.x(), 0);
2113 case DETACH_AFTER
: {
2114 gfx::Point
right_edge(source
->width(), 0);
2115 views::View::ConvertPointToWidget(source
, &right_edge
);
2116 new_bounds
.set_x(point_in_screen
.x() - right_edge
.x());
2117 new_bounds
.Offset(drag_bounds
->back().right() - mouse_offset_
.x(), 0);
2118 int delta
= (*drag_bounds
)[0].x();
2119 for (size_t i
= 0; i
< drag_bounds
->size(); ++i
)
2120 (*drag_bounds
)[i
].Offset(-delta
, 0);
2124 break; // Nothing to do for DETACH_ABOVE_OR_BELOW.
2127 *drag_offset
= point_in_screen
- new_bounds
.origin();
2130 Profile::FromBrowserContext(drag_data_
[0].contents
->GetBrowserContext());
2131 Browser::CreateParams
create_params(Browser::TYPE_TABBED
,
2133 host_desktop_type_
);
2134 create_params
.initial_bounds
= new_bounds
;
2135 Browser
* browser
= new Browser(create_params
);
2136 SetTrackedByWorkspace(browser
->window()->GetNativeWindow(), false);
2137 SetWindowPositionManaged(browser
->window()->GetNativeWindow(), false);
2138 // If the window is created maximized then the bounds we supplied are ignored.
2139 // We need to reset them again so they are honored.
2140 browser
->window()->SetBounds(new_bounds
);
2145 gfx::Point
TabDragController::GetCursorScreenPoint() {
2146 #if defined(USE_ASH)
2147 if (host_desktop_type_
== chrome::HOST_DESKTOP_TYPE_ASH
&&
2148 event_source_
== EVENT_SOURCE_TOUCH
&&
2149 aura::Env::GetInstance()->is_touch_down()) {
2150 views::Widget
* widget
= GetAttachedBrowserWidget();
2152 aura::Window
* widget_window
= widget
->GetNativeWindow();
2153 DCHECK(widget_window
->GetRootWindow());
2154 gfx::Point touch_point
;
2155 bool got_touch_point
= widget_window
->GetRootWindow()->
2156 gesture_recognizer()->GetLastTouchPointForTarget(widget_window
,
2158 DCHECK(got_touch_point
);
2159 ash::wm::ConvertPointToScreen(widget_window
->GetRootWindow(), &touch_point
);
2163 return screen_
->GetCursorScreenPoint();
2166 gfx::Vector2d
TabDragController::GetWindowOffset(
2167 const gfx::Point
& point_in_screen
) {
2168 TabStrip
* owning_tabstrip
= (attached_tabstrip_
&& detach_into_browser_
) ?
2169 attached_tabstrip_
: source_tabstrip_
;
2170 views::View
* toplevel_view
= owning_tabstrip
->GetWidget()->GetContentsView();
2172 gfx::Point point
= point_in_screen
;
2173 views::View::ConvertPointFromScreen(toplevel_view
, &point
);
2174 return point
.OffsetFromOrigin();