1 // Copyright 2013 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/default_activation_client.h"
7 #include "ui/aura/window.h"
8 #include "ui/wm/public/activation_change_observer.h"
9 #include "ui/wm/public/activation_delegate.h"
13 // Takes care of observing root window destruction & destroying the client.
14 class DefaultActivationClient::Deleter
: public aura::WindowObserver
{
16 Deleter(DefaultActivationClient
* client
, aura::Window
* root_window
)
18 root_window_(root_window
) {
19 root_window_
->AddObserver(this);
23 ~Deleter() override
{}
25 // Overridden from WindowObserver:
26 void OnWindowDestroyed(aura::Window
* window
) override
{
27 DCHECK_EQ(window
, root_window_
);
28 root_window_
->RemoveObserver(this);
33 DefaultActivationClient
* client_
;
34 aura::Window
* root_window_
;
36 DISALLOW_COPY_AND_ASSIGN(Deleter
);
39 ////////////////////////////////////////////////////////////////////////////////
40 // DefaultActivationClient, public:
42 DefaultActivationClient::DefaultActivationClient(aura::Window
* root_window
)
43 : last_active_(NULL
) {
44 aura::client::SetActivationClient(root_window
, this);
45 new Deleter(this, root_window
);
48 ////////////////////////////////////////////////////////////////////////////////
49 // DefaultActivationClient, client::ActivationClient implementation:
51 void DefaultActivationClient::AddObserver(
52 aura::client::ActivationChangeObserver
* observer
) {
53 observers_
.AddObserver(observer
);
56 void DefaultActivationClient::RemoveObserver(
57 aura::client::ActivationChangeObserver
* observer
) {
58 observers_
.RemoveObserver(observer
);
61 void DefaultActivationClient::ActivateWindow(aura::Window
* window
) {
62 aura::Window
* last_active
= GetActiveWindow();
63 if (last_active
== window
)
66 last_active_
= last_active
;
67 RemoveActiveWindow(window
);
68 active_windows_
.push_back(window
);
69 window
->parent()->StackChildAtTop(window
);
70 window
->AddObserver(this);
72 FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver
,
74 OnWindowActivated(window
, last_active
));
76 aura::client::ActivationChangeObserver
* observer
=
77 aura::client::GetActivationChangeObserver(last_active
);
79 observer
->OnWindowActivated(window
, last_active
);
80 observer
= aura::client::GetActivationChangeObserver(window
);
82 observer
->OnWindowActivated(window
, last_active
);
85 void DefaultActivationClient::DeactivateWindow(aura::Window
* window
) {
86 aura::client::ActivationChangeObserver
* observer
=
87 aura::client::GetActivationChangeObserver(window
);
89 observer
->OnWindowActivated(NULL
, window
);
91 ActivateWindow(last_active_
);
94 aura::Window
* DefaultActivationClient::GetActiveWindow() {
95 if (active_windows_
.empty())
97 return active_windows_
.back();
100 aura::Window
* DefaultActivationClient::GetActivatableWindow(
101 aura::Window
* window
) {
105 aura::Window
* DefaultActivationClient::GetToplevelWindow(aura::Window
* window
) {
109 bool DefaultActivationClient::CanActivateWindow(aura::Window
* window
) const {
113 ////////////////////////////////////////////////////////////////////////////////
114 // DefaultActivationClient, aura::WindowObserver implementation:
116 void DefaultActivationClient::OnWindowDestroyed(aura::Window
* window
) {
117 if (window
== last_active_
)
120 if (window
== GetActiveWindow()) {
121 active_windows_
.pop_back();
122 aura::Window
* next_active
= GetActiveWindow();
123 if (next_active
&& aura::client::GetActivationChangeObserver(next_active
)) {
124 aura::client::GetActivationChangeObserver(next_active
)->OnWindowActivated(
130 RemoveActiveWindow(window
);
133 ////////////////////////////////////////////////////////////////////////////////
134 // DefaultActivationClient, private:
136 DefaultActivationClient::~DefaultActivationClient() {
137 for (unsigned int i
= 0; i
< active_windows_
.size(); ++i
) {
138 active_windows_
[i
]->RemoveObserver(this);
142 void DefaultActivationClient::RemoveActiveWindow(aura::Window
* window
) {
143 for (unsigned int i
= 0; i
< active_windows_
.size(); ++i
) {
144 if (active_windows_
[i
] == window
) {
145 active_windows_
.erase(active_windows_
.begin() + i
);
146 window
->RemoveObserver(this);