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/window_positioner.h"
7 #include "ash/ash_switches.h"
8 #include "ash/screen_util.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/mru_window_tracker.h"
12 #include "ash/wm/window_resizer.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "base/command_line.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/compositor/scoped_layer_animation_settings.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/wm/core/window_animations.h"
23 #include "ui/wm/core/window_util.h"
27 const int WindowPositioner::kMinimumWindowOffset
= 32;
29 // The number of pixels which are kept free top, left and right when a window
30 // gets positioned to its default location.
32 const int WindowPositioner::kDesktopBorderSize
= 16;
34 // Maximum width of a window even if there is more room on the desktop.
36 const int WindowPositioner::kMaximumWindowWidth
= 1100;
40 // When a window gets opened in default mode and the screen is less than or
41 // equal to this width, the window will get opened in maximized mode. This value
42 // can be reduced to a "tame" number if the feature is disabled.
43 const int kForceMaximizeWidthLimit
= 1366;
45 // The time in milliseconds which should be used to visually move a window
46 // through an automatic "intelligent" window management option.
47 const int kWindowAutoMoveDurationMS
= 125;
49 // If set to true all window repositioning actions will be ignored. Set through
50 // WindowPositioner::SetIgnoreActivations().
51 static bool disable_auto_positioning
= false;
53 // If set to true, by default the first window in ASH will be maximized.
54 static bool maximize_first_window
= false;
56 // Check if any management should be performed (with a given |window|).
57 bool UseAutoWindowManager(const aura::Window
* window
) {
58 if (disable_auto_positioning
)
60 const wm::WindowState
* window_state
= wm::GetWindowState(window
);
61 return !window_state
->is_dragged() && window_state
->window_position_managed();
64 // Check if a given |window| can be managed. This includes that it's state is
65 // not minimized/maximized/the user has changed it's size by hand already.
66 // It furthermore checks for the WindowIsManaged status.
67 bool WindowPositionCanBeManaged(const aura::Window
* window
) {
68 if (disable_auto_positioning
)
70 const wm::WindowState
* window_state
= wm::GetWindowState(window
);
71 return window_state
->window_position_managed() &&
72 !window_state
->IsMinimized() &&
73 !window_state
->IsMaximized() &&
74 !window_state
->bounds_changed_by_user();
77 // Get the work area for a given |window| in parent coordinates.
78 gfx::Rect
GetWorkAreaForWindowInParent(aura::Window
* window
) {
80 // On Win 8, the host window can't be resized, so
81 // use window's bounds instead.
82 // TODO(oshima): Emulate host window resize on win8.
83 gfx::Rect work_area
= gfx::Rect(window
->parent()->bounds().size());
84 work_area
.Inset(Shell::GetScreen()->GetDisplayMatching(
85 window
->parent()->GetBoundsInScreen()).GetWorkAreaInsets());
88 return ScreenUtil::GetDisplayWorkAreaBoundsInParent(window
);
92 // Move the given |bounds| on the available |work_area| in the direction
93 // indicated by |move_right|. If |move_right| is true, the rectangle gets moved
94 // to the right edge, otherwise to the left one.
95 bool MoveRectToOneSide(const gfx::Rect
& work_area
,
99 if (work_area
.right() > bounds
->right()) {
100 bounds
->set_x(work_area
.right() - bounds
->width());
104 if (work_area
.x() < bounds
->x()) {
105 bounds
->set_x(work_area
.x());
112 // Move a |window| to new |bounds|. Animate if desired by user.
113 // Moves the transient children of the |window| as well by the same |offset| as
114 // the parent |window|.
115 void SetBoundsAndOffsetTransientChildren(aura::Window
* window
,
116 const gfx::Rect
& bounds
,
117 const gfx::Rect
& work_area
,
118 const gfx::Vector2d
& offset
) {
119 aura::Window::Windows transient_children
=
120 ::wm::GetTransientChildren(window
);
121 for (aura::Window::Windows::iterator iter
= transient_children
.begin();
122 iter
!= transient_children
.end(); ++iter
) {
123 aura::Window
* transient_child
= *iter
;
124 gfx::Rect child_bounds
= transient_child
->bounds();
125 gfx::Rect new_child_bounds
= child_bounds
+ offset
;
126 if ((child_bounds
.x() <= work_area
.x() &&
127 new_child_bounds
.x() <= work_area
.x()) ||
128 (child_bounds
.right() >= work_area
.right() &&
129 new_child_bounds
.right() >= work_area
.right())) {
132 if (new_child_bounds
.right() > work_area
.right())
133 new_child_bounds
.set_x(work_area
.right() - bounds
.width());
134 else if (new_child_bounds
.x() < work_area
.x())
135 new_child_bounds
.set_x(work_area
.x());
136 SetBoundsAndOffsetTransientChildren(transient_child
,
137 new_child_bounds
, work_area
, offset
);
140 if (::wm::WindowAnimationsDisabled(window
)) {
141 window
->SetBounds(bounds
);
145 ui::ScopedLayerAnimationSettings
settings(window
->layer()->GetAnimator());
146 settings
.SetTransitionDuration(
147 base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS
));
148 window
->SetBounds(bounds
);
151 // Move a |window| to new |bounds|. Animate if desired by user.
152 // Note: The function will do nothing if the bounds did not change.
153 void SetBoundsAnimated(aura::Window
* window
,
154 const gfx::Rect
& bounds
,
155 const gfx::Rect
& work_area
) {
156 gfx::Rect old_bounds
= window
->GetTargetBounds();
157 if (bounds
== old_bounds
)
159 gfx::Vector2d
offset(bounds
.origin() - old_bounds
.origin());
160 SetBoundsAndOffsetTransientChildren(window
, bounds
, work_area
, offset
);
163 // Move |window| into the center of the screen - or restore it to the previous
165 void AutoPlaceSingleWindow(aura::Window
* window
, bool animated
) {
166 gfx::Rect work_area
= GetWorkAreaForWindowInParent(window
);
167 gfx::Rect bounds
= window
->bounds();
168 const gfx::Rect
* user_defined_area
=
169 wm::GetWindowState(window
)->pre_auto_manage_window_bounds();
170 if (user_defined_area
) {
171 bounds
= *user_defined_area
;
172 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area
, &bounds
);
174 // Center the window (only in x).
175 bounds
.set_x(work_area
.x() + (work_area
.width() - bounds
.width()) / 2);
179 SetBoundsAnimated(window
, bounds
, work_area
);
181 window
->SetBounds(bounds
);
184 // Get the first open (non minimized) window which is on the screen defined.
185 aura::Window
* GetReferenceWindow(const aura::Window
* root_window
,
186 const aura::Window
* exclude
,
187 bool *single_window
) {
189 *single_window
= true;
190 // Get the active window.
191 aura::Window
* active
= ash::wm::GetActiveWindow();
192 if (active
&& active
->GetRootWindow() != root_window
)
195 // Get a list of all windows.
196 const std::vector
<aura::Window
*> windows
=
197 ash::MruWindowTracker::BuildWindowList(false);
202 aura::Window::Windows::const_iterator iter
= windows
.begin();
203 // Find the index of the current active window.
205 iter
= std::find(windows
.begin(), windows
.end(), active
);
207 int index
= (iter
== windows
.end()) ? 0 : (iter
- windows
.begin());
209 // Scan the cycle list backwards to see which is the second topmost window
210 // (and so on). Note that we might cycle a few indices twice if there is no
211 // suitable window. However - since the list is fairly small this should be
212 // very fast anyways.
213 aura::Window
* found
= NULL
;
214 for (int i
= index
+ windows
.size(); i
>= 0; i
--) {
215 aura::Window
* window
= windows
[i
% windows
.size()];
216 while (::wm::GetTransientParent(window
))
217 window
= ::wm::GetTransientParent(window
);
218 if (window
!= exclude
&& window
->type() == ui::wm::WINDOW_TYPE_NORMAL
&&
219 window
->GetRootWindow() == root_window
&& window
->TargetVisibility() &&
220 wm::GetWindowState(window
)->window_position_managed()) {
221 if (found
&& found
!= window
) {
222 // no need to check !single_window because the function must have
223 // been already returned in the "if (!single_window)" below.
224 *single_window
= false;
228 // If there is no need to check single window, return now.
239 int WindowPositioner::GetForceMaximizedWidthLimit() {
240 return kForceMaximizeWidthLimit
;
244 void WindowPositioner::GetBoundsAndShowStateForNewWindow(
245 const gfx::Screen
* screen
,
246 const aura::Window
* new_window
,
247 bool is_saved_bounds
,
248 ui::WindowShowState show_state_in
,
249 gfx::Rect
* bounds_in_out
,
250 ui::WindowShowState
* show_state_out
) {
252 // Always open new window in the target display.
253 aura::Window
* target
= Shell::GetTargetRootWindow();
255 aura::Window
* top_window
= GetReferenceWindow(target
, NULL
, NULL
);
256 // Our window should not have any impact if we are already on top.
257 if (top_window
== new_window
)
260 // If there is no valid other window we take and adjust the passed coordinates
263 gfx::Rect work_area
= screen
->GetDisplayNearestWindow(target
).work_area();
265 bounds_in_out
->AdjustToFit(work_area
);
266 // Use adjusted saved bounds, if there is one.
269 // When using "small screens" we want to always open in full screen mode.
270 if (show_state_in
== ui::SHOW_STATE_DEFAULT
&& (maximize_first_window
||
271 (work_area
.width() <= GetForceMaximizedWidthLimit() &&
272 (!new_window
|| !wm::GetWindowState(new_window
)->IsFullscreen())))) {
273 *show_state_out
= ui::SHOW_STATE_MAXIMIZED
;
277 wm::WindowState
* top_window_state
= wm::GetWindowState(top_window
);
278 bool maximized
= top_window_state
->IsMaximized();
279 // We ignore the saved show state, but look instead for the top level
280 // window's show state.
281 if (show_state_in
== ui::SHOW_STATE_DEFAULT
) {
282 *show_state_out
= maximized
? ui::SHOW_STATE_MAXIMIZED
:
283 ui::SHOW_STATE_DEFAULT
;
287 bool has_restore_bounds
= top_window_state
->HasRestoreBounds();
288 if (has_restore_bounds
) {
289 // For a maximized window ignore the real bounds of the top level window
290 // and use its restore bounds instead. Offset the bounds to prevent the
291 // windows from overlapping exactly when restored.
292 *bounds_in_out
= top_window_state
->GetRestoreBoundsInScreen() +
293 gfx::Vector2d(kMinimumWindowOffset
, kMinimumWindowOffset
);
295 if (is_saved_bounds
|| has_restore_bounds
) {
296 gfx::Rect work_area
= screen
->GetDisplayNearestWindow(target
).work_area();
297 bounds_in_out
->AdjustToFit(work_area
);
298 // Use adjusted saved bounds or restore bounds, if there is one.
303 // Use the size of the other window. The window's bound will be rearranged
304 // in ash::WorkspaceLayoutManager using this location.
305 *bounds_in_out
= top_window
->GetBoundsInScreen();
309 void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
310 const aura::Window
* removed_window
) {
311 if (!UseAutoWindowManager(removed_window
))
313 // Find a single open browser window.
315 aura::Window
* other_shown_window
= GetReferenceWindow(
316 removed_window
->GetRootWindow(), removed_window
, &single_window
);
317 if (!other_shown_window
|| !single_window
||
318 !WindowPositionCanBeManaged(other_shown_window
))
320 AutoPlaceSingleWindow(other_shown_window
, true);
324 bool WindowPositioner::DisableAutoPositioning(bool ignore
) {
325 bool old_state
= disable_auto_positioning
;
326 disable_auto_positioning
= ignore
;
331 void WindowPositioner::RearrangeVisibleWindowOnShow(
332 aura::Window
* added_window
) {
333 wm::WindowState
* added_window_state
= wm::GetWindowState(added_window
);
334 if (!added_window
->TargetVisibility())
337 if (!UseAutoWindowManager(added_window
) ||
338 added_window_state
->bounds_changed_by_user()) {
339 if (added_window_state
->minimum_visibility()) {
340 // Guarantee minimum visibility within the work area.
341 gfx::Rect work_area
= GetWorkAreaForWindowInParent(added_window
);
342 gfx::Rect bounds
= added_window
->bounds();
343 gfx::Rect new_bounds
= bounds
;
344 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area
,
346 if (new_bounds
!= bounds
)
347 added_window
->SetBounds(new_bounds
);
351 // Find a single open managed window.
353 aura::Window
* other_shown_window
= GetReferenceWindow(
354 added_window
->GetRootWindow(), added_window
, &single_window
);
356 if (!other_shown_window
) {
357 // It could be that this window is the first window joining the workspace.
358 if (!WindowPositionCanBeManaged(added_window
) || other_shown_window
)
360 // Since we might be going from 0 to 1 window, we have to arrange the new
361 // window to a good default.
362 AutoPlaceSingleWindow(added_window
, false);
366 gfx::Rect other_bounds
= other_shown_window
->bounds();
367 gfx::Rect work_area
= GetWorkAreaForWindowInParent(added_window
);
368 bool move_other_right
=
369 other_bounds
.CenterPoint().x() > work_area
.x() + work_area
.width() / 2;
371 // Push the other window to the size only if there are two windows left.
373 // When going from one to two windows both windows loose their
374 // "positioned by user" flags.
375 added_window_state
->set_bounds_changed_by_user(false);
376 wm::WindowState
* other_window_state
=
377 wm::GetWindowState(other_shown_window
);
378 other_window_state
->set_bounds_changed_by_user(false);
380 if (WindowPositionCanBeManaged(other_shown_window
)) {
381 // Don't override pre auto managed bounds as the current bounds
382 // may not be original.
383 if (!other_window_state
->pre_auto_manage_window_bounds())
384 other_window_state
->SetPreAutoManageWindowBounds(other_bounds
);
386 // Push away the other window after remembering its current position.
387 if (MoveRectToOneSide(work_area
, move_other_right
, &other_bounds
))
388 SetBoundsAnimated(other_shown_window
, other_bounds
, work_area
);
392 // Remember the current location of the window if it's new and push
393 // it also to the opposite location if needed. Since it is just
394 // being shown, we do not need to animate it.
395 gfx::Rect added_bounds
= added_window
->bounds();
396 if (!added_window_state
->pre_auto_manage_window_bounds())
397 added_window_state
->SetPreAutoManageWindowBounds(added_bounds
);
398 if (MoveRectToOneSide(work_area
, !move_other_right
, &added_bounds
))
399 added_window
->SetBounds(added_bounds
);
402 WindowPositioner::WindowPositioner()
403 : pop_position_offset_increment_x(0),
404 pop_position_offset_increment_y(0),
405 popup_position_offset_from_screen_corner_x(0),
406 popup_position_offset_from_screen_corner_y(0),
407 last_popup_position_x_(0),
408 last_popup_position_y_(0) {
411 WindowPositioner::~WindowPositioner() {
414 gfx::Rect
WindowPositioner::GetDefaultWindowBounds(
415 const gfx::Display
& display
) {
416 const gfx::Rect work_area
= display
.work_area();
417 // There should be a 'desktop' border around the window at the left and right
419 int default_width
= work_area
.width() - 2 * kDesktopBorderSize
;
420 // There should also be a 'desktop' border around the window at the top.
421 // Since the workspace excludes the tray area we only need one border size.
422 int default_height
= work_area
.height() - kDesktopBorderSize
;
423 int offset_x
= kDesktopBorderSize
;
424 if (default_width
> kMaximumWindowWidth
) {
425 // The window should get centered on the screen and not follow the grid.
426 offset_x
= (work_area
.width() - kMaximumWindowWidth
) / 2;
427 default_width
= kMaximumWindowWidth
;
429 return gfx::Rect(work_area
.x() + offset_x
,
430 work_area
.y() + kDesktopBorderSize
,
435 gfx::Rect
WindowPositioner::GetPopupPosition(const gfx::Rect
& old_pos
) {
436 int grid
= kMinimumWindowOffset
;
437 popup_position_offset_from_screen_corner_x
= grid
;
438 popup_position_offset_from_screen_corner_y
= grid
;
439 if (!pop_position_offset_increment_x
) {
440 // When the popup position increment is 0, the last popup position
441 // was not yet initialized.
442 last_popup_position_x_
= popup_position_offset_from_screen_corner_x
;
443 last_popup_position_y_
= popup_position_offset_from_screen_corner_y
;
445 pop_position_offset_increment_x
= grid
;
446 pop_position_offset_increment_y
= grid
;
447 // We handle the Multi monitor support by retrieving the active window's
449 aura::Window
* window
= wm::GetActiveWindow();
450 const gfx::Rect work_area
= window
&& window
->IsVisible() ?
451 Shell::GetScreen()->GetDisplayNearestWindow(window
).work_area() :
452 Shell::GetScreen()->GetPrimaryDisplay().work_area();
453 // Only try to reposition the popup when it is not spanning the entire
455 if ((old_pos
.width() + popup_position_offset_from_screen_corner_x
>=
456 work_area
.width()) ||
457 (old_pos
.height() + popup_position_offset_from_screen_corner_y
>=
459 return AlignPopupPosition(old_pos
, work_area
, grid
);
460 const gfx::Rect result
= SmartPopupPosition(old_pos
, work_area
, grid
);
461 if (!result
.IsEmpty())
462 return AlignPopupPosition(result
, work_area
, grid
);
463 return NormalPopupPosition(old_pos
, work_area
);
467 void WindowPositioner::SetMaximizeFirstWindow(bool maximize
) {
468 maximize_first_window
= maximize
;
471 gfx::Rect
WindowPositioner::NormalPopupPosition(
472 const gfx::Rect
& old_pos
,
473 const gfx::Rect
& work_area
) {
474 int w
= old_pos
.width();
475 int h
= old_pos
.height();
476 // Note: The 'last_popup_position' is checked and kept relative to the
477 // screen size. The offsetting will be done in the last step when the
478 // target rectangle gets returned.
480 if (last_popup_position_y_
+ h
> work_area
.height() ||
481 last_popup_position_x_
+ w
> work_area
.width()) {
482 // Popup does not fit on screen. Reset to next diagonal row.
483 last_popup_position_x_
-= last_popup_position_y_
-
484 popup_position_offset_from_screen_corner_x
-
485 pop_position_offset_increment_x
;
486 last_popup_position_y_
= popup_position_offset_from_screen_corner_y
;
489 if (last_popup_position_x_
+ w
> work_area
.width()) {
491 last_popup_position_x_
= popup_position_offset_from_screen_corner_x
;
492 last_popup_position_y_
= popup_position_offset_from_screen_corner_y
;
495 int x
= last_popup_position_x_
;
496 int y
= last_popup_position_y_
;
498 last_popup_position_x_
+= pop_position_offset_increment_x
;
499 last_popup_position_y_
+= pop_position_offset_increment_y
;
501 return gfx::Rect(x
+ work_area
.x(), y
+ work_area
.y(), w
, h
);
504 gfx::Rect
WindowPositioner::SmartPopupPosition(
505 const gfx::Rect
& old_pos
,
506 const gfx::Rect
& work_area
,
508 const std::vector
<aura::Window
*> windows
=
509 MruWindowTracker::BuildWindowList(false);
511 std::vector
<const gfx::Rect
*> regions
;
512 // Process the window list and check if we can bail immediately.
513 for (size_t i
= 0; i
< windows
.size(); i
++) {
514 // We only include opaque and visible windows.
515 if (windows
[i
] && windows
[i
]->IsVisible() && windows
[i
]->layer() &&
516 (!windows
[i
]->transparent() ||
517 windows
[i
]->layer()->GetTargetOpacity() == 1.0)) {
518 wm::WindowState
* window_state
= wm::GetWindowState(windows
[i
]);
519 // When any window is maximized we cannot find any free space.
520 if (window_state
->IsMaximizedOrFullscreen())
521 return gfx::Rect(0, 0, 0, 0);
522 if (window_state
->IsNormalOrSnapped())
523 regions
.push_back(&windows
[i
]->bounds());
528 return gfx::Rect(0, 0, 0, 0);
530 int w
= old_pos
.width();
531 int h
= old_pos
.height();
532 int x_end
= work_area
.width() / 2;
534 // We parse for a proper location on the screen. We do this in two runs:
535 // The first run will start from the left, parsing down, skipping any
536 // overlapping windows it will encounter until the popup's height can not
537 // be served anymore. Then the next grid position to the right will be
538 // taken, and the same cycle starts again. This will be repeated until we
539 // hit the middle of the screen (or we find a suitable location).
540 // In the second run we parse beginning from the right corner downwards and
542 // When no location was found, an empty rectangle will be returned.
543 for (int run
= 0; run
< 2; run
++) {
544 if (run
== 0) { // First run: Start left, parse right till mid screen.
546 x_increment
= pop_position_offset_increment_x
;
547 } else { // Second run: Start right, parse left till mid screen.
548 x
= work_area
.width() - w
;
549 x_increment
= -pop_position_offset_increment_x
;
551 // Note: The passing (x,y,w,h) window is always relative to the work area's
553 for (; x_increment
> 0 ? (x
< x_end
) : (x
> x_end
); x
+= x_increment
) {
555 while (y
+ h
<= work_area
.height()) {
557 for (i
= 0; i
< regions
.size(); i
++) {
558 if (regions
[i
]->Intersects(gfx::Rect(x
+ work_area
.x(),
559 y
+ work_area
.y(), w
, h
))) {
560 y
= regions
[i
]->bottom() - work_area
.y();
564 if (i
>= regions
.size())
565 return gfx::Rect(x
+ work_area
.x(), y
+ work_area
.y(), w
, h
);
569 return gfx::Rect(0, 0, 0, 0);
572 gfx::Rect
WindowPositioner::AlignPopupPosition(
573 const gfx::Rect
& pos
,
574 const gfx::Rect
& work_area
,
579 int x
= pos
.x() - (pos
.x() - work_area
.x()) % grid
;
580 int y
= pos
.y() - (pos
.y() - work_area
.y()) % grid
;
582 int h
= pos
.height();
584 // If the alignment was pushing the window out of the screen, we ignore the
585 // alignment for that call.
586 if (abs(pos
.right() - work_area
.right()) < grid
)
587 x
= work_area
.right() - w
;
588 if (abs(pos
.bottom() - work_area
.bottom()) < grid
)
589 y
= work_area
.bottom() - h
;
590 return gfx::Rect(x
, y
, w
, h
);