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