14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// found in the LICENSE file.
44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/window_positioner.h"
64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/screen_util.h"
84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/shell.h"
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/shell_window_ids.h"
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/mru_window_tracker.h"
114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/window_resizer.h"
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/window_state.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/window_util.h"
144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/aura/window.h"
154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/aura/window_delegate.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/compositor/layer.h"
184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/compositor/scoped_layer_animation_settings.h"
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/gfx/screen.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/wm/core/window_animations.h"
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ui/wm/core/window_util.h"
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace ash {
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int WindowPositioner::kMinimumWindowOffset = 32;
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// The number of pixels which are kept free top, left and right when a window
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// gets positioned to its default location.
294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int WindowPositioner::kDesktopBorderSize = 16;
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Maximum width of a window even if there is more room on the desktop.
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int WindowPositioner::kMaximumWindowWidth = 1100;
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace {
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// When a window gets opened in default mode and the screen is less than or
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// equal to this width, the window will get opened in maximized mode. This value
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// can be reduced to a "tame" number if the feature is disabled.
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int kForceMaximizeWidthLimit = 1366;
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// The time in milliseconds which should be used to visually move a window
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// through an automatic "intelligent" window management option.
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int kWindowAutoMoveDurationMS = 125;
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// If set to true all window repositioning actions will be ignored. Set through
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// WindowPositioner::SetIgnoreActivations().
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)static bool disable_auto_positioning = false;
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// If set to true, by default the first window in ASH will be maximized.
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static bool maximize_first_window = false;
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Check if any management should be performed (with a given |window|).
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool UseAutoWindowManager(const aura::Window* window) {
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (disable_auto_positioning)
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return false;
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const wm::WindowState* window_state = wm::GetWindowState(window);
59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return !window_state->is_dragged() && window_state->window_position_managed();
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Check if a given |window| can be managed. This includes that it's state is
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// not minimized/maximized/the user has changed it's size by hand already.
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// It furthermore checks for the WindowIsManaged status.
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool WindowPositionCanBeManaged(const aura::Window* window) {
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (disable_auto_positioning)
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return false;
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const wm::WindowState* window_state = wm::GetWindowState(window);
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return window_state->window_position_managed() &&
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      !window_state->IsMinimized() &&
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      !window_state->IsMaximized() &&
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      !window_state->bounds_changed_by_user();
734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Get the work area for a given |window| in parent coordinates.
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect GetWorkAreaForWindowInParent(aura::Window* window) {
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#if defined(OS_WIN)
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // On Win 8, the host window can't be resized, so
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // use window's bounds instead.
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(oshima): Emulate host window resize on win8.
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect work_area = gfx::Rect(window->parent()->bounds().size());
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  work_area.Inset(Shell::GetScreen()->GetDisplayMatching(
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      window->parent()->GetBoundsInScreen()).GetWorkAreaInsets());
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return work_area;
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ScreenUtil::GetDisplayWorkAreaBoundsInParent(window);
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Move the given |bounds| on the available |work_area| in the direction
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// indicated by |move_right|. If |move_right| is true, the rectangle gets moved
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// to the right edge, otherwise to the left one.
934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool MoveRectToOneSide(const gfx::Rect& work_area,
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                       bool move_right,
954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                       gfx::Rect* bounds) {
964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (move_right) {
974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (work_area.right() > bounds->right()) {
984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      bounds->set_x(work_area.right() - bounds->width());
994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return true;
1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (work_area.x() < bounds->x()) {
1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      bounds->set_x(work_area.x());
1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return true;
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return false;
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Move a |window| to new |bounds|. Animate if desired by user.
111a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Moves the transient children of the |window| as well by the same |offset| as
112a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// the parent |window|.
113a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid SetBoundsAndOffsetTransientChildren(aura::Window* window,
114a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                         const gfx::Rect& bounds,
115a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                         const gfx::Rect& work_area,
116a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                         const gfx::Vector2d& offset) {
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  aura::Window::Windows transient_children =
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ::wm::GetTransientChildren(window);
119a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  for (aura::Window::Windows::iterator iter = transient_children.begin();
120a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      iter != transient_children.end(); ++iter) {
121a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::Window* transient_child = *iter;
122a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    gfx::Rect child_bounds = transient_child->bounds();
123a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    gfx::Rect new_child_bounds = child_bounds + offset;
124a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if ((child_bounds.x() <= work_area.x() &&
125a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch         new_child_bounds.x() <= work_area.x()) ||
126a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        (child_bounds.right() >= work_area.right() &&
127a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch         new_child_bounds.right() >= work_area.right())) {
128a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      continue;
129a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    }
130a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (new_child_bounds.right() > work_area.right())
131a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new_child_bounds.set_x(work_area.right() - bounds.width());
132a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    else if (new_child_bounds.x() < work_area.x())
133a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new_child_bounds.set_x(work_area.x());
134a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    SetBoundsAndOffsetTransientChildren(transient_child,
135a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                        new_child_bounds, work_area, offset);
136a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
1374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (::wm::WindowAnimationsDisabled(window)) {
1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    window->SetBounds(bounds);
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  settings.SetTransitionDuration(
1454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  window->SetBounds(bounds);
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
149a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Move a |window| to new |bounds|. Animate if desired by user.
150a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Note: The function will do nothing if the bounds did not change.
151a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid SetBoundsAnimated(aura::Window* window,
152a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const gfx::Rect& bounds,
153a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const gfx::Rect& work_area) {
154a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::Rect old_bounds = window->GetTargetBounds();
155a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (bounds == old_bounds)
156a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return;
157a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::Vector2d offset(bounds.origin() - old_bounds.origin());
158a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  SetBoundsAndOffsetTransientChildren(window, bounds, work_area, offset);
159a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
160a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Move |window| into the center of the screen - or restore it to the previous
1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// position.
1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutoPlaceSingleWindow(aura::Window* window, bool animated) {
1644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect work_area = GetWorkAreaForWindowInParent(window);
1654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect bounds = window->bounds();
1664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const gfx::Rect* user_defined_area =
1674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      wm::GetWindowState(window)->pre_auto_manage_window_bounds();
1684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (user_defined_area) {
1694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bounds = *user_defined_area;
1704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &bounds);
1714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
1724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Center the window (only in x).
1734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bounds.set_x(work_area.x() + (work_area.width() - bounds.width()) / 2);
1744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (animated)
177a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    SetBoundsAnimated(window, bounds, work_area);
1784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  else
1794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    window->SetBounds(bounds);
1804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Get the first open (non minimized) window which is on the screen defined.
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)aura::Window* GetReferenceWindow(const aura::Window* root_window,
1844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 const aura::Window* exclude,
1854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 bool *single_window) {
1864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (single_window)
1874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    *single_window = true;
1884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Get the active window.
1894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* active = ash::wm::GetActiveWindow();
1904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (active && active->GetRootWindow() != root_window)
1914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    active = NULL;
1924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Get a list of all windows.
1944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const std::vector<aura::Window*> windows =
1954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ash::MruWindowTracker::BuildWindowList(false);
1964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (windows.empty())
1984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return NULL;
1994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window::Windows::const_iterator iter = windows.begin();
2014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Find the index of the current active window.
2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (active)
2034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    iter = std::find(windows.begin(), windows.end(), active);
2044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int index = (iter == windows.end()) ? 0 : (iter - windows.begin());
2064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Scan the cycle list backwards to see which is the second topmost window
2084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // (and so on). Note that we might cycle a few indices twice if there is no
2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // suitable window. However - since the list is fairly small this should be
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // very fast anyways.
2114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* found = NULL;
2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  for (int i = index + windows.size(); i >= 0; i--) {
2134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    aura::Window* window = windows[i % windows.size()];
214a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    while (::wm::GetTransientParent(window))
215a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      window = ::wm::GetTransientParent(window);
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (window != exclude && window->type() == ui::wm::WINDOW_TYPE_NORMAL &&
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        window->GetRootWindow() == root_window && window->TargetVisibility() &&
2184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        wm::GetWindowState(window)->window_position_managed()) {
2194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (found && found != window) {
2200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // no need to check !single_window because the function must have
2214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        // been already returned in the "if (!single_window)" below.
2224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        *single_window = false;
2234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        return found;
2244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      }
2254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      found = window;
2264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // If there is no need to check single window, return now.
2274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (!single_window)
2284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        return found;
2294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
2304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return found;
2324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // namespace
2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
2374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)int WindowPositioner::GetForceMaximizedWidthLimit() {
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return kForceMaximizeWidthLimit;
2394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
2424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void WindowPositioner::GetBoundsAndShowStateForNewWindow(
2434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Screen* screen,
2444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const aura::Window* new_window,
2454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool is_saved_bounds,
2464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ui::WindowShowState show_state_in,
2474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    gfx::Rect* bounds_in_out,
2484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ui::WindowShowState* show_state_out) {
2494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Always open new window in the target display.
2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  aura::Window* target = Shell::GetTargetRootWindow();
2524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* top_window = GetReferenceWindow(target, NULL, NULL);
2544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Our window should not have any impact if we are already on top.
2554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (top_window == new_window)
2564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    top_window = NULL;
2574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If there is no valid other window we take and adjust the passed coordinates
2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // and show state.
2604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!top_window) {
2614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
2624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bounds_in_out->AdjustToFit(work_area);
2641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Use adjusted saved bounds, if there is one.
2651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (is_saved_bounds)
2664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return;
2674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // When using "small screens" we want to always open in full screen mode.
268f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (show_state_in == ui::SHOW_STATE_DEFAULT && (maximize_first_window ||
269f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         (work_area.width() <= GetForceMaximizedWidthLimit() &&
270f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         (!new_window || !wm::GetWindowState(new_window)->IsFullscreen())))) {
2714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      *show_state_out = ui::SHOW_STATE_MAXIMIZED;
2724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
2734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
2744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  wm::WindowState* top_window_state = wm::GetWindowState(top_window);
2760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  bool maximized = top_window_state->IsMaximized();
2774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We ignore the saved show state, but look instead for the top level
2784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // window's show state.
2794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (show_state_in == ui::SHOW_STATE_DEFAULT) {
2804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    *show_state_out = maximized ? ui::SHOW_STATE_MAXIMIZED :
2814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        ui::SHOW_STATE_DEFAULT;
2824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (maximized) {
2850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    bool has_restore_bounds = top_window_state->HasRestoreBounds();
2860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (has_restore_bounds) {
2870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // For a maximized window ignore the real bounds of the top level window
2880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // and use its restore bounds instead. Offset the bounds to prevent the
2890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // windows from overlapping exactly when restored.
2900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      *bounds_in_out = top_window_state->GetRestoreBoundsInScreen() +
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          gfx::Vector2d(kMinimumWindowOffset, kMinimumWindowOffset);
2920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (is_saved_bounds || has_restore_bounds) {
2940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
2950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      bounds_in_out->AdjustToFit(work_area);
2960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // Use adjusted saved bounds or restore bounds, if there is one.
2970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
2980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
3000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Use the size of the other window. The window's bound will be rearranged
3024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // in ash::WorkspaceLayoutManager using this location.
3034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  *bounds_in_out = top_window->GetBoundsInScreen();
3044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
3074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
3084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const aura::Window* removed_window) {
3094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!UseAutoWindowManager(removed_window))
3104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Find a single open browser window.
3124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool single_window;
3134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* other_shown_window = GetReferenceWindow(
3144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      removed_window->GetRootWindow(), removed_window, &single_window);
3154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!other_shown_window || !single_window ||
3164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      !WindowPositionCanBeManaged(other_shown_window))
3174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AutoPlaceSingleWindow(other_shown_window, true);
3194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
3221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool WindowPositioner::DisableAutoPositioning(bool ignore) {
3231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool old_state = disable_auto_positioning;
3241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  disable_auto_positioning = ignore;
3251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return old_state;
3261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static
3294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void WindowPositioner::RearrangeVisibleWindowOnShow(
3304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    aura::Window* added_window) {
3314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  wm::WindowState* added_window_state = wm::GetWindowState(added_window);
3320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!added_window->TargetVisibility())
3330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
3344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!UseAutoWindowManager(added_window) ||
3360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      added_window_state->bounds_changed_by_user()) {
3370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (added_window_state->minimum_visibility()) {
338a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      // Guarantee minimum visibility within the work area.
3390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
3400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      gfx::Rect bounds = added_window->bounds();
3410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      gfx::Rect new_bounds = bounds;
3420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area,
3430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                                           &new_bounds);
3440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      if (new_bounds != bounds)
3450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        added_window->SetBounds(new_bounds);
3460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
3494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Find a single open managed window.
3504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool single_window;
3514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* other_shown_window = GetReferenceWindow(
3524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      added_window->GetRootWindow(), added_window, &single_window);
3534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!other_shown_window) {
3554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // It could be that this window is the first window joining the workspace.
3564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!WindowPositionCanBeManaged(added_window) || other_shown_window)
3574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return;
3584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Since we might be going from 0 to 1 window, we have to arrange the new
3594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // window to a good default.
3604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutoPlaceSingleWindow(added_window, false);
3614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
3634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect other_bounds = other_shown_window->bounds();
3654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
3664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool move_other_right =
3674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      other_bounds.CenterPoint().x() > work_area.x() + work_area.width() / 2;
3684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Push the other window to the size only if there are two windows left.
3704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (single_window) {
3714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // When going from one to two windows both windows loose their
3724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // "positioned by user" flags.
3734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    added_window_state->set_bounds_changed_by_user(false);
3744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    wm::WindowState* other_window_state =
3754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        wm::GetWindowState(other_shown_window);
3764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    other_window_state->set_bounds_changed_by_user(false);
3774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (WindowPositionCanBeManaged(other_shown_window)) {
3794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Don't override pre auto managed bounds as the current bounds
3804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // may not be original.
3814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (!other_window_state->pre_auto_manage_window_bounds())
3824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        other_window_state->SetPreAutoManageWindowBounds(other_bounds);
3834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Push away the other window after remembering its current position.
3854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (MoveRectToOneSide(work_area, move_other_right, &other_bounds))
386a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        SetBoundsAnimated(other_shown_window, other_bounds, work_area);
3874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
3884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
3894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Remember the current location of the window if it's new and push
3914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // it also to the opposite location if needed.  Since it is just
3924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // being shown, we do not need to animate it.
3934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Rect added_bounds = added_window->bounds();
3944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!added_window_state->pre_auto_manage_window_bounds())
3954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    added_window_state->SetPreAutoManageWindowBounds(added_bounds);
3964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (MoveRectToOneSide(work_area, !move_other_right, &added_bounds))
3974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    added_window->SetBounds(added_bounds);
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)WindowPositioner::WindowPositioner()
4014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    : pop_position_offset_increment_x(0),
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      pop_position_offset_increment_y(0),
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      popup_position_offset_from_screen_corner_x(0),
4044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      popup_position_offset_from_screen_corner_y(0),
4054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      last_popup_position_x_(0),
4064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      last_popup_position_y_(0) {
4074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)WindowPositioner::~WindowPositioner() {
4104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect WindowPositioner::GetDefaultWindowBounds(
4134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Display& display) {
4144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const gfx::Rect work_area = display.work_area();
4154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // There should be a 'desktop' border around the window at the left and right
4164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // side.
4174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int default_width = work_area.width() - 2 * kDesktopBorderSize;
4184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // There should also be a 'desktop' border around the window at the top.
4194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Since the workspace excludes the tray area we only need one border size.
4204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int default_height = work_area.height() - kDesktopBorderSize;
4214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int offset_x = kDesktopBorderSize;
4224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (default_width > kMaximumWindowWidth) {
4234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // The window should get centered on the screen and not follow the grid.
4244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    offset_x = (work_area.width() - kMaximumWindowWidth) / 2;
4254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    default_width = kMaximumWindowWidth;
4264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return gfx::Rect(work_area.x() + offset_x,
4284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                   work_area.y() + kDesktopBorderSize,
4294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                   default_width,
4304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                   default_height);
4314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect WindowPositioner::GetPopupPosition(const gfx::Rect& old_pos) {
4344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int grid = kMinimumWindowOffset;
4354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  popup_position_offset_from_screen_corner_x = grid;
4364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  popup_position_offset_from_screen_corner_y = grid;
4374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!pop_position_offset_increment_x) {
4380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // When the popup position increment is 0, the last popup position
4394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // was not yet initialized.
4404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
4414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
4424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  pop_position_offset_increment_x = grid;
4444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  pop_position_offset_increment_y = grid;
4454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We handle the Multi monitor support by retrieving the active window's
4464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // work area.
4474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  aura::Window* window = wm::GetActiveWindow();
4484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const gfx::Rect work_area = window && window->IsVisible() ?
4494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      Shell::GetScreen()->GetDisplayNearestWindow(window).work_area() :
4504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      Shell::GetScreen()->GetPrimaryDisplay().work_area();
4514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Only try to reposition the popup when it is not spanning the entire
4524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // screen.
4534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if ((old_pos.width() + popup_position_offset_from_screen_corner_x >=
4544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      work_area.width()) ||
4554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      (old_pos.height() + popup_position_offset_from_screen_corner_y >=
4564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)       work_area.height()))
4574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return AlignPopupPosition(old_pos, work_area, grid);
4584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid);
4594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!result.IsEmpty())
4604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return AlignPopupPosition(result, work_area, grid);
4614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return NormalPopupPosition(old_pos, work_area);
4624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
464f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
465f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void WindowPositioner::SetMaximizeFirstWindow(bool maximize) {
466f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  maximize_first_window = maximize;
467f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
4694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect WindowPositioner::NormalPopupPosition(
4704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& old_pos,
4714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& work_area) {
4724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int w = old_pos.width();
4734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int h = old_pos.height();
4744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Note: The 'last_popup_position' is checked and kept relative to the
4754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // screen size. The offsetting will be done in the last step when the
4764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // target rectangle gets returned.
4774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool reset = false;
4784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (last_popup_position_y_ + h > work_area.height() ||
4794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      last_popup_position_x_ + w > work_area.width()) {
4804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Popup does not fit on screen. Reset to next diagonal row.
4814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_x_ -= last_popup_position_y_ -
4824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                              popup_position_offset_from_screen_corner_x -
4834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                              pop_position_offset_increment_x;
4844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
4854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    reset = true;
4864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (last_popup_position_x_ + w > work_area.width()) {
4884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Start again over.
4894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
4904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
4914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    reset = true;
4924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int x = last_popup_position_x_;
4944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int y = last_popup_position_y_;
4954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!reset) {
4964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_x_ += pop_position_offset_increment_x;
4974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_popup_position_y_ += pop_position_offset_increment_y;
4984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
5004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
5014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect WindowPositioner::SmartPopupPosition(
5034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& old_pos,
5044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& work_area,
5054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int grid) {
5064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const std::vector<aura::Window*> windows =
5074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      MruWindowTracker::BuildWindowList(false);
5084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::vector<const gfx::Rect*> regions;
5104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Process the window list and check if we can bail immediately.
5114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  for (size_t i = 0; i < windows.size(); i++) {
5124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // We only include opaque and visible windows.
5134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() &&
5144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        (!windows[i]->transparent() ||
5154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)         windows[i]->layer()->GetTargetOpacity() == 1.0)) {
5164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      wm::WindowState* window_state = wm::GetWindowState(windows[i]);
5174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // When any window is maximized we cannot find any free space.
5184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (window_state->IsMaximizedOrFullscreen())
5194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        return gfx::Rect(0, 0, 0, 0);
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (window_state->IsNormalOrSnapped())
5214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        regions.push_back(&windows[i]->bounds());
5224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
5234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
5244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (regions.empty())
5264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return gfx::Rect(0, 0, 0, 0);
5274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int w = old_pos.width();
5294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int h = old_pos.height();
5304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int x_end = work_area.width() / 2;
5314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int x, x_increment;
5324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We parse for a proper location on the screen. We do this in two runs:
5334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The first run will start from the left, parsing down, skipping any
5344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // overlapping windows it will encounter until the popup's height can not
5354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // be served anymore. Then the next grid position to the right will be
5364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // taken, and the same cycle starts again. This will be repeated until we
5374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // hit the middle of the screen (or we find a suitable location).
5384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // In the second run we parse beginning from the right corner downwards and
5394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // then to the left.
5404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // When no location was found, an empty rectangle will be returned.
5414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  for (int run = 0; run < 2; run++) {
5424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (run == 0) { // First run: Start left, parse right till mid screen.
5434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      x = 0;
5444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      x_increment = pop_position_offset_increment_x;
5454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    } else { // Second run: Start right, parse left till mid screen.
5464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      x = work_area.width() - w;
5474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      x_increment = -pop_position_offset_increment_x;
5484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
5494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Note: The passing (x,y,w,h) window is always relative to the work area's
5504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // origin.
5514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) {
5524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      int y = 0;
5534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      while (y + h <= work_area.height()) {
5544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        size_t i;
5554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        for (i = 0; i < regions.size(); i++) {
5564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          if (regions[i]->Intersects(gfx::Rect(x + work_area.x(),
5574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                               y + work_area.y(), w, h))) {
5584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            y = regions[i]->bottom() - work_area.y();
5594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            break;
5604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          }
5614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        }
5624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        if (i >= regions.size())
5634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
5644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      }
5654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
5664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
5674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return gfx::Rect(0, 0, 0, 0);
5684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
5694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Rect WindowPositioner::AlignPopupPosition(
5714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& pos,
5724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const gfx::Rect& work_area,
5734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int grid) {
5744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (grid <= 1)
5754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return pos;
5764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int x = pos.x() - (pos.x() - work_area.x()) % grid;
5784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int y = pos.y() - (pos.y() - work_area.y()) % grid;
5794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int w = pos.width();
5804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int h = pos.height();
5814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // If the alignment was pushing the window out of the screen, we ignore the
5834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // alignment for that call.
5844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (abs(pos.right() - work_area.right()) < grid)
5854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    x = work_area.right() - w;
5864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (abs(pos.bottom() - work_area.bottom()) < grid)
5874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    y = work_area.bottom() - h;
5884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return gfx::Rect(x, y, w, h);
5894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
5904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // namespace ash
592