Cleaning up MruWindowTracker::BuildWindowList()
[chromium-blink-merge.git] / ash / wm / mru_window_tracker.cc
blob6b8b4cf6e2583b7de44f3c30b6d2bc5105547951
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"
7 #include <algorithm>
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"
21 namespace ash {
23 namespace {
25 // Adds the windows that can be cycled through for the specified window id to
26 // |windows|.
27 void AddTrackedWindows(aura::Window* root,
28 int container_id,
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 more recently used than |w2|. This
49 // is used for a stable sort to move minimized windows to the LRU end of the
50 // list.
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 MruWindowTracker::WindowList BuildWindowListInternal(
58 const std::list<aura::Window*>* mru_windows) {
59 MruWindowTracker::WindowList windows;
60 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
62 aura::Window* active_root = Shell::GetTargetRootWindow();
63 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
64 iter != root_windows.end(); ++iter) {
65 if (*iter == active_root)
66 continue;
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 // Dragged windows are temporarily parented in docked container. Include them
77 // in the in tracked list.
78 AddDraggedWindows(active_root, &windows);
80 // Removes unfocusable windows.
81 MruWindowTracker::WindowList::iterator last =
82 std::remove_if(
83 windows.begin(),
84 windows.end(),
85 std::not1(std::ptr_fun(ash::wm::CanActivateWindow)));
86 windows.erase(last, windows.end());
88 // Put the windows in the mru_windows list at the head, if it's available.
89 if (mru_windows) {
90 // Iterate through the list backwards, so that we can move each window to
91 // the front of the windows list as we find them.
92 for (std::list<aura::Window*>::const_reverse_iterator ix =
93 mru_windows->rbegin();
94 ix != mru_windows->rend(); ++ix) {
95 // Exclude windows in non-switchable containers and those which cannot
96 // be activated.
97 if (!IsSwitchableContainer((*ix)->parent()) ||
98 !ash::wm::CanActivateWindow(*ix)) {
99 continue;
102 MruWindowTracker::WindowList::iterator window =
103 std::find(windows.begin(), windows.end(), *ix);
104 if (window != windows.end()) {
105 windows.erase(window);
106 windows.push_back(*ix);
111 // Window cycling expects the topmost window at the front of the list.
112 // Move minimized windows to the end (LRU end) of the list.
113 std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
115 return windows;
118 } // namespace
120 //////////////////////////////////////////////////////////////////////////////
121 // MruWindowTracker, public:
123 MruWindowTracker::MruWindowTracker(
124 aura::client::ActivationClient* activation_client)
125 : activation_client_(activation_client),
126 ignore_window_activations_(false) {
127 activation_client_->AddObserver(this);
130 MruWindowTracker::~MruWindowTracker() {
131 for (std::list<aura::Window*>::iterator iter = mru_windows_.begin();
132 iter != mru_windows_.end(); ++iter) {
133 (*iter)->RemoveObserver(this);
136 activation_client_->RemoveObserver(this);
139 // static
140 MruWindowTracker::WindowList MruWindowTracker::BuildWindowList() {
141 return BuildWindowListInternal(NULL);
144 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() {
145 return BuildWindowListInternal(&mru_windows_);
148 void MruWindowTracker::SetIgnoreActivations(bool ignore) {
149 ignore_window_activations_ = ignore;
151 // If no longer ignoring window activations, move currently active window
152 // to front.
153 if (!ignore)
154 SetActiveWindow(wm::GetActiveWindow());
157 //////////////////////////////////////////////////////////////////////////////
158 // MruWindowTracker, private:
160 void MruWindowTracker::SetActiveWindow(aura::Window* active_window) {
161 if (!active_window)
162 return;
164 std::list<aura::Window*>::iterator iter =
165 std::find(mru_windows_.begin(), mru_windows_.end(), active_window);
166 // Observe all newly tracked windows.
167 if (iter == mru_windows_.end())
168 active_window->AddObserver(this);
169 else
170 mru_windows_.erase(iter);
171 // TODO(flackr): Remove this check if this doesn't fire for a while. This
172 // should verify that all tracked windows start with a layer, see
173 // http://crbug.com/291354.
174 CHECK(active_window->layer());
175 mru_windows_.push_front(active_window);
178 void MruWindowTracker::OnWindowActivated(aura::Window* gained_active,
179 aura::Window* lost_active) {
180 if (!ignore_window_activations_)
181 SetActiveWindow(gained_active);
184 void MruWindowTracker::OnWindowDestroyed(aura::Window* window) {
185 // It's possible for OnWindowActivated() to be called after
186 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
187 // else we may end up with a deleted window in |mru_windows_|.
188 mru_windows_.remove(window);
189 window->RemoveObserver(this);
192 } // namespace ash