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 "ui/wm/core/window_util.h"
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"
16 // Invokes RecreateLayer() on all the children of |to_clone|, adding the newly
17 // cloned children to |parent|.
19 // WARNING: It is assumed that |parent| is ultimately owned by a LayerTreeOwner.
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
;
28 parent
->Add(old_layer
);
29 // RecreateLayer() moves the existing children to the new layer. Create a
31 CloneChildren(owner
->layer(), old_layer
);
40 void ActivateWindow(aura::Window
* window
) {
42 DCHECK(window
->GetRootWindow());
43 aura::client::GetActivationClient(window
->GetRootWindow())->ActivateWindow(
47 void DeactivateWindow(aura::Window
* window
) {
49 DCHECK(window
->GetRootWindow());
50 aura::client::GetActivationClient(window
->GetRootWindow())->DeactivateWindow(
54 bool IsActiveWindow(aura::Window
* window
) {
56 if (!window
->GetRootWindow())
58 aura::client::ActivationClient
* client
=
59 aura::client::GetActivationClient(window
->GetRootWindow());
60 return client
&& client
->GetActiveWindow() == window
;
63 bool CanActivateWindow(aura::Window
* window
) {
65 if (!window
->GetRootWindow())
67 aura::client::ActivationClient
* client
=
68 aura::client::GetActivationClient(window
->GetRootWindow());
69 return client
&& client
->CanActivateWindow(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
;
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
;
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();
92 aura::Window
* GetTransientParent(aura::Window
* window
) {
93 return const_cast<aura::Window
*>(GetTransientParent(
94 const_cast<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
;
102 const std::vector
<aura::Window
*>& GetTransientChildren(
103 const aura::Window
* window
) {
104 const TransientWindowManager
* manager
= TransientWindowManager::Get(window
);
106 return manager
->transient_children();
108 static std::vector
<aura::Window
*>* shared
= new std::vector
<aura::Window
*>;
112 void AddTransientChild(aura::Window
* parent
, aura::Window
* child
) {
113 TransientWindowManager::Get(parent
)->AddTransientChild(child
);
116 void RemoveTransientChild(aura::Window
* parent
, aura::Window
* child
) {
117 TransientWindowManager::Get(parent
)->RemoveTransientChild(child
);
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
)
125 return transient_parent
?
126 HasTransientAncestor(transient_parent
, ancestor
) : false;