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 "ash/wm/mru_window_tracker.h"
9 #include "ash/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/window_cycle_list.h"
13 #include "ash/wm/window_util.h"
14 #include "ash/wm/workspace_controller.h"
15 #include "ui/aura/client/activation_client.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/events/event.h"
18 #include "ui/events/event_handler.h"
24 // Adds the windows that can be cycled through for the specified window id to
26 void AddTrackedWindows(aura::RootWindow
* root
,
28 MruWindowTracker::WindowList
* windows
) {
29 aura::Window
* container
= Shell::GetContainer(root
, container_id
);
30 const MruWindowTracker::WindowList
& children(container
->children());
31 windows
->insert(windows
->end(), children
.begin(), children
.end());
34 // Returns true if |window| is a container whose windows can be cycled to.
35 bool IsSwitchableContainer(aura::Window
* window
) {
38 for (size_t i
= 0; i
< kSwitchableWindowContainerIdsLength
; ++i
) {
39 if (window
->id() == kSwitchableWindowContainerIds
[i
])
45 // Returns whether |w1| should be considered less recently used than |w2|. This
46 // is used for a stable sort to move minimized windows to the LRU end of the
48 bool CompareWindowState(aura::Window
* w1
, aura::Window
* w2
) {
49 return ash::wm::IsWindowMinimized(w1
) && !ash::wm::IsWindowMinimized(w2
);
52 // Returns a list of windows ordered by their stacking order.
53 // If |mru_windows| is passed, these windows are moved to the front of the list.
54 // If |top_most_at_end|, the list is returned in descending (bottom-most / least
55 // recently used) order.
56 MruWindowTracker::WindowList
BuildWindowListInternal(
57 const std::list
<aura::Window
*>* mru_windows
,
58 bool top_most_at_end
) {
59 MruWindowTracker::WindowList windows
;
60 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
62 aura::RootWindow
* active_root
= Shell::GetTargetRootWindow();
63 for (Shell::RootWindowList::const_iterator iter
= root_windows
.begin();
64 iter
!= root_windows
.end(); ++iter
) {
65 if (*iter
== active_root
)
67 for (size_t i
= 0; i
< kSwitchableWindowContainerIdsLength
; ++i
)
68 AddTrackedWindows(*iter
, kSwitchableWindowContainerIds
[i
], &windows
);
71 // Add windows in the active root windows last so that the topmost window
72 // in the active root window becomes the front of the list.
73 for (size_t i
= 0; i
< kSwitchableWindowContainerIdsLength
; ++i
)
74 AddTrackedWindows(active_root
, kSwitchableWindowContainerIds
[i
], &windows
);
76 // Removes unfocusable windows.
77 MruWindowTracker::WindowList::iterator last
=
81 std::not1(std::ptr_fun(ash::wm::CanActivateWindow
)));
82 windows
.erase(last
, windows
.end());
84 // Put the windows in the mru_windows list at the head, if it's available.
86 // Iterate through the list backwards, so that we can move each window to
87 // the front of the windows list as we find them.
88 for (std::list
<aura::Window
*>::const_reverse_iterator ix
=
89 mru_windows
->rbegin();
90 ix
!= mru_windows
->rend(); ++ix
) {
91 // Exclude windows in non-switchable containers and those which cannot
93 if (!IsSwitchableContainer((*ix
)->parent()) ||
94 !ash::wm::CanActivateWindow(*ix
)) {
98 MruWindowTracker::WindowList::iterator window
=
99 std::find(windows
.begin(), windows
.end(), *ix
);
100 if (window
!= windows
.end()) {
101 windows
.erase(window
);
102 windows
.push_back(*ix
);
107 // Move minimized windows to the beginning (LRU end) of the list.
108 std::stable_sort(windows
.begin(), windows
.end(), CompareWindowState
);
110 // Window cycling expects the topmost window at the front of the list.
111 if (!top_most_at_end
)
112 std::reverse(windows
.begin(), windows
.end());
119 const int kSwitchableWindowContainerIds
[] = {
120 internal::kShellWindowId_DefaultContainer
,
121 internal::kShellWindowId_AlwaysOnTopContainer
,
122 internal::kShellWindowId_PanelContainer
125 const size_t kSwitchableWindowContainerIdsLength
=
126 arraysize(kSwitchableWindowContainerIds
);
128 //////////////////////////////////////////////////////////////////////////////
129 // MruWindowTracker, public:
131 MruWindowTracker::MruWindowTracker(
132 aura::client::ActivationClient
* activation_client
)
133 : activation_client_(activation_client
),
134 ignore_window_activations_(false) {
135 activation_client_
->AddObserver(this);
138 MruWindowTracker::~MruWindowTracker() {
139 for (std::list
<aura::Window
*>::iterator iter
= mru_windows_
.begin();
140 iter
!= mru_windows_
.end(); ++iter
) {
141 (*iter
)->RemoveObserver(this);
144 activation_client_
->RemoveObserver(this);
148 MruWindowTracker::WindowList
MruWindowTracker::BuildWindowList(
149 bool top_most_at_end
) {
150 return BuildWindowListInternal(NULL
, top_most_at_end
);
153 MruWindowTracker::WindowList
MruWindowTracker::BuildMruWindowList() {
154 return BuildWindowListInternal(&mru_windows_
, false);
157 void MruWindowTracker::SetIgnoreActivations(bool ignore
) {
158 ignore_window_activations_
= ignore
;
160 // If no longer ignoring window activations, move currently active window
163 SetActiveWindow(wm::GetActiveWindow());
166 //////////////////////////////////////////////////////////////////////////////
167 // MruWindowTracker, private:
169 void MruWindowTracker::SetActiveWindow(aura::Window
* active_window
) {
173 std::list
<aura::Window
*>::iterator iter
=
174 std::find(mru_windows_
.begin(), mru_windows_
.end(), active_window
);
175 // Observe all newly tracked windows.
176 if (iter
== mru_windows_
.end())
177 active_window
->AddObserver(this);
179 mru_windows_
.erase(iter
);
180 // TODO(flackr): Remove this check if this doesn't fire for a while. This
181 // should verify that all tracked windows start with a layer, see
182 // http://crbug.com/291354.
183 CHECK(active_window
->layer());
184 mru_windows_
.push_front(active_window
);
187 void MruWindowTracker::OnWindowActivated(aura::Window
* gained_active
,
188 aura::Window
* lost_active
) {
189 if (!ignore_window_activations_
)
190 SetActiveWindow(gained_active
);
193 void MruWindowTracker::OnWindowDestroying(aura::Window
* window
) {
194 mru_windows_
.remove(window
);
195 window
->RemoveObserver(this);