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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 #endif | 203 #endif |
204 } | 204 } |
205 | 205 |
206 void SetWindowPositionManaged(gfx::NativeWindow window, bool value) { | 206 void SetWindowPositionManaged(gfx::NativeWindow window, bool value) { |
207 #if defined(USE_ASH) | 207 #if defined(USE_ASH) |
208 ash::wm::GetWindowState(window)->set_window_position_managed(value); | 208 ash::wm::GetWindowState(window)->set_window_position_managed(value); |
209 #endif | 209 #endif |
210 } | 210 } |
211 | 211 |
212 // Returns true if |tab_strip| browser window is docked. | 212 // Returns true if |tab_strip| browser window is docked. |
213 bool IsDocked(const TabStrip* tab_strip) { | 213 bool IsDockedOrSnapped(const TabStrip* tab_strip) { |
214 #if defined(USE_ASH) | 214 #if defined(USE_ASH) |
215 DCHECK(tab_strip); | 215 DCHECK(tab_strip); |
216 return ash::wm::GetWindowState( | 216 ash::wm::WindowState* window_state = |
217 tab_strip->GetWidget()->GetNativeWindow())->IsDocked(); | 217 ash::wm::GetWindowState(tab_strip->GetWidget()->GetNativeWindow()); |
| 218 return window_state->IsDocked() || |
| 219 window_state->window_show_type() == ash::wm::SHOW_TYPE_LEFT_SNAPPED || |
| 220 window_state->window_show_type() == ash::wm::SHOW_TYPE_RIGHT_SNAPPED; |
218 #endif | 221 #endif |
219 return false; | 222 return false; |
220 } | 223 } |
221 | 224 |
222 // Returns true if |bounds| contains the y-coordinate |y|. The y-coordinate | 225 // Returns true if |bounds| contains the y-coordinate |y|. The y-coordinate |
223 // of |bounds| is adjusted by |vertical_adjustment|. | 226 // of |bounds| is adjusted by |vertical_adjustment|. |
224 bool DoesRectContainVerticalPointExpanded( | 227 bool DoesRectContainVerticalPointExpanded( |
225 const gfx::Rect& bounds, | 228 const gfx::Rect& bounds, |
226 int vertical_adjustment, | 229 int vertical_adjustment, |
227 int y) { | 230 int y) { |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 { | 542 { |
540 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr()); | 543 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr()); |
541 SaveFocus(); | 544 SaveFocus(); |
542 if (!ref) | 545 if (!ref) |
543 return; | 546 return; |
544 } | 547 } |
545 started_drag_ = true; | 548 started_drag_ = true; |
546 Attach(source_tabstrip_, gfx::Point()); | 549 Attach(source_tabstrip_, gfx::Point()); |
547 if (detach_into_browser_ && static_cast<int>(drag_data_.size()) == | 550 if (detach_into_browser_ && static_cast<int>(drag_data_.size()) == |
548 GetModel(source_tabstrip_)->count()) { | 551 GetModel(source_tabstrip_)->count()) { |
| 552 #if defined(USE_ASH) |
| 553 if (host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH && |
| 554 (was_source_maximized_ || was_source_fullscreen_)) { |
| 555 // When all tabs in a maximized browser are dragged the browser gets |
| 556 // restored during the drag and maximized back when the drag ends. |
| 557 views::Widget* widget = GetAttachedBrowserWidget(); |
| 558 const int last_tabstrip_width = attached_tabstrip_->tab_area_width(); |
| 559 std::vector<gfx::Rect> drag_bounds = CalculateBoundsForDraggedTabs(); |
| 560 OffsetX(GetAttachedDragPoint(point_in_screen).x(), &drag_bounds); |
| 561 gfx::Rect new_bounds(CalculateDraggedBrowserBounds(source_tabstrip_, |
| 562 point_in_screen, |
| 563 &drag_bounds)); |
| 564 new_bounds.Offset(-widget->GetRestoredBounds().x() + |
| 565 point_in_screen.x() - |
| 566 mouse_offset_.x(), 0); |
| 567 widget->SetVisibilityChangedAnimationsEnabled(false); |
| 568 widget->Restore(); |
| 569 widget->SetBounds(new_bounds); |
| 570 AdjustBrowserAndTabBoundsForDrag(last_tabstrip_width, |
| 571 point_in_screen, |
| 572 &drag_bounds); |
| 573 widget->SetVisibilityChangedAnimationsEnabled(true); |
| 574 is_dragging_new_browser_ = true; |
| 575 } |
| 576 #endif |
549 RunMoveLoop(GetWindowOffset(point_in_screen)); | 577 RunMoveLoop(GetWindowOffset(point_in_screen)); |
550 return; | 578 return; |
551 } | 579 } |
552 } | 580 } |
553 | 581 |
554 ContinueDragging(point_in_screen); | 582 ContinueDragging(point_in_screen); |
555 } | 583 } |
556 | 584 |
557 void TabDragController::EndDrag(EndDragReason reason) { | 585 void TabDragController::EndDrag(EndDragReason reason) { |
558 TRACE_EVENT0("views", "TabDragController::EndDrag"); | 586 TRACE_EVENT0("views", "TabDragController::EndDrag"); |
(...skipping 910 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1469 // gesture sequence and the GR's touch events queue to the new window. This | 1497 // gesture sequence and the GR's touch events queue to the new window. This |
1470 // should really be done somewhere in capture change code and or inside the | 1498 // should really be done somewhere in capture change code and or inside the |
1471 // GR. But we currently do not have a consistent way for doing it that would | 1499 // GR. But we currently do not have a consistent way for doing it that would |
1472 // work in all cases. Hence this hack. | 1500 // work in all cases. Hence this hack. |
1473 ui::GestureRecognizer::Get()->TransferEventsTo( | 1501 ui::GestureRecognizer::Get()->TransferEventsTo( |
1474 attached_native_view, | 1502 attached_native_view, |
1475 dragged_widget->GetNativeView()); | 1503 dragged_widget->GetNativeView()); |
1476 #endif | 1504 #endif |
1477 dragged_widget->SetVisibilityChangedAnimationsEnabled(false); | 1505 dragged_widget->SetVisibilityChangedAnimationsEnabled(false); |
1478 Attach(dragged_browser_view->tabstrip(), gfx::Point()); | 1506 Attach(dragged_browser_view->tabstrip(), gfx::Point()); |
1479 attached_tabstrip_->InvalidateLayout(); | 1507 AdjustBrowserAndTabBoundsForDrag(last_tabstrip_width, |
1480 dragged_widget->non_client_view()->Layout(); | 1508 point_in_screen, |
1481 const int dragged_tabstrip_width = attached_tabstrip_->tab_area_width(); | 1509 &drag_bounds); |
1482 | |
1483 // If the new tabstrip is smaller than the old resize the tabs. | |
1484 if (dragged_tabstrip_width < last_tabstrip_width) { | |
1485 const float leading_ratio = | |
1486 drag_bounds.front().x() / static_cast<float>(last_tabstrip_width); | |
1487 drag_bounds = CalculateBoundsForDraggedTabs(); | |
1488 | |
1489 if (drag_bounds.back().right() < dragged_tabstrip_width) { | |
1490 const int delta_x = | |
1491 std::min(static_cast<int>(leading_ratio * dragged_tabstrip_width), | |
1492 dragged_tabstrip_width - | |
1493 (drag_bounds.back().right() - | |
1494 drag_bounds.front().x())); | |
1495 OffsetX(delta_x, &drag_bounds); | |
1496 } | |
1497 | |
1498 // Reposition the restored window such that the tab that was dragged remains | |
1499 // under the mouse cursor. | |
1500 gfx::Point offset( | |
1501 static_cast<int>(drag_bounds[source_tab_index_].width() * | |
1502 offset_to_width_ratio_) + | |
1503 drag_bounds[source_tab_index_].x(), 0); | |
1504 views::View::ConvertPointToWidget(attached_tabstrip_, &offset); | |
1505 gfx::Rect new_bounds = browser->window()->GetBounds(); | |
1506 new_bounds.set_x(point_in_screen.x() - offset.x()); | |
1507 browser->window()->SetBounds(new_bounds); | |
1508 } | |
1509 | |
1510 attached_tabstrip_->SetTabBoundsForDrag(drag_bounds); | |
1511 | |
1512 WindowPositionManagedUpdater updater; | 1510 WindowPositionManagedUpdater updater; |
1513 dragged_widget->AddObserver(&updater); | 1511 dragged_widget->AddObserver(&updater); |
1514 browser->window()->Show(); | 1512 browser->window()->Show(); |
1515 dragged_widget->RemoveObserver(&updater); | 1513 dragged_widget->RemoveObserver(&updater); |
1516 dragged_widget->SetVisibilityChangedAnimationsEnabled(true); | 1514 dragged_widget->SetVisibilityChangedAnimationsEnabled(true); |
1517 // Activate may trigger a focus loss, destroying us. | 1515 // Activate may trigger a focus loss, destroying us. |
1518 { | 1516 { |
1519 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr()); | 1517 base::WeakPtr<TabDragController> ref(weak_factory_.GetWeakPtr()); |
1520 browser->window()->Activate(); | 1518 browser->window()->Activate(); |
1521 if (!ref) | 1519 if (!ref) |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1928 data->source_model_index, data->contents, | 1926 data->source_model_index, data->contents, |
1929 (data->pinned ? TabStripModel::ADD_PINNED : 0)); | 1927 (data->pinned ? TabStripModel::ADD_PINNED : 0)); |
1930 } | 1928 } |
1931 } | 1929 } |
1932 | 1930 |
1933 void TabDragController::CompleteDrag() { | 1931 void TabDragController::CompleteDrag() { |
1934 DCHECK(started_drag_); | 1932 DCHECK(started_drag_); |
1935 | 1933 |
1936 if (attached_tabstrip_) { | 1934 if (attached_tabstrip_) { |
1937 if (is_dragging_new_browser_) { | 1935 if (is_dragging_new_browser_) { |
1938 if (IsDocked(attached_tabstrip_)) { | 1936 if (IsDockedOrSnapped(attached_tabstrip_)) { |
1939 DCHECK_EQ(host_desktop_type_, chrome::HOST_DESKTOP_TYPE_ASH); | 1937 DCHECK_EQ(host_desktop_type_, chrome::HOST_DESKTOP_TYPE_ASH); |
1940 was_source_maximized_ = false; | 1938 was_source_maximized_ = false; |
1941 was_source_fullscreen_ = false; | 1939 was_source_fullscreen_ = false; |
1942 } | 1940 } |
1943 // If source window was maximized - maximize the new window as well. | 1941 // If source window was maximized - maximize the new window as well. |
1944 if (was_source_maximized_) | 1942 if (was_source_maximized_ || was_source_fullscreen_) |
1945 attached_tabstrip_->GetWidget()->Maximize(); | 1943 GetAttachedBrowserWidget()->Maximize(); |
1946 #if defined(USE_ASH) | 1944 #if defined(USE_ASH) |
1947 if (was_source_fullscreen_ && | 1945 if (was_source_fullscreen_ && |
1948 host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH) { | 1946 host_desktop_type_ == chrome::HOST_DESKTOP_TYPE_ASH) { |
1949 // In fullscreen mode it is only possible to get here if the source | 1947 // In fullscreen mode it is only possible to get here if the source |
1950 // was in "immersive fullscreen" mode, so toggle it back on. | 1948 // was in "immersive fullscreen" mode, so toggle it back on. |
1951 ash::accelerators::ToggleFullscreen(); | 1949 ash::accelerators::ToggleFullscreen(); |
1952 } | 1950 } |
1953 #endif | 1951 #endif |
1954 } else { | |
1955 // When dragging results in maximized or fullscreen browser window getting | |
1956 // docked, restore it. | |
1957 if ((was_source_fullscreen_ || was_source_maximized_) && | |
1958 (IsDocked(attached_tabstrip_))) { | |
1959 DCHECK_EQ(host_desktop_type_, chrome::HOST_DESKTOP_TYPE_ASH); | |
1960 attached_tabstrip_->GetWidget()->Restore(); | |
1961 } | |
1962 } | 1952 } |
1963 attached_tabstrip_->StoppedDraggingTabs( | 1953 attached_tabstrip_->StoppedDraggingTabs( |
1964 GetTabsMatchingDraggedContents(attached_tabstrip_), | 1954 GetTabsMatchingDraggedContents(attached_tabstrip_), |
1965 initial_tab_positions_, | 1955 initial_tab_positions_, |
1966 move_behavior_ == MOVE_VISIBILE_TABS, | 1956 move_behavior_ == MOVE_VISIBILE_TABS, |
1967 true); | 1957 true); |
1968 } else { | 1958 } else { |
1969 if (dock_info_.type() != DockInfo::NONE) { | 1959 if (dock_info_.type() != DockInfo::NONE) { |
1970 switch (dock_info_.type()) { | 1960 switch (dock_info_.type()) { |
1971 case DockInfo::LEFT_OF_WINDOW: | 1961 case DockInfo::LEFT_OF_WINDOW: |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2241 bool TabDragController::AreTabsConsecutive() { | 2231 bool TabDragController::AreTabsConsecutive() { |
2242 for (size_t i = 1; i < drag_data_.size(); ++i) { | 2232 for (size_t i = 1; i < drag_data_.size(); ++i) { |
2243 if (drag_data_[i - 1].source_model_index + 1 != | 2233 if (drag_data_[i - 1].source_model_index + 1 != |
2244 drag_data_[i].source_model_index) { | 2234 drag_data_[i].source_model_index) { |
2245 return false; | 2235 return false; |
2246 } | 2236 } |
2247 } | 2237 } |
2248 return true; | 2238 return true; |
2249 } | 2239 } |
2250 | 2240 |
2251 Browser* TabDragController::CreateBrowserForDrag( | 2241 gfx::Rect TabDragController::CalculateDraggedBrowserBounds( |
2252 TabStrip* source, | 2242 TabStrip* source, |
2253 const gfx::Point& point_in_screen, | 2243 const gfx::Point& point_in_screen, |
2254 gfx::Vector2d* drag_offset, | |
2255 std::vector<gfx::Rect>* drag_bounds) { | 2244 std::vector<gfx::Rect>* drag_bounds) { |
2256 gfx::Point center(0, source->height() / 2); | 2245 gfx::Point center(0, source->height() / 2); |
2257 views::View::ConvertPointToWidget(source, ¢er); | 2246 views::View::ConvertPointToWidget(source, ¢er); |
2258 gfx::Rect new_bounds(source->GetWidget()->GetRestoredBounds()); | 2247 gfx::Rect new_bounds(source->GetWidget()->GetRestoredBounds()); |
2259 if (source->GetWidget()->IsMaximized()) { | 2248 if (source->GetWidget()->IsMaximized()) { |
2260 // If the restore bounds is really small, we don't want to honor it | 2249 // If the restore bounds is really small, we don't want to honor it |
2261 // (dragging a really small window looks wrong), instead make sure the new | 2250 // (dragging a really small window looks wrong), instead make sure the new |
2262 // window is at least 50% the size of the old. | 2251 // window is at least 50% the size of the old. |
2263 const gfx::Size max_size( | 2252 const gfx::Size max_size( |
2264 source->GetWidget()->GetWindowBoundsInScreen().size()); | 2253 source->GetWidget()->GetWindowBoundsInScreen().size()); |
2265 new_bounds.set_width( | 2254 new_bounds.set_width( |
2266 std::max(max_size.width() / 2, new_bounds.width())); | 2255 std::max(max_size.width() / 2, new_bounds.width())); |
2267 new_bounds.set_height( | 2256 new_bounds.set_height( |
2268 std::max(max_size.height() / 2, new_bounds.width())); | 2257 std::max(max_size.height() / 2, new_bounds.height())); |
2269 } | 2258 } |
2270 new_bounds.set_y(point_in_screen.y() - center.y()); | 2259 new_bounds.set_y(point_in_screen.y() - center.y()); |
2271 switch (GetDetachPosition(point_in_screen)) { | 2260 switch (GetDetachPosition(point_in_screen)) { |
2272 case DETACH_BEFORE: | 2261 case DETACH_BEFORE: |
2273 new_bounds.set_x(point_in_screen.x() - center.x()); | 2262 new_bounds.set_x(point_in_screen.x() - center.x()); |
2274 new_bounds.Offset(-mouse_offset_.x(), 0); | 2263 new_bounds.Offset(-mouse_offset_.x(), 0); |
2275 break; | 2264 break; |
2276 case DETACH_AFTER: { | 2265 case DETACH_AFTER: { |
2277 gfx::Point right_edge(source->width(), 0); | 2266 gfx::Point right_edge(source->width(), 0); |
2278 views::View::ConvertPointToWidget(source, &right_edge); | 2267 views::View::ConvertPointToWidget(source, &right_edge); |
2279 new_bounds.set_x(point_in_screen.x() - right_edge.x()); | 2268 new_bounds.set_x(point_in_screen.x() - right_edge.x()); |
2280 new_bounds.Offset(drag_bounds->back().right() - mouse_offset_.x(), 0); | 2269 new_bounds.Offset(drag_bounds->back().right() - mouse_offset_.x(), 0); |
2281 OffsetX(-(*drag_bounds)[0].x(), drag_bounds); | 2270 OffsetX(-(*drag_bounds)[0].x(), drag_bounds); |
2282 break; | 2271 break; |
2283 } | 2272 } |
2284 default: | 2273 default: |
2285 break; // Nothing to do for DETACH_ABOVE_OR_BELOW. | 2274 break; // Nothing to do for DETACH_ABOVE_OR_BELOW. |
2286 } | 2275 } |
2287 | 2276 |
2288 // To account for the extra vertical on restored windows that is absent on | 2277 // To account for the extra vertical on restored windows that is absent on |
2289 // maximized windows, add an additional vertical offset extracted from the tab | 2278 // maximized windows, add an additional vertical offset extracted from the tab |
2290 // strip. | 2279 // strip. |
2291 if (source->GetWidget()->IsMaximized()) | 2280 if (source->GetWidget()->IsMaximized()) |
2292 new_bounds.Offset(0, -source->button_v_offset()); | 2281 new_bounds.Offset(0, -source->button_v_offset()); |
| 2282 return new_bounds; |
| 2283 } |
2293 | 2284 |
| 2285 void TabDragController::AdjustBrowserAndTabBoundsForDrag( |
| 2286 int last_tabstrip_width, |
| 2287 const gfx::Point& point_in_screen, |
| 2288 std::vector<gfx::Rect>* drag_bounds) { |
| 2289 attached_tabstrip_->InvalidateLayout(); |
| 2290 attached_tabstrip_->DoLayout(); |
| 2291 const int dragged_tabstrip_width = attached_tabstrip_->tab_area_width(); |
| 2292 |
| 2293 // If the new tabstrip is smaller than the old resize the tabs. |
| 2294 if (dragged_tabstrip_width < last_tabstrip_width) { |
| 2295 const float leading_ratio = |
| 2296 drag_bounds->front().x() / static_cast<float>(last_tabstrip_width); |
| 2297 *drag_bounds = CalculateBoundsForDraggedTabs(); |
| 2298 |
| 2299 if (drag_bounds->back().right() < dragged_tabstrip_width) { |
| 2300 const int delta_x = |
| 2301 std::min(static_cast<int>(leading_ratio * dragged_tabstrip_width), |
| 2302 dragged_tabstrip_width - |
| 2303 (drag_bounds->back().right() - |
| 2304 drag_bounds->front().x())); |
| 2305 OffsetX(delta_x, drag_bounds); |
| 2306 } |
| 2307 |
| 2308 // Reposition the restored window such that the tab that was dragged remains |
| 2309 // under the mouse cursor. |
| 2310 gfx::Point offset( |
| 2311 static_cast<int>((*drag_bounds)[source_tab_index_].width() * |
| 2312 offset_to_width_ratio_) + |
| 2313 (*drag_bounds)[source_tab_index_].x(), 0); |
| 2314 views::View::ConvertPointToWidget(attached_tabstrip_, &offset); |
| 2315 gfx::Rect bounds = GetAttachedBrowserWidget()->GetWindowBoundsInScreen(); |
| 2316 bounds.set_x(point_in_screen.x() - offset.x()); |
| 2317 GetAttachedBrowserWidget()->SetBounds(bounds); |
| 2318 } |
| 2319 attached_tabstrip_->SetTabBoundsForDrag(*drag_bounds); |
| 2320 } |
| 2321 |
| 2322 Browser* TabDragController::CreateBrowserForDrag( |
| 2323 TabStrip* source, |
| 2324 const gfx::Point& point_in_screen, |
| 2325 gfx::Vector2d* drag_offset, |
| 2326 std::vector<gfx::Rect>* drag_bounds) { |
| 2327 gfx::Rect new_bounds(CalculateDraggedBrowserBounds(source, |
| 2328 point_in_screen, |
| 2329 drag_bounds)); |
2294 *drag_offset = point_in_screen - new_bounds.origin(); | 2330 *drag_offset = point_in_screen - new_bounds.origin(); |
2295 | 2331 |
2296 Profile* profile = | 2332 Profile* profile = |
2297 Profile::FromBrowserContext(drag_data_[0].contents->GetBrowserContext()); | 2333 Profile::FromBrowserContext(drag_data_[0].contents->GetBrowserContext()); |
2298 Browser::CreateParams create_params(Browser::TYPE_TABBED, | 2334 Browser::CreateParams create_params(Browser::TYPE_TABBED, |
2299 profile, | 2335 profile, |
2300 host_desktop_type_); | 2336 host_desktop_type_); |
2301 create_params.initial_bounds = new_bounds; | 2337 create_params.initial_bounds = new_bounds; |
2302 Browser* browser = new Browser(create_params); | 2338 Browser* browser = new Browser(create_params); |
2303 is_dragging_new_browser_ = true; | 2339 is_dragging_new_browser_ = true; |
(...skipping 29 matching lines...) Expand all Loading... |
2333 gfx::Vector2d TabDragController::GetWindowOffset( | 2369 gfx::Vector2d TabDragController::GetWindowOffset( |
2334 const gfx::Point& point_in_screen) { | 2370 const gfx::Point& point_in_screen) { |
2335 TabStrip* owning_tabstrip = (attached_tabstrip_ && detach_into_browser_) ? | 2371 TabStrip* owning_tabstrip = (attached_tabstrip_ && detach_into_browser_) ? |
2336 attached_tabstrip_ : source_tabstrip_; | 2372 attached_tabstrip_ : source_tabstrip_; |
2337 views::View* toplevel_view = owning_tabstrip->GetWidget()->GetContentsView(); | 2373 views::View* toplevel_view = owning_tabstrip->GetWidget()->GetContentsView(); |
2338 | 2374 |
2339 gfx::Point point = point_in_screen; | 2375 gfx::Point point = point_in_screen; |
2340 views::View::ConvertPointFromScreen(toplevel_view, &point); | 2376 views::View::ConvertPointFromScreen(toplevel_view, &point); |
2341 return point.OffsetFromOrigin(); | 2377 return point.OffsetFromOrigin(); |
2342 } | 2378 } |
OLD | NEW |