window_util.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/screen_util.h" 11#include "ash/shell.h" 12#include "ash/wm/window_properties.h" 13#include "ash/wm/window_state.h" 14#include "ui/aura/client/activation_client.h" 15#include "ui/aura/client/aura_constants.h" 16#include "ui/aura/root_window.h" 17#include "ui/aura/window.h" 18#include "ui/gfx/display.h" 19#include "ui/gfx/rect.h" 20#include "ui/gfx/screen.h" 21#include "ui/gfx/size.h" 22#include "ui/views/corewm/window_util.h" 23#include "ui/views/view.h" 24#include "ui/views/widget/widget.h" 25 26namespace ash { 27namespace wm { 28 29// TODO(beng): replace many of these functions with the corewm versions. 30void ActivateWindow(aura::Window* window) { 31 views::corewm::ActivateWindow(window); 32} 33 34void DeactivateWindow(aura::Window* window) { 35 views::corewm::DeactivateWindow(window); 36} 37 38bool IsActiveWindow(aura::Window* window) { 39 return views::corewm::IsActiveWindow(window); 40} 41 42aura::Window* GetActiveWindow() { 43 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())-> 44 GetActiveWindow(); 45} 46 47aura::Window* GetActivatableWindow(aura::Window* window) { 48 return views::corewm::GetActivatableWindow(window); 49} 50 51bool CanActivateWindow(aura::Window* window) { 52 return views::corewm::CanActivateWindow(window); 53} 54 55bool IsWindowMinimized(aura::Window* window) { 56 return ash::wm::GetWindowState(window)->IsMinimized(); 57} 58 59void CenterWindow(aura::Window* window) { 60 wm::WindowState* window_state = wm::GetWindowState(window); 61 if (!window_state->IsNormalShowState()) 62 return; 63 const gfx::Display display = 64 Shell::GetScreen()->GetDisplayNearestWindow(window); 65 gfx::Rect center = display.work_area(); 66 gfx::Size size = window->bounds().size(); 67 if (window_state->IsSnapped()) { 68 if (window_state->HasRestoreBounds()) 69 size = window_state->GetRestoreBoundsInScreen().size(); 70 center.ClampToCenteredSize(size); 71 window_state->SetRestoreBoundsInScreen(center); 72 window_state->Restore(); 73 } else { 74 center = ScreenUtil::ConvertRectFromScreen(window->parent(), 75 center); 76 center.ClampToCenteredSize(size); 77 window->SetBounds(center); 78 } 79} 80 81void AdjustBoundsSmallerThan(const gfx::Size& max_size, gfx::Rect* bounds) { 82 bounds->set_width(std::min(bounds->width(), max_size.width())); 83 bounds->set_height(std::min(bounds->height(), max_size.height())); 84} 85 86void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area, 87 gfx::Rect* bounds) { 88 AdjustBoundsToEnsureWindowVisibility( 89 visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds); 90} 91 92void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area, 93 int min_width, 94 int min_height, 95 gfx::Rect* bounds) { 96 AdjustBoundsSmallerThan(visible_area.size(), bounds); 97 98 min_width = std::min(min_width, visible_area.width()); 99 min_height = std::min(min_height, visible_area.height()); 100 101 if (bounds->right() < visible_area.x() + min_width) { 102 bounds->set_x(visible_area.x() + min_width - bounds->width()); 103 } else if (bounds->x() > visible_area.right() - min_width) { 104 bounds->set_x(visible_area.right() - min_width); 105 } 106 if (bounds->bottom() < visible_area.y() + min_height) { 107 bounds->set_y(visible_area.y() + min_height - bounds->height()); 108 } else if (bounds->y() > visible_area.bottom() - min_height) { 109 bounds->set_y(visible_area.bottom() - min_height); 110 } 111 if (bounds->y() < visible_area.y()) 112 bounds->set_y(visible_area.y()); 113} 114 115bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) { 116 views::View* target = static_cast<views::View*>(event.target()); 117 if (!target) 118 return false; 119 aura::Window* target_root = 120 target->GetWidget()->GetNativeView()->GetRootWindow(); 121 if (!target_root || target_root == window->GetRootWindow()) 122 return false; 123 aura::Window* window_container = 124 ash::Shell::GetContainer(target_root, window->parent()->id()); 125 // Move the window to the target launcher. 126 window_container->AddChild(window); 127 return true; 128} 129 130void ReparentChildWithTransientChildren(aura::Window* child, 131 aura::Window* old_parent, 132 aura::Window* new_parent) { 133 if (child->parent() == old_parent) 134 new_parent->AddChild(child); 135 ReparentTransientChildrenOfChild(child, old_parent, new_parent); 136} 137 138void ReparentTransientChildrenOfChild(aura::Window* child, 139 aura::Window* old_parent, 140 aura::Window* new_parent) { 141 for (size_t i = 0; 142 i < views::corewm::GetTransientChildren(child).size(); 143 ++i) { 144 ReparentChildWithTransientChildren( 145 views::corewm::GetTransientChildren(child)[i], 146 old_parent, 147 new_parent); 148 } 149} 150 151} // namespace wm 152} // namespace ash 153