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_(nullptr) {
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 ActivateWindowImpl(aura::client::ActivationChangeObserver::ActivationReason::
67 void DefaultActivationClient::ActivateWindowImpl(
68 aura::client::ActivationChangeObserver::ActivationReason reason
,
69 aura::Window
* window
) {
70 aura::Window
* last_active
= GetActiveWindow();
71 if (last_active
== window
)
74 last_active_
= last_active
;
75 RemoveActiveWindow(window
);
76 active_windows_
.push_back(window
);
77 window
->parent()->StackChildAtTop(window
);
78 window
->AddObserver(this);
80 FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver
, observers_
,
81 OnWindowActivated(reason
, window
, last_active
));
83 aura::client::ActivationChangeObserver
* observer
=
84 aura::client::GetActivationChangeObserver(last_active
);
86 observer
->OnWindowActivated(reason
, window
, last_active
);
88 observer
= aura::client::GetActivationChangeObserver(window
);
90 observer
->OnWindowActivated(reason
, window
, last_active
);
94 void DefaultActivationClient::DeactivateWindow(aura::Window
* window
) {
95 aura::client::ActivationChangeObserver
* observer
=
96 aura::client::GetActivationChangeObserver(window
);
98 observer
->OnWindowActivated(aura::client::ActivationChangeObserver::
99 ActivationReason::ACTIVATION_CLIENT
,
103 ActivateWindow(last_active_
);
106 aura::Window
* DefaultActivationClient::GetActiveWindow() {
107 if (active_windows_
.empty())
109 return active_windows_
.back();
112 aura::Window
* DefaultActivationClient::GetActivatableWindow(
113 aura::Window
* window
) {
117 aura::Window
* DefaultActivationClient::GetToplevelWindow(aura::Window
* window
) {
121 bool DefaultActivationClient::CanActivateWindow(aura::Window
* window
) const {
125 ////////////////////////////////////////////////////////////////////////////////
126 // DefaultActivationClient, aura::WindowObserver implementation:
128 void DefaultActivationClient::OnWindowDestroyed(aura::Window
* window
) {
129 if (window
== last_active_
)
130 last_active_
= nullptr;
132 if (window
== GetActiveWindow()) {
133 active_windows_
.pop_back();
134 aura::Window
* next_active
= GetActiveWindow();
135 if (next_active
&& aura::client::GetActivationChangeObserver(next_active
)) {
136 aura::client::GetActivationChangeObserver(next_active
)
137 ->OnWindowActivated(aura::client::ActivationChangeObserver::
138 ActivationReason::WINDOW_DISPOSITION_CHANGED
,
139 next_active
, nullptr);
144 RemoveActiveWindow(window
);
147 ////////////////////////////////////////////////////////////////////////////////
148 // DefaultActivationClient, private:
150 DefaultActivationClient::~DefaultActivationClient() {
151 for (unsigned int i
= 0; i
< active_windows_
.size(); ++i
) {
152 active_windows_
[i
]->RemoveObserver(this);
156 void DefaultActivationClient::RemoveActiveWindow(aura::Window
* window
) {
157 for (unsigned int i
= 0; i
< active_windows_
.size(); ++i
) {
158 if (active_windows_
[i
] == window
) {
159 active_windows_
.erase(active_windows_
.begin() + i
);
160 window
->RemoveObserver(this);