Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / ash / wm / window_util.cc
blobe6907384ccc762adbe3cf7aac2d17e090531bb10
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"
7 #include <vector>
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/views/corewm/window_util.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
25 namespace ash {
26 namespace wm {
28 // TODO(beng): replace many of these functions with the corewm versions.
29 void ActivateWindow(aura::Window* window) {
30 views::corewm::ActivateWindow(window);
33 void DeactivateWindow(aura::Window* window) {
34 views::corewm::DeactivateWindow(window);
37 bool IsActiveWindow(aura::Window* window) {
38 return views::corewm::IsActiveWindow(window);
41 aura::Window* GetActiveWindow() {
42 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
43 GetActiveWindow();
46 aura::Window* GetActivatableWindow(aura::Window* window) {
47 return views::corewm::GetActivatableWindow(window);
50 bool CanActivateWindow(aura::Window* window) {
51 return views::corewm::CanActivateWindow(window);
54 bool IsWindowMinimized(aura::Window* window) {
55 return window->GetProperty(aura::client::kShowStateKey) ==
56 ui::SHOW_STATE_MINIMIZED;
59 void 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);
81 void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area,
82 gfx::Rect* bounds) {
83 AdjustBoundsToEnsureWindowVisibility(
84 visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds);
87 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area,
88 int min_width,
89 int min_height,
90 gfx::Rect* bounds) {
91 bounds->set_width(std::min(bounds->width(), visible_area.width()));
92 bounds->set_height(std::min(bounds->height(), visible_area.height()));
94 min_width = std::min(min_width, visible_area.width());
95 min_height = std::min(min_height, visible_area.height());
97 if (bounds->right() < visible_area.x() + min_width) {
98 bounds->set_x(visible_area.x() + min_width - bounds->width());
99 } else if (bounds->x() > visible_area.right() - min_width) {
100 bounds->set_x(visible_area.right() - min_width);
102 if (bounds->bottom() < visible_area.y() + min_height) {
103 bounds->set_y(visible_area.y() + min_height - bounds->height());
104 } else if (bounds->y() > visible_area.bottom() - min_height) {
105 bounds->set_y(visible_area.bottom() - min_height);
107 if (bounds->y() < visible_area.y())
108 bounds->set_y(visible_area.y());
111 bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) {
112 views::View* target = static_cast<views::View*>(event.target());
113 if (!target)
114 return false;
115 aura::Window* target_root =
116 target->GetWidget()->GetNativeView()->GetRootWindow();
117 if (!target_root || target_root == window->GetRootWindow())
118 return false;
119 aura::Window* window_container =
120 ash::Shell::GetContainer(target_root, window->parent()->id());
121 // Move the window to the target launcher.
122 window_container->AddChild(window);
123 return true;
126 void ReparentChildWithTransientChildren(aura::Window* child,
127 aura::Window* old_parent,
128 aura::Window* new_parent) {
129 if (child->parent() == old_parent)
130 new_parent->AddChild(child);
131 ReparentTransientChildrenOfChild(child, old_parent, new_parent);
134 void ReparentTransientChildrenOfChild(aura::Window* child,
135 aura::Window* old_parent,
136 aura::Window* new_parent) {
137 for (size_t i = 0;
138 i < views::corewm::GetTransientChildren(child).size();
139 ++i) {
140 ReparentChildWithTransientChildren(
141 views::corewm::GetTransientChildren(child)[i],
142 old_parent,
143 new_parent);
147 } // namespace wm
148 } // namespace ash