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/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/switchable_windows.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/workspace_controller.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/events/event.h"
18 #include "ui/events/event_handler.h"
19 #include "ui/wm/public/activation_client.h"
25 // Adds the windows that can be cycled through for the specified window id to
27 void AddTrackedWindows(aura::Window
* root
,
29 MruWindowTracker::WindowList
* windows
) {
30 aura::Window
* container
= Shell::GetContainer(root
, container_id
);
31 const MruWindowTracker::WindowList
& children(container
->children());
32 windows
->insert(windows
->end(), children
.begin(), children
.end());
35 // Adds windows being dragged in the docked container to |windows| list.
36 void AddDraggedWindows(aura::Window
* root
,
37 MruWindowTracker::WindowList
* windows
) {
38 aura::Window
* container
=
39 Shell::GetContainer(root
, kShellWindowId_DockedContainer
);
40 const MruWindowTracker::WindowList
& children
= container
->children();
41 for (MruWindowTracker::WindowList::const_iterator iter
= children
.begin();
42 iter
!= children
.end(); ++iter
) {
43 if (wm::GetWindowState(*iter
)->is_dragged())
44 windows
->insert(windows
->end(), *iter
);
48 // Returns whether |w1| should be considered less recently used than |w2|. This
49 // is used for a stable sort to move minimized windows to the LRU end of the
51 bool CompareWindowState(aura::Window
* w1
, aura::Window
* w2
) {
52 return ash::wm::IsWindowMinimized(w1
) && !ash::wm::IsWindowMinimized(w2
);
55 // Returns a list of windows ordered by their stacking order.
56 // If |mru_windows| is passed, these windows are moved to the front of the list.
57 // If |top_most_at_end|, the list is returned in descending (bottom-most / least
58 // recently used) order.
59 MruWindowTracker::WindowList
BuildWindowListInternal(
60 const std::list
<aura::Window
*>* mru_windows
,
61 bool top_most_at_end
) {
62 MruWindowTracker::WindowList windows
;
63 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
65 aura::Window
* active_root
= Shell::GetTargetRootWindow();
66 for (aura::Window::Windows::const_iterator iter
= root_windows
.begin();
67 iter
!= root_windows
.end(); ++iter
) {
68 if (*iter
== active_root
)
70 for (size_t i
= 0; i
< kSwitchableWindowContainerIdsLength
; ++i
)
71 AddTrackedWindows(*iter
, kSwitchableWindowContainerIds
[i
], &windows
);
74 // Add windows in the active root windows last so that the topmost window
75 // in the active root window becomes the front of the list.
76 for (size_t i
= 0; i
< kSwitchableWindowContainerIdsLength
; ++i
)
77 AddTrackedWindows(active_root
, kSwitchableWindowContainerIds
[i
], &windows
);
79 // Dragged windows are temporarily parented in docked container. Include them
80 // in the in tracked list.
81 AddDraggedWindows(active_root
, &windows
);
83 // Removes unfocusable windows.
84 MruWindowTracker::WindowList::iterator last
=
88 std::not1(std::ptr_fun(ash::wm::CanActivateWindow
)));
89 windows
.erase(last
, windows
.end());
91 // Put the windows in the mru_windows list at the head, if it's available.
93 // Iterate through the list backwards, so that we can move each window to
94 // the front of the windows list as we find them.
95 for (std::list
<aura::Window
*>::const_reverse_iterator ix
=
96 mru_windows
->rbegin();
97 ix
!= mru_windows
->rend(); ++ix
) {
98 // Exclude windows in non-switchable containers and those which cannot
100 if (!IsSwitchableContainer((*ix
)->parent()) ||
101 !ash::wm::CanActivateWindow(*ix
)) {
105 MruWindowTracker::WindowList::iterator window
=
106 std::find(windows
.begin(), windows
.end(), *ix
);
107 if (window
!= windows
.end()) {
108 windows
.erase(window
);
109 windows
.push_back(*ix
);
114 // Move minimized windows to the beginning (LRU end) of the list.
115 std::stable_sort(windows
.begin(), windows
.end(), CompareWindowState
);
117 // Window cycling expects the topmost window at the front of the list.
118 if (!top_most_at_end
)
119 std::reverse(windows
.begin(), windows
.end());
126 //////////////////////////////////////////////////////////////////////////////
127 // MruWindowTracker, public:
129 MruWindowTracker::MruWindowTracker(
130 aura::client::ActivationClient
* activation_client
)
131 : activation_client_(activation_client
),
132 ignore_window_activations_(false) {
133 activation_client_
->AddObserver(this);
136 MruWindowTracker::~MruWindowTracker() {
137 for (std::list
<aura::Window
*>::iterator iter
= mru_windows_
.begin();
138 iter
!= mru_windows_
.end(); ++iter
) {
139 (*iter
)->RemoveObserver(this);
142 activation_client_
->RemoveObserver(this);
146 MruWindowTracker::WindowList
MruWindowTracker::BuildWindowList(
147 bool top_most_at_end
) {
148 return BuildWindowListInternal(NULL
, top_most_at_end
);
151 MruWindowTracker::WindowList
MruWindowTracker::BuildMruWindowList() {
152 return BuildWindowListInternal(&mru_windows_
, false);
155 void MruWindowTracker::SetIgnoreActivations(bool ignore
) {
156 ignore_window_activations_
= ignore
;
158 // If no longer ignoring window activations, move currently active window
161 SetActiveWindow(wm::GetActiveWindow());
164 //////////////////////////////////////////////////////////////////////////////
165 // MruWindowTracker, private:
167 void MruWindowTracker::SetActiveWindow(aura::Window
* active_window
) {
171 std::list
<aura::Window
*>::iterator iter
=
172 std::find(mru_windows_
.begin(), mru_windows_
.end(), active_window
);
173 // Observe all newly tracked windows.
174 if (iter
== mru_windows_
.end())
175 active_window
->AddObserver(this);
177 mru_windows_
.erase(iter
);
178 // TODO(flackr): Remove this check if this doesn't fire for a while. This
179 // should verify that all tracked windows start with a layer, see
180 // http://crbug.com/291354.
181 CHECK(active_window
->layer());
182 mru_windows_
.push_front(active_window
);
185 void MruWindowTracker::OnWindowActivated(aura::Window
* gained_active
,
186 aura::Window
* lost_active
) {
187 if (!ignore_window_activations_
)
188 SetActiveWindow(gained_active
);
191 void MruWindowTracker::OnWindowDestroyed(aura::Window
* window
) {
192 // It's possible for OnWindowActivated() to be called after
193 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
194 // else we may end up with a deleted window in |mru_windows_|.
195 mru_windows_
.remove(window
);
196 window
->RemoveObserver(this);