1 // Copyright (c) 2012 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_util.h"
9 #include "ash/ash_constants.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/shell.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/wm/activation_controller.h"
14 #include "ash/wm/window_properties.h"
15 #include "ui/aura/client/activation_client.h"
16 #include "ui/aura/client/aura_constants.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_delegate.h"
20 #include "ui/compositor/layer.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/views/corewm/window_util.h"
25 #include "ui/views/view.h"
26 #include "ui/views/widget/widget.h"
31 // TODO(beng): replace many of these functions with the corewm versions.
32 void ActivateWindow(aura::Window
* window
) {
33 views::corewm::ActivateWindow(window
);
36 void DeactivateWindow(aura::Window
* window
) {
37 views::corewm::DeactivateWindow(window
);
40 bool IsActiveWindow(aura::Window
* window
) {
41 return views::corewm::IsActiveWindow(window
);
44 aura::Window
* GetActiveWindow() {
45 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
49 aura::Window
* GetActivatableWindow(aura::Window
* window
) {
50 return views::corewm::GetActivatableWindow(window
);
53 bool CanActivateWindow(aura::Window
* window
) {
54 return views::corewm::CanActivateWindow(window
);
57 bool CanMaximizeWindow(const aura::Window
* window
) {
58 return window
->GetProperty(aura::client::kCanMaximizeKey
);
61 bool CanMinimizeWindow(const aura::Window
* window
) {
62 internal::RootWindowController
* controller
=
63 internal::RootWindowController::ForWindow(window
);
66 aura::Window
* lockscreen
= controller
->GetContainer(
67 internal::kShellWindowId_LockScreenContainersContainer
);
68 if (lockscreen
->Contains(window
))
74 bool CanResizeWindow(const aura::Window
* window
) {
75 return window
->GetProperty(aura::client::kCanResizeKey
);
78 bool CanSnapWindow(aura::Window
* window
) {
79 if (!CanResizeWindow(window
))
81 // If a window has a maximum size defined, snapping may make it too big.
82 return window
->delegate() ? window
->delegate()->GetMaximumSize().IsEmpty() :
86 bool IsWindowNormal(const aura::Window
* window
) {
87 return IsWindowStateNormal(window
->GetProperty(aura::client::kShowStateKey
));
90 bool IsWindowStateNormal(ui::WindowShowState state
) {
91 return state
== ui::SHOW_STATE_NORMAL
|| state
== ui::SHOW_STATE_DEFAULT
;
94 bool IsWindowMaximized(const aura::Window
* window
) {
95 return window
->GetProperty(aura::client::kShowStateKey
) ==
96 ui::SHOW_STATE_MAXIMIZED
;
99 bool IsWindowMinimized(const aura::Window
* window
) {
100 return window
->GetProperty(aura::client::kShowStateKey
) ==
101 ui::SHOW_STATE_MINIMIZED
;
104 bool IsWindowFullscreen(const aura::Window
* window
) {
105 return window
->GetProperty(aura::client::kShowStateKey
) ==
106 ui::SHOW_STATE_FULLSCREEN
;
109 void MaximizeWindow(aura::Window
* window
) {
110 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
113 void MinimizeWindow(aura::Window
* window
) {
114 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
117 void RestoreWindow(aura::Window
* window
) {
118 window
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
121 void ToggleMaximizedWindow(aura::Window
* window
) {
122 if (ash::wm::IsWindowMaximized(window
))
123 ash::wm::RestoreWindow(window
);
124 else if (ash::wm::CanMaximizeWindow(window
))
125 ash::wm::MaximizeWindow(window
);
128 void CenterWindow(aura::Window
* window
) {
129 const gfx::Display display
=
130 Shell::GetScreen()->GetDisplayNearestWindow(window
);
131 gfx::Rect center
= display
.work_area();
132 center
.ClampToCenteredSize(window
->bounds().size());
133 window
->SetBoundsInScreen(center
, display
);
136 bool IsWindowPositionManaged(const aura::Window
* window
) {
137 return window
->GetProperty(ash::internal::kWindowPositionManagedKey
);
140 void SetWindowPositionManaged(aura::Window
* window
, bool managed
) {
141 window
->SetProperty(ash::internal::kWindowPositionManagedKey
, managed
);
144 bool HasUserChangedWindowPositionOrSize(const aura::Window
* window
) {
145 return window
->GetProperty(
146 ash::internal::kUserChangedWindowPositionOrSizeKey
);
149 void SetUserHasChangedWindowPositionOrSize(aura::Window
* window
, bool changed
) {
150 window
->SetProperty(ash::internal::kUserChangedWindowPositionOrSizeKey
,
154 const gfx::Rect
* GetPreAutoManageWindowBounds(const aura::Window
* window
) {
155 return window
->GetProperty(ash::internal::kPreAutoManagedWindowBoundsKey
);
158 void SetPreAutoManageWindowBounds(aura::Window
* window
,
159 const gfx::Rect
& bounds
) {
160 window
->SetProperty(ash::internal::kPreAutoManagedWindowBoundsKey
,
161 new gfx::Rect(bounds
));
164 void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect
& work_area
,
166 AdjustBoundsToEnsureWindowVisibility(
167 work_area
, kMinimumOnScreenArea
, kMinimumOnScreenArea
, bounds
);
170 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect
& work_area
,
174 bounds
->set_width(std::min(bounds
->width(), work_area
.width()));
175 bounds
->set_height(std::min(bounds
->height(), work_area
.height()));
176 if (!work_area
.Intersects(*bounds
)) {
178 if (work_area
.bottom() < bounds
->y()) {
179 y_offset
= work_area
.bottom() - bounds
->y() - min_height
;
180 } else if (bounds
->bottom() < work_area
.y()) {
181 y_offset
= work_area
.y() - bounds
->bottom() + min_height
;
185 if (work_area
.right() < bounds
->x()) {
186 x_offset
= work_area
.right() - bounds
->x() - min_width
;
187 } else if (bounds
->right() < work_area
.x()) {
188 x_offset
= work_area
.x() - bounds
->right() + min_width
;
190 bounds
->Offset(x_offset
, y_offset
);
194 bool MoveWindowToEventRoot(aura::Window
* window
, const ui::Event
& event
) {
195 views::View
* target
= static_cast<views::View
*>(event
.target());
198 aura::RootWindow
* target_root
=
199 target
->GetWidget()->GetNativeView()->GetRootWindow();
200 if (!target_root
|| target_root
== window
->GetRootWindow())
202 aura::Window
* window_container
=
203 ash::Shell::GetContainer(target_root
, window
->parent()->id());
204 // Move the window to the target launcher.
205 window_container
->AddChild(window
);