OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/tabs/tab_drag_controller.h" | 5 #include "chrome/browser/ui/views/tabs/tab_drag_controller.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 105 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
106 | 106 |
107 gfx::ImageSkia* high_icon = rb.GetImageSkiaNamed(IDR_DOCK_HIGH); | 107 gfx::ImageSkia* high_icon = rb.GetImageSkiaNamed(IDR_DOCK_HIGH); |
108 gfx::ImageSkia* wide_icon = rb.GetImageSkiaNamed(IDR_DOCK_WIDE); | 108 gfx::ImageSkia* wide_icon = rb.GetImageSkiaNamed(IDR_DOCK_WIDE); |
109 | 109 |
110 canvas->Save(); | 110 canvas->Save(); |
111 bool rtl_ui = base::i18n::IsRTL(); | 111 bool rtl_ui = base::i18n::IsRTL(); |
112 if (rtl_ui) { | 112 if (rtl_ui) { |
113 // Flip canvas to draw the mirrored tab images for RTL UI. | 113 // Flip canvas to draw the mirrored tab images for RTL UI. |
114 canvas->Translate(gfx::Point(width(), 0)); | 114 canvas->Translate(gfx::Vector2d(width(), 0)); |
115 canvas->Scale(-1, 1); | 115 canvas->Scale(-1, 1); |
116 } | 116 } |
117 int x_of_active_tab = width() / 2 + kTabSpacing / 2; | 117 int x_of_active_tab = width() / 2 + kTabSpacing / 2; |
118 int x_of_inactive_tab = width() / 2 - high_icon->width() - kTabSpacing / 2; | 118 int x_of_inactive_tab = width() / 2 - high_icon->width() - kTabSpacing / 2; |
119 switch (type_) { | 119 switch (type_) { |
120 case DockInfo::LEFT_OF_WINDOW: | 120 case DockInfo::LEFT_OF_WINDOW: |
121 case DockInfo::LEFT_HALF: | 121 case DockInfo::LEFT_HALF: |
122 if (!rtl_ui) | 122 if (!rtl_ui) |
123 std::swap(x_of_active_tab, x_of_inactive_tab); | 123 std::swap(x_of_active_tab, x_of_inactive_tab); |
124 canvas->DrawImageInt(*high_icon, x_of_active_tab, | 124 canvas->DrawImageInt(*high_icon, x_of_active_tab, |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 | 474 |
475 if (!started_drag_) { | 475 if (!started_drag_) { |
476 if (!CanStartDrag(real_point_in_screen)) | 476 if (!CanStartDrag(real_point_in_screen)) |
477 return; // User hasn't dragged far enough yet. | 477 return; // User hasn't dragged far enough yet. |
478 | 478 |
479 started_drag_ = true; | 479 started_drag_ = true; |
480 SaveFocus(); | 480 SaveFocus(); |
481 Attach(source_tabstrip_, gfx::Point()); | 481 Attach(source_tabstrip_, gfx::Point()); |
482 if (detach_into_browser_ && static_cast<int>(drag_data_.size()) == | 482 if (detach_into_browser_ && static_cast<int>(drag_data_.size()) == |
483 GetModel(source_tabstrip_)->count()) { | 483 GetModel(source_tabstrip_)->count()) { |
484 gfx::Point dragged_view_point = GetWindowOffset(point_in_screen); | 484 gfx::Vector2d dragged_view_offset = GetWindowOffset(point_in_screen); |
485 RunMoveLoop(dragged_view_point); | 485 RunMoveLoop(dragged_view_offset); |
486 return; | 486 return; |
487 } | 487 } |
488 } | 488 } |
489 | 489 |
490 ContinueDragging(real_point_in_screen); | 490 ContinueDragging(real_point_in_screen); |
491 } | 491 } |
492 | 492 |
493 void TabDragController::EndDrag(EndDragReason reason) { | 493 void TabDragController::EndDrag(EndDragReason reason) { |
494 // If we're dragging a window ignore capture lost since it'll ultimately | 494 // If we're dragging a window ignore capture lost since it'll ultimately |
495 // trigger the move loop to end and we'll revert the drag when RunMoveLoop() | 495 // trigger the move loop to end and we'll revert the drag when RunMoveLoop() |
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1265 attached_tabstrip_ = NULL; | 1265 attached_tabstrip_ = NULL; |
1266 } | 1266 } |
1267 | 1267 |
1268 void TabDragController::DetachIntoNewBrowserAndRunMoveLoop( | 1268 void TabDragController::DetachIntoNewBrowserAndRunMoveLoop( |
1269 const gfx::Point& point_in_screen) { | 1269 const gfx::Point& point_in_screen) { |
1270 if (GetModel(attached_tabstrip_)->count() == | 1270 if (GetModel(attached_tabstrip_)->count() == |
1271 static_cast<int>(drag_data_.size())) { | 1271 static_cast<int>(drag_data_.size())) { |
1272 // All the tabs in a browser are being dragged but all the tabs weren't | 1272 // All the tabs in a browser are being dragged but all the tabs weren't |
1273 // initially being dragged. For this to happen the user would have to | 1273 // initially being dragged. For this to happen the user would have to |
1274 // start dragging a set of tabs, the other tabs close, then detach. | 1274 // start dragging a set of tabs, the other tabs close, then detach. |
1275 gfx::Point dragged_view_point = GetWindowOffset(point_in_screen); | 1275 gfx::Vector2d dragged_view_offset = GetWindowOffset(point_in_screen); |
1276 RunMoveLoop(dragged_view_point); | 1276 RunMoveLoop(dragged_view_offset); |
1277 return; | 1277 return; |
1278 } | 1278 } |
1279 | 1279 |
1280 // Create a new browser to house the dragged tabs and have the OS run a move | 1280 // Create a new browser to house the dragged tabs and have the OS run a move |
1281 // loop. | 1281 // loop. |
1282 | 1282 |
1283 gfx::Point attached_point = GetAttachedDragPoint(point_in_screen); | 1283 gfx::Point attached_point = GetAttachedDragPoint(point_in_screen); |
1284 | 1284 |
1285 // Calculate the bounds for the tabs from the attached_tab_strip. We do this | 1285 // Calculate the bounds for the tabs from the attached_tab_strip. We do this |
1286 // so that the tabs don't change size when detached. | 1286 // so that the tabs don't change size when detached. |
1287 std::vector<gfx::Rect> drag_bounds = | 1287 std::vector<gfx::Rect> drag_bounds = |
1288 CalculateBoundsForDraggedTabs(attached_point.x()); | 1288 CalculateBoundsForDraggedTabs(attached_point.x()); |
1289 | 1289 |
1290 gfx::Point drag_offset; | 1290 gfx::Vector2d drag_offset; |
1291 Browser* browser = CreateBrowserForDrag( | 1291 Browser* browser = CreateBrowserForDrag( |
1292 attached_tabstrip_, point_in_screen, &drag_offset, &drag_bounds); | 1292 attached_tabstrip_, point_in_screen, &drag_offset, &drag_bounds); |
1293 Detach(DONT_RELEASE_CAPTURE); | 1293 Detach(DONT_RELEASE_CAPTURE); |
1294 BrowserView* dragged_browser_view = | 1294 BrowserView* dragged_browser_view = |
1295 BrowserView::GetBrowserViewForBrowser(browser); | 1295 BrowserView::GetBrowserViewForBrowser(browser); |
1296 dragged_browser_view->GetWidget()->SetVisibilityChangedAnimationsEnabled( | 1296 dragged_browser_view->GetWidget()->SetVisibilityChangedAnimationsEnabled( |
1297 false); | 1297 false); |
1298 Attach(dragged_browser_view->tabstrip(), gfx::Point()); | 1298 Attach(dragged_browser_view->tabstrip(), gfx::Point()); |
1299 // TODO: come up with a cleaner way to do this. | 1299 // TODO: come up with a cleaner way to do this. |
1300 attached_tabstrip_->SetTabBoundsForDrag(drag_bounds); | 1300 attached_tabstrip_->SetTabBoundsForDrag(drag_bounds); |
1301 | 1301 |
1302 browser->window()->Show(); | 1302 browser->window()->Show(); |
1303 browser->window()->Activate(); | 1303 browser->window()->Activate(); |
1304 dragged_browser_view->GetWidget()->SetVisibilityChangedAnimationsEnabled( | 1304 dragged_browser_view->GetWidget()->SetVisibilityChangedAnimationsEnabled( |
1305 true); | 1305 true); |
1306 RunMoveLoop(drag_offset); | 1306 RunMoveLoop(drag_offset); |
1307 } | 1307 } |
1308 | 1308 |
1309 void TabDragController::RunMoveLoop(const gfx::Point& drag_offset) { | 1309 void TabDragController::RunMoveLoop(const gfx::Vector2d& drag_offset) { |
1310 // If the user drags the whole window we'll assume they are going to attach to | 1310 // If the user drags the whole window we'll assume they are going to attach to |
1311 // another window and therefor want to reorder. | 1311 // another window and therefor want to reorder. |
1312 move_behavior_ = REORDER; | 1312 move_behavior_ = REORDER; |
1313 | 1313 |
1314 move_loop_widget_ = GetAttachedBrowserWidget(); | 1314 move_loop_widget_ = GetAttachedBrowserWidget(); |
1315 DCHECK(move_loop_widget_); | 1315 DCHECK(move_loop_widget_); |
1316 move_loop_widget_->AddObserver(this); | 1316 move_loop_widget_->AddObserver(this); |
1317 is_dragging_window_ = true; | 1317 is_dragging_window_ = true; |
1318 bool destroyed = false; | 1318 bool destroyed = false; |
1319 destroyed_ = &destroyed; | 1319 destroyed_ = &destroyed; |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1930 drag_data_[i].source_model_index) { | 1930 drag_data_[i].source_model_index) { |
1931 return false; | 1931 return false; |
1932 } | 1932 } |
1933 } | 1933 } |
1934 return true; | 1934 return true; |
1935 } | 1935 } |
1936 | 1936 |
1937 Browser* TabDragController::CreateBrowserForDrag( | 1937 Browser* TabDragController::CreateBrowserForDrag( |
1938 TabStrip* source, | 1938 TabStrip* source, |
1939 const gfx::Point& point_in_screen, | 1939 const gfx::Point& point_in_screen, |
1940 gfx::Point* drag_offset, | 1940 gfx::Vector2d* drag_offset, |
1941 std::vector<gfx::Rect>* drag_bounds) { | 1941 std::vector<gfx::Rect>* drag_bounds) { |
1942 gfx::Point center(0, source->height() / 2); | 1942 gfx::Point center(0, source->height() / 2); |
1943 views::View::ConvertPointToWidget(source, ¢er); | 1943 views::View::ConvertPointToWidget(source, ¢er); |
1944 gfx::Rect new_bounds(source->GetWidget()->GetWindowBoundsInScreen()); | 1944 gfx::Rect new_bounds(source->GetWidget()->GetWindowBoundsInScreen()); |
1945 new_bounds.set_y(point_in_screen.y() - center.y()); | 1945 new_bounds.set_y(point_in_screen.y() - center.y()); |
1946 switch (GetDetachPosition(point_in_screen)) { | 1946 switch (GetDetachPosition(point_in_screen)) { |
1947 case DETACH_BEFORE: | 1947 case DETACH_BEFORE: |
1948 new_bounds.set_x(point_in_screen.x() - center.x()); | 1948 new_bounds.set_x(point_in_screen.x() - center.x()); |
1949 new_bounds.Offset(-mouse_offset_.x(), 0); | 1949 new_bounds.Offset(-mouse_offset_.x(), 0); |
1950 break; | 1950 break; |
1951 | 1951 |
1952 case DETACH_AFTER: { | 1952 case DETACH_AFTER: { |
1953 gfx::Point right_edge(source->width(), 0); | 1953 gfx::Point right_edge(source->width(), 0); |
1954 views::View::ConvertPointToWidget(source, &right_edge); | 1954 views::View::ConvertPointToWidget(source, &right_edge); |
1955 new_bounds.set_x(point_in_screen.x() - right_edge.x()); | 1955 new_bounds.set_x(point_in_screen.x() - right_edge.x()); |
1956 new_bounds.Offset(drag_bounds->back().right() - mouse_offset_.x(), 0); | 1956 new_bounds.Offset(drag_bounds->back().right() - mouse_offset_.x(), 0); |
1957 int delta = (*drag_bounds)[0].x(); | 1957 int delta = (*drag_bounds)[0].x(); |
1958 for (size_t i = 0; i < drag_bounds->size(); ++i) | 1958 for (size_t i = 0; i < drag_bounds->size(); ++i) |
1959 (*drag_bounds)[i].Offset(-delta, 0); | 1959 (*drag_bounds)[i].Offset(-delta, 0); |
1960 break; | 1960 break; |
1961 } | 1961 } |
1962 | 1962 |
1963 default: | 1963 default: |
1964 break; // Nothing to do for DETACH_ABOVE_OR_BELOW. | 1964 break; // Nothing to do for DETACH_ABOVE_OR_BELOW. |
1965 } | 1965 } |
1966 | 1966 |
1967 *drag_offset = point_in_screen.Subtract(new_bounds.origin()); | 1967 *drag_offset = point_in_screen.DistanceFrom(new_bounds.origin()); |
1968 | 1968 |
1969 Browser::CreateParams create_params(drag_data_[0].contents->profile()); | 1969 Browser::CreateParams create_params(drag_data_[0].contents->profile()); |
1970 create_params.initial_bounds = new_bounds; | 1970 create_params.initial_bounds = new_bounds; |
1971 Browser* browser = new Browser(create_params); | 1971 Browser* browser = new Browser(create_params); |
1972 SetTrackedByWorkspace(browser->window()->GetNativeWindow(), false); | 1972 SetTrackedByWorkspace(browser->window()->GetNativeWindow(), false); |
1973 // If the window is created maximized then the bounds we supplied are ignored. | 1973 // If the window is created maximized then the bounds we supplied are ignored. |
1974 // We need to reset them again so they are honored. | 1974 // We need to reset them again so they are honored. |
1975 browser->window()->SetBounds(new_bounds); | 1975 browser->window()->SetBounds(new_bounds); |
1976 return browser; | 1976 return browser; |
1977 } | 1977 } |
1978 | 1978 |
1979 gfx::Point TabDragController::GetCursorScreenPoint() { | 1979 gfx::Point TabDragController::GetCursorScreenPoint() { |
1980 #if defined(USE_ASH) | 1980 #if defined(USE_ASH) |
1981 views::Widget* widget = GetAttachedBrowserWidget(); | 1981 views::Widget* widget = GetAttachedBrowserWidget(); |
1982 DCHECK(widget); | 1982 DCHECK(widget); |
1983 if (aura::Env::GetInstance()->is_touch_down()) { | 1983 if (aura::Env::GetInstance()->is_touch_down()) { |
1984 aura::Window* widget_window = widget->GetNativeWindow(); | 1984 aura::Window* widget_window = widget->GetNativeWindow(); |
1985 DCHECK(widget_window->GetRootWindow()); | 1985 DCHECK(widget_window->GetRootWindow()); |
1986 gfx::Point touch_point; | 1986 gfx::Point touch_point; |
1987 bool got_touch_point = widget_window->GetRootWindow()-> | 1987 bool got_touch_point = widget_window->GetRootWindow()-> |
1988 gesture_recognizer()->GetLastTouchPointForTarget(widget_window, | 1988 gesture_recognizer()->GetLastTouchPointForTarget(widget_window, |
1989 &touch_point); | 1989 &touch_point); |
1990 DCHECK(got_touch_point); | 1990 DCHECK(got_touch_point); |
1991 return touch_point; | 1991 return touch_point; |
1992 } | 1992 } |
1993 #endif | 1993 #endif |
1994 return screen_->GetCursorScreenPoint(); | 1994 return screen_->GetCursorScreenPoint(); |
1995 } | 1995 } |
1996 | 1996 |
1997 gfx::Point TabDragController::GetWindowOffset( | 1997 gfx::Vector2d TabDragController::GetWindowOffset( |
1998 const gfx::Point& point_in_screen) { | 1998 const gfx::Point& point_in_screen) { |
1999 TabStrip* owning_tabstrip = (attached_tabstrip_ && detach_into_browser_) ? | 1999 TabStrip* owning_tabstrip = (attached_tabstrip_ && detach_into_browser_) ? |
2000 attached_tabstrip_ : source_tabstrip_; | 2000 attached_tabstrip_ : source_tabstrip_; |
2001 views::View* toplevel_view = owning_tabstrip->GetWidget()->GetContentsView(); | 2001 views::View* toplevel_view = owning_tabstrip->GetWidget()->GetContentsView(); |
2002 | 2002 |
2003 gfx::Point offset = point_in_screen; | 2003 gfx::Point point = point_in_screen; |
2004 views::View::ConvertPointFromScreen(toplevel_view, &offset); | 2004 views::View::ConvertPointFromScreen(toplevel_view, &point); |
2005 return offset; | 2005 return point.DistanceFromOrigin(); |
2006 } | 2006 } |
OLD | NEW |