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_ash.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" 24 25 namespace ash { 26 namespace wm { 27 28 // TODO(beng): replace many of these functions with the corewm versions. 29 void ActivateWindow(aura::Window* window) { 30 views::corewm::ActivateWindow(window); 31 } 32 33 void DeactivateWindow(aura::Window* window) { 34 views::corewm::DeactivateWindow(window); 35 } 36 37 bool IsActiveWindow(aura::Window* window) { 38 return views::corewm::IsActiveWindow(window); 39 } 40 41 aura::Window* GetActiveWindow() { 42 return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())-> 43 GetActiveWindow(); 44 } 45 46 aura::Window* GetActivatableWindow(aura::Window* window) { 47 return views::corewm::GetActivatableWindow(window); 48 } 49 50 bool CanActivateWindow(aura::Window* window) { 51 return views::corewm::CanActivateWindow(window); 52 } 53 54 bool IsWindowMinimized(aura::Window* window) { 55 return window->GetProperty(aura::client::kShowStateKey) == 56 ui::SHOW_STATE_MINIMIZED; 57 } 58 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 = ScreenAsh::ConvertRectFromScreen(window->parent(), 75 center); 76 center.ClampToCenteredSize(size); 77 window->SetBounds(center); 78 } 79 } 80 81 void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area, 82 gfx::Rect* bounds) { 83 AdjustBoundsToEnsureWindowVisibility( 84 visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds); 85 } 86 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())); 93 94 min_width = std::min(min_width, visible_area.width()); 95 min_height = std::min(min_height, visible_area.height()); 96 97 if (bounds->x() + min_width > visible_area.right()) { 98 bounds->set_x(visible_area.right() - min_width); 99 } else if (bounds->right() - min_width < 0) { 100 bounds->set_x(min_width - bounds->width()); 101 } 102 if (bounds->y() + min_height > visible_area.bottom()) { 103 bounds->set_y(visible_area.bottom() - min_height); 104 } else if (bounds->bottom() - min_height < 0) { 105 bounds->set_y(min_height - bounds->height()); 106 } 107 if (bounds->y() < 0) 108 bounds->set_y(0); 109 } 110 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; 124 } 125 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); 132 } 133 134 void ReparentTransientChildrenOfChild(aura::Window* child, 135 aura::Window* old_parent, 136 aura::Window* new_parent) { 137 for (size_t i = 0; i < child->transient_children().size(); ++i) { 138 ReparentChildWithTransientChildren(child->transient_children()[i], 139 old_parent, 140 new_parent); 141 } 142 } 143 144 } // namespace wm 145 } // namespace ash 146