window_util.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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. 4 5#include "ash/wm/window_util.h" 6 7#include <vector> 8 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" 27 28namespace ash { 29namespace wm { 30 31// TODO(beng): replace many of these functions with the corewm versions. 32void ActivateWindow(aura::Window* window) { 33 views::corewm::ActivateWindow(window); 34} 35 36void DeactivateWindow(aura::Window* window) { 37 views::corewm::DeactivateWindow(window); 38} 39 40bool IsActiveWindow(aura::Window* window) { 41 return views::corewm::IsActiveWindow(window); 42} 43 44aura::Window* GetActiveWindow() { 45 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())-> 46 GetActiveWindow(); 47} 48 49aura::Window* GetActivatableWindow(aura::Window* window) { 50 return views::corewm::GetActivatableWindow(window); 51} 52 53bool CanActivateWindow(aura::Window* window) { 54 return views::corewm::CanActivateWindow(window); 55} 56 57bool CanMaximizeWindow(const aura::Window* window) { 58 return window->GetProperty(aura::client::kCanMaximizeKey); 59} 60 61bool CanMinimizeWindow(const aura::Window* window) { 62 internal::RootWindowController* controller = 63 internal::RootWindowController::ForWindow(window); 64 if (!controller) 65 return false; 66 aura::Window* lockscreen = controller->GetContainer( 67 internal::kShellWindowId_LockScreenContainersContainer); 68 if (lockscreen->Contains(window)) 69 return false; 70 71 return true; 72} 73 74bool CanResizeWindow(const aura::Window* window) { 75 return window->GetProperty(aura::client::kCanResizeKey); 76} 77 78bool CanSnapWindow(aura::Window* window) { 79 if (!CanResizeWindow(window)) 80 return false; 81 // If a window has a maximum size defined, snapping may make it too big. 82 return window->delegate() ? window->delegate()->GetMaximumSize().IsEmpty() : 83 true; 84} 85 86bool IsWindowNormal(const aura::Window* window) { 87 return IsWindowStateNormal(window->GetProperty(aura::client::kShowStateKey)); 88} 89 90bool IsWindowStateNormal(ui::WindowShowState state) { 91 return state == ui::SHOW_STATE_NORMAL || state == ui::SHOW_STATE_DEFAULT; 92} 93 94bool IsWindowMaximized(const aura::Window* window) { 95 return window->GetProperty(aura::client::kShowStateKey) == 96 ui::SHOW_STATE_MAXIMIZED; 97} 98 99bool IsWindowMinimized(const aura::Window* window) { 100 return window->GetProperty(aura::client::kShowStateKey) == 101 ui::SHOW_STATE_MINIMIZED; 102} 103 104bool IsWindowFullscreen(const aura::Window* window) { 105 return window->GetProperty(aura::client::kShowStateKey) == 106 ui::SHOW_STATE_FULLSCREEN; 107} 108 109void MaximizeWindow(aura::Window* window) { 110 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED); 111} 112 113void MinimizeWindow(aura::Window* window) { 114 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED); 115} 116 117void RestoreWindow(aura::Window* window) { 118 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL); 119} 120 121void 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); 126} 127 128void 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); 134} 135 136bool IsWindowPositionManaged(const aura::Window* window) { 137 return window->GetProperty(ash::internal::kWindowPositionManagedKey); 138} 139 140void SetWindowPositionManaged(aura::Window* window, bool managed) { 141 window->SetProperty(ash::internal::kWindowPositionManagedKey, managed); 142} 143 144void SetAnimateToFullscreen(aura::Window* window, bool animate) { 145 window->SetProperty(ash::internal::kAnimateToFullscreenKey, animate); 146} 147 148bool HasUserChangedWindowPositionOrSize(const aura::Window* window) { 149 return window->GetProperty( 150 ash::internal::kUserChangedWindowPositionOrSizeKey); 151} 152 153void SetUserHasChangedWindowPositionOrSize(aura::Window* window, bool changed) { 154 window->SetProperty(ash::internal::kUserChangedWindowPositionOrSizeKey, 155 changed); 156} 157 158const gfx::Rect* GetPreAutoManageWindowBounds(const aura::Window* window) { 159 return window->GetProperty(ash::internal::kPreAutoManagedWindowBoundsKey); 160} 161 162void SetPreAutoManageWindowBounds(aura::Window* window, 163 const gfx::Rect& bounds) { 164 window->SetProperty(ash::internal::kPreAutoManagedWindowBoundsKey, 165 new gfx::Rect(bounds)); 166} 167 168void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area, 169 gfx::Rect* bounds) { 170 AdjustBoundsToEnsureWindowVisibility( 171 visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds); 172} 173 174void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area, 175 int min_width, 176 int min_height, 177 gfx::Rect* bounds) { 178 bounds->set_width(std::min(bounds->width(), visible_area.width())); 179 bounds->set_height(std::min(bounds->height(), visible_area.height())); 180 181 min_width = std::min(min_width, visible_area.width()); 182 min_height = std::min(min_height, visible_area.height()); 183 184 if (bounds->x() + min_width > visible_area.right()) { 185 bounds->set_x(visible_area.right() - min_width); 186 } else if (bounds->right() - min_width < 0) { 187 bounds->set_x(min_width - bounds->width()); 188 } 189 if (bounds->y() + min_height > visible_area.bottom()) { 190 bounds->set_y(visible_area.bottom() - min_height); 191 } else if (bounds->bottom() - min_height < 0) { 192 bounds->set_y(min_height - bounds->height()); 193 } 194} 195 196bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) { 197 views::View* target = static_cast<views::View*>(event.target()); 198 if (!target) 199 return false; 200 aura::RootWindow* target_root = 201 target->GetWidget()->GetNativeView()->GetRootWindow(); 202 if (!target_root || target_root == window->GetRootWindow()) 203 return false; 204 aura::Window* window_container = 205 ash::Shell::GetContainer(target_root, window->parent()->id()); 206 // Move the window to the target launcher. 207 window_container->AddChild(window); 208 return true; 209} 210 211} // namespace wm 212} // namespace ash 213