| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/wm/dock/docked_window_resizer.h" |
| 6 |
| 7 #include "ash/ash_switches.h" |
| 8 #include "ash/display/display_controller.h" |
| 9 #include "ash/launcher/launcher.h" |
| 10 #include "ash/root_window_controller.h" |
| 11 #include "ash/screen_ash.h" |
| 12 #include "ash/shelf/shelf_types.h" |
| 13 #include "ash/shelf/shelf_widget.h" |
| 14 #include "ash/shell.h" |
| 15 #include "ash/shell_window_ids.h" |
| 16 #include "ash/wm/coordinate_conversion.h" |
| 17 #include "ash/wm/dock/docked_window_layout_manager.h" |
| 18 #include "ash/wm/property_util.h" |
| 19 #include "ash/wm/window_properties.h" |
| 20 #include "ash/wm/workspace_controller.h" |
| 21 #include "base/command_line.h" |
| 22 #include "ui/aura/client/aura_constants.h" |
| 23 #include "ui/aura/env.h" |
| 24 #include "ui/aura/root_window.h" |
| 25 #include "ui/aura/window.h" |
| 26 #include "ui/aura/window_delegate.h" |
| 27 #include "ui/base/hit_test.h" |
| 28 #include "ui/base/ui_base_types.h" |
| 29 #include "ui/gfx/screen.h" |
| 30 #include "ui/views/widget/widget.h" |
| 31 |
| 32 namespace ash { |
| 33 |
| 34 namespace { |
| 35 |
| 36 // How far we have to drag to undock a window. |
| 37 const int kSnapToDockDistance = 32; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 DockedWindowResizer::~DockedWindowResizer() { |
| 42 } |
| 43 |
| 44 // static |
| 45 DockedWindowResizer* |
| 46 DockedWindowResizer::Create(WindowResizer* next_window_resizer, |
| 47 aura::Window* window, |
| 48 const gfx::Point& location, |
| 49 int window_component, |
| 50 aura::client::WindowMoveSource source) { |
| 51 Details details(window, location, window_component, source); |
| 52 return details.is_resizable ? |
| 53 new DockedWindowResizer(next_window_resizer, details) : NULL; |
| 54 } |
| 55 |
| 56 void DockedWindowResizer::Drag(const gfx::Point& location, int event_flags) { |
| 57 last_location_ = location; |
| 58 wm::ConvertPointToScreen(GetTarget()->parent(), &last_location_); |
| 59 if (!did_move_or_resize_) { |
| 60 did_move_or_resize_ = true; |
| 61 StartedDragging(); |
| 62 } |
| 63 gfx::Point offset; |
| 64 gfx::Rect bounds(CalculateBoundsForDrag(details_, location)); |
| 65 MaybeSnapToSide(bounds, &offset); |
| 66 gfx::Point modified_location(location.x() + offset.x(), |
| 67 location.y() + offset.y()); |
| 68 next_window_resizer_->Drag(modified_location, event_flags); |
| 69 } |
| 70 |
| 71 void DockedWindowResizer::CompleteDrag(int event_flags) { |
| 72 // The root window can change when dragging into a different screen. |
| 73 next_window_resizer_->CompleteDrag(event_flags); |
| 74 FinishDragging(); |
| 75 } |
| 76 |
| 77 void DockedWindowResizer::RevertDrag() { |
| 78 next_window_resizer_->RevertDrag(); |
| 79 FinishDragging(); |
| 80 } |
| 81 |
| 82 aura::Window* DockedWindowResizer::GetTarget() { |
| 83 return next_window_resizer_->GetTarget(); |
| 84 } |
| 85 |
| 86 const gfx::Point& DockedWindowResizer::GetInitialLocation() const { |
| 87 return details_.initial_location_in_parent; |
| 88 } |
| 89 |
| 90 DockedWindowResizer::DockedWindowResizer(WindowResizer* next_window_resizer, |
| 91 const Details& details) |
| 92 : details_(details), |
| 93 next_window_resizer_(next_window_resizer), |
| 94 dock_layout_(NULL), |
| 95 did_move_or_resize_(false) { |
| 96 DCHECK(details_.is_resizable); |
| 97 aura::Window* dock_container = Shell::GetContainer( |
| 98 details.window->GetRootWindow(), |
| 99 internal::kShellWindowId_DockedContainer); |
| 100 DCHECK(dock_container->id() == internal::kShellWindowId_DockedContainer); |
| 101 dock_layout_ = static_cast<internal::DockedWindowLayoutManager*>( |
| 102 dock_container->layout_manager()); |
| 103 } |
| 104 |
| 105 void DockedWindowResizer::MaybeSnapToSide(const gfx::Rect& bounds, |
| 106 gfx::Point* offset) { |
| 107 aura::Window* dock_container = Shell::GetContainer( |
| 108 wm::GetRootWindowAt(last_location_), |
| 109 internal::kShellWindowId_DockedContainer); |
| 110 DCHECK(dock_container->id() == internal::kShellWindowId_DockedContainer); |
| 111 internal::DockedWindowLayoutManager* dock_layout = |
| 112 static_cast<internal::DockedWindowLayoutManager*>( |
| 113 dock_container->layout_manager()); |
| 114 |
| 115 internal::DockedAlignment dock_alignment = dock_layout->alignment(); |
| 116 if (dock_alignment == internal::DOCKED_ALIGNMENT_NONE) { |
| 117 if (GetTarget() != dock_layout_->dragged_window()) |
| 118 return; |
| 119 dock_alignment = internal::DOCKED_ALIGNMENT_ANY; |
| 120 } |
| 121 |
| 122 gfx::Rect dock_bounds = ScreenAsh::ConvertRectFromScreen( |
| 123 GetTarget()->parent(), dock_container->GetBoundsInScreen()); |
| 124 |
| 125 switch (dock_alignment) { |
| 126 case internal::DOCKED_ALIGNMENT_LEFT: |
| 127 if (abs(bounds.x() - dock_bounds.x()) < kSnapToDockDistance) |
| 128 offset->set_x(dock_bounds.x() - bounds.x()); |
| 129 break; |
| 130 case internal::DOCKED_ALIGNMENT_RIGHT: |
| 131 if (abs(bounds.right() - dock_bounds.right()) < kSnapToDockDistance) |
| 132 offset->set_x(dock_bounds.right() - bounds.right()); |
| 133 break; |
| 134 case internal::DOCKED_ALIGNMENT_ANY: |
| 135 if (abs(bounds.x() - dock_bounds.x()) < kSnapToDockDistance) |
| 136 offset->set_x(dock_bounds.x() - bounds.x()); |
| 137 if (abs(bounds.right() - dock_bounds.right()) < kSnapToDockDistance) |
| 138 offset->set_x(dock_bounds.right() - bounds.right()); |
| 139 break; |
| 140 case internal::DOCKED_ALIGNMENT_NONE: |
| 141 NOTREACHED() << "Nothing to do when no windows are docked"; |
| 142 break; |
| 143 } |
| 144 } |
| 145 |
| 146 void DockedWindowResizer::StartedDragging() { |
| 147 // Tell the dock layout manager that we are dragging this window. |
| 148 dock_layout_->StartDragging(GetTarget()); |
| 149 } |
| 150 |
| 151 void DockedWindowResizer::FinishDragging() { |
| 152 if (!did_move_or_resize_) |
| 153 return; |
| 154 |
| 155 aura::Window* window = GetTarget(); |
| 156 bool should_dock = false; |
| 157 if ((details_.bounds_change & WindowResizer::kBoundsChange_Repositions) && |
| 158 CommandLine::ForCurrentProcess()->HasSwitch( |
| 159 switches::kAshEnableDockedWindows)) { |
| 160 should_dock = internal::DockedWindowLayoutManager::ShouldWindowDock( |
| 161 window, last_location_); |
| 162 } |
| 163 |
| 164 // Check if desired docked state is not same as current. |
| 165 // If not same dock or undock accordingly. |
| 166 if (should_dock != |
| 167 (window->parent()->id() == internal::kShellWindowId_DockedContainer)) { |
| 168 if (should_dock) { |
| 169 aura::Window* dock_container = Shell::GetContainer( |
| 170 window->GetRootWindow(), |
| 171 internal::kShellWindowId_DockedContainer); |
| 172 dock_container->AddChild(window); |
| 173 } else { |
| 174 // Reparent the window back to workspace. |
| 175 // We need to be careful to give SetDefaultParentByRootWindow location in |
| 176 // the right root window (matching the logic in DragWindowResizer) based |
| 177 // on which root window a mouse pointer is in. We want to undock into the |
| 178 // right screen near the edge of a multiscreen setup (based on where the |
| 179 // mouse is). |
| 180 gfx::Rect near_last_location(last_location_, gfx::Size()); |
| 181 // Reparenting will cause Relayout and possible dock shrinking. |
| 182 window->SetDefaultParentByRootWindow(window->GetRootWindow(), |
| 183 near_last_location); |
| 184 // A maximized workspace may be active so we may need to switch |
| 185 // to a parent workspace of the window being dragged out. |
| 186 internal::WorkspaceController* workspace_controller = |
| 187 GetRootWindowController( |
| 188 window->GetRootWindow())->workspace_controller(); |
| 189 workspace_controller->SetActiveWorkspaceByWindow(window); |
| 190 } |
| 191 } |
| 192 dock_layout_->FinishDragging(); |
| 193 } |
| 194 |
| 195 } // namespace aura |
| OLD | NEW |