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