Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / wm / window_positioner.cc
blobeed15a053525596e4b47969a4ff2ee264ae757d5
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/screen_util.h"
8 #include "ash/shell.h"
9 #include "ash/shell_delegate.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 "ui/aura/window.h"
16 #include "ui/aura/window_delegate.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/wm/core/window_animations.h"
22 #include "ui/wm/core/window_util.h"
24 namespace ash {
26 const int WindowPositioner::kMinimumWindowOffset = 32;
28 // The number of pixels which are kept free top, left and right when a window
29 // gets positioned to its default location.
30 // static
31 const int WindowPositioner::kDesktopBorderSize = 16;
33 // Maximum width of a window even if there is more room on the desktop.
34 // static
35 const int WindowPositioner::kMaximumWindowWidth = 1100;
37 namespace {
39 // When a window gets opened in default mode and the screen is less than or
40 // equal to this width, the window will get opened in maximized mode. This value
41 // can be reduced to a "tame" number if the feature is disabled.
42 const int kForceMaximizeWidthLimit = 1366;
44 // The time in milliseconds which should be used to visually move a window
45 // through an automatic "intelligent" window management option.
46 const int kWindowAutoMoveDurationMS = 125;
48 // If set to true all window repositioning actions will be ignored. Set through
49 // WindowPositioner::SetIgnoreActivations().
50 static bool disable_auto_positioning = false;
52 // If set to true, by default the first window in ASH will be maximized.
53 static bool maximize_first_window = false;
55 // Check if any management should be performed (with a given |window|).
56 bool UseAutoWindowManager(const aura::Window* window) {
57 if (disable_auto_positioning)
58 return false;
59 const wm::WindowState* window_state = wm::GetWindowState(window);
60 return !window_state->is_dragged() && window_state->window_position_managed();
63 // Check if a given |window| can be managed. This includes that it's state is
64 // not minimized/maximized/the user has changed it's size by hand already.
65 // It furthermore checks for the WindowIsManaged status.
66 bool WindowPositionCanBeManaged(const aura::Window* window) {
67 if (disable_auto_positioning)
68 return false;
69 const wm::WindowState* window_state = wm::GetWindowState(window);
70 return window_state->window_position_managed() &&
71 !window_state->IsMinimized() &&
72 !window_state->IsMaximized() &&
73 !window_state->bounds_changed_by_user();
76 // Get the work area for a given |window| in parent coordinates.
77 gfx::Rect GetWorkAreaForWindowInParent(aura::Window* window) {
78 #if defined(OS_WIN)
79 // On Win 8, the host window can't be resized, so
80 // use window's bounds instead.
81 // TODO(oshima): Emulate host window resize on win8.
82 gfx::Rect work_area = gfx::Rect(window->parent()->bounds().size());
83 work_area.Inset(Shell::GetScreen()->GetDisplayMatching(
84 window->parent()->GetBoundsInScreen()).GetWorkAreaInsets());
85 return work_area;
86 #else
87 return ScreenUtil::GetDisplayWorkAreaBoundsInParent(window);
88 #endif
91 // Move the given |bounds| on the available |work_area| in the direction
92 // indicated by |move_right|. If |move_right| is true, the rectangle gets moved
93 // to the right edge, otherwise to the left one.
94 bool MoveRectToOneSide(const gfx::Rect& work_area,
95 bool move_right,
96 gfx::Rect* bounds) {
97 if (move_right) {
98 if (work_area.right() > bounds->right()) {
99 bounds->set_x(work_area.right() - bounds->width());
100 return true;
102 } else {
103 if (work_area.x() < bounds->x()) {
104 bounds->set_x(work_area.x());
105 return true;
108 return false;
111 // Move a |window| to new |bounds|. Animate if desired by user.
112 // Moves the transient children of the |window| as well by the same |offset| as
113 // the parent |window|.
114 void SetBoundsAndOffsetTransientChildren(aura::Window* window,
115 const gfx::Rect& bounds,
116 const gfx::Rect& work_area,
117 const gfx::Vector2d& offset) {
118 aura::Window::Windows transient_children =
119 ::wm::GetTransientChildren(window);
120 for (aura::Window::Windows::iterator iter = transient_children.begin();
121 iter != transient_children.end(); ++iter) {
122 aura::Window* transient_child = *iter;
123 gfx::Rect child_bounds = transient_child->bounds();
124 gfx::Rect new_child_bounds = child_bounds + offset;
125 if ((child_bounds.x() <= work_area.x() &&
126 new_child_bounds.x() <= work_area.x()) ||
127 (child_bounds.right() >= work_area.right() &&
128 new_child_bounds.right() >= work_area.right())) {
129 continue;
131 if (new_child_bounds.right() > work_area.right())
132 new_child_bounds.set_x(work_area.right() - bounds.width());
133 else if (new_child_bounds.x() < work_area.x())
134 new_child_bounds.set_x(work_area.x());
135 SetBoundsAndOffsetTransientChildren(transient_child,
136 new_child_bounds, work_area, offset);
139 if (::wm::WindowAnimationsDisabled(window)) {
140 window->SetBounds(bounds);
141 return;
144 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
145 settings.SetTransitionDuration(
146 base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
147 window->SetBounds(bounds);
150 // Move a |window| to new |bounds|. Animate if desired by user.
151 // Note: The function will do nothing if the bounds did not change.
152 void SetBoundsAnimated(aura::Window* window,
153 const gfx::Rect& bounds,
154 const gfx::Rect& work_area) {
155 gfx::Rect old_bounds = window->GetTargetBounds();
156 if (bounds == old_bounds)
157 return;
158 gfx::Vector2d offset(bounds.origin() - old_bounds.origin());
159 SetBoundsAndOffsetTransientChildren(window, bounds, work_area, offset);
162 // Move |window| into the center of the screen - or restore it to the previous
163 // position.
164 void AutoPlaceSingleWindow(aura::Window* window, bool animated) {
165 gfx::Rect work_area = GetWorkAreaForWindowInParent(window);
166 gfx::Rect bounds = window->bounds();
167 const gfx::Rect* user_defined_area =
168 wm::GetWindowState(window)->pre_auto_manage_window_bounds();
169 if (user_defined_area) {
170 bounds = *user_defined_area;
171 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &bounds);
172 } else {
173 // Center the window (only in x).
174 bounds.set_x(work_area.x() + (work_area.width() - bounds.width()) / 2);
177 if (animated)
178 SetBoundsAnimated(window, bounds, work_area);
179 else
180 window->SetBounds(bounds);
183 // Get the first open (non minimized) window which is on the screen defined.
184 aura::Window* GetReferenceWindow(const aura::Window* root_window,
185 const aura::Window* exclude,
186 bool *single_window) {
187 if (single_window)
188 *single_window = true;
189 // Get the active window.
190 aura::Window* active = ash::wm::GetActiveWindow();
191 if (active && active->GetRootWindow() != root_window)
192 active = NULL;
194 // Get a list of all windows.
195 const std::vector<aura::Window*> windows = ash::Shell::GetInstance()->
196 mru_window_tracker()->BuildWindowListIgnoreModal();
198 if (windows.empty())
199 return NULL;
201 aura::Window::Windows::const_iterator iter = windows.begin();
202 // Find the index of the current active window.
203 if (active)
204 iter = std::find(windows.begin(), windows.end(), active);
206 int index = (iter == windows.end()) ? 0 : (iter - windows.begin());
208 // Scan the cycle list backwards to see which is the second topmost window
209 // (and so on). Note that we might cycle a few indices twice if there is no
210 // suitable window. However - since the list is fairly small this should be
211 // very fast anyways.
212 aura::Window* found = NULL;
213 for (int i = index + windows.size(); i >= 0; i--) {
214 aura::Window* window = windows[i % windows.size()];
215 while (::wm::GetTransientParent(window))
216 window = ::wm::GetTransientParent(window);
217 if (window != exclude && window->type() == ui::wm::WINDOW_TYPE_NORMAL &&
218 window->GetRootWindow() == root_window && window->TargetVisibility() &&
219 wm::GetWindowState(window)->window_position_managed()) {
220 if (found && found != window) {
221 // no need to check !single_window because the function must have
222 // been already returned in the "if (!single_window)" below.
223 *single_window = false;
224 return found;
226 found = window;
227 // If there is no need to check single window, return now.
228 if (!single_window)
229 return found;
232 return found;
235 } // namespace
237 // static
238 int WindowPositioner::GetForceMaximizedWidthLimit() {
239 return kForceMaximizeWidthLimit;
242 // static
243 void WindowPositioner::GetBoundsAndShowStateForNewWindow(
244 const gfx::Screen* screen,
245 const aura::Window* new_window,
246 bool is_saved_bounds,
247 ui::WindowShowState show_state_in,
248 gfx::Rect* bounds_in_out,
249 ui::WindowShowState* show_state_out) {
250 // Always open new window in the target display.
251 aura::Window* target = Shell::GetTargetRootWindow();
253 aura::Window* top_window = GetReferenceWindow(target, NULL, NULL);
254 // Our window should not have any impact if we are already on top.
255 if (top_window == new_window)
256 top_window = NULL;
258 // If there is no valid other window we take and adjust the passed coordinates
259 // and show state.
260 if (!top_window) {
261 gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
263 bounds_in_out->AdjustToFit(work_area);
264 // Use adjusted saved bounds, if there is one.
265 if (is_saved_bounds)
266 return;
268 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
269 const bool maximize_first_window_on_first_run =
270 Shell::GetInstance()->delegate()->IsForceMaximizeOnFirstRun();
271 // We want to always open maximized on "small screens" or when policy
272 // tells us to.
273 const bool set_maximized =
274 maximize_first_window ||
275 ((work_area.width() <= GetForceMaximizedWidthLimit() ||
276 maximize_first_window_on_first_run) &&
277 (!new_window || !wm::GetWindowState(new_window)->IsFullscreen()));
279 if (set_maximized)
280 *show_state_out = ui::SHOW_STATE_MAXIMIZED;
282 return;
285 wm::WindowState* top_window_state = wm::GetWindowState(top_window);
286 bool maximized = top_window_state->IsMaximized();
287 // We ignore the saved show state, but look instead for the top level
288 // window's show state.
289 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
290 *show_state_out = maximized ? ui::SHOW_STATE_MAXIMIZED :
291 ui::SHOW_STATE_DEFAULT;
294 if (maximized) {
295 bool has_restore_bounds = top_window_state->HasRestoreBounds();
296 if (has_restore_bounds) {
297 // For a maximized window ignore the real bounds of the top level window
298 // and use its restore bounds instead. Offset the bounds to prevent the
299 // windows from overlapping exactly when restored.
300 *bounds_in_out = top_window_state->GetRestoreBoundsInScreen() +
301 gfx::Vector2d(kMinimumWindowOffset, kMinimumWindowOffset);
303 if (is_saved_bounds || has_restore_bounds) {
304 gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
305 bounds_in_out->AdjustToFit(work_area);
306 // Use adjusted saved bounds or restore bounds, if there is one.
307 return;
311 // Use the size of the other window. The window's bound will be rearranged
312 // in ash::WorkspaceLayoutManager using this location.
313 *bounds_in_out = top_window->GetBoundsInScreen();
316 // static
317 void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
318 const aura::Window* removed_window) {
319 if (!UseAutoWindowManager(removed_window))
320 return;
321 // Find a single open browser window.
322 bool single_window;
323 aura::Window* other_shown_window = GetReferenceWindow(
324 removed_window->GetRootWindow(), removed_window, &single_window);
325 if (!other_shown_window || !single_window ||
326 !WindowPositionCanBeManaged(other_shown_window))
327 return;
328 AutoPlaceSingleWindow(other_shown_window, true);
331 // static
332 bool WindowPositioner::DisableAutoPositioning(bool ignore) {
333 bool old_state = disable_auto_positioning;
334 disable_auto_positioning = ignore;
335 return old_state;
338 // static
339 void WindowPositioner::RearrangeVisibleWindowOnShow(
340 aura::Window* added_window) {
341 wm::WindowState* added_window_state = wm::GetWindowState(added_window);
342 if (!added_window->TargetVisibility())
343 return;
345 if (!UseAutoWindowManager(added_window) ||
346 added_window_state->bounds_changed_by_user()) {
347 if (added_window_state->minimum_visibility()) {
348 // Guarantee minimum visibility within the work area.
349 gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
350 gfx::Rect bounds = added_window->bounds();
351 gfx::Rect new_bounds = bounds;
352 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area,
353 &new_bounds);
354 if (new_bounds != bounds)
355 added_window->SetBounds(new_bounds);
357 return;
359 // Find a single open managed window.
360 bool single_window;
361 aura::Window* other_shown_window = GetReferenceWindow(
362 added_window->GetRootWindow(), added_window, &single_window);
364 if (!other_shown_window) {
365 // It could be that this window is the first window joining the workspace.
366 if (!WindowPositionCanBeManaged(added_window) || other_shown_window)
367 return;
368 // Since we might be going from 0 to 1 window, we have to arrange the new
369 // window to a good default.
370 AutoPlaceSingleWindow(added_window, false);
371 return;
374 gfx::Rect other_bounds = other_shown_window->bounds();
375 gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
376 bool move_other_right =
377 other_bounds.CenterPoint().x() > work_area.x() + work_area.width() / 2;
379 // Push the other window to the size only if there are two windows left.
380 if (single_window) {
381 // When going from one to two windows both windows loose their
382 // "positioned by user" flags.
383 added_window_state->set_bounds_changed_by_user(false);
384 wm::WindowState* other_window_state =
385 wm::GetWindowState(other_shown_window);
386 other_window_state->set_bounds_changed_by_user(false);
388 if (WindowPositionCanBeManaged(other_shown_window)) {
389 // Don't override pre auto managed bounds as the current bounds
390 // may not be original.
391 if (!other_window_state->pre_auto_manage_window_bounds())
392 other_window_state->SetPreAutoManageWindowBounds(other_bounds);
394 // Push away the other window after remembering its current position.
395 if (MoveRectToOneSide(work_area, move_other_right, &other_bounds))
396 SetBoundsAnimated(other_shown_window, other_bounds, work_area);
400 // Remember the current location of the window if it's new and push
401 // it also to the opposite location if needed. Since it is just
402 // being shown, we do not need to animate it.
403 gfx::Rect added_bounds = added_window->bounds();
404 if (!added_window_state->pre_auto_manage_window_bounds())
405 added_window_state->SetPreAutoManageWindowBounds(added_bounds);
406 if (MoveRectToOneSide(work_area, !move_other_right, &added_bounds))
407 added_window->SetBounds(added_bounds);
410 WindowPositioner::WindowPositioner()
411 : pop_position_offset_increment_x(0),
412 pop_position_offset_increment_y(0),
413 popup_position_offset_from_screen_corner_x(0),
414 popup_position_offset_from_screen_corner_y(0),
415 last_popup_position_x_(0),
416 last_popup_position_y_(0) {
419 WindowPositioner::~WindowPositioner() {
422 gfx::Rect WindowPositioner::GetDefaultWindowBounds(
423 const gfx::Display& display) {
424 const gfx::Rect work_area = display.work_area();
425 // There should be a 'desktop' border around the window at the left and right
426 // side.
427 int default_width = work_area.width() - 2 * kDesktopBorderSize;
428 // There should also be a 'desktop' border around the window at the top.
429 // Since the workspace excludes the tray area we only need one border size.
430 int default_height = work_area.height() - kDesktopBorderSize;
431 int offset_x = kDesktopBorderSize;
432 if (default_width > kMaximumWindowWidth) {
433 // The window should get centered on the screen and not follow the grid.
434 offset_x = (work_area.width() - kMaximumWindowWidth) / 2;
435 default_width = kMaximumWindowWidth;
437 return gfx::Rect(work_area.x() + offset_x,
438 work_area.y() + kDesktopBorderSize,
439 default_width,
440 default_height);
443 gfx::Rect WindowPositioner::GetPopupPosition(const gfx::Rect& old_pos) {
444 int grid = kMinimumWindowOffset;
445 popup_position_offset_from_screen_corner_x = grid;
446 popup_position_offset_from_screen_corner_y = grid;
447 if (!pop_position_offset_increment_x) {
448 // When the popup position increment is 0, the last popup position
449 // was not yet initialized.
450 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
451 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
453 pop_position_offset_increment_x = grid;
454 pop_position_offset_increment_y = grid;
455 // We handle the Multi monitor support by retrieving the active window's
456 // work area.
457 aura::Window* window = wm::GetActiveWindow();
458 const gfx::Rect work_area = window && window->IsVisible() ?
459 Shell::GetScreen()->GetDisplayNearestWindow(window).work_area() :
460 Shell::GetScreen()->GetPrimaryDisplay().work_area();
461 // Only try to reposition the popup when it is not spanning the entire
462 // screen.
463 if ((old_pos.width() + popup_position_offset_from_screen_corner_x >=
464 work_area.width()) ||
465 (old_pos.height() + popup_position_offset_from_screen_corner_y >=
466 work_area.height()))
467 return AlignPopupPosition(old_pos, work_area, grid);
468 const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid);
469 if (!result.IsEmpty())
470 return AlignPopupPosition(result, work_area, grid);
471 return NormalPopupPosition(old_pos, work_area);
474 // static
475 void WindowPositioner::SetMaximizeFirstWindow(bool maximize) {
476 maximize_first_window = maximize;
479 gfx::Rect WindowPositioner::NormalPopupPosition(
480 const gfx::Rect& old_pos,
481 const gfx::Rect& work_area) {
482 int w = old_pos.width();
483 int h = old_pos.height();
484 // Note: The 'last_popup_position' is checked and kept relative to the
485 // screen size. The offsetting will be done in the last step when the
486 // target rectangle gets returned.
487 bool reset = false;
488 if (last_popup_position_y_ + h > work_area.height() ||
489 last_popup_position_x_ + w > work_area.width()) {
490 // Popup does not fit on screen. Reset to next diagonal row.
491 last_popup_position_x_ -= last_popup_position_y_ -
492 popup_position_offset_from_screen_corner_x -
493 pop_position_offset_increment_x;
494 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
495 reset = true;
497 if (last_popup_position_x_ + w > work_area.width()) {
498 // Start again over.
499 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
500 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
501 reset = true;
503 int x = last_popup_position_x_;
504 int y = last_popup_position_y_;
505 if (!reset) {
506 last_popup_position_x_ += pop_position_offset_increment_x;
507 last_popup_position_y_ += pop_position_offset_increment_y;
509 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
512 gfx::Rect WindowPositioner::SmartPopupPosition(
513 const gfx::Rect& old_pos,
514 const gfx::Rect& work_area,
515 int grid) {
516 const std::vector<aura::Window*> windows = ash::Shell::GetInstance()->
517 mru_window_tracker()->BuildWindowListIgnoreModal();
519 std::vector<const gfx::Rect*> regions;
520 // Process the window list and check if we can bail immediately.
521 for (size_t i = 0; i < windows.size(); i++) {
522 // We only include opaque and visible windows.
523 if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() &&
524 (!windows[i]->transparent() ||
525 windows[i]->layer()->GetTargetOpacity() == 1.0)) {
526 wm::WindowState* window_state = wm::GetWindowState(windows[i]);
527 // When any window is maximized we cannot find any free space.
528 if (window_state->IsMaximizedOrFullscreen())
529 return gfx::Rect(0, 0, 0, 0);
530 if (window_state->IsNormalOrSnapped())
531 regions.push_back(&windows[i]->bounds());
535 if (regions.empty())
536 return gfx::Rect(0, 0, 0, 0);
538 int w = old_pos.width();
539 int h = old_pos.height();
540 int x_end = work_area.width() / 2;
541 int x, x_increment;
542 // We parse for a proper location on the screen. We do this in two runs:
543 // The first run will start from the left, parsing down, skipping any
544 // overlapping windows it will encounter until the popup's height can not
545 // be served anymore. Then the next grid position to the right will be
546 // taken, and the same cycle starts again. This will be repeated until we
547 // hit the middle of the screen (or we find a suitable location).
548 // In the second run we parse beginning from the right corner downwards and
549 // then to the left.
550 // When no location was found, an empty rectangle will be returned.
551 for (int run = 0; run < 2; run++) {
552 if (run == 0) { // First run: Start left, parse right till mid screen.
553 x = 0;
554 x_increment = pop_position_offset_increment_x;
555 } else { // Second run: Start right, parse left till mid screen.
556 x = work_area.width() - w;
557 x_increment = -pop_position_offset_increment_x;
559 // Note: The passing (x,y,w,h) window is always relative to the work area's
560 // origin.
561 for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) {
562 int y = 0;
563 while (y + h <= work_area.height()) {
564 size_t i;
565 for (i = 0; i < regions.size(); i++) {
566 if (regions[i]->Intersects(gfx::Rect(x + work_area.x(),
567 y + work_area.y(), w, h))) {
568 y = regions[i]->bottom() - work_area.y();
569 break;
572 if (i >= regions.size())
573 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
577 return gfx::Rect(0, 0, 0, 0);
580 gfx::Rect WindowPositioner::AlignPopupPosition(
581 const gfx::Rect& pos,
582 const gfx::Rect& work_area,
583 int grid) {
584 if (grid <= 1)
585 return pos;
587 int x = pos.x() - (pos.x() - work_area.x()) % grid;
588 int y = pos.y() - (pos.y() - work_area.y()) % grid;
589 int w = pos.width();
590 int h = pos.height();
592 // If the alignment was pushing the window out of the screen, we ignore the
593 // alignment for that call.
594 if (abs(pos.right() - work_area.right()) < grid)
595 x = work_area.right() - w;
596 if (abs(pos.bottom() - work_area.bottom()) < grid)
597 y = work_area.bottom() - h;
598 return gfx::Rect(x, y, w, h);
601 } // namespace ash