[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / ui / views / tabs / tab_drag_controller.cc
blobe3b07ec9b9e91211d2686cc13e165172821c25da
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"
7 #include <math.h>
8 #include <set>
10 #include "base/auto_reset.h"
11 #include "base/callback.h"
12 #include "base/i18n/rtl.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
19 #include "chrome/browser/ui/views/frame/browser_view.h"
20 #include "chrome/browser/ui/views/tabs/browser_tab_strip_controller.h"
21 #include "chrome/browser/ui/views/tabs/stacked_tab_strip_layout.h"
22 #include "chrome/browser/ui/views/tabs/tab.h"
23 #include "chrome/browser/ui/views/tabs/tab_strip.h"
24 #include "chrome/browser/ui/views/tabs/window_finder.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_source.h"
28 #include "content/public/browser/notification_types.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/browser/extension_function_dispatcher.h"
32 #include "ui/events/event_constants.h"
33 #include "ui/events/gestures/gesture_recognizer.h"
34 #include "ui/gfx/geometry/point_conversions.h"
35 #include "ui/gfx/screen.h"
36 #include "ui/views/event_monitor.h"
37 #include "ui/views/focus/view_storage.h"
38 #include "ui/views/widget/root_view.h"
39 #include "ui/views/widget/widget.h"
41 #if defined(USE_ASH)
42 #include "ash/accelerators/accelerator_commands.h"
43 #include "ash/shell.h"
44 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
45 #include "ash/wm/window_state.h"
46 #include "ui/wm/core/coordinate_conversion.h"
47 #endif
49 #if defined(USE_AURA)
50 #include "ui/aura/env.h"
51 #include "ui/aura/window.h"
52 #include "ui/wm/core/window_modality_controller.h"
53 #endif
55 using base::UserMetricsAction;
56 using content::OpenURLParams;
57 using content::WebContents;
59 // If non-null there is a drag underway.
60 static TabDragController* instance_ = NULL;
62 namespace {
64 // Delay, in ms, during dragging before we bring a window to front.
65 const int kBringToFrontDelay = 750;
67 // Initial delay before moving tabs when the dragged tab is close to the edge of
68 // the stacked tabs.
69 const int kMoveAttachedInitialDelay = 600;
71 // Delay for moving tabs after the initial delay has passed.
72 const int kMoveAttachedSubsequentDelay = 300;
74 const int kHorizontalMoveThreshold = 16; // Pixels.
76 // Distance from the next/previous stacked before before we consider the tab
77 // close enough to trigger moving.
78 const int kStackedDistance = 36;
80 #if defined(USE_ASH)
81 void SetWindowPositionManaged(gfx::NativeWindow window, bool value) {
82 ash::wm::GetWindowState(window)->set_window_position_managed(value);
85 // Returns true if |tab_strip| browser window is docked.
86 bool IsDockedOrSnapped(const TabStrip* tab_strip) {
87 DCHECK(tab_strip);
88 ash::wm::WindowState* window_state =
89 ash::wm::GetWindowState(tab_strip->GetWidget()->GetNativeWindow());
90 return window_state->IsDocked() || window_state->IsSnapped();
92 #else
93 void SetWindowPositionManaged(gfx::NativeWindow window, bool value) {
96 bool IsDockedOrSnapped(const TabStrip* tab_strip) {
97 return false;
99 #endif
101 #if defined(USE_AURA)
102 gfx::NativeWindow GetModalTransient(gfx::NativeWindow window) {
103 return wm::GetModalTransient(window);
105 #else
106 gfx::NativeWindow GetModalTransient(gfx::NativeWindow window) {
107 NOTIMPLEMENTED();
108 return NULL;
110 #endif
112 // Returns true if |bounds| contains the y-coordinate |y|. The y-coordinate
113 // of |bounds| is adjusted by |vertical_adjustment|.
114 bool DoesRectContainVerticalPointExpanded(
115 const gfx::Rect& bounds,
116 int vertical_adjustment,
117 int y) {
118 int upper_threshold = bounds.bottom() + vertical_adjustment;
119 int lower_threshold = bounds.y() - vertical_adjustment;
120 return y >= lower_threshold && y <= upper_threshold;
123 // Adds |x_offset| to all the rectangles in |rects|.
124 void OffsetX(int x_offset, std::vector<gfx::Rect>* rects) {
125 if (x_offset == 0)
126 return;
128 for (size_t i = 0; i < rects->size(); ++i)
129 (*rects)[i].set_x((*rects)[i].x() + x_offset);
132 // WidgetObserver implementation that resets the window position managed
133 // property on Show.
134 // We're forced to do this here since BrowserFrameAsh resets the 'window
135 // position managed' property during a show and we need the property set to
136 // false before WorkspaceLayoutManager sees the visibility change.
137 class WindowPositionManagedUpdater : public views::WidgetObserver {
138 public:
139 void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
140 SetWindowPositionManaged(widget->GetNativeWindow(), false);
144 // EscapeTracker installs an event monitor and runs a callback when it receives
145 // the escape key.
146 class EscapeTracker : public ui::EventHandler {
147 public:
148 explicit EscapeTracker(const base::Closure& callback)
149 : escape_callback_(callback),
150 event_monitor_(views::EventMonitor::CreateApplicationMonitor(this)) {
153 private:
154 // ui::EventHandler:
155 void OnKeyEvent(ui::KeyEvent* key) override {
156 if (key->type() == ui::ET_KEY_PRESSED &&
157 key->key_code() == ui::VKEY_ESCAPE) {
158 escape_callback_.Run();
162 base::Closure escape_callback_;
163 scoped_ptr<views::EventMonitor> event_monitor_;
165 DISALLOW_COPY_AND_ASSIGN(EscapeTracker);
168 } // namespace
170 TabDragController::TabDragData::TabDragData()
171 : contents(NULL),
172 source_model_index(-1),
173 attached_tab(NULL),
174 pinned(false) {
177 TabDragController::TabDragData::~TabDragData() {
180 ///////////////////////////////////////////////////////////////////////////////
181 // TabDragController, public:
183 // static
184 const int TabDragController::kTouchVerticalDetachMagnetism = 50;
186 // static
187 const int TabDragController::kVerticalDetachMagnetism = 15;
189 TabDragController::TabDragController()
190 : event_source_(EVENT_SOURCE_MOUSE),
191 source_tabstrip_(NULL),
192 attached_tabstrip_(NULL),
193 screen_(NULL),
194 host_desktop_type_(chrome::HOST_DESKTOP_TYPE_NATIVE),
195 can_release_capture_(true),
196 offset_to_width_ratio_(0),
197 old_focused_view_id_(
198 views::ViewStorage::GetInstance()->CreateStorageID()),
199 last_move_screen_loc_(0),
200 started_drag_(false),
201 active_(true),
202 source_tab_index_(std::numeric_limits<size_t>::max()),
203 initial_move_(true),
204 detach_behavior_(DETACHABLE),
205 move_behavior_(REORDER),
206 mouse_move_direction_(0),
207 is_dragging_window_(false),
208 is_dragging_new_browser_(false),
209 was_source_maximized_(false),
210 was_source_fullscreen_(false),
211 did_restore_window_(false),
212 end_run_loop_behavior_(END_RUN_LOOP_STOP_DRAGGING),
213 waiting_for_run_loop_to_exit_(false),
214 tab_strip_to_attach_to_after_exit_(NULL),
215 move_loop_widget_(NULL),
216 is_mutating_(false),
217 attach_x_(-1),
218 attach_index_(-1),
219 weak_factory_(this) {
220 instance_ = this;
223 TabDragController::~TabDragController() {
224 views::ViewStorage::GetInstance()->RemoveView(old_focused_view_id_);
226 if (instance_ == this)
227 instance_ = NULL;
229 if (move_loop_widget_) {
230 move_loop_widget_->RemoveObserver(this);
231 SetWindowPositionManaged(move_loop_widget_->GetNativeWindow(), true);
234 if (source_tabstrip_)
235 GetModel(source_tabstrip_)->RemoveObserver(this);
237 if (event_source_ == EVENT_SOURCE_TOUCH) {
238 TabStrip* capture_tabstrip = attached_tabstrip_ ?
239 attached_tabstrip_ : source_tabstrip_;
240 capture_tabstrip->GetWidget()->ReleaseCapture();
244 void TabDragController::Init(
245 TabStrip* source_tabstrip,
246 Tab* source_tab,
247 const std::vector<Tab*>& tabs,
248 const gfx::Point& mouse_offset,
249 int source_tab_offset,
250 const ui::ListSelectionModel& initial_selection_model,
251 MoveBehavior move_behavior,
252 EventSource event_source) {
253 DCHECK(!tabs.empty());
254 DCHECK(std::find(tabs.begin(), tabs.end(), source_tab) != tabs.end());
255 source_tabstrip_ = source_tabstrip;
256 was_source_maximized_ = source_tabstrip->GetWidget()->IsMaximized();
257 was_source_fullscreen_ = source_tabstrip->GetWidget()->IsFullscreen();
258 screen_ = gfx::Screen::GetScreenFor(
259 source_tabstrip->GetWidget()->GetNativeView());
260 host_desktop_type_ = chrome::GetHostDesktopTypeForNativeView(
261 source_tabstrip->GetWidget()->GetNativeView());
262 // Do not release capture when transferring capture between widgets on:
263 // - Desktop Linux
264 // Mouse capture is not synchronous on desktop Linux. Chrome makes
265 // transferring capture between widgets without releasing capture appear
266 // synchronous on desktop Linux, so use that.
267 // - Ash
268 // Releasing capture on Ash cancels gestures so avoid it.
269 #if defined(OS_LINUX)
270 can_release_capture_ = false;
271 #else
272 can_release_capture_ =
273 (host_desktop_type_ != chrome::HOST_DESKTOP_TYPE_ASH);
274 #endif
275 start_point_in_screen_ = gfx::Point(source_tab_offset, mouse_offset.y());
276 views::View::ConvertPointToScreen(source_tab, &start_point_in_screen_);
277 event_source_ = event_source;
278 mouse_offset_ = mouse_offset;
279 move_behavior_ = move_behavior;
280 last_point_in_screen_ = start_point_in_screen_;
281 last_move_screen_loc_ = start_point_in_screen_.x();
282 initial_tab_positions_ = source_tabstrip->GetTabXCoordinates();
284 GetModel(source_tabstrip_)->AddObserver(this);
286 drag_data_.resize(tabs.size());
287 for (size_t i = 0; i < tabs.size(); ++i)
288 InitTabDragData(tabs[i], &(drag_data_[i]));
289 source_tab_index_ =
290 std::find(tabs.begin(), tabs.end(), source_tab) - tabs.begin();
292 // Listen for Esc key presses.
293 escape_tracker_.reset(
294 new EscapeTracker(base::Bind(&TabDragController::EndDrag,
295 weak_factory_.GetWeakPtr(),
296 END_DRAG_CANCEL)));
298 if (source_tab->width() > 0) {
299 offset_to_width_ratio_ = static_cast<float>(
300 source_tab->GetMirroredXInView(source_tab_offset)) /
301 static_cast<float>(source_tab->width());
303 InitWindowCreatePoint();
304 initial_selection_model_.Copy(initial_selection_model);
306 // Gestures don't automatically do a capture. We don't allow multiple drags at
307 // the same time, so we explicitly capture.
308 if (event_source == EVENT_SOURCE_TOUCH)
309 source_tabstrip_->GetWidget()->SetCapture(source_tabstrip_);
311 #if defined(USE_ASH)
312 if (ash::Shell::HasInstance() &&
313 ash::Shell::GetInstance()->maximize_mode_controller()->
314 IsMaximizeModeWindowManagerEnabled()) {
315 detach_behavior_ = NOT_DETACHABLE;
317 #endif
320 // static
321 bool TabDragController::IsAttachedTo(const TabStrip* tab_strip) {
322 return (instance_ && instance_->active() &&
323 instance_->attached_tabstrip() == tab_strip);
326 // static
327 bool TabDragController::IsActive() {
328 return instance_ && instance_->active();
331 void TabDragController::SetMoveBehavior(MoveBehavior behavior) {
332 if (started_drag())
333 return;
335 move_behavior_ = behavior;
338 void TabDragController::Drag(const gfx::Point& point_in_screen) {
339 TRACE_EVENT1("views", "TabDragController::Drag",
340 "point_in_screen", point_in_screen.ToString());
342 bring_to_front_timer_.Stop();
343 move_stacked_timer_.Stop();
345 if (waiting_for_run_loop_to_exit_)
346 return;
348 if (!started_drag_) {
349 if (!CanStartDrag(point_in_screen))
350 return; // User hasn't dragged far enough yet.
352 // On windows SaveFocus() may trigger a capture lost, which destroys us.
354 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr());
355 SaveFocus();
356 if (!ref)
357 return;
359 started_drag_ = true;
360 Attach(source_tabstrip_, gfx::Point());
361 if (static_cast<int>(drag_data_.size()) ==
362 GetModel(source_tabstrip_)->count()) {
363 if (was_source_maximized_ || was_source_fullscreen_) {
364 did_restore_window_ = true;
365 // When all tabs in a maximized browser are dragged the browser gets
366 // restored during the drag and maximized back when the drag ends.
367 views::Widget* widget = GetAttachedBrowserWidget();
368 const int last_tabstrip_width = attached_tabstrip_->tab_area_width();
369 std::vector<gfx::Rect> drag_bounds = CalculateBoundsForDraggedTabs();
370 OffsetX(GetAttachedDragPoint(point_in_screen).x(), &drag_bounds);
371 gfx::Rect new_bounds(CalculateDraggedBrowserBounds(source_tabstrip_,
372 point_in_screen,
373 &drag_bounds));
374 new_bounds.Offset(-widget->GetRestoredBounds().x() +
375 point_in_screen.x() -
376 mouse_offset_.x(), 0);
377 widget->SetVisibilityChangedAnimationsEnabled(false);
378 widget->Restore();
379 widget->SetBounds(new_bounds);
380 AdjustBrowserAndTabBoundsForDrag(last_tabstrip_width,
381 point_in_screen,
382 &drag_bounds);
383 widget->SetVisibilityChangedAnimationsEnabled(true);
385 RunMoveLoop(GetWindowOffset(point_in_screen));
386 return;
390 ContinueDragging(point_in_screen);
393 void TabDragController::EndDrag(EndDragReason reason) {
394 TRACE_EVENT0("views", "TabDragController::EndDrag");
396 // If we're dragging a window ignore capture lost since it'll ultimately
397 // trigger the move loop to end and we'll revert the drag when RunMoveLoop()
398 // finishes.
399 if (reason == END_DRAG_CAPTURE_LOST && is_dragging_window_)
400 return;
401 EndDragImpl(reason != END_DRAG_COMPLETE && source_tabstrip_ ?
402 CANCELED : NORMAL);
405 void TabDragController::InitTabDragData(Tab* tab,
406 TabDragData* drag_data) {
407 TRACE_EVENT0("views", "TabDragController::InitTabDragData");
408 drag_data->source_model_index =
409 source_tabstrip_->GetModelIndexOfTab(tab);
410 drag_data->contents = GetModel(source_tabstrip_)->GetWebContentsAt(
411 drag_data->source_model_index);
412 drag_data->pinned = source_tabstrip_->IsTabPinned(tab);
413 registrar_.Add(
414 this,
415 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
416 content::Source<WebContents>(drag_data->contents));
419 ///////////////////////////////////////////////////////////////////////////////
420 // TabDragController, content::NotificationObserver implementation:
422 void TabDragController::Observe(
423 int type,
424 const content::NotificationSource& source,
425 const content::NotificationDetails& details) {
426 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
427 WebContents* destroyed_web_contents =
428 content::Source<WebContents>(source).ptr();
429 for (size_t i = 0; i < drag_data_.size(); ++i) {
430 if (drag_data_[i].contents == destroyed_web_contents) {
431 // One of the tabs we're dragging has been destroyed. Cancel the drag.
432 drag_data_[i].contents = NULL;
433 EndDragImpl(TAB_DESTROYED);
434 return;
437 // If we get here it means we got notification for a tab we don't know about.
438 NOTREACHED();
441 void TabDragController::OnWidgetBoundsChanged(views::Widget* widget,
442 const gfx::Rect& new_bounds) {
443 TRACE_EVENT1("views", "TabDragController::OnWidgetBoundsChanged",
444 "new_bounds", new_bounds.ToString());
446 Drag(GetCursorScreenPoint());
449 void TabDragController::TabStripEmpty() {
450 GetModel(source_tabstrip_)->RemoveObserver(this);
451 // NULL out source_tabstrip_ so that we don't attempt to add back to it (in
452 // the case of a revert).
453 source_tabstrip_ = NULL;
456 ///////////////////////////////////////////////////////////////////////////////
457 // TabDragController, private:
459 void TabDragController::InitWindowCreatePoint() {
460 // window_create_point_ is only used in CompleteDrag() (through
461 // GetWindowCreatePoint() to get the start point of the docked window) when
462 // the attached_tabstrip_ is NULL and all the window's related bound
463 // information are obtained from source_tabstrip_. So, we need to get the
464 // first_tab based on source_tabstrip_, not attached_tabstrip_. Otherwise,
465 // the window_create_point_ is not in the correct coordinate system. Please
466 // refer to http://crbug.com/6223 comment #15 for detailed information.
467 views::View* first_tab = source_tabstrip_->tab_at(0);
468 views::View::ConvertPointToWidget(first_tab, &first_source_tab_point_);
469 window_create_point_ = first_source_tab_point_;
470 window_create_point_.Offset(mouse_offset_.x(), mouse_offset_.y());
473 gfx::Point TabDragController::GetWindowCreatePoint(
474 const gfx::Point& origin) const {
475 // If the cursor is outside the monitor area, move it inside. For example,
476 // dropping a tab onto the task bar on Windows produces this situation.
477 gfx::Rect work_area = screen_->GetDisplayNearestPoint(origin).work_area();
478 gfx::Point create_point(origin);
479 if (!work_area.IsEmpty()) {
480 if (create_point.x() < work_area.x())
481 create_point.set_x(work_area.x());
482 else if (create_point.x() > work_area.right())
483 create_point.set_x(work_area.right());
484 if (create_point.y() < work_area.y())
485 create_point.set_y(work_area.y());
486 else if (create_point.y() > work_area.bottom())
487 create_point.set_y(work_area.bottom());
489 return gfx::Point(create_point.x() - window_create_point_.x(),
490 create_point.y() - window_create_point_.y());
493 void TabDragController::SaveFocus() {
494 DCHECK(source_tabstrip_);
495 views::View* focused_view =
496 source_tabstrip_->GetFocusManager()->GetFocusedView();
497 if (focused_view)
498 views::ViewStorage::GetInstance()->StoreView(old_focused_view_id_,
499 focused_view);
500 source_tabstrip_->GetFocusManager()->SetFocusedView(source_tabstrip_);
501 // WARNING: we may have been deleted.
504 void TabDragController::RestoreFocus() {
505 if (attached_tabstrip_ != source_tabstrip_) {
506 if (is_dragging_new_browser_) {
507 content::WebContents* active_contents = source_dragged_contents();
508 if (active_contents && !active_contents->FocusLocationBarByDefault())
509 active_contents->Focus();
511 return;
513 views::View* old_focused_view =
514 views::ViewStorage::GetInstance()->RetrieveView(old_focused_view_id_);
515 if (!old_focused_view)
516 return;
517 old_focused_view->GetFocusManager()->SetFocusedView(old_focused_view);
520 bool TabDragController::CanStartDrag(const gfx::Point& point_in_screen) const {
521 // Determine if the mouse has moved beyond a minimum elasticity distance in
522 // any direction from the starting point.
523 static const int kMinimumDragDistance = 10;
524 int x_offset = abs(point_in_screen.x() - start_point_in_screen_.x());
525 int y_offset = abs(point_in_screen.y() - start_point_in_screen_.y());
526 return sqrt(pow(static_cast<float>(x_offset), 2) +
527 pow(static_cast<float>(y_offset), 2)) > kMinimumDragDistance;
530 void TabDragController::ContinueDragging(const gfx::Point& point_in_screen) {
531 TRACE_EVENT1("views", "TabDragController::ContinueDragging",
532 "point_in_screen", point_in_screen.ToString());
534 DCHECK(attached_tabstrip_);
536 TabStrip* target_tabstrip = detach_behavior_ == DETACHABLE ?
537 GetTargetTabStripForPoint(point_in_screen) : source_tabstrip_;
538 bool tab_strip_changed = (target_tabstrip != attached_tabstrip_);
540 if (attached_tabstrip_) {
541 int move_delta = point_in_screen.x() - last_point_in_screen_.x();
542 if (move_delta > 0)
543 mouse_move_direction_ |= kMovedMouseRight;
544 else if (move_delta < 0)
545 mouse_move_direction_ |= kMovedMouseLeft;
547 last_point_in_screen_ = point_in_screen;
549 if (tab_strip_changed) {
550 is_dragging_new_browser_ = false;
551 did_restore_window_ = false;
552 if (DragBrowserToNewTabStrip(target_tabstrip, point_in_screen) ==
553 DRAG_BROWSER_RESULT_STOP) {
554 return;
557 if (is_dragging_window_) {
558 static_cast<base::Timer*>(&bring_to_front_timer_)->Start(FROM_HERE,
559 base::TimeDelta::FromMilliseconds(kBringToFrontDelay),
560 base::Bind(&TabDragController::BringWindowUnderPointToFront,
561 base::Unretained(this), point_in_screen));
564 if (!is_dragging_window_ && attached_tabstrip_) {
565 if (move_only()) {
566 DragActiveTabStacked(point_in_screen);
567 } else {
568 MoveAttached(point_in_screen);
569 if (tab_strip_changed) {
570 // Move the corresponding window to the front. We do this after the
571 // move as on windows activate triggers a synchronous paint.
572 attached_tabstrip_->GetWidget()->Activate();
578 TabDragController::DragBrowserResultType
579 TabDragController::DragBrowserToNewTabStrip(
580 TabStrip* target_tabstrip,
581 const gfx::Point& point_in_screen) {
582 TRACE_EVENT1("views", "TabDragController::DragBrowserToNewTabStrip",
583 "point_in_screen", point_in_screen.ToString());
585 if (!target_tabstrip) {
586 DetachIntoNewBrowserAndRunMoveLoop(point_in_screen);
587 return DRAG_BROWSER_RESULT_STOP;
590 #if defined(USE_AURA)
591 // Only Aura windows are gesture consumers.
592 ui::GestureRecognizer::Get()->TransferEventsTo(
593 GetAttachedBrowserWidget()->GetNativeView(),
594 target_tabstrip->GetWidget()->GetNativeView());
595 #endif
597 if (is_dragging_window_) {
598 // ReleaseCapture() is going to result in calling back to us (because it
599 // results in a move). That'll cause all sorts of problems. Reset the
600 // observer so we don't get notified and process the event.
601 if (host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH) {
602 move_loop_widget_->RemoveObserver(this);
603 move_loop_widget_ = NULL;
605 views::Widget* browser_widget = GetAttachedBrowserWidget();
606 // Need to release the drag controller before starting the move loop as it's
607 // going to trigger capture lost, which cancels drag.
608 attached_tabstrip_->ReleaseDragController();
609 target_tabstrip->OwnDragController(this);
610 // Disable animations so that we don't see a close animation on aero.
611 browser_widget->SetVisibilityChangedAnimationsEnabled(false);
612 if (can_release_capture_)
613 browser_widget->ReleaseCapture();
614 else
615 target_tabstrip->GetWidget()->SetCapture(attached_tabstrip_);
617 // The window is going away. Since the drag is still on going we don't want
618 // that to effect the position of any windows.
619 SetWindowPositionManaged(browser_widget->GetNativeWindow(), false);
621 #if !defined(OS_LINUX) || defined(OS_CHROMEOS)
622 // EndMoveLoop is going to snap the window back to its original location.
623 // Hide it so users don't see this. Hiding a window in Linux aura causes
624 // it to lose capture so skip it.
625 browser_widget->Hide();
626 #endif
627 browser_widget->EndMoveLoop();
629 // Ideally we would always swap the tabs now, but on non-ash Windows, it
630 // seems that running the move loop implicitly activates the window when
631 // done, leading to all sorts of flicker. So, on non-ash Windows, instead
632 // we process the move after the loop completes. But on chromeos, we can
633 // do tab swapping now to avoid the tab flashing issue
634 // (crbug.com/116329).
635 if (can_release_capture_) {
636 tab_strip_to_attach_to_after_exit_ = target_tabstrip;
637 } else {
638 is_dragging_window_ = false;
639 Detach(DONT_RELEASE_CAPTURE);
640 Attach(target_tabstrip, point_in_screen);
641 // Move the tabs into position.
642 MoveAttached(point_in_screen);
643 attached_tabstrip_->GetWidget()->Activate();
646 waiting_for_run_loop_to_exit_ = true;
647 end_run_loop_behavior_ = END_RUN_LOOP_CONTINUE_DRAGGING;
648 return DRAG_BROWSER_RESULT_STOP;
650 Detach(DONT_RELEASE_CAPTURE);
651 Attach(target_tabstrip, point_in_screen);
652 return DRAG_BROWSER_RESULT_CONTINUE;
655 void TabDragController::DragActiveTabStacked(
656 const gfx::Point& point_in_screen) {
657 if (attached_tabstrip_->tab_count() !=
658 static_cast<int>(initial_tab_positions_.size()))
659 return; // TODO: should cancel drag if this happens.
661 int delta = point_in_screen.x() - start_point_in_screen_.x();
662 attached_tabstrip_->DragActiveTab(initial_tab_positions_, delta);
665 void TabDragController::MoveAttachedToNextStackedIndex(
666 const gfx::Point& point_in_screen) {
667 int index = attached_tabstrip_->touch_layout_->active_index();
668 if (index + 1 >= attached_tabstrip_->tab_count())
669 return;
671 GetModel(attached_tabstrip_)->MoveSelectedTabsTo(index + 1);
672 StartMoveStackedTimerIfNecessary(point_in_screen,
673 kMoveAttachedSubsequentDelay);
676 void TabDragController::MoveAttachedToPreviousStackedIndex(
677 const gfx::Point& point_in_screen) {
678 int index = attached_tabstrip_->touch_layout_->active_index();
679 if (index <= attached_tabstrip_->GetMiniTabCount())
680 return;
682 GetModel(attached_tabstrip_)->MoveSelectedTabsTo(index - 1);
683 StartMoveStackedTimerIfNecessary(point_in_screen,
684 kMoveAttachedSubsequentDelay);
687 void TabDragController::MoveAttached(const gfx::Point& point_in_screen) {
688 DCHECK(attached_tabstrip_);
689 DCHECK(!is_dragging_window_);
691 gfx::Point dragged_view_point = GetAttachedDragPoint(point_in_screen);
693 // Determine the horizontal move threshold. This is dependent on the width
694 // of tabs. The smaller the tabs compared to the standard size, the smaller
695 // the threshold.
696 int threshold = kHorizontalMoveThreshold;
697 if (!attached_tabstrip_->touch_layout_.get()) {
698 double unselected, selected;
699 attached_tabstrip_->GetCurrentTabWidths(&unselected, &selected);
700 double ratio = unselected / Tab::GetStandardSize().width();
701 threshold = static_cast<int>(ratio * kHorizontalMoveThreshold);
703 // else case: touch tabs never shrink.
705 std::vector<Tab*> tabs(drag_data_.size());
706 for (size_t i = 0; i < drag_data_.size(); ++i)
707 tabs[i] = drag_data_[i].attached_tab;
709 bool did_layout = false;
710 // Update the model, moving the WebContents from one index to another. Do this
711 // only if we have moved a minimum distance since the last reorder (to prevent
712 // jitter) or if this the first move and the tabs are not consecutive.
713 if ((abs(point_in_screen.x() - last_move_screen_loc_) > threshold ||
714 (initial_move_ && !AreTabsConsecutive()))) {
715 TabStripModel* attached_model = GetModel(attached_tabstrip_);
716 int to_index = GetInsertionIndexForDraggedBounds(
717 GetDraggedViewTabStripBounds(dragged_view_point));
718 bool do_move = true;
719 // While dragging within a tabstrip the expectation is the insertion index
720 // is based on the left edge of the tabs being dragged. OTOH when dragging
721 // into a new tabstrip (attaching) the expectation is the insertion index is
722 // based on the cursor. This proves problematic as insertion may change the
723 // size of the tabs, resulting in the index calculated before the insert
724 // differing from the index calculated after the insert. To alleviate this
725 // the index is chosen before insertion, and subsequently a new index is
726 // only used once the mouse moves enough such that the index changes based
727 // on the direction the mouse moved relative to |attach_x_| (smaller
728 // x-coordinate should yield a smaller index or larger x-coordinate yields a
729 // larger index).
730 if (attach_index_ != -1) {
731 gfx::Point tab_strip_point(point_in_screen);
732 views::View::ConvertPointFromScreen(attached_tabstrip_, &tab_strip_point);
733 const int new_x =
734 attached_tabstrip_->GetMirroredXInView(tab_strip_point.x());
735 if (new_x < attach_x_)
736 to_index = std::min(to_index, attach_index_);
737 else
738 to_index = std::max(to_index, attach_index_);
739 if (to_index != attach_index_)
740 attach_index_ = -1; // Once a valid move is detected, don't constrain.
741 else
742 do_move = false;
744 if (do_move) {
745 WebContents* last_contents = drag_data_[drag_data_.size() - 1].contents;
746 int index_of_last_item =
747 attached_model->GetIndexOfWebContents(last_contents);
748 if (initial_move_) {
749 // TabStrip determines if the tabs needs to be animated based on model
750 // position. This means we need to invoke LayoutDraggedTabsAt before
751 // changing the model.
752 attached_tabstrip_->LayoutDraggedTabsAt(
753 tabs, source_tab_drag_data()->attached_tab, dragged_view_point,
754 initial_move_);
755 did_layout = true;
757 attached_model->MoveSelectedTabsTo(to_index);
759 // Move may do nothing in certain situations (such as when dragging pinned
760 // tabs). Make sure the tabstrip actually changed before updating
761 // last_move_screen_loc_.
762 if (index_of_last_item !=
763 attached_model->GetIndexOfWebContents(last_contents)) {
764 last_move_screen_loc_ = point_in_screen.x();
769 if (!did_layout) {
770 attached_tabstrip_->LayoutDraggedTabsAt(
771 tabs, source_tab_drag_data()->attached_tab, dragged_view_point,
772 initial_move_);
775 StartMoveStackedTimerIfNecessary(point_in_screen, kMoveAttachedInitialDelay);
777 initial_move_ = false;
780 void TabDragController::StartMoveStackedTimerIfNecessary(
781 const gfx::Point& point_in_screen,
782 int delay_ms) {
783 DCHECK(attached_tabstrip_);
785 StackedTabStripLayout* touch_layout = attached_tabstrip_->touch_layout_.get();
786 if (!touch_layout)
787 return;
789 gfx::Point dragged_view_point = GetAttachedDragPoint(point_in_screen);
790 gfx::Rect bounds = GetDraggedViewTabStripBounds(dragged_view_point);
791 int index = touch_layout->active_index();
792 if (ShouldDragToNextStackedTab(bounds, index)) {
793 static_cast<base::Timer*>(&move_stacked_timer_)->Start(
794 FROM_HERE,
795 base::TimeDelta::FromMilliseconds(delay_ms),
796 base::Bind(&TabDragController::MoveAttachedToNextStackedIndex,
797 base::Unretained(this), point_in_screen));
798 } else if (ShouldDragToPreviousStackedTab(bounds, index)) {
799 static_cast<base::Timer*>(&move_stacked_timer_)->Start(
800 FROM_HERE,
801 base::TimeDelta::FromMilliseconds(delay_ms),
802 base::Bind(&TabDragController::MoveAttachedToPreviousStackedIndex,
803 base::Unretained(this), point_in_screen));
807 TabDragController::DetachPosition TabDragController::GetDetachPosition(
808 const gfx::Point& point_in_screen) {
809 DCHECK(attached_tabstrip_);
810 gfx::Point attached_point(point_in_screen);
811 views::View::ConvertPointFromScreen(attached_tabstrip_, &attached_point);
812 if (attached_point.x() < 0)
813 return DETACH_BEFORE;
814 if (attached_point.x() >= attached_tabstrip_->width())
815 return DETACH_AFTER;
816 return DETACH_ABOVE_OR_BELOW;
819 TabStrip* TabDragController::GetTargetTabStripForPoint(
820 const gfx::Point& point_in_screen) {
821 TRACE_EVENT1("views", "TabDragController::GetTargetTabStripForPoint",
822 "point_in_screen", point_in_screen.ToString());
824 if (move_only() && attached_tabstrip_) {
825 // move_only() is intended for touch, in which case we only want to detach
826 // if the touch point moves significantly in the vertical distance.
827 gfx::Rect tabstrip_bounds = GetViewScreenBounds(attached_tabstrip_);
828 if (DoesRectContainVerticalPointExpanded(tabstrip_bounds,
829 kTouchVerticalDetachMagnetism,
830 point_in_screen.y()))
831 return attached_tabstrip_;
833 gfx::NativeWindow local_window =
834 GetLocalProcessWindow(point_in_screen, is_dragging_window_);
835 // Do not allow dragging into a window with a modal dialog, it causes a weird
836 // behavior. See crbug.com/336691
837 if (!GetModalTransient(local_window)) {
838 TabStrip* tab_strip = GetTabStripForWindow(local_window);
839 if (tab_strip && DoesTabStripContain(tab_strip, point_in_screen))
840 return tab_strip;
843 return is_dragging_window_ ? attached_tabstrip_ : NULL;
846 TabStrip* TabDragController::GetTabStripForWindow(gfx::NativeWindow window) {
847 if (!window)
848 return NULL;
849 BrowserView* browser_view =
850 BrowserView::GetBrowserViewForNativeWindow(window);
851 // We don't allow drops on windows that don't have tabstrips.
852 if (!browser_view ||
853 !browser_view->browser()->SupportsWindowFeature(
854 Browser::FEATURE_TABSTRIP))
855 return NULL;
857 TabStrip* other_tabstrip = browser_view->tabstrip();
858 TabStrip* tab_strip =
859 attached_tabstrip_ ? attached_tabstrip_ : source_tabstrip_;
860 DCHECK(tab_strip);
862 return other_tabstrip->controller()->IsCompatibleWith(tab_strip) ?
863 other_tabstrip : NULL;
866 bool TabDragController::DoesTabStripContain(
867 TabStrip* tabstrip,
868 const gfx::Point& point_in_screen) const {
869 // Make sure the specified screen point is actually within the bounds of the
870 // specified tabstrip...
871 gfx::Rect tabstrip_bounds = GetViewScreenBounds(tabstrip);
872 return point_in_screen.x() < tabstrip_bounds.right() &&
873 point_in_screen.x() >= tabstrip_bounds.x() &&
874 DoesRectContainVerticalPointExpanded(tabstrip_bounds,
875 kVerticalDetachMagnetism,
876 point_in_screen.y());
879 void TabDragController::Attach(TabStrip* attached_tabstrip,
880 const gfx::Point& point_in_screen) {
881 TRACE_EVENT1("views", "TabDragController::Attach",
882 "point_in_screen", point_in_screen.ToString());
884 DCHECK(!attached_tabstrip_); // We should already have detached by the time
885 // we get here.
887 attached_tabstrip_ = attached_tabstrip;
889 std::vector<Tab*> tabs =
890 GetTabsMatchingDraggedContents(attached_tabstrip_);
892 if (tabs.empty()) {
893 // Transitioning from detached to attached to a new tabstrip. Add tabs to
894 // the new model.
896 selection_model_before_attach_.Copy(attached_tabstrip->GetSelectionModel());
898 // Inserting counts as a move. We don't want the tabs to jitter when the
899 // user moves the tab immediately after attaching it.
900 last_move_screen_loc_ = point_in_screen.x();
902 // Figure out where to insert the tab based on the bounds of the dragged
903 // representation and the ideal bounds of the other Tabs already in the
904 // strip. ("ideal bounds" are stable even if the Tabs' actual bounds are
905 // changing due to animation).
906 gfx::Point tab_strip_point(point_in_screen);
907 views::View::ConvertPointFromScreen(attached_tabstrip_, &tab_strip_point);
908 tab_strip_point.set_x(
909 attached_tabstrip_->GetMirroredXInView(tab_strip_point.x()));
910 tab_strip_point.Offset(0, -mouse_offset_.y());
911 int index = GetInsertionIndexForDraggedBounds(
912 GetDraggedViewTabStripBounds(tab_strip_point));
913 attach_index_ = index;
914 attach_x_ = tab_strip_point.x();
915 base::AutoReset<bool> setter(&is_mutating_, true);
916 for (size_t i = 0; i < drag_data_.size(); ++i) {
917 int add_types = TabStripModel::ADD_NONE;
918 if (attached_tabstrip_->touch_layout_.get()) {
919 // StackedTabStripLayout positions relative to the active tab, if we
920 // don't add the tab as active things bounce around.
921 DCHECK_EQ(1u, drag_data_.size());
922 add_types |= TabStripModel::ADD_ACTIVE;
924 if (drag_data_[i].pinned)
925 add_types |= TabStripModel::ADD_PINNED;
926 GetModel(attached_tabstrip_)->InsertWebContentsAt(
927 index + i, drag_data_[i].contents, add_types);
930 tabs = GetTabsMatchingDraggedContents(attached_tabstrip_);
932 DCHECK_EQ(tabs.size(), drag_data_.size());
933 for (size_t i = 0; i < drag_data_.size(); ++i)
934 drag_data_[i].attached_tab = tabs[i];
936 attached_tabstrip_->StartedDraggingTabs(tabs);
938 ResetSelection(GetModel(attached_tabstrip_));
940 // The size of the dragged tab may have changed. Adjust the x offset so that
941 // ratio of mouse_offset_ to original width is maintained.
942 std::vector<Tab*> tabs_to_source(tabs);
943 tabs_to_source.erase(tabs_to_source.begin() + source_tab_index_ + 1,
944 tabs_to_source.end());
945 int new_x = attached_tabstrip_->GetSizeNeededForTabs(tabs_to_source) -
946 tabs[source_tab_index_]->width() +
947 static_cast<int>(offset_to_width_ratio_ *
948 tabs[source_tab_index_]->width());
949 mouse_offset_.set_x(new_x);
951 // Transfer ownership of us to the new tabstrip as well as making sure the
952 // window has capture. This is important so that if activation changes the
953 // drag isn't prematurely canceled.
954 attached_tabstrip_->GetWidget()->SetCapture(attached_tabstrip_);
955 attached_tabstrip_->OwnDragController(this);
958 void TabDragController::Detach(ReleaseCapture release_capture) {
959 TRACE_EVENT1("views", "TabDragController::Detach",
960 "release_capture", release_capture);
962 attach_index_ = -1;
964 // When the user detaches we assume they want to reorder.
965 move_behavior_ = REORDER;
967 // Release ownership of the drag controller and mouse capture. When we
968 // reattach ownership is transfered.
969 attached_tabstrip_->ReleaseDragController();
970 if (release_capture == RELEASE_CAPTURE)
971 attached_tabstrip_->GetWidget()->ReleaseCapture();
973 mouse_move_direction_ = kMovedMouseLeft | kMovedMouseRight;
975 std::vector<gfx::Rect> drag_bounds = CalculateBoundsForDraggedTabs();
976 TabStripModel* attached_model = GetModel(attached_tabstrip_);
977 std::vector<TabRendererData> tab_data;
978 for (size_t i = 0; i < drag_data_.size(); ++i) {
979 tab_data.push_back(drag_data_[i].attached_tab->data());
980 int index = attached_model->GetIndexOfWebContents(drag_data_[i].contents);
981 DCHECK_NE(-1, index);
983 // Hide the tab so that the user doesn't see it animate closed.
984 drag_data_[i].attached_tab->SetVisible(false);
985 drag_data_[i].attached_tab->set_detached();
987 attached_model->DetachWebContentsAt(index);
989 // Detaching may end up deleting the tab, drop references to it.
990 drag_data_[i].attached_tab = NULL;
993 // If we've removed the last Tab from the TabStrip, hide the frame now.
994 if (!attached_model->empty()) {
995 if (!selection_model_before_attach_.empty() &&
996 selection_model_before_attach_.active() >= 0 &&
997 selection_model_before_attach_.active() < attached_model->count()) {
998 // Restore the selection.
999 attached_model->SetSelectionFromModel(selection_model_before_attach_);
1000 } else if (attached_tabstrip_ == source_tabstrip_ &&
1001 !initial_selection_model_.empty()) {
1002 RestoreInitialSelection();
1006 attached_tabstrip_->DraggedTabsDetached();
1007 attached_tabstrip_ = NULL;
1010 void TabDragController::DetachIntoNewBrowserAndRunMoveLoop(
1011 const gfx::Point& point_in_screen) {
1012 if (GetModel(attached_tabstrip_)->count() ==
1013 static_cast<int>(drag_data_.size())) {
1014 // All the tabs in a browser are being dragged but all the tabs weren't
1015 // initially being dragged. For this to happen the user would have to
1016 // start dragging a set of tabs, the other tabs close, then detach.
1017 RunMoveLoop(GetWindowOffset(point_in_screen));
1018 return;
1021 const int last_tabstrip_width = attached_tabstrip_->tab_area_width();
1022 std::vector<gfx::Rect> drag_bounds = CalculateBoundsForDraggedTabs();
1023 OffsetX(GetAttachedDragPoint(point_in_screen).x(), &drag_bounds);
1025 gfx::Vector2d drag_offset;
1026 Browser* browser = CreateBrowserForDrag(
1027 attached_tabstrip_, point_in_screen, &drag_offset, &drag_bounds);
1029 BrowserView* dragged_browser_view =
1030 BrowserView::GetBrowserViewForBrowser(browser);
1031 views::Widget* dragged_widget = dragged_browser_view->GetWidget();
1033 #if defined(USE_AURA)
1034 // Only Aura windows are gesture consumers.
1035 gfx::NativeView attached_native_view =
1036 attached_tabstrip_->GetWidget()->GetNativeView();
1037 ui::GestureRecognizer::Get()->TransferEventsTo(
1038 attached_native_view, dragged_widget->GetNativeView());
1039 #endif
1041 Detach(can_release_capture_ ? RELEASE_CAPTURE : DONT_RELEASE_CAPTURE);
1043 dragged_widget->SetVisibilityChangedAnimationsEnabled(false);
1044 Attach(dragged_browser_view->tabstrip(), gfx::Point());
1045 AdjustBrowserAndTabBoundsForDrag(last_tabstrip_width,
1046 point_in_screen,
1047 &drag_bounds);
1048 WindowPositionManagedUpdater updater;
1049 dragged_widget->AddObserver(&updater);
1050 browser->window()->Show();
1051 dragged_widget->RemoveObserver(&updater);
1052 dragged_widget->SetVisibilityChangedAnimationsEnabled(true);
1053 // Activate may trigger a focus loss, destroying us.
1055 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr());
1056 browser->window()->Activate();
1057 if (!ref)
1058 return;
1060 RunMoveLoop(drag_offset);
1063 void TabDragController::RunMoveLoop(const gfx::Vector2d& drag_offset) {
1064 // If the user drags the whole window we'll assume they are going to attach to
1065 // another window and therefore want to reorder.
1066 move_behavior_ = REORDER;
1068 move_loop_widget_ = GetAttachedBrowserWidget();
1069 DCHECK(move_loop_widget_);
1070 move_loop_widget_->AddObserver(this);
1071 is_dragging_window_ = true;
1072 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr());
1073 if (can_release_capture_) {
1074 // Running the move loop releases mouse capture, which triggers destroying
1075 // the drag loop. Release mouse capture now while the DragController is not
1076 // owned by the TabStrip.
1077 attached_tabstrip_->ReleaseDragController();
1078 attached_tabstrip_->GetWidget()->ReleaseCapture();
1079 attached_tabstrip_->OwnDragController(this);
1081 const views::Widget::MoveLoopSource move_loop_source =
1082 event_source_ == EVENT_SOURCE_MOUSE ?
1083 views::Widget::MOVE_LOOP_SOURCE_MOUSE :
1084 views::Widget::MOVE_LOOP_SOURCE_TOUCH;
1085 const views::Widget::MoveLoopEscapeBehavior escape_behavior =
1086 is_dragging_new_browser_ ?
1087 views::Widget::MOVE_LOOP_ESCAPE_BEHAVIOR_HIDE :
1088 views::Widget::MOVE_LOOP_ESCAPE_BEHAVIOR_DONT_HIDE;
1089 views::Widget::MoveLoopResult result =
1090 move_loop_widget_->RunMoveLoop(
1091 drag_offset, move_loop_source, escape_behavior);
1092 content::NotificationService::current()->Notify(
1093 chrome::NOTIFICATION_TAB_DRAG_LOOP_DONE,
1094 content::NotificationService::AllBrowserContextsAndSources(),
1095 content::NotificationService::NoDetails());
1097 if (!ref)
1098 return;
1099 if (move_loop_widget_) {
1100 move_loop_widget_->RemoveObserver(this);
1101 move_loop_widget_ = NULL;
1103 is_dragging_window_ = false;
1104 waiting_for_run_loop_to_exit_ = false;
1105 if (end_run_loop_behavior_ == END_RUN_LOOP_CONTINUE_DRAGGING) {
1106 end_run_loop_behavior_ = END_RUN_LOOP_STOP_DRAGGING;
1107 if (tab_strip_to_attach_to_after_exit_) {
1108 gfx::Point point_in_screen(GetCursorScreenPoint());
1109 Detach(DONT_RELEASE_CAPTURE);
1110 Attach(tab_strip_to_attach_to_after_exit_, point_in_screen);
1111 // Move the tabs into position.
1112 MoveAttached(point_in_screen);
1113 attached_tabstrip_->GetWidget()->Activate();
1114 // Activate may trigger a focus loss, destroying us.
1115 if (!ref)
1116 return;
1117 tab_strip_to_attach_to_after_exit_ = NULL;
1119 DCHECK(attached_tabstrip_);
1120 attached_tabstrip_->GetWidget()->SetCapture(attached_tabstrip_);
1121 } else if (active_) {
1122 EndDrag(result == views::Widget::MOVE_LOOP_CANCELED ?
1123 END_DRAG_CANCEL : END_DRAG_COMPLETE);
1127 int TabDragController::GetInsertionIndexFrom(const gfx::Rect& dragged_bounds,
1128 int start) const {
1129 const int last_tab = attached_tabstrip_->tab_count() - 1;
1130 // Make the actual "drag insertion point" be just after the leading edge of
1131 // the first dragged tab. This is closer to where the user thinks of the tab
1132 // as "starting" than just dragged_bounds.x(), especially with narrow tabs.
1133 const int dragged_x = dragged_bounds.x() + Tab::leading_width_for_drag();
1134 if (start < 0 || start > last_tab ||
1135 dragged_x < attached_tabstrip_->ideal_bounds(start).x())
1136 return -1;
1138 for (int i = start; i <= last_tab; ++i) {
1139 const gfx::Rect& ideal_bounds = attached_tabstrip_->ideal_bounds(i);
1140 if (dragged_x < (ideal_bounds.x() + (ideal_bounds.width() / 2)))
1141 return i;
1144 return (dragged_x < attached_tabstrip_->ideal_bounds(last_tab).right()) ?
1145 (last_tab + 1) : -1;
1148 int TabDragController::GetInsertionIndexFromReversed(
1149 const gfx::Rect& dragged_bounds,
1150 int start) const {
1151 // Make the actual "drag insertion point" be just after the leading edge of
1152 // the first dragged tab. This is closer to where the user thinks of the tab
1153 // as "starting" than just dragged_bounds.x(), especially with narrow tabs.
1154 const int dragged_x = dragged_bounds.x() + Tab::leading_width_for_drag();
1155 if (start < 0 || start >= attached_tabstrip_->tab_count() ||
1156 dragged_x >= attached_tabstrip_->ideal_bounds(start).right())
1157 return -1;
1159 for (int i = start; i >= 0; --i) {
1160 const gfx::Rect& ideal_bounds = attached_tabstrip_->ideal_bounds(i);
1161 if (dragged_x >= (ideal_bounds.x() + (ideal_bounds.width() / 2)))
1162 return i + 1;
1165 return (dragged_x >= attached_tabstrip_->ideal_bounds(0).x()) ? 0 : -1;
1168 int TabDragController::GetInsertionIndexForDraggedBounds(
1169 const gfx::Rect& dragged_bounds) const {
1170 // If the strip has no tabs, the only position to insert at is 0.
1171 const int tab_count = attached_tabstrip_->tab_count();
1172 if (!tab_count)
1173 return 0;
1175 int index = -1;
1176 if (attached_tabstrip_->touch_layout_.get()) {
1177 index = GetInsertionIndexForDraggedBoundsStacked(dragged_bounds);
1178 if (index != -1) {
1179 // Only move the tab to the left/right if the user actually moved the
1180 // mouse that way. This is necessary as tabs with stacked tabs
1181 // before/after them have multiple drag positions.
1182 int active_index = attached_tabstrip_->touch_layout_->active_index();
1183 if ((index < active_index &&
1184 (mouse_move_direction_ & kMovedMouseLeft) == 0) ||
1185 (index > active_index &&
1186 (mouse_move_direction_ & kMovedMouseRight) == 0)) {
1187 index = active_index;
1190 } else {
1191 index = GetInsertionIndexFrom(dragged_bounds, 0);
1193 if (index == -1) {
1194 const int last_tab_right =
1195 attached_tabstrip_->ideal_bounds(tab_count - 1).right();
1196 index = (dragged_bounds.right() > last_tab_right) ? tab_count : 0;
1199 const Tab* last_visible_tab = attached_tabstrip_->GetLastVisibleTab();
1200 int last_insertion_point = last_visible_tab ?
1201 (attached_tabstrip_->GetModelIndexOfTab(last_visible_tab) + 1) : 0;
1202 if (drag_data_[0].attached_tab) {
1203 // We're not in the process of attaching, so clamp the insertion point to
1204 // keep it within the visible region.
1205 last_insertion_point = std::max(
1206 0, last_insertion_point - static_cast<int>(drag_data_.size()));
1209 // Ensure the first dragged tab always stays in the visible index range.
1210 return std::min(index, last_insertion_point);
1213 bool TabDragController::ShouldDragToNextStackedTab(
1214 const gfx::Rect& dragged_bounds,
1215 int index) const {
1216 if (index + 1 >= attached_tabstrip_->tab_count() ||
1217 !attached_tabstrip_->touch_layout_->IsStacked(index + 1) ||
1218 (mouse_move_direction_ & kMovedMouseRight) == 0)
1219 return false;
1221 int active_x = attached_tabstrip_->ideal_bounds(index).x();
1222 int next_x = attached_tabstrip_->ideal_bounds(index + 1).x();
1223 int mid_x = std::min(next_x - kStackedDistance,
1224 active_x + (next_x - active_x) / 4);
1225 // TODO(pkasting): Should this add Tab::leading_width_for_drag() as
1226 // GetInsertionIndexFrom() does?
1227 return dragged_bounds.x() >= mid_x;
1230 bool TabDragController::ShouldDragToPreviousStackedTab(
1231 const gfx::Rect& dragged_bounds,
1232 int index) const {
1233 if (index - 1 < attached_tabstrip_->GetMiniTabCount() ||
1234 !attached_tabstrip_->touch_layout_->IsStacked(index - 1) ||
1235 (mouse_move_direction_ & kMovedMouseLeft) == 0)
1236 return false;
1238 int active_x = attached_tabstrip_->ideal_bounds(index).x();
1239 int previous_x = attached_tabstrip_->ideal_bounds(index - 1).x();
1240 int mid_x = std::max(previous_x + kStackedDistance,
1241 active_x - (active_x - previous_x) / 4);
1242 // TODO(pkasting): Should this add Tab::leading_width_for_drag() as
1243 // GetInsertionIndexFrom() does?
1244 return dragged_bounds.x() <= mid_x;
1247 int TabDragController::GetInsertionIndexForDraggedBoundsStacked(
1248 const gfx::Rect& dragged_bounds) const {
1249 StackedTabStripLayout* touch_layout = attached_tabstrip_->touch_layout_.get();
1250 int active_index = touch_layout->active_index();
1251 // Search from the active index to the front of the tabstrip. Do this as tabs
1252 // overlap each other from the active index.
1253 int index = GetInsertionIndexFromReversed(dragged_bounds, active_index);
1254 if (index != active_index)
1255 return index;
1256 if (index == -1)
1257 return GetInsertionIndexFrom(dragged_bounds, active_index + 1);
1259 // The position to drag to corresponds to the active tab. If the next/previous
1260 // tab is stacked, then shorten the distance used to determine insertion
1261 // bounds. We do this as GetInsertionIndexFrom() uses the bounds of the
1262 // tabs. When tabs are stacked the next/previous tab is on top of the tab.
1263 if (active_index + 1 < attached_tabstrip_->tab_count() &&
1264 touch_layout->IsStacked(active_index + 1)) {
1265 index = GetInsertionIndexFrom(dragged_bounds, active_index + 1);
1266 if (index == -1 && ShouldDragToNextStackedTab(dragged_bounds, active_index))
1267 index = active_index + 1;
1268 else if (index == -1)
1269 index = active_index;
1270 } else if (ShouldDragToPreviousStackedTab(dragged_bounds, active_index)) {
1271 index = active_index - 1;
1273 return index;
1276 gfx::Rect TabDragController::GetDraggedViewTabStripBounds(
1277 const gfx::Point& tab_strip_point) {
1278 // attached_tab is NULL when inserting into a new tabstrip.
1279 if (source_tab_drag_data()->attached_tab) {
1280 return gfx::Rect(tab_strip_point.x(), tab_strip_point.y(),
1281 source_tab_drag_data()->attached_tab->width(),
1282 source_tab_drag_data()->attached_tab->height());
1285 double sel_width, unselected_width;
1286 attached_tabstrip_->GetCurrentTabWidths(&sel_width, &unselected_width);
1287 return gfx::Rect(tab_strip_point.x(), tab_strip_point.y(),
1288 static_cast<int>(sel_width),
1289 Tab::GetStandardSize().height());
1292 gfx::Point TabDragController::GetAttachedDragPoint(
1293 const gfx::Point& point_in_screen) {
1294 DCHECK(attached_tabstrip_); // The tab must be attached.
1296 gfx::Point tab_loc(point_in_screen);
1297 views::View::ConvertPointFromScreen(attached_tabstrip_, &tab_loc);
1298 const int x =
1299 attached_tabstrip_->GetMirroredXInView(tab_loc.x()) - mouse_offset_.x();
1301 // TODO: consider caching this.
1302 std::vector<Tab*> attached_tabs;
1303 for (size_t i = 0; i < drag_data_.size(); ++i)
1304 attached_tabs.push_back(drag_data_[i].attached_tab);
1305 const int size = attached_tabstrip_->GetSizeNeededForTabs(attached_tabs);
1306 const int max_x = attached_tabstrip_->width() - size;
1307 return gfx::Point(std::min(std::max(x, 0), max_x), 0);
1310 std::vector<Tab*> TabDragController::GetTabsMatchingDraggedContents(
1311 TabStrip* tabstrip) {
1312 TabStripModel* model = GetModel(attached_tabstrip_);
1313 std::vector<Tab*> tabs;
1314 for (size_t i = 0; i < drag_data_.size(); ++i) {
1315 int model_index = model->GetIndexOfWebContents(drag_data_[i].contents);
1316 if (model_index == TabStripModel::kNoTab)
1317 return std::vector<Tab*>();
1318 tabs.push_back(tabstrip->tab_at(model_index));
1320 return tabs;
1323 std::vector<gfx::Rect> TabDragController::CalculateBoundsForDraggedTabs() {
1324 std::vector<gfx::Rect> drag_bounds;
1325 std::vector<Tab*> attached_tabs;
1326 for (size_t i = 0; i < drag_data_.size(); ++i)
1327 attached_tabs.push_back(drag_data_[i].attached_tab);
1328 attached_tabstrip_->CalculateBoundsForDraggedTabs(attached_tabs,
1329 &drag_bounds);
1330 return drag_bounds;
1333 void TabDragController::EndDragImpl(EndDragType type) {
1334 DCHECK(active_);
1335 active_ = false;
1337 bring_to_front_timer_.Stop();
1338 move_stacked_timer_.Stop();
1340 if (is_dragging_window_) {
1341 waiting_for_run_loop_to_exit_ = true;
1343 if (type == NORMAL || (type == TAB_DESTROYED && drag_data_.size() > 1)) {
1344 SetWindowPositionManaged(GetAttachedBrowserWidget()->GetNativeWindow(),
1345 true);
1348 // End the nested drag loop.
1349 GetAttachedBrowserWidget()->EndMoveLoop();
1352 if (type != TAB_DESTROYED) {
1353 // We only finish up the drag if we were actually dragging. If start_drag_
1354 // is false, the user just clicked and released and didn't move the mouse
1355 // enough to trigger a drag.
1356 if (started_drag_) {
1357 RestoreFocus();
1358 if (type == CANCELED)
1359 RevertDrag();
1360 else
1361 CompleteDrag();
1363 } else if (drag_data_.size() > 1) {
1364 initial_selection_model_.Clear();
1365 if (started_drag_)
1366 RevertDrag();
1367 } // else case the only tab we were dragging was deleted. Nothing to do.
1369 // Clear out drag data so we don't attempt to do anything with it.
1370 drag_data_.clear();
1372 TabStrip* owning_tabstrip = attached_tabstrip_ ?
1373 attached_tabstrip_ : source_tabstrip_;
1374 owning_tabstrip->DestroyDragController();
1377 void TabDragController::RevertDrag() {
1378 std::vector<Tab*> tabs;
1379 for (size_t i = 0; i < drag_data_.size(); ++i) {
1380 if (drag_data_[i].contents) {
1381 // Contents is NULL if a tab was destroyed while the drag was under way.
1382 tabs.push_back(drag_data_[i].attached_tab);
1383 RevertDragAt(i);
1387 if (attached_tabstrip_) {
1388 if (did_restore_window_)
1389 MaximizeAttachedWindow();
1390 if (attached_tabstrip_ == source_tabstrip_) {
1391 source_tabstrip_->StoppedDraggingTabs(
1392 tabs, initial_tab_positions_, move_behavior_ == MOVE_VISIBILE_TABS,
1393 false);
1394 } else {
1395 attached_tabstrip_->DraggedTabsDetached();
1399 if (initial_selection_model_.empty())
1400 ResetSelection(GetModel(source_tabstrip_));
1401 else
1402 GetModel(source_tabstrip_)->SetSelectionFromModel(initial_selection_model_);
1404 if (source_tabstrip_)
1405 source_tabstrip_->GetWidget()->Activate();
1408 void TabDragController::ResetSelection(TabStripModel* model) {
1409 DCHECK(model);
1410 ui::ListSelectionModel selection_model;
1411 bool has_one_valid_tab = false;
1412 for (size_t i = 0; i < drag_data_.size(); ++i) {
1413 // |contents| is NULL if a tab was deleted out from under us.
1414 if (drag_data_[i].contents) {
1415 int index = model->GetIndexOfWebContents(drag_data_[i].contents);
1416 DCHECK_NE(-1, index);
1417 selection_model.AddIndexToSelection(index);
1418 if (!has_one_valid_tab || i == source_tab_index_) {
1419 // Reset the active/lead to the first tab. If the source tab is still
1420 // valid we'll reset these again later on.
1421 selection_model.set_active(index);
1422 selection_model.set_anchor(index);
1423 has_one_valid_tab = true;
1427 if (!has_one_valid_tab)
1428 return;
1430 model->SetSelectionFromModel(selection_model);
1433 void TabDragController::RestoreInitialSelection() {
1434 // First time detaching from the source tabstrip. Reset selection model to
1435 // initial_selection_model_. Before resetting though we have to remove all
1436 // the tabs from initial_selection_model_ as it was created with the tabs
1437 // still there.
1438 ui::ListSelectionModel selection_model;
1439 selection_model.Copy(initial_selection_model_);
1440 for (DragData::const_reverse_iterator i(drag_data_.rbegin());
1441 i != drag_data_.rend(); ++i) {
1442 selection_model.DecrementFrom(i->source_model_index);
1444 // We may have cleared out the selection model. Only reset it if it
1445 // contains something.
1446 if (selection_model.empty())
1447 return;
1449 // The anchor/active may have been among the tabs that were dragged out. Force
1450 // the anchor/active to be valid.
1451 if (selection_model.anchor() == ui::ListSelectionModel::kUnselectedIndex)
1452 selection_model.set_anchor(selection_model.selected_indices()[0]);
1453 if (selection_model.active() == ui::ListSelectionModel::kUnselectedIndex)
1454 selection_model.set_active(selection_model.selected_indices()[0]);
1455 GetModel(source_tabstrip_)->SetSelectionFromModel(selection_model);
1458 void TabDragController::RevertDragAt(size_t drag_index) {
1459 DCHECK(started_drag_);
1460 DCHECK(source_tabstrip_);
1462 base::AutoReset<bool> setter(&is_mutating_, true);
1463 TabDragData* data = &(drag_data_[drag_index]);
1464 if (attached_tabstrip_) {
1465 int index =
1466 GetModel(attached_tabstrip_)->GetIndexOfWebContents(data->contents);
1467 if (attached_tabstrip_ != source_tabstrip_) {
1468 // The Tab was inserted into another TabStrip. We need to put it back
1469 // into the original one.
1470 GetModel(attached_tabstrip_)->DetachWebContentsAt(index);
1471 // TODO(beng): (Cleanup) seems like we should use Attach() for this
1472 // somehow.
1473 GetModel(source_tabstrip_)->InsertWebContentsAt(
1474 data->source_model_index, data->contents,
1475 (data->pinned ? TabStripModel::ADD_PINNED : 0));
1476 } else {
1477 // The Tab was moved within the TabStrip where the drag was initiated.
1478 // Move it back to the starting location.
1479 GetModel(source_tabstrip_)->MoveWebContentsAt(
1480 index, data->source_model_index, false);
1482 } else {
1483 // The Tab was detached from the TabStrip where the drag began, and has not
1484 // been attached to any other TabStrip. We need to put it back into the
1485 // source TabStrip.
1486 GetModel(source_tabstrip_)->InsertWebContentsAt(
1487 data->source_model_index, data->contents,
1488 (data->pinned ? TabStripModel::ADD_PINNED : 0));
1492 void TabDragController::CompleteDrag() {
1493 DCHECK(started_drag_);
1495 if (attached_tabstrip_) {
1496 if (is_dragging_new_browser_ || did_restore_window_) {
1497 if (IsDockedOrSnapped(attached_tabstrip_)) {
1498 was_source_maximized_ = false;
1499 was_source_fullscreen_ = false;
1502 // If source window was maximized - maximize the new window as well.
1503 if (was_source_maximized_ || was_source_fullscreen_)
1504 MaximizeAttachedWindow();
1506 attached_tabstrip_->StoppedDraggingTabs(
1507 GetTabsMatchingDraggedContents(attached_tabstrip_),
1508 initial_tab_positions_,
1509 move_behavior_ == MOVE_VISIBILE_TABS,
1510 true);
1511 } else {
1512 // Compel the model to construct a new window for the detached
1513 // WebContentses.
1514 views::Widget* widget = source_tabstrip_->GetWidget();
1515 gfx::Rect window_bounds(widget->GetRestoredBounds());
1516 window_bounds.set_origin(GetWindowCreatePoint(last_point_in_screen_));
1518 base::AutoReset<bool> setter(&is_mutating_, true);
1520 std::vector<TabStripModelDelegate::NewStripContents> contentses;
1521 for (size_t i = 0; i < drag_data_.size(); ++i) {
1522 TabStripModelDelegate::NewStripContents item;
1523 item.web_contents = drag_data_[i].contents;
1524 item.add_types = drag_data_[i].pinned ? TabStripModel::ADD_PINNED
1525 : TabStripModel::ADD_NONE;
1526 contentses.push_back(item);
1529 Browser* new_browser =
1530 GetModel(source_tabstrip_)->delegate()->CreateNewStripWithContents(
1531 contentses, window_bounds, widget->IsMaximized());
1532 ResetSelection(new_browser->tab_strip_model());
1533 new_browser->window()->Show();
1537 void TabDragController::MaximizeAttachedWindow() {
1538 GetAttachedBrowserWidget()->Maximize();
1539 #if defined(USE_ASH)
1540 if (was_source_fullscreen_ &&
1541 host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH) {
1542 // In fullscreen mode it is only possible to get here if the source
1543 // was in "immersive fullscreen" mode, so toggle it back on.
1544 ash::accelerators::ToggleFullscreen();
1546 #endif
1549 gfx::Rect TabDragController::GetViewScreenBounds(
1550 views::View* view) const {
1551 gfx::Point view_topleft;
1552 views::View::ConvertPointToScreen(view, &view_topleft);
1553 gfx::Rect view_screen_bounds = view->GetLocalBounds();
1554 view_screen_bounds.Offset(view_topleft.x(), view_topleft.y());
1555 return view_screen_bounds;
1558 void TabDragController::BringWindowUnderPointToFront(
1559 const gfx::Point& point_in_screen) {
1560 gfx::NativeWindow window = GetLocalProcessWindow(point_in_screen, true);
1562 // Only bring browser windows to front - only windows with a TabStrip can
1563 // be tab drag targets.
1564 if (!GetTabStripForWindow(window))
1565 return;
1567 if (window) {
1568 views::Widget* widget_window = views::Widget::GetWidgetForNativeWindow(
1569 window);
1570 if (!widget_window)
1571 return;
1573 #if defined(USE_ASH)
1574 if (host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH) {
1575 // TODO(varkha): The code below ensures that the phantom drag widget
1576 // is shown on top of browser windows. The code should be moved to ash/
1577 // and the phantom should be able to assert its top-most state on its own.
1578 // One strategy would be for DragWindowController to
1579 // be able to observe stacking changes to the phantom drag widget's
1580 // siblings in order to keep it on top. One way is to implement a
1581 // notification that is sent to a window parent's observers when a
1582 // stacking order is changed among the children of that same parent.
1583 // Note that OnWindowStackingChanged is sent only to the child that is the
1584 // argument of one of the Window::StackChildX calls and not to all its
1585 // siblings affected by the stacking change.
1586 aura::Window* browser_window = widget_window->GetNativeView();
1587 // Find a topmost non-popup window and stack the recipient browser above
1588 // it in order to avoid stacking the browser window on top of the phantom
1589 // drag widget created by DragWindowController in a second display.
1590 for (aura::Window::Windows::const_reverse_iterator it =
1591 browser_window->parent()->children().rbegin();
1592 it != browser_window->parent()->children().rend(); ++it) {
1593 // If the iteration reached the recipient browser window then it is
1594 // already topmost and it is safe to return with no stacking change.
1595 if (*it == browser_window)
1596 return;
1597 if ((*it)->type() != ui::wm::WINDOW_TYPE_POPUP) {
1598 widget_window->StackAbove(*it);
1599 break;
1602 } else {
1603 widget_window->StackAtTop();
1605 #else
1606 widget_window->StackAtTop();
1607 #endif
1609 // The previous call made the window appear on top of the dragged window,
1610 // move the dragged window to the front.
1611 if (is_dragging_window_)
1612 attached_tabstrip_->GetWidget()->StackAtTop();
1616 TabStripModel* TabDragController::GetModel(
1617 TabStrip* tabstrip) const {
1618 return static_cast<BrowserTabStripController*>(tabstrip->controller())->
1619 model();
1622 views::Widget* TabDragController::GetAttachedBrowserWidget() {
1623 return attached_tabstrip_->GetWidget();
1626 bool TabDragController::AreTabsConsecutive() {
1627 for (size_t i = 1; i < drag_data_.size(); ++i) {
1628 if (drag_data_[i - 1].source_model_index + 1 !=
1629 drag_data_[i].source_model_index) {
1630 return false;
1633 return true;
1636 gfx::Rect TabDragController::CalculateDraggedBrowserBounds(
1637 TabStrip* source,
1638 const gfx::Point& point_in_screen,
1639 std::vector<gfx::Rect>* drag_bounds) {
1640 gfx::Point center(0, source->height() / 2);
1641 views::View::ConvertPointToWidget(source, &center);
1642 gfx::Rect new_bounds(source->GetWidget()->GetRestoredBounds());
1643 if (source->GetWidget()->IsMaximized()) {
1644 // If the restore bounds is really small, we don't want to honor it
1645 // (dragging a really small window looks wrong), instead make sure the new
1646 // window is at least 50% the size of the old.
1647 const gfx::Size max_size(
1648 source->GetWidget()->GetWindowBoundsInScreen().size());
1649 new_bounds.set_width(
1650 std::max(max_size.width() / 2, new_bounds.width()));
1651 new_bounds.set_height(
1652 std::max(max_size.height() / 2, new_bounds.height()));
1654 new_bounds.set_y(point_in_screen.y() - center.y());
1655 switch (GetDetachPosition(point_in_screen)) {
1656 case DETACH_BEFORE:
1657 new_bounds.set_x(point_in_screen.x() - center.x());
1658 new_bounds.Offset(-mouse_offset_.x(), 0);
1659 break;
1660 case DETACH_AFTER: {
1661 gfx::Point right_edge(source->width(), 0);
1662 views::View::ConvertPointToWidget(source, &right_edge);
1663 new_bounds.set_x(point_in_screen.x() - right_edge.x());
1664 new_bounds.Offset(drag_bounds->back().right() - mouse_offset_.x(), 0);
1665 OffsetX(-(*drag_bounds)[0].x(), drag_bounds);
1666 break;
1668 default:
1669 break; // Nothing to do for DETACH_ABOVE_OR_BELOW.
1672 // To account for the extra vertical on restored windows that is absent on
1673 // maximized windows, add an additional vertical offset extracted from the tab
1674 // strip.
1675 if (source->GetWidget()->IsMaximized())
1676 new_bounds.Offset(0, -source->kNewTabButtonVerticalOffset);
1677 return new_bounds;
1680 void TabDragController::AdjustBrowserAndTabBoundsForDrag(
1681 int last_tabstrip_width,
1682 const gfx::Point& point_in_screen,
1683 std::vector<gfx::Rect>* drag_bounds) {
1684 attached_tabstrip_->InvalidateLayout();
1685 attached_tabstrip_->DoLayout();
1686 const int dragged_tabstrip_width = attached_tabstrip_->tab_area_width();
1688 // If the new tabstrip is smaller than the old resize the tabs.
1689 if (dragged_tabstrip_width < last_tabstrip_width) {
1690 const float leading_ratio =
1691 drag_bounds->front().x() / static_cast<float>(last_tabstrip_width);
1692 *drag_bounds = CalculateBoundsForDraggedTabs();
1694 if (drag_bounds->back().right() < dragged_tabstrip_width) {
1695 const int delta_x =
1696 std::min(static_cast<int>(leading_ratio * dragged_tabstrip_width),
1697 dragged_tabstrip_width -
1698 (drag_bounds->back().right() -
1699 drag_bounds->front().x()));
1700 OffsetX(delta_x, drag_bounds);
1703 // Reposition the restored window such that the tab that was dragged remains
1704 // under the mouse cursor.
1705 gfx::Point offset(
1706 static_cast<int>((*drag_bounds)[source_tab_index_].width() *
1707 offset_to_width_ratio_) +
1708 (*drag_bounds)[source_tab_index_].x(), 0);
1709 views::View::ConvertPointToWidget(attached_tabstrip_, &offset);
1710 gfx::Rect bounds = GetAttachedBrowserWidget()->GetWindowBoundsInScreen();
1711 bounds.set_x(point_in_screen.x() - offset.x());
1712 GetAttachedBrowserWidget()->SetBounds(bounds);
1714 attached_tabstrip_->SetTabBoundsForDrag(*drag_bounds);
1717 Browser* TabDragController::CreateBrowserForDrag(
1718 TabStrip* source,
1719 const gfx::Point& point_in_screen,
1720 gfx::Vector2d* drag_offset,
1721 std::vector<gfx::Rect>* drag_bounds) {
1722 gfx::Rect new_bounds(CalculateDraggedBrowserBounds(source,
1723 point_in_screen,
1724 drag_bounds));
1725 *drag_offset = point_in_screen - new_bounds.origin();
1727 Profile* profile =
1728 Profile::FromBrowserContext(drag_data_[0].contents->GetBrowserContext());
1729 Browser::CreateParams create_params(Browser::TYPE_TABBED,
1730 profile,
1731 host_desktop_type_);
1732 create_params.initial_bounds = new_bounds;
1733 Browser* browser = new Browser(create_params);
1734 is_dragging_new_browser_ = true;
1735 SetWindowPositionManaged(browser->window()->GetNativeWindow(), false);
1736 // If the window is created maximized then the bounds we supplied are ignored.
1737 // We need to reset them again so they are honored.
1738 browser->window()->SetBounds(new_bounds);
1740 return browser;
1743 gfx::Point TabDragController::GetCursorScreenPoint() {
1744 #if defined(USE_ASH)
1745 if (host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH &&
1746 event_source_ == EVENT_SOURCE_TOUCH &&
1747 aura::Env::GetInstance()->is_touch_down()) {
1748 views::Widget* widget = GetAttachedBrowserWidget();
1749 DCHECK(widget);
1750 aura::Window* widget_window = widget->GetNativeWindow();
1751 DCHECK(widget_window->GetRootWindow());
1752 gfx::PointF touch_point_f;
1753 bool got_touch_point = ui::GestureRecognizer::Get()->
1754 GetLastTouchPointForTarget(widget_window, &touch_point_f);
1755 // TODO(tdresser): Switch to using gfx::PointF. See crbug.com/337824.
1756 gfx::Point touch_point = gfx::ToFlooredPoint(touch_point_f);
1757 DCHECK(got_touch_point);
1758 wm::ConvertPointToScreen(widget_window->GetRootWindow(), &touch_point);
1759 return touch_point;
1761 #endif
1763 return screen_->GetCursorScreenPoint();
1766 gfx::Vector2d TabDragController::GetWindowOffset(
1767 const gfx::Point& point_in_screen) {
1768 TabStrip* owning_tabstrip = attached_tabstrip_ ?
1769 attached_tabstrip_ : source_tabstrip_;
1770 views::View* toplevel_view = owning_tabstrip->GetWidget()->GetContentsView();
1772 gfx::Point point = point_in_screen;
1773 views::View::ConvertPointFromScreen(toplevel_view, &point);
1774 return point.OffsetFromOrigin();
1777 gfx::NativeWindow TabDragController::GetLocalProcessWindow(
1778 const gfx::Point& screen_point,
1779 bool exclude_dragged_view) {
1780 std::set<gfx::NativeWindow> exclude;
1781 if (exclude_dragged_view) {
1782 gfx::NativeWindow dragged_window =
1783 attached_tabstrip_->GetWidget()->GetNativeWindow();
1784 if (dragged_window)
1785 exclude.insert(dragged_window);
1787 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1788 // Exclude windows which are pending deletion via Browser::TabStripEmpty().
1789 // These windows can be returned in the Linux Aura port because the browser
1790 // window which was used for dragging is not hidden once all of its tabs are
1791 // attached to another browser window in DragBrowserToNewTabStrip().
1792 // TODO(pkotwicz): Fix this properly (crbug.com/358482)
1793 BrowserList* browser_list = BrowserList::GetInstance(
1794 chrome::HOST_DESKTOP_TYPE_NATIVE);
1795 for (BrowserList::const_iterator it = browser_list->begin();
1796 it != browser_list->end(); ++it) {
1797 if ((*it)->tab_strip_model()->empty())
1798 exclude.insert((*it)->window()->GetNativeWindow());
1800 #endif
1801 return GetLocalProcessWindowAtPoint(host_desktop_type_,
1802 screen_point,
1803 exclude);