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