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.
5 #include "ash/wm/dock/docked_window_resizer.h"
7 #include "ash/display/window_tree_host_manager.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/screen_util.h"
10 #include "ash/shelf/shelf.h"
11 #include "ash/shelf/shelf_types.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/shell_window_ids.h"
15 #include "ash/wm/dock/docked_window_layout_manager.h"
16 #include "ash/wm/window_state.h"
17 #include "ash/wm/window_util.h"
18 #include "ash/wm/wm_event.h"
19 #include "ash/wm/workspace/magnetism_matcher.h"
20 #include "ash/wm/workspace/workspace_window_resizer.h"
21 #include "base/command_line.h"
22 #include "base/memory/weak_ptr.h"
23 #include "ui/aura/client/aura_constants.h"
24 #include "ui/aura/client/window_tree_client.h"
25 #include "ui/aura/env.h"
26 #include "ui/aura/window.h"
27 #include "ui/aura/window_delegate.h"
28 #include "ui/aura/window_event_dispatcher.h"
29 #include "ui/base/hit_test.h"
30 #include "ui/base/ui_base_types.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/views/widget/widget.h"
33 #include "ui/wm/core/coordinate_conversion.h"
38 DockedWindowLayoutManager
* GetDockedLayoutManagerAtPoint(
39 const gfx::Point
& point
) {
40 gfx::Display display
= ScreenUtil::FindDisplayContainingPoint(point
);
41 if (!display
.is_valid())
43 aura::Window
* root
= Shell::GetInstance()
44 ->window_tree_host_manager()
45 ->GetRootWindowForDisplayId(display
.id());
46 aura::Window
* dock_container
= Shell::GetContainer(
47 root
, kShellWindowId_DockedContainer
);
48 return static_cast<DockedWindowLayoutManager
*>(
49 dock_container
->layout_manager());
54 DockedWindowResizer::~DockedWindowResizer() {
59 DockedWindowResizer::Create(WindowResizer
* next_window_resizer
,
60 wm::WindowState
* window_state
) {
61 return new DockedWindowResizer(next_window_resizer
, window_state
);
64 void DockedWindowResizer::Drag(const gfx::Point
& location
, int event_flags
) {
65 last_location_
= location
;
66 ::wm::ConvertPointToScreen(GetTarget()->parent(), &last_location_
);
67 if (!did_move_or_resize_
) {
68 did_move_or_resize_
= true;
72 gfx::Rect
bounds(CalculateBoundsForDrag(location
));
73 MaybeSnapToEdge(bounds
, &offset
);
74 gfx::Point
modified_location(location
);
75 modified_location
.Offset(offset
.x(), offset
.y());
77 base::WeakPtr
<DockedWindowResizer
> resizer(weak_ptr_factory_
.GetWeakPtr());
78 next_window_resizer_
->Drag(modified_location
, event_flags
);
82 DockedWindowLayoutManager
* new_dock_layout
=
83 GetDockedLayoutManagerAtPoint(last_location_
);
84 if (new_dock_layout
&& new_dock_layout
!= dock_layout_
) {
85 // The window is being dragged to a new display. If the previous
86 // container is the current parent of the window it will be informed of
87 // the end of drag when the window is reparented, otherwise let the
88 // previous container know the drag is complete. If we told the
89 // window's parent that the drag was complete it would begin
90 // positioning the window.
91 if (is_docked_
&& dock_layout_
->is_dragged_window_docked())
92 dock_layout_
->UndockDraggedWindow();
93 if (dock_layout_
!= initial_dock_layout_
)
94 dock_layout_
->FinishDragging(
96 details().source
== aura::client::WINDOW_MOVE_SOURCE_MOUSE
?
97 DOCKED_ACTION_SOURCE_MOUSE
: DOCKED_ACTION_SOURCE_TOUCH
);
99 dock_layout_
= new_dock_layout
;
100 // The window's initial layout manager already knows that the drag is
101 // in progress for this window.
102 if (new_dock_layout
!= initial_dock_layout_
)
103 new_dock_layout
->StartDragging(GetTarget());
105 // Window could get docked by the WorkspaceWindowResizer, update the state.
106 is_docked_
= dock_layout_
->is_dragged_window_docked();
107 // Whenever a window is dragged out of the dock it will be auto-sized
108 // in the dock if it gets docked again.
110 was_bounds_changed_by_user_
= false;
113 void DockedWindowResizer::CompleteDrag() {
114 // The root window can change when dragging into a different screen.
115 next_window_resizer_
->CompleteDrag();
116 FinishedDragging(aura::client::MOVE_SUCCESSFUL
);
119 void DockedWindowResizer::RevertDrag() {
120 next_window_resizer_
->RevertDrag();
121 // Restore docked state to what it was before the drag if necessary.
122 if (is_docked_
!= was_docked_
) {
123 is_docked_
= was_docked_
;
125 dock_layout_
->DockDraggedWindow(GetTarget());
127 dock_layout_
->UndockDraggedWindow();
129 FinishedDragging(aura::client::MOVE_CANCELED
);
132 DockedWindowResizer::DockedWindowResizer(WindowResizer
* next_window_resizer
,
133 wm::WindowState
* window_state
)
134 : WindowResizer(window_state
),
135 next_window_resizer_(next_window_resizer
),
137 initial_dock_layout_(NULL
),
138 did_move_or_resize_(false),
141 was_bounds_changed_by_user_(window_state
->bounds_changed_by_user()),
142 weak_ptr_factory_(this) {
143 DCHECK(details().is_resizable
);
144 aura::Window
* dock_container
= Shell::GetContainer(
145 GetTarget()->GetRootWindow(),
146 kShellWindowId_DockedContainer
);
147 dock_layout_
= static_cast<DockedWindowLayoutManager
*>(
148 dock_container
->layout_manager());
149 initial_dock_layout_
= dock_layout_
;
150 was_docked_
= GetTarget()->parent() == dock_container
;
151 is_docked_
= was_docked_
;
154 void DockedWindowResizer::MaybeSnapToEdge(const gfx::Rect
& bounds
,
155 gfx::Point
* offset
) {
156 // Windows only snap magnetically when they were previously docked.
159 DockedAlignment dock_alignment
= dock_layout_
->CalculateAlignment();
160 gfx::Rect dock_bounds
= ScreenUtil::ConvertRectFromScreen(
161 GetTarget()->parent(),
162 dock_layout_
->dock_container()->GetBoundsInScreen());
164 // Short-range magnetism when retaining docked state. Same constant as in
165 // MagnetismMatcher is used for consistency.
166 const int kSnapToDockDistance
= MagnetismMatcher::kMagneticDistance
;
168 if (dock_alignment
== DOCKED_ALIGNMENT_LEFT
||
169 dock_alignment
== DOCKED_ALIGNMENT_NONE
) {
170 const int distance
= bounds
.x() - dock_bounds
.x();
171 if (distance
< kSnapToDockDistance
&& distance
> 0) {
172 offset
->set_x(-distance
);
176 if (dock_alignment
== DOCKED_ALIGNMENT_RIGHT
||
177 dock_alignment
== DOCKED_ALIGNMENT_NONE
) {
178 const int distance
= dock_bounds
.right() - bounds
.right();
179 if (distance
< kSnapToDockDistance
&& distance
> 0)
180 offset
->set_x(distance
);
184 void DockedWindowResizer::StartedDragging() {
185 // During resizing the window width is preserved by DockedwindowLayoutManager.
187 (details().bounds_change
& WindowResizer::kBoundsChange_Resizes
)) {
188 window_state_
->set_bounds_changed_by_user(true);
191 // Tell the dock layout manager that we are dragging this window.
192 // At this point we are not yet animating the window as it may not be
193 // inside the docked area.
194 dock_layout_
->StartDragging(GetTarget());
195 // Reparent workspace windows during the drag to elevate them above workspace.
196 // Other windows for which the DockedWindowResizer is instantiated include
197 // panels and windows that are already docked. Those do not need reparenting.
198 if (GetTarget()->type() != ui::wm::WINDOW_TYPE_PANEL
&&
199 GetTarget()->parent()->id() == kShellWindowId_DefaultContainer
) {
200 // Reparent the window into the docked windows container in order to get it
201 // on top of other docked windows.
202 aura::Window
* docked_container
= Shell::GetContainer(
203 GetTarget()->GetRootWindow(),
204 kShellWindowId_DockedContainer
);
205 wm::ReparentChildWithTransientChildren(GetTarget(),
206 GetTarget()->parent(),
210 dock_layout_
->DockDraggedWindow(GetTarget());
213 void DockedWindowResizer::FinishedDragging(
214 aura::client::WindowMoveResult move_result
) {
215 if (!did_move_or_resize_
)
217 did_move_or_resize_
= false;
218 aura::Window
* window
= GetTarget();
219 const bool is_attached_panel
= window
->type() == ui::wm::WINDOW_TYPE_PANEL
&&
220 window_state_
->panel_attached();
221 const bool is_resized
=
222 (details().bounds_change
& WindowResizer::kBoundsChange_Resizes
) != 0;
224 // Undock the window if it is not in the normal, docked or minimized state
225 // type. This happens if a user snaps or maximizes a window using a
226 // keyboard shortcut while it is being dragged.
227 if (!window_state_
->IsMinimized() && !window_state_
->IsDocked() &&
228 !window_state_
->IsNormalStateType())
231 // When drag is completed the dragged docked window is resized to the bounds
232 // calculated by the layout manager that conform to other docked windows.
233 if (!is_attached_panel
&& is_docked_
&& !is_resized
) {
234 gfx::Rect bounds
= ScreenUtil::ConvertRectFromScreen(
235 window
->parent(), dock_layout_
->dragged_bounds());
236 if (!bounds
.IsEmpty() && bounds
.width() != window
->bounds().width()) {
237 window
->SetBounds(bounds
);
240 // If a window has restore bounds, update the restore origin but not the size.
241 // The size gets restored when a window is undocked.
242 if (is_resized
&& is_docked_
&& window_state_
->HasRestoreBounds()) {
243 gfx::Rect restore_bounds
= window
->GetBoundsInScreen();
244 restore_bounds
.set_size(window_state_
->GetRestoreBoundsInScreen().size());
245 window_state_
->SetRestoreBoundsInScreen(restore_bounds
);
248 // Check if the window needs to be docked or returned to workspace.
249 DockedAction action
= MaybeReparentWindowOnDragCompletion(is_resized
,
251 dock_layout_
->FinishDragging(
252 move_result
== aura::client::MOVE_CANCELED
? DOCKED_ACTION_NONE
: action
,
253 details().source
== aura::client::WINDOW_MOVE_SOURCE_MOUSE
?
254 DOCKED_ACTION_SOURCE_MOUSE
: DOCKED_ACTION_SOURCE_TOUCH
);
256 // If we started the drag in one root window and moved into another root
257 // but then canceled the drag we may need to inform the original layout
258 // manager that the drag is finished.
259 if (initial_dock_layout_
!= dock_layout_
)
260 initial_dock_layout_
->FinishDragging(
262 details().source
== aura::client::WINDOW_MOVE_SOURCE_MOUSE
?
263 DOCKED_ACTION_SOURCE_MOUSE
: DOCKED_ACTION_SOURCE_TOUCH
);
267 DockedAction
DockedWindowResizer::MaybeReparentWindowOnDragCompletion(
268 bool is_resized
, bool is_attached_panel
) {
269 aura::Window
* window
= GetTarget();
271 // Check if the window needs to be docked or returned to workspace.
272 DockedAction action
= DOCKED_ACTION_NONE
;
273 aura::Window
* dock_container
= Shell::GetContainer(
274 window
->GetRootWindow(),
275 kShellWindowId_DockedContainer
);
276 if ((is_resized
|| !is_attached_panel
) &&
277 is_docked_
!= (window
->parent() == dock_container
)) {
279 wm::ReparentChildWithTransientChildren(window
,
282 action
= DOCKED_ACTION_DOCK
;
283 } else if (window
->parent()->id() == kShellWindowId_DockedContainer
) {
284 // Reparent the window back to workspace.
285 // We need to be careful to give ParentWindowWithContext a location in
286 // the right root window (matching the logic in DragWindowResizer) based
287 // on which root window a mouse pointer is in. We want to undock into the
288 // right screen near the edge of a multiscreen setup (based on where the
290 gfx::Rect
near_last_location(last_location_
, gfx::Size());
291 // Reparenting will cause Relayout and possible dock shrinking.
292 aura::Window
* previous_parent
= window
->parent();
293 aura::client::ParentWindowWithContext(window
, window
, near_last_location
);
294 if (window
->parent() != previous_parent
) {
295 wm::ReparentTransientChildrenOfChild(window
,
299 action
= was_docked_
? DOCKED_ACTION_UNDOCK
: DOCKED_ACTION_NONE
;
302 // |action| is recorded in UMA and used to maintain |window_state_|.
303 if (is_resized
&& is_docked_
&& was_docked_
)
304 action
= DOCKED_ACTION_RESIZE
;
305 else if (is_docked_
&& was_docked_
)
306 action
= DOCKED_ACTION_REORDER
;
307 else if (is_docked_
&& !was_docked_
)
308 action
= DOCKED_ACTION_DOCK
;
309 else if (!is_docked_
&& was_docked_
)
310 action
= DOCKED_ACTION_UNDOCK
;
312 action
= DOCKED_ACTION_NONE
;
314 // When a window is newly docked it is auto-sized by docked layout adjusting
315 // to other windows. If it is just dragged (but not resized) while being
316 // docked it is auto-sized unless it has been resized while being docked
319 wm::GetWindowState(window
)->set_bounds_changed_by_user(
320 was_docked_
&& (is_resized
|| was_bounds_changed_by_user_
));
323 if (action
== DOCKED_ACTION_DOCK
) {
324 const wm::WMEvent
event(wm::WM_EVENT_DOCK
);
325 window_state_
->OnWMEvent(&event
);
326 } else if (wm::GetWindowState(window
)->IsDocked() &&
327 action
== DOCKED_ACTION_UNDOCK
) {
328 const wm::WMEvent
event(wm::WM_EVENT_NORMAL
);
329 window_state_
->OnWMEvent(&event
);