Added llvm into exceptions as we can't add README.chromium into 3rd party repository
[chromium-blink-merge.git] / ash / wm / mru_window_tracker.cc
blobf006d4565f33c458b931f2ac1091bc7a79c0d046
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/ash_focus_rules.h"
14 #include "ash/wm/window_state.h"
15 #include "ash/wm/window_util.h"
16 #include "ash/wm/workspace_controller.h"
17 #include "base/bind.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_handler.h"
21 #include "ui/wm/public/activation_client.h"
23 namespace ash {
25 namespace {
27 typedef base::Callback<bool(aura::Window*)> CanActivateWindowPredicate;
29 // Adds the windows that can be cycled through for the specified window id to
30 // |windows|.
31 void AddTrackedWindows(aura::Window* root,
32 int container_id,
33 MruWindowTracker::WindowList* windows) {
34 aura::Window* container = Shell::GetContainer(root, container_id);
35 const MruWindowTracker::WindowList& children(container->children());
36 windows->insert(windows->end(), children.begin(), children.end());
39 // Adds windows being dragged in the docked container to |windows| list.
40 void AddDraggedWindows(aura::Window* root,
41 MruWindowTracker::WindowList* windows) {
42 aura::Window* container =
43 Shell::GetContainer(root, kShellWindowId_DockedContainer);
44 const MruWindowTracker::WindowList& children = container->children();
45 for (MruWindowTracker::WindowList::const_iterator iter = children.begin();
46 iter != children.end(); ++iter) {
47 if (wm::GetWindowState(*iter)->is_dragged())
48 windows->insert(windows->end(), *iter);
52 // Returns whether |w1| should be considered less recently used than |w2|. This
53 // is used for a stable sort to move minimized windows to the LRU end of the
54 // list.
55 bool CompareWindowState(aura::Window* w1, aura::Window* w2) {
56 return ash::wm::IsWindowMinimized(w1) && !ash::wm::IsWindowMinimized(w2);
59 // Returns a list of windows ordered by their stacking order.
60 // If |mru_windows| is passed, these windows are moved to the front of the list.
61 // It uses the given |should_include_window_predicate| to determine whether to
62 // include a window in the returned list or not.
63 MruWindowTracker::WindowList BuildWindowListInternal(
64 const std::list<aura::Window*>* mru_windows,
65 const CanActivateWindowPredicate& should_include_window_predicate) {
66 MruWindowTracker::WindowList windows;
67 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
69 aura::Window* active_root = Shell::GetTargetRootWindow();
70 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
71 iter != root_windows.end(); ++iter) {
72 if (*iter == active_root)
73 continue;
74 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i)
75 AddTrackedWindows(*iter, kSwitchableWindowContainerIds[i], &windows);
78 // Add windows in the active root windows last so that the topmost window
79 // in the active root window becomes the front of the list.
80 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i)
81 AddTrackedWindows(active_root, kSwitchableWindowContainerIds[i], &windows);
83 // Dragged windows are temporarily parented in docked container. Include them
84 // in the in tracked list.
85 AddDraggedWindows(active_root, &windows);
87 // Removes unfocusable windows.
88 std::vector<aura::Window*>::iterator itr = windows.begin();
89 while (itr != windows.end()) {
90 if (!should_include_window_predicate.Run(*itr))
91 itr = windows.erase(itr);
92 else
93 ++itr;
96 // Put the windows in the mru_windows list at the head, if it's available.
97 if (mru_windows) {
98 // Iterate through the list backwards, so that we can move each window to
99 // the front of the windows list as we find them.
100 for (std::list<aura::Window*>::const_reverse_iterator ix =
101 mru_windows->rbegin();
102 ix != mru_windows->rend(); ++ix) {
103 // Exclude windows in non-switchable containers and those which cannot
104 // be activated.
105 if (!IsSwitchableContainer((*ix)->parent()) ||
106 !should_include_window_predicate.Run(*ix)) {
107 continue;
110 MruWindowTracker::WindowList::iterator window =
111 std::find(windows.begin(), windows.end(), *ix);
112 if (window != windows.end()) {
113 windows.erase(window);
114 windows.push_back(*ix);
119 // Move minimized windows to the beginning (LRU end) of the list.
120 std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
122 // Window cycling expects the topmost window at the front of the list.
123 std::reverse(windows.begin(), windows.end());
125 return windows;
128 } // namespace
130 //////////////////////////////////////////////////////////////////////////////
131 // MruWindowTracker, public:
133 MruWindowTracker::MruWindowTracker(
134 aura::client::ActivationClient* activation_client,
135 ash::wm::AshFocusRules* focus_rules)
136 : activation_client_(activation_client),
137 focus_rules_(focus_rules),
138 ignore_window_activations_(false) {
139 activation_client_->AddObserver(this);
142 MruWindowTracker::~MruWindowTracker() {
143 for (std::list<aura::Window*>::iterator iter = mru_windows_.begin();
144 iter != mru_windows_.end(); ++iter) {
145 (*iter)->RemoveObserver(this);
148 activation_client_->RemoveObserver(this);
151 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() const {
152 return BuildWindowListInternal(&mru_windows_,
153 base::Bind(&ash::wm::CanActivateWindow));
156 MruWindowTracker::WindowList
157 MruWindowTracker::BuildWindowListIgnoreModal() const {
158 return BuildWindowListInternal(
159 NULL,
160 base::Bind(&MruWindowTracker::IsWindowConsideredActivateable,
161 base::Unretained(this)));
164 void MruWindowTracker::SetIgnoreActivations(bool ignore) {
165 ignore_window_activations_ = ignore;
167 // If no longer ignoring window activations, move currently active window
168 // to front.
169 if (!ignore)
170 SetActiveWindow(wm::GetActiveWindow());
173 //////////////////////////////////////////////////////////////////////////////
174 // MruWindowTracker, private:
176 void MruWindowTracker::SetActiveWindow(aura::Window* active_window) {
177 if (!active_window)
178 return;
180 std::list<aura::Window*>::iterator iter =
181 std::find(mru_windows_.begin(), mru_windows_.end(), active_window);
182 // Observe all newly tracked windows.
183 if (iter == mru_windows_.end())
184 active_window->AddObserver(this);
185 else
186 mru_windows_.erase(iter);
187 // TODO(flackr): Remove this check if this doesn't fire for a while. This
188 // should verify that all tracked windows start with a layer, see
189 // http://crbug.com/291354.
190 CHECK(active_window->layer());
191 mru_windows_.push_front(active_window);
194 void MruWindowTracker::OnWindowActivated(aura::Window* gained_active,
195 aura::Window* lost_active) {
196 if (!ignore_window_activations_)
197 SetActiveWindow(gained_active);
200 void MruWindowTracker::OnWindowDestroyed(aura::Window* window) {
201 // It's possible for OnWindowActivated() to be called after
202 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
203 // else we may end up with a deleted window in |mru_windows_|.
204 mru_windows_.remove(window);
205 window->RemoveObserver(this);
208 bool MruWindowTracker::IsWindowConsideredActivateable(
209 aura::Window* window) const {
210 return focus_rules_->IsWindowConsideredActivatable(window);
213 } // namespace ash