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/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"
26 // TODO(beng): replace many of these functions with the corewm versions.
27 void ActivateWindow(aura::Window* window) {
28 views::corewm::ActivateWindow(window);
31 void DeactivateWindow(aura::Window* window) {
32 views::corewm::DeactivateWindow(window);
35 bool IsActiveWindow(aura::Window* window) {
36 return views::corewm::IsActiveWindow(window);
39 aura::Window* GetActiveWindow() {
40 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
44 aura::Window* GetActivatableWindow(aura::Window* window) {
45 return views::corewm::GetActivatableWindow(window);
48 bool CanActivateWindow(aura::Window* window) {
49 return views::corewm::CanActivateWindow(window);
52 bool IsWindowMinimized(aura::Window* window) {
53 return window->GetProperty(aura::client::kShowStateKey) ==
54 ui::SHOW_STATE_MINIMIZED;
57 void 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);
65 void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area,
67 AdjustBoundsToEnsureWindowVisibility(
68 visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds);
71 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area,
75 bounds->set_width(std::min(bounds->width(), visible_area.width()));
76 bounds->set_height(std::min(bounds->height(), visible_area.height()));
78 min_width = std::min(min_width, visible_area.width());
79 min_height = std::min(min_height, visible_area.height());
81 if (bounds->x() + min_width > visible_area.right()) {
82 bounds->set_x(visible_area.right() - min_width);
83 } else if (bounds->right() - min_width < 0) {
84 bounds->set_x(min_width - bounds->width());
86 if (bounds->y() + min_height > visible_area.bottom()) {
87 bounds->set_y(visible_area.bottom() - min_height);
88 } else if (bounds->bottom() - min_height < 0) {
89 bounds->set_y(min_height - bounds->height());
95 bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) {
96 views::View* target = static_cast<views::View*>(event.target());
99 aura::Window* target_root =
100 target->GetWidget()->GetNativeView()->GetRootWindow();
101 if (!target_root || target_root == window->GetRootWindow())
103 aura::Window* window_container =
104 ash::Shell::GetContainer(target_root, window->parent()->id());
105 // Move the window to the target launcher.
106 window_container->AddChild(window);
110 void ReparentChildWithTransientChildren(aura::Window* window,
111 aura::Window* child) {
112 window->AddChild(child);
113 ReparentTransientChildrenOfChild(window, child);
116 void ReparentTransientChildrenOfChild(aura::Window* window,
117 aura::Window* child) {
118 for (size_t i = 0; i < child->transient_children().size(); ++i)
119 ReparentChildWithTransientChildren(window, child->transient_children()[i]);