| OLD | NEW |
| (Empty) |
| 1 // Copyright 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/common/wm/dock/docked_window_resizer.h" | |
| 6 | |
| 7 #include "ash/common/wm/dock/docked_window_layout_manager.h" | |
| 8 #include "ash/common/wm/window_parenting_utils.h" | |
| 9 #include "ash/common/wm/window_state.h" | |
| 10 #include "ash/common/wm/wm_event.h" | |
| 11 #include "ash/common/wm/workspace/magnetism_matcher.h" | |
| 12 #include "ash/common/wm_window.h" | |
| 13 #include "ash/public/cpp/shell_window_ids.h" | |
| 14 #include "ash/public/cpp/window_properties.h" | |
| 15 #include "ash/root_window_controller.h" | |
| 16 #include "ash/shell.h" | |
| 17 #include "ui/base/hit_test.h" | |
| 18 #include "ui/base/ui_base_types.h" | |
| 19 #include "ui/display/display.h" | |
| 20 #include "ui/display/screen.h" | |
| 21 | |
| 22 namespace ash { | |
| 23 namespace { | |
| 24 | |
| 25 DockedWindowLayoutManager* GetDockedLayoutManagerAtPoint( | |
| 26 const gfx::Point& point) { | |
| 27 display::Display display = | |
| 28 display::Screen::GetScreen()->GetDisplayNearestPoint(point); | |
| 29 if (!display.bounds().Contains(point)) | |
| 30 return nullptr; | |
| 31 | |
| 32 return DockedWindowLayoutManager::Get( | |
| 33 Shell::GetRootWindowControllerWithDisplayId(display.id())->GetWindow()); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 DockedWindowResizer::~DockedWindowResizer() {} | |
| 39 | |
| 40 // static | |
| 41 DockedWindowResizer* DockedWindowResizer::Create( | |
| 42 WindowResizer* next_window_resizer, | |
| 43 wm::WindowState* window_state) { | |
| 44 return new DockedWindowResizer(next_window_resizer, window_state); | |
| 45 } | |
| 46 | |
| 47 void DockedWindowResizer::Drag(const gfx::Point& location, int event_flags) { | |
| 48 last_location_ = GetTarget()->GetParent()->ConvertPointToScreen(location); | |
| 49 base::WeakPtr<DockedWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr()); | |
| 50 | |
| 51 if (!did_move_or_resize_) { | |
| 52 did_move_or_resize_ = true; | |
| 53 StartedDragging(resizer); | |
| 54 } | |
| 55 if (!resizer) | |
| 56 return; | |
| 57 | |
| 58 gfx::Point offset; | |
| 59 gfx::Rect bounds(CalculateBoundsForDrag(location)); | |
| 60 MaybeSnapToEdge(bounds, &offset); | |
| 61 gfx::Point modified_location(location); | |
| 62 modified_location += offset.OffsetFromOrigin(); | |
| 63 | |
| 64 next_window_resizer_->Drag(modified_location, event_flags); | |
| 65 if (!resizer) | |
| 66 return; | |
| 67 | |
| 68 DockedWindowLayoutManager* new_dock_layout = | |
| 69 GetDockedLayoutManagerAtPoint(last_location_); | |
| 70 if (new_dock_layout && new_dock_layout != dock_layout_) { | |
| 71 // The window is being dragged to a new display. If the previous | |
| 72 // container is the current parent of the window it will be informed of | |
| 73 // the end of drag when the window is reparented, otherwise let the | |
| 74 // previous container know the drag is complete. If we told the | |
| 75 // window's parent that the drag was complete it would begin | |
| 76 // positioning the window. | |
| 77 if (is_docked_ && dock_layout_->is_dragged_window_docked()) | |
| 78 dock_layout_->UndockDraggedWindow(); | |
| 79 if (dock_layout_ != initial_dock_layout_) | |
| 80 dock_layout_->FinishDragging( | |
| 81 DOCKED_ACTION_NONE, | |
| 82 details().source == aura::client::WINDOW_MOVE_SOURCE_MOUSE | |
| 83 ? DOCKED_ACTION_SOURCE_MOUSE | |
| 84 : DOCKED_ACTION_SOURCE_TOUCH); | |
| 85 is_docked_ = false; | |
| 86 dock_layout_ = new_dock_layout; | |
| 87 // The window's initial layout manager already knows that the drag is | |
| 88 // in progress for this window. | |
| 89 if (new_dock_layout != initial_dock_layout_) | |
| 90 new_dock_layout->StartDragging(GetTarget()); | |
| 91 } | |
| 92 // Window could get docked by the WorkspaceWindowResizer, update the state. | |
| 93 is_docked_ = dock_layout_->is_dragged_window_docked(); | |
| 94 // Whenever a window is dragged out of the dock it will be auto-sized | |
| 95 // in the dock if it gets docked again. | |
| 96 if (!is_docked_) | |
| 97 was_bounds_changed_by_user_ = false; | |
| 98 } | |
| 99 | |
| 100 void DockedWindowResizer::CompleteDrag() { | |
| 101 // The root window can change when dragging into a different screen. | |
| 102 next_window_resizer_->CompleteDrag(); | |
| 103 FinishedDragging(aura::client::MOVE_SUCCESSFUL); | |
| 104 } | |
| 105 | |
| 106 void DockedWindowResizer::RevertDrag() { | |
| 107 next_window_resizer_->RevertDrag(); | |
| 108 // Restore docked state to what it was before the drag if necessary. | |
| 109 if (is_docked_ != was_docked_) { | |
| 110 is_docked_ = was_docked_; | |
| 111 if (is_docked_) | |
| 112 dock_layout_->DockDraggedWindow(GetTarget()); | |
| 113 else | |
| 114 dock_layout_->UndockDraggedWindow(); | |
| 115 } | |
| 116 FinishedDragging(aura::client::MOVE_CANCELED); | |
| 117 } | |
| 118 | |
| 119 DockedWindowResizer::DockedWindowResizer(WindowResizer* next_window_resizer, | |
| 120 wm::WindowState* window_state) | |
| 121 : WindowResizer(window_state), | |
| 122 next_window_resizer_(next_window_resizer), | |
| 123 dock_layout_(NULL), | |
| 124 initial_dock_layout_(NULL), | |
| 125 did_move_or_resize_(false), | |
| 126 was_docked_(false), | |
| 127 is_docked_(false), | |
| 128 was_bounds_changed_by_user_(window_state->bounds_changed_by_user()), | |
| 129 weak_ptr_factory_(this) { | |
| 130 DCHECK(details().is_resizable); | |
| 131 dock_layout_ = DockedWindowLayoutManager::Get(GetTarget()->GetRootWindow()); | |
| 132 initial_dock_layout_ = dock_layout_; | |
| 133 was_docked_ = GetTarget()->GetParent() == dock_layout_->dock_container(); | |
| 134 is_docked_ = was_docked_; | |
| 135 } | |
| 136 | |
| 137 void DockedWindowResizer::MaybeSnapToEdge(const gfx::Rect& bounds, | |
| 138 gfx::Point* offset) { | |
| 139 // Windows only snap magnetically when they were previously docked. | |
| 140 if (!was_docked_) | |
| 141 return; | |
| 142 DockedAlignment dock_alignment = dock_layout_->CalculateAlignment(); | |
| 143 gfx::Rect dock_bounds = GetTarget()->GetParent()->ConvertRectFromScreen( | |
| 144 dock_layout_->dock_container()->GetBoundsInScreen()); | |
| 145 | |
| 146 // Short-range magnetism when retaining docked state. Same constant as in | |
| 147 // MagnetismMatcher is used for consistency. | |
| 148 const int kSnapToDockDistance = MagnetismMatcher::kMagneticDistance; | |
| 149 | |
| 150 if (dock_alignment == DOCKED_ALIGNMENT_LEFT || | |
| 151 dock_alignment == DOCKED_ALIGNMENT_NONE) { | |
| 152 const int distance = bounds.x() - dock_bounds.x(); | |
| 153 if (distance < kSnapToDockDistance && distance > 0) { | |
| 154 offset->set_x(-distance); | |
| 155 return; | |
| 156 } | |
| 157 } | |
| 158 if (dock_alignment == DOCKED_ALIGNMENT_RIGHT || | |
| 159 dock_alignment == DOCKED_ALIGNMENT_NONE) { | |
| 160 const int distance = dock_bounds.right() - bounds.right(); | |
| 161 if (distance < kSnapToDockDistance && distance > 0) | |
| 162 offset->set_x(distance); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 void DockedWindowResizer::StartedDragging( | |
| 167 base::WeakPtr<DockedWindowResizer>& resizer) { | |
| 168 // During resizing the window width is preserved by DockedwindowLayoutManager. | |
| 169 if (is_docked_ && | |
| 170 (details().bounds_change & WindowResizer::kBoundsChange_Resizes)) { | |
| 171 window_state_->set_bounds_changed_by_user(true); | |
| 172 } | |
| 173 | |
| 174 // Tell the dock layout manager that we are dragging this window. | |
| 175 // At this point we are not yet animating the window as it may not be | |
| 176 // inside the docked area. | |
| 177 dock_layout_->StartDragging(GetTarget()); | |
| 178 if (!resizer) | |
| 179 return; | |
| 180 // Reparent workspace windows during the drag to elevate them above workspace. | |
| 181 // Other windows for which the DockedWindowResizer is instantiated include | |
| 182 // panels and windows that are already docked. Those do not need reparenting. | |
| 183 if (GetTarget()->GetType() != ui::wm::WINDOW_TYPE_PANEL && | |
| 184 GetTarget()->GetParent()->GetShellWindowId() == | |
| 185 kShellWindowId_DefaultContainer) { | |
| 186 // Reparent the window into the docked windows container in order to get it | |
| 187 // on top of other docked windows. | |
| 188 WmWindow* docked_container = | |
| 189 GetTarget()->GetRootWindow()->GetChildByShellWindowId( | |
| 190 kShellWindowId_DockedContainer); | |
| 191 wm::ReparentChildWithTransientChildren(GetTarget()->aura_window(), | |
| 192 GetTarget()->aura_window()->parent(), | |
| 193 docked_container->aura_window()); | |
| 194 if (!resizer) | |
| 195 return; | |
| 196 } | |
| 197 if (is_docked_) | |
| 198 dock_layout_->DockDraggedWindow(GetTarget()); | |
| 199 } | |
| 200 | |
| 201 void DockedWindowResizer::FinishedDragging( | |
| 202 aura::client::WindowMoveResult move_result) { | |
| 203 if (!did_move_or_resize_) | |
| 204 return; | |
| 205 did_move_or_resize_ = false; | |
| 206 WmWindow* window = GetTarget(); | |
| 207 const bool is_attached_panel = | |
| 208 window->GetType() == ui::wm::WINDOW_TYPE_PANEL && | |
| 209 window->aura_window()->GetProperty(kPanelAttachedKey); | |
| 210 const bool is_resized = | |
| 211 (details().bounds_change & WindowResizer::kBoundsChange_Resizes) != 0; | |
| 212 | |
| 213 // Undock the window if it is not in the normal, docked or minimized state | |
| 214 // type. This happens if a user snaps or maximizes a window using a | |
| 215 // keyboard shortcut while it is being dragged. | |
| 216 if (!window_state_->IsMinimized() && !window_state_->IsDocked() && | |
| 217 !window_state_->IsNormalStateType()) | |
| 218 is_docked_ = false; | |
| 219 | |
| 220 // When drag is completed the dragged docked window is resized to the bounds | |
| 221 // calculated by the layout manager that conform to other docked windows. | |
| 222 if (!is_attached_panel && is_docked_ && !is_resized) { | |
| 223 gfx::Rect bounds = window->GetParent()->ConvertRectFromScreen( | |
| 224 dock_layout_->dragged_bounds()); | |
| 225 if (!bounds.IsEmpty() && bounds.width() != window->GetBounds().width()) { | |
| 226 window->SetBounds(bounds); | |
| 227 } | |
| 228 } | |
| 229 // If a window has restore bounds, update the restore origin but not the size. | |
| 230 // The size gets restored when a window is undocked. | |
| 231 if (is_resized && is_docked_ && window_state_->HasRestoreBounds()) { | |
| 232 gfx::Rect restore_bounds = window->GetBoundsInScreen(); | |
| 233 restore_bounds.set_size(window_state_->GetRestoreBoundsInScreen().size()); | |
| 234 window_state_->SetRestoreBoundsInScreen(restore_bounds); | |
| 235 } | |
| 236 | |
| 237 // Check if the window needs to be docked or returned to workspace. | |
| 238 DockedAction action = | |
| 239 MaybeReparentWindowOnDragCompletion(is_resized, is_attached_panel); | |
| 240 dock_layout_->FinishDragging( | |
| 241 move_result == aura::client::MOVE_CANCELED ? DOCKED_ACTION_NONE : action, | |
| 242 details().source == aura::client::WINDOW_MOVE_SOURCE_MOUSE | |
| 243 ? DOCKED_ACTION_SOURCE_MOUSE | |
| 244 : DOCKED_ACTION_SOURCE_TOUCH); | |
| 245 | |
| 246 // If we started the drag in one root window and moved into another root | |
| 247 // but then canceled the drag we may need to inform the original layout | |
| 248 // manager that the drag is finished. | |
| 249 if (initial_dock_layout_ != dock_layout_) | |
| 250 initial_dock_layout_->FinishDragging( | |
| 251 DOCKED_ACTION_NONE, | |
| 252 details().source == aura::client::WINDOW_MOVE_SOURCE_MOUSE | |
| 253 ? DOCKED_ACTION_SOURCE_MOUSE | |
| 254 : DOCKED_ACTION_SOURCE_TOUCH); | |
| 255 is_docked_ = false; | |
| 256 } | |
| 257 | |
| 258 DockedAction DockedWindowResizer::MaybeReparentWindowOnDragCompletion( | |
| 259 bool is_resized, | |
| 260 bool is_attached_panel) { | |
| 261 WmWindow* window = GetTarget(); | |
| 262 | |
| 263 // Check if the window needs to be docked or returned to workspace. | |
| 264 DockedAction action = DOCKED_ACTION_NONE; | |
| 265 WmWindow* dock_container = window->GetRootWindow()->GetChildByShellWindowId( | |
| 266 kShellWindowId_DockedContainer); | |
| 267 if ((is_resized || !is_attached_panel) && | |
| 268 is_docked_ != (window->GetParent() == dock_container)) { | |
| 269 if (is_docked_) { | |
| 270 wm::ReparentChildWithTransientChildren(window->aura_window(), | |
| 271 window->aura_window()->parent(), | |
| 272 dock_container->aura_window()); | |
| 273 action = DOCKED_ACTION_DOCK; | |
| 274 } else if (window->GetParent()->GetShellWindowId() == | |
| 275 kShellWindowId_DockedContainer) { | |
| 276 // Reparent the window back to workspace. | |
| 277 // We need to be careful to give ParentWindowWithContext a location in | |
| 278 // the right root window (matching the logic in DragWindowResizer) based | |
| 279 // on which root window a mouse pointer is in. We want to undock into the | |
| 280 // right screen near the edge of a multiscreen setup (based on where the | |
| 281 // mouse is). | |
| 282 gfx::Rect near_last_location(last_location_, gfx::Size()); | |
| 283 // Reparenting will cause Relayout and possible dock shrinking. | |
| 284 aura::Window* previous_parent = window->aura_window()->parent(); | |
| 285 window->SetParentUsingContext(window, near_last_location); | |
| 286 if (window->aura_window()->parent() != previous_parent) { | |
| 287 wm::ReparentTransientChildrenOfChild(window->aura_window(), | |
| 288 previous_parent, | |
| 289 window->aura_window()->parent()); | |
| 290 } | |
| 291 action = was_docked_ ? DOCKED_ACTION_UNDOCK : DOCKED_ACTION_NONE; | |
| 292 } | |
| 293 } else { | |
| 294 // |action| is recorded in UMA and used to maintain |window_state_|. | |
| 295 if (is_resized && is_docked_ && was_docked_) | |
| 296 action = DOCKED_ACTION_RESIZE; | |
| 297 else if (is_docked_ && was_docked_) | |
| 298 action = DOCKED_ACTION_REORDER; | |
| 299 else if (is_docked_ && !was_docked_) | |
| 300 action = DOCKED_ACTION_DOCK; | |
| 301 else if (!is_docked_ && was_docked_) | |
| 302 action = DOCKED_ACTION_UNDOCK; | |
| 303 else | |
| 304 action = DOCKED_ACTION_NONE; | |
| 305 } | |
| 306 // When a window is newly docked it is auto-sized by docked layout adjusting | |
| 307 // to other windows. If it is just dragged (but not resized) while being | |
| 308 // docked it is auto-sized unless it has been resized while being docked | |
| 309 // before. | |
| 310 if (is_docked_) { | |
| 311 window->GetWindowState()->set_bounds_changed_by_user( | |
| 312 was_docked_ && (is_resized || was_bounds_changed_by_user_)); | |
| 313 } | |
| 314 | |
| 315 if (action == DOCKED_ACTION_DOCK) { | |
| 316 const wm::WMEvent event(wm::WM_EVENT_DOCK); | |
| 317 window_state_->OnWMEvent(&event); | |
| 318 } else if (window->GetWindowState()->IsDocked() && | |
| 319 action == DOCKED_ACTION_UNDOCK) { | |
| 320 const wm::WMEvent event(wm::WM_EVENT_NORMAL); | |
| 321 window_state_->OnWMEvent(&event); | |
| 322 } | |
| 323 | |
| 324 return action; | |
| 325 } | |
| 326 | |
| 327 } // namespace ash | |
| OLD | NEW |