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 "ui/wm/core/window_util.h"
6
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_event_dispatcher.h"
9 #include "ui/compositor/layer.h"
10 #include "ui/compositor/layer_tree_owner.h"
11 #include "ui/wm/core/transient_window_manager.h"
12 #include "ui/wm/public/activation_client.h"
13
14 namespace {
15
16 // Invokes RecreateLayer() on all the children of |to_clone|, adding the newly
17 // cloned children to |parent|.
18 //
19 // WARNING: It is assumed that |parent| is ultimately owned by a LayerTreeOwner.
CloneChildren(ui::Layer * to_clone,ui::Layer * parent)20 void CloneChildren(ui::Layer* to_clone, ui::Layer* parent) {
21 typedef std::vector<ui::Layer*> Layers;
22 // Make a copy of the children since RecreateLayer() mutates it.
23 Layers children(to_clone->children());
24 for (Layers::const_iterator i = children.begin(); i != children.end(); ++i) {
25 ui::LayerOwner* owner = (*i)->owner();
26 ui::Layer* old_layer = owner ? owner->RecreateLayer().release() : NULL;
27 if (old_layer) {
28 parent->Add(old_layer);
29 // RecreateLayer() moves the existing children to the new layer. Create a
30 // copy of those.
31 CloneChildren(owner->layer(), old_layer);
32 }
33 }
34 }
35
36 } // namespace
37
38 namespace wm {
39
ActivateWindow(aura::Window * window)40 void ActivateWindow(aura::Window* window) {
41 DCHECK(window);
42 DCHECK(window->GetRootWindow());
43 aura::client::GetActivationClient(window->GetRootWindow())->ActivateWindow(
44 window);
45 }
46
DeactivateWindow(aura::Window * window)47 void DeactivateWindow(aura::Window* window) {
48 DCHECK(window);
49 DCHECK(window->GetRootWindow());
50 aura::client::GetActivationClient(window->GetRootWindow())->DeactivateWindow(
51 window);
52 }
53
IsActiveWindow(aura::Window * window)54 bool IsActiveWindow(aura::Window* window) {
55 DCHECK(window);
56 if (!window->GetRootWindow())
57 return false;
58 aura::client::ActivationClient* client =
59 aura::client::GetActivationClient(window->GetRootWindow());
60 return client && client->GetActiveWindow() == window;
61 }
62
CanActivateWindow(aura::Window * window)63 bool CanActivateWindow(aura::Window* window) {
64 DCHECK(window);
65 if (!window->GetRootWindow())
66 return false;
67 aura::client::ActivationClient* client =
68 aura::client::GetActivationClient(window->GetRootWindow());
69 return client && client->CanActivateWindow(window);
70 }
71
GetActivatableWindow(aura::Window * window)72 aura::Window* GetActivatableWindow(aura::Window* window) {
73 aura::client::ActivationClient* client =
74 aura::client::GetActivationClient(window->GetRootWindow());
75 return client ? client->GetActivatableWindow(window) : NULL;
76 }
77
GetToplevelWindow(aura::Window * window)78 aura::Window* GetToplevelWindow(aura::Window* window) {
79 aura::client::ActivationClient* client =
80 aura::client::GetActivationClient(window->GetRootWindow());
81 return client ? client->GetToplevelWindow(window) : NULL;
82 }
83
RecreateLayers(ui::LayerOwner * root)84 scoped_ptr<ui::LayerTreeOwner> RecreateLayers(ui::LayerOwner* root) {
85 scoped_ptr<ui::LayerTreeOwner> old_layer(
86 new ui::LayerTreeOwner(root->RecreateLayer().release()));
87 if (old_layer->root())
88 CloneChildren(root->layer(), old_layer->root());
89 return old_layer.Pass();
90 }
91
GetTransientParent(aura::Window * window)92 aura::Window* GetTransientParent(aura::Window* window) {
93 return const_cast<aura::Window*>(GetTransientParent(
94 const_cast<const aura::Window*>(window)));
95 }
96
GetTransientParent(const aura::Window * window)97 const aura::Window* GetTransientParent(const aura::Window* window) {
98 const TransientWindowManager* manager = TransientWindowManager::Get(window);
99 return manager ? manager->transient_parent() : NULL;
100 }
101
GetTransientChildren(const aura::Window * window)102 const std::vector<aura::Window*>& GetTransientChildren(
103 const aura::Window* window) {
104 const TransientWindowManager* manager = TransientWindowManager::Get(window);
105 if (manager)
106 return manager->transient_children();
107
108 static std::vector<aura::Window*>* shared = new std::vector<aura::Window*>;
109 return *shared;
110 }
111
AddTransientChild(aura::Window * parent,aura::Window * child)112 void AddTransientChild(aura::Window* parent, aura::Window* child) {
113 TransientWindowManager::Get(parent)->AddTransientChild(child);
114 }
115
RemoveTransientChild(aura::Window * parent,aura::Window * child)116 void RemoveTransientChild(aura::Window* parent, aura::Window* child) {
117 TransientWindowManager::Get(parent)->RemoveTransientChild(child);
118 }
119
HasTransientAncestor(const aura::Window * window,const aura::Window * ancestor)120 bool HasTransientAncestor(const aura::Window* window,
121 const aura::Window* ancestor) {
122 const aura::Window* transient_parent = GetTransientParent(window);
123 if (transient_parent == ancestor)
124 return true;
125 return transient_parent ?
126 HasTransientAncestor(transient_parent, ancestor) : false;
127 }
128
129 } // namespace wm
130