Revert of Rename isSystemLocationEnabled to isLocationEnabled. (patchset #1 id:1...
[chromium-blink-merge.git] / ash / wm / mru_window_tracker.cc
blob0952f490dd0da33a0923e1af8f922974001f5c79
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 less 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 // Move minimized windows to the beginning (LRU end) of the list.
112 std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
114 // Window cycling expects the topmost window at the front of the list.
115 std::reverse(windows.begin(), windows.end());
117 return windows;
120 } // namespace
122 //////////////////////////////////////////////////////////////////////////////
123 // MruWindowTracker, public:
125 MruWindowTracker::MruWindowTracker(
126 aura::client::ActivationClient* activation_client)
127 : activation_client_(activation_client),
128 ignore_window_activations_(false) {
129 activation_client_->AddObserver(this);
132 MruWindowTracker::~MruWindowTracker() {
133 for (std::list<aura::Window*>::iterator iter = mru_windows_.begin();
134 iter != mru_windows_.end(); ++iter) {
135 (*iter)->RemoveObserver(this);
138 activation_client_->RemoveObserver(this);
141 // static
142 MruWindowTracker::WindowList MruWindowTracker::BuildWindowList() {
143 return BuildWindowListInternal(NULL);
146 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() {
147 return BuildWindowListInternal(&mru_windows_);
150 void MruWindowTracker::SetIgnoreActivations(bool ignore) {
151 ignore_window_activations_ = ignore;
153 // If no longer ignoring window activations, move currently active window
154 // to front.
155 if (!ignore)
156 SetActiveWindow(wm::GetActiveWindow());
159 //////////////////////////////////////////////////////////////////////////////
160 // MruWindowTracker, private:
162 void MruWindowTracker::SetActiveWindow(aura::Window* active_window) {
163 if (!active_window)
164 return;
166 std::list<aura::Window*>::iterator iter =
167 std::find(mru_windows_.begin(), mru_windows_.end(), active_window);
168 // Observe all newly tracked windows.
169 if (iter == mru_windows_.end())
170 active_window->AddObserver(this);
171 else
172 mru_windows_.erase(iter);
173 // TODO(flackr): Remove this check if this doesn't fire for a while. This
174 // should verify that all tracked windows start with a layer, see
175 // http://crbug.com/291354.
176 CHECK(active_window->layer());
177 mru_windows_.push_front(active_window);
180 void MruWindowTracker::OnWindowActivated(aura::Window* gained_active,
181 aura::Window* lost_active) {
182 if (!ignore_window_activations_)
183 SetActiveWindow(gained_active);
186 void MruWindowTracker::OnWindowDestroyed(aura::Window* window) {
187 // It's possible for OnWindowActivated() to be called after
188 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
189 // else we may end up with a deleted window in |mru_windows_|.
190 mru_windows_.remove(window);
191 window->RemoveObserver(this);
194 } // namespace ash