This source file includes following definitions.
- UseAutoWindowManager
- WindowPositionCanBeManaged
- GetWorkAreaForWindowInParent
- MoveRectToOneSide
- SetBoundsAnimated
- AutoPlaceSingleWindow
- GetReferenceWindow
- GetForceMaximizedWidthLimit
- GetBoundsAndShowStateForNewWindow
- RearrangeVisibleWindowOnHideOrRemove
- DisableAutoPositioning
- RearrangeVisibleWindowOnShow
- last_popup_position_y_
- GetDefaultWindowBounds
- GetPopupPosition
- SetMaximizeFirstWindow
- NormalPopupPosition
- SmartPopupPosition
- AlignPopupPosition
#include "ash/wm/window_positioner.h"
#include "ash/ash_switches.h"
#include "ash/screen_util.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/command_line.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/screen.h"
#include "ui/wm/core/window_animations.h"
namespace ash {
const int WindowPositioner::kMinimumWindowOffset = 32;
const int WindowPositioner::kDesktopBorderSize = 16;
const int WindowPositioner::kMaximumWindowWidth = 1100;
namespace {
const int kForceMaximizeWidthLimit = 1366;
const int kWindowAutoMoveDurationMS = 125;
static bool disable_auto_positioning = false;
static bool maximize_first_window = false;
bool UseAutoWindowManager(const aura::Window* window) {
if (disable_auto_positioning)
return false;
const wm::WindowState* window_state = wm::GetWindowState(window);
return !window_state->is_dragged() && window_state->window_position_managed();
}
bool WindowPositionCanBeManaged(const aura::Window* window) {
if (disable_auto_positioning)
return false;
const wm::WindowState* window_state = wm::GetWindowState(window);
return window_state->window_position_managed() &&
!window_state->IsMinimized() &&
!window_state->IsMaximized() &&
!window_state->bounds_changed_by_user();
}
gfx::Rect GetWorkAreaForWindowInParent(aura::Window* window) {
#if defined(OS_WIN)
gfx::Rect work_area = gfx::Rect(window->parent()->bounds().size());
work_area.Inset(Shell::GetScreen()->GetDisplayMatching(
window->parent()->GetBoundsInScreen()).GetWorkAreaInsets());
return work_area;
#else
return ScreenUtil::GetDisplayWorkAreaBoundsInParent(window);
#endif
}
bool MoveRectToOneSide(const gfx::Rect& work_area,
bool move_right,
gfx::Rect* bounds) {
if (move_right) {
if (work_area.right() > bounds->right()) {
bounds->set_x(work_area.right() - bounds->width());
return true;
}
} else {
if (work_area.x() < bounds->x()) {
bounds->set_x(work_area.x());
return true;
}
}
return false;
}
void SetBoundsAnimated(aura::Window* window, const gfx::Rect& bounds) {
if (bounds == window->GetTargetBounds())
return;
if (::wm::WindowAnimationsDisabled(window)) {
window->SetBounds(bounds);
return;
}
ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
window->SetBounds(bounds);
}
void AutoPlaceSingleWindow(aura::Window* window, bool animated) {
gfx::Rect work_area = GetWorkAreaForWindowInParent(window);
gfx::Rect bounds = window->bounds();
const gfx::Rect* user_defined_area =
wm::GetWindowState(window)->pre_auto_manage_window_bounds();
if (user_defined_area) {
bounds = *user_defined_area;
ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &bounds);
} else {
bounds.set_x(work_area.x() + (work_area.width() - bounds.width()) / 2);
}
if (animated)
SetBoundsAnimated(window, bounds);
else
window->SetBounds(bounds);
}
aura::Window* GetReferenceWindow(const aura::Window* root_window,
const aura::Window* exclude,
bool *single_window) {
if (single_window)
*single_window = true;
aura::Window* active = ash::wm::GetActiveWindow();
if (active && active->GetRootWindow() != root_window)
active = NULL;
const std::vector<aura::Window*> windows =
ash::MruWindowTracker::BuildWindowList(false);
if (windows.empty())
return NULL;
aura::Window::Windows::const_iterator iter = windows.begin();
if (active)
iter = std::find(windows.begin(), windows.end(), active);
int index = (iter == windows.end()) ? 0 : (iter - windows.begin());
aura::Window* found = NULL;
for (int i = index + windows.size(); i >= 0; i--) {
aura::Window* window = windows[i % windows.size()];
if (window != exclude && window->type() == ui::wm::WINDOW_TYPE_NORMAL &&
window->GetRootWindow() == root_window && window->TargetVisibility() &&
wm::GetWindowState(window)->window_position_managed()) {
if (found && found != window) {
*single_window = false;
return found;
}
found = window;
if (!single_window)
return found;
}
}
return found;
}
}
int WindowPositioner::GetForceMaximizedWidthLimit() {
return kForceMaximizeWidthLimit;
}
void WindowPositioner::GetBoundsAndShowStateForNewWindow(
const gfx::Screen* screen,
const aura::Window* new_window,
bool is_saved_bounds,
ui::WindowShowState show_state_in,
gfx::Rect* bounds_in_out,
ui::WindowShowState* show_state_out) {
aura::Window* target = Shell::GetTargetRootWindow();
aura::Window* top_window = GetReferenceWindow(target, NULL, NULL);
if (top_window == new_window)
top_window = NULL;
if (!top_window) {
gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
bounds_in_out->AdjustToFit(work_area);
if (is_saved_bounds)
return;
if (show_state_in == ui::SHOW_STATE_DEFAULT && (maximize_first_window ||
(work_area.width() <= GetForceMaximizedWidthLimit() &&
(!new_window || !wm::GetWindowState(new_window)->IsFullscreen())))) {
*show_state_out = ui::SHOW_STATE_MAXIMIZED;
}
return;
}
bool maximized = wm::GetWindowState(top_window)->IsMaximized();
if (show_state_in == ui::SHOW_STATE_DEFAULT) {
*show_state_out = maximized ? ui::SHOW_STATE_MAXIMIZED :
ui::SHOW_STATE_DEFAULT;
}
*bounds_in_out = top_window->GetBoundsInScreen();
}
void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
const aura::Window* removed_window) {
if (!UseAutoWindowManager(removed_window))
return;
bool single_window;
aura::Window* other_shown_window = GetReferenceWindow(
removed_window->GetRootWindow(), removed_window, &single_window);
if (!other_shown_window || !single_window ||
!WindowPositionCanBeManaged(other_shown_window))
return;
AutoPlaceSingleWindow(other_shown_window, true);
}
bool WindowPositioner::DisableAutoPositioning(bool ignore) {
bool old_state = disable_auto_positioning;
disable_auto_positioning = ignore;
return old_state;
}
void WindowPositioner::RearrangeVisibleWindowOnShow(
aura::Window* added_window) {
wm::WindowState* added_window_state = wm::GetWindowState(added_window);
if (!added_window->TargetVisibility())
return;
if (!UseAutoWindowManager(added_window) ||
added_window_state->bounds_changed_by_user()) {
if (added_window_state->minimum_visibility()) {
gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
gfx::Rect bounds = added_window->bounds();
gfx::Rect new_bounds = bounds;
ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area,
&new_bounds);
if (new_bounds != bounds)
added_window->SetBounds(new_bounds);
}
return;
}
bool single_window;
aura::Window* other_shown_window = GetReferenceWindow(
added_window->GetRootWindow(), added_window, &single_window);
if (!other_shown_window) {
if (!WindowPositionCanBeManaged(added_window) || other_shown_window)
return;
AutoPlaceSingleWindow(added_window, false);
return;
}
gfx::Rect other_bounds = other_shown_window->bounds();
gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
bool move_other_right =
other_bounds.CenterPoint().x() > work_area.x() + work_area.width() / 2;
if (single_window) {
added_window_state->set_bounds_changed_by_user(false);
wm::WindowState* other_window_state =
wm::GetWindowState(other_shown_window);
other_window_state->set_bounds_changed_by_user(false);
if (WindowPositionCanBeManaged(other_shown_window)) {
if (!other_window_state->pre_auto_manage_window_bounds())
other_window_state->SetPreAutoManageWindowBounds(other_bounds);
if (MoveRectToOneSide(work_area, move_other_right, &other_bounds))
SetBoundsAnimated(other_shown_window, other_bounds);
}
}
gfx::Rect added_bounds = added_window->bounds();
if (!added_window_state->pre_auto_manage_window_bounds())
added_window_state->SetPreAutoManageWindowBounds(added_bounds);
if (MoveRectToOneSide(work_area, !move_other_right, &added_bounds))
added_window->SetBounds(added_bounds);
}
WindowPositioner::WindowPositioner()
: pop_position_offset_increment_x(0),
pop_position_offset_increment_y(0),
popup_position_offset_from_screen_corner_x(0),
popup_position_offset_from_screen_corner_y(0),
last_popup_position_x_(0),
last_popup_position_y_(0) {
}
WindowPositioner::~WindowPositioner() {
}
gfx::Rect WindowPositioner::GetDefaultWindowBounds(
const gfx::Display& display) {
const gfx::Rect work_area = display.work_area();
int default_width = work_area.width() - 2 * kDesktopBorderSize;
int default_height = work_area.height() - kDesktopBorderSize;
int offset_x = kDesktopBorderSize;
if (default_width > kMaximumWindowWidth) {
offset_x = (work_area.width() - kMaximumWindowWidth) / 2;
default_width = kMaximumWindowWidth;
}
return gfx::Rect(work_area.x() + offset_x,
work_area.y() + kDesktopBorderSize,
default_width,
default_height);
}
gfx::Rect WindowPositioner::GetPopupPosition(const gfx::Rect& old_pos) {
int grid = kMinimumWindowOffset;
popup_position_offset_from_screen_corner_x = grid;
popup_position_offset_from_screen_corner_y = grid;
if (!pop_position_offset_increment_x) {
last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
}
pop_position_offset_increment_x = grid;
pop_position_offset_increment_y = grid;
aura::Window* window = wm::GetActiveWindow();
const gfx::Rect work_area = window && window->IsVisible() ?
Shell::GetScreen()->GetDisplayNearestWindow(window).work_area() :
Shell::GetScreen()->GetPrimaryDisplay().work_area();
if ((old_pos.width() + popup_position_offset_from_screen_corner_x >=
work_area.width()) ||
(old_pos.height() + popup_position_offset_from_screen_corner_y >=
work_area.height()))
return AlignPopupPosition(old_pos, work_area, grid);
const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid);
if (!result.IsEmpty())
return AlignPopupPosition(result, work_area, grid);
return NormalPopupPosition(old_pos, work_area);
}
void WindowPositioner::SetMaximizeFirstWindow(bool maximize) {
maximize_first_window = maximize;
}
gfx::Rect WindowPositioner::NormalPopupPosition(
const gfx::Rect& old_pos,
const gfx::Rect& work_area) {
int w = old_pos.width();
int h = old_pos.height();
bool reset = false;
if (last_popup_position_y_ + h > work_area.height() ||
last_popup_position_x_ + w > work_area.width()) {
last_popup_position_x_ -= last_popup_position_y_ -
popup_position_offset_from_screen_corner_x -
pop_position_offset_increment_x;
last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
reset = true;
}
if (last_popup_position_x_ + w > work_area.width()) {
last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
reset = true;
}
int x = last_popup_position_x_;
int y = last_popup_position_y_;
if (!reset) {
last_popup_position_x_ += pop_position_offset_increment_x;
last_popup_position_y_ += pop_position_offset_increment_y;
}
return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
}
gfx::Rect WindowPositioner::SmartPopupPosition(
const gfx::Rect& old_pos,
const gfx::Rect& work_area,
int grid) {
const std::vector<aura::Window*> windows =
MruWindowTracker::BuildWindowList(false);
std::vector<const gfx::Rect*> regions;
for (size_t i = 0; i < windows.size(); i++) {
if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() &&
(!windows[i]->transparent() ||
windows[i]->layer()->GetTargetOpacity() == 1.0)) {
wm::WindowState* window_state = wm::GetWindowState(windows[i]);
if (window_state->IsMaximizedOrFullscreen())
return gfx::Rect(0, 0, 0, 0);
if (window_state->IsNormalOrSnapped())
regions.push_back(&windows[i]->bounds());
}
}
if (regions.empty())
return gfx::Rect(0, 0, 0, 0);
int w = old_pos.width();
int h = old_pos.height();
int x_end = work_area.width() / 2;
int x, x_increment;
for (int run = 0; run < 2; run++) {
if (run == 0) {
x = 0;
x_increment = pop_position_offset_increment_x;
} else {
x = work_area.width() - w;
x_increment = -pop_position_offset_increment_x;
}
for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) {
int y = 0;
while (y + h <= work_area.height()) {
size_t i;
for (i = 0; i < regions.size(); i++) {
if (regions[i]->Intersects(gfx::Rect(x + work_area.x(),
y + work_area.y(), w, h))) {
y = regions[i]->bottom() - work_area.y();
break;
}
}
if (i >= regions.size())
return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
}
}
}
return gfx::Rect(0, 0, 0, 0);
}
gfx::Rect WindowPositioner::AlignPopupPosition(
const gfx::Rect& pos,
const gfx::Rect& work_area,
int grid) {
if (grid <= 1)
return pos;
int x = pos.x() - (pos.x() - work_area.x()) % grid;
int y = pos.y() - (pos.y() - work_area.y()) % grid;
int w = pos.width();
int h = pos.height();
if (abs(pos.right() - work_area.right()) < grid)
x = work_area.right() - w;
if (abs(pos.bottom() - work_area.bottom()) < grid)
y = work_area.bottom() - h;
return gfx::Rect(x, y, w, h);
}
}