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( | |
192 GetTarget(), GetTarget()->GetParent(), docked_container); | |
193 if (!resizer) | |
194 return; | |
195 } | |
196 if (is_docked_) | |
197 dock_layout_->DockDraggedWindow(GetTarget()); | |
198 } | |
199 | |
200 void DockedWindowResizer::FinishedDragging( | |
201 aura::client::WindowMoveResult move_result) { | |
202 if (!did_move_or_resize_) | |
203 return; | |
204 did_move_or_resize_ = false; | |
205 WmWindow* window = GetTarget(); | |
206 const bool is_attached_panel = | |
207 window->GetType() == ui::wm::WINDOW_TYPE_PANEL && | |
208 window->aura_window()->GetProperty(kPanelAttachedKey); | |
209 const bool is_resized = | |
210 (details().bounds_change & WindowResizer::kBoundsChange_Resizes) != 0; | |
211 | |
212 // Undock the window if it is not in the normal, docked or minimized state | |
213 // type. This happens if a user snaps or maximizes a window using a | |
214 // keyboard shortcut while it is being dragged. | |
215 if (!window_state_->IsMinimized() && !window_state_->IsDocked() && | |
216 !window_state_->IsNormalStateType()) | |
217 is_docked_ = false; | |
218 | |
219 // When drag is completed the dragged docked window is resized to the bounds | |
220 // calculated by the layout manager that conform to other docked windows. | |
221 if (!is_attached_panel && is_docked_ && !is_resized) { | |
222 gfx::Rect bounds = window->GetParent()->ConvertRectFromScreen( | |
223 dock_layout_->dragged_bounds()); | |
224 if (!bounds.IsEmpty() && bounds.width() != window->GetBounds().width()) { | |
225 window->SetBounds(bounds); | |
226 } | |
227 } | |
228 // If a window has restore bounds, update the restore origin but not the size. | |
229 // The size gets restored when a window is undocked. | |
230 if (is_resized && is_docked_ && window_state_->HasRestoreBounds()) { | |
231 gfx::Rect restore_bounds = window->GetBoundsInScreen(); | |
232 restore_bounds.set_size(window_state_->GetRestoreBoundsInScreen().size()); | |
233 window_state_->SetRestoreBoundsInScreen(restore_bounds); | |
234 } | |
235 | |
236 // Check if the window needs to be docked or returned to workspace. | |
237 DockedAction action = | |
238 MaybeReparentWindowOnDragCompletion(is_resized, is_attached_panel); | |
239 dock_layout_->FinishDragging( | |
240 move_result == aura::client::MOVE_CANCELED ? DOCKED_ACTION_NONE : action, | |
241 details().source == aura::client::WINDOW_MOVE_SOURCE_MOUSE | |
242 ? DOCKED_ACTION_SOURCE_MOUSE | |
243 : DOCKED_ACTION_SOURCE_TOUCH); | |
244 | |
245 // If we started the drag in one root window and moved into another root | |
246 // but then canceled the drag we may need to inform the original layout | |
247 // manager that the drag is finished. | |
248 if (initial_dock_layout_ != dock_layout_) | |
249 initial_dock_layout_->FinishDragging( | |
250 DOCKED_ACTION_NONE, | |
251 details().source == aura::client::WINDOW_MOVE_SOURCE_MOUSE | |
252 ? DOCKED_ACTION_SOURCE_MOUSE | |
253 : DOCKED_ACTION_SOURCE_TOUCH); | |
254 is_docked_ = false; | |
255 } | |
256 | |
257 DockedAction DockedWindowResizer::MaybeReparentWindowOnDragCompletion( | |
258 bool is_resized, | |
259 bool is_attached_panel) { | |
260 WmWindow* window = GetTarget(); | |
261 | |
262 // Check if the window needs to be docked or returned to workspace. | |
263 DockedAction action = DOCKED_ACTION_NONE; | |
264 WmWindow* dock_container = window->GetRootWindow()->GetChildByShellWindowId( | |
265 kShellWindowId_DockedContainer); | |
266 if ((is_resized || !is_attached_panel) && | |
267 is_docked_ != (window->GetParent() == dock_container)) { | |
268 if (is_docked_) { | |
269 wm::ReparentChildWithTransientChildren(window, window->GetParent(), | |
270 dock_container); | |
271 action = DOCKED_ACTION_DOCK; | |
272 } else if (window->GetParent()->GetShellWindowId() == | |
273 kShellWindowId_DockedContainer) { | |
274 // Reparent the window back to workspace. | |
275 // We need to be careful to give ParentWindowWithContext a location in | |
276 // the right root window (matching the logic in DragWindowResizer) based | |
277 // on which root window a mouse pointer is in. We want to undock into the | |
278 // right screen near the edge of a multiscreen setup (based on where the | |
279 // mouse is). | |
280 gfx::Rect near_last_location(last_location_, gfx::Size()); | |
281 // Reparenting will cause Relayout and possible dock shrinking. | |
282 WmWindow* previous_parent = window->GetParent(); | |
283 window->SetParentUsingContext(window, near_last_location); | |
284 if (window->GetParent() != previous_parent) { | |
285 wm::ReparentTransientChildrenOfChild(window, previous_parent, | |
286 window->GetParent()); | |
287 } | |
288 action = was_docked_ ? DOCKED_ACTION_UNDOCK : DOCKED_ACTION_NONE; | |
289 } | |
290 } else { | |
291 // |action| is recorded in UMA and used to maintain |window_state_|. | |
292 if (is_resized && is_docked_ && was_docked_) | |
293 action = DOCKED_ACTION_RESIZE; | |
294 else if (is_docked_ && was_docked_) | |
295 action = DOCKED_ACTION_REORDER; | |
296 else if (is_docked_ && !was_docked_) | |
297 action = DOCKED_ACTION_DOCK; | |
298 else if (!is_docked_ && was_docked_) | |
299 action = DOCKED_ACTION_UNDOCK; | |
300 else | |
301 action = DOCKED_ACTION_NONE; | |
302 } | |
303 // When a window is newly docked it is auto-sized by docked layout adjusting | |
304 // to other windows. If it is just dragged (but not resized) while being | |
305 // docked it is auto-sized unless it has been resized while being docked | |
306 // before. | |
307 if (is_docked_) { | |
308 window->GetWindowState()->set_bounds_changed_by_user( | |
309 was_docked_ && (is_resized || was_bounds_changed_by_user_)); | |
310 } | |
311 | |
312 if (action == DOCKED_ACTION_DOCK) { | |
313 const wm::WMEvent event(wm::WM_EVENT_DOCK); | |
314 window_state_->OnWMEvent(&event); | |
315 } else if (window->GetWindowState()->IsDocked() && | |
316 action == DOCKED_ACTION_UNDOCK) { | |
317 const wm::WMEvent event(wm::WM_EVENT_NORMAL); | |
318 window_state_->OnWMEvent(&event); | |
319 } | |
320 | |
321 return action; | |
322 } | |
323 | |
324 } // namespace ash | |
OLD | NEW |