window_util.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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} 96 97bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) { 98 views::View* target = static_cast<views::View*>(event.target()); 99 if (!target) 100 return false; 101 aura::RootWindow* target_root = 102 target->GetWidget()->GetNativeView()->GetRootWindow(); 103 if (!target_root || target_root == window->GetRootWindow()) 104 return false; 105 aura::Window* window_container = 106 ash::Shell::GetContainer(target_root, window->parent()->id()); 107 // Move the window to the target launcher. 108 window_container->AddChild(window); 109 return true; 110} 111 112} // namespace wm 113} // namespace ash 114