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.
4
5#include "ash/wm/mru_window_tracker.h"
6
7#include <algorithm>
8
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"
19
20namespace ash {
21
22namespace {
23
24// Adds the windows that can be cycled through for the specified window id to
25// |windows|.
26void AddTrackedWindows(aura::Window* root,
27                       int container_id,
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());
32}
33
34// Returns true if |window| is a container whose windows can be cycled to.
35bool IsSwitchableContainer(aura::Window* window) {
36  if (!window)
37    return false;
38  for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
39    if (window->id() == kSwitchableWindowContainerIds[i])
40      return true;
41  }
42  return false;
43}
44
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
47// list.
48bool CompareWindowState(aura::Window* w1, aura::Window* w2) {
49  return ash::wm::IsWindowMinimized(w1) && !ash::wm::IsWindowMinimized(w2);
50}
51
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.
56MruWindowTracker::WindowList BuildWindowListInternal(
57    const std::list<aura::Window*>* mru_windows,
58    bool top_most_at_end) {
59  MruWindowTracker::WindowList windows;
60  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
61
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);
69  }
70
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);
75
76  // Removes unfocusable windows.
77  MruWindowTracker::WindowList::iterator last =
78      std::remove_if(
79          windows.begin(),
80          windows.end(),
81          std::not1(std::ptr_fun(ash::wm::CanActivateWindow)));
82  windows.erase(last, windows.end());
83
84  // Put the windows in the mru_windows list at the head, if it's available.
85  if (mru_windows) {
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
92      // be activated.
93      if (!IsSwitchableContainer((*ix)->parent()) ||
94          !ash::wm::CanActivateWindow(*ix)) {
95        continue;
96      }
97
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);
103      }
104    }
105  }
106
107  // Move minimized windows to the beginning (LRU end) of the list.
108  std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
109
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());
113
114  return windows;
115}
116
117}  // namespace
118
119const int kSwitchableWindowContainerIds[] = {
120  internal::kShellWindowId_DefaultContainer,
121  internal::kShellWindowId_AlwaysOnTopContainer,
122  internal::kShellWindowId_PanelContainer
123};
124
125const size_t kSwitchableWindowContainerIdsLength =
126    arraysize(kSwitchableWindowContainerIds);
127
128//////////////////////////////////////////////////////////////////////////////
129// MruWindowTracker, public:
130
131MruWindowTracker::MruWindowTracker(
132    aura::client::ActivationClient* activation_client)
133    : activation_client_(activation_client),
134      ignore_window_activations_(false) {
135  activation_client_->AddObserver(this);
136}
137
138MruWindowTracker::~MruWindowTracker() {
139  for (std::list<aura::Window*>::iterator iter = mru_windows_.begin();
140       iter != mru_windows_.end(); ++iter) {
141    (*iter)->RemoveObserver(this);
142  }
143
144  activation_client_->RemoveObserver(this);
145}
146
147// static
148MruWindowTracker::WindowList MruWindowTracker::BuildWindowList(
149    bool top_most_at_end) {
150  return BuildWindowListInternal(NULL, top_most_at_end);
151}
152
153MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() {
154  return BuildWindowListInternal(&mru_windows_, false);
155}
156
157void MruWindowTracker::SetIgnoreActivations(bool ignore) {
158  ignore_window_activations_ = ignore;
159
160  // If no longer ignoring window activations, move currently active window
161  // to front.
162  if (!ignore)
163    SetActiveWindow(wm::GetActiveWindow());
164}
165
166//////////////////////////////////////////////////////////////////////////////
167// MruWindowTracker, private:
168
169void MruWindowTracker::SetActiveWindow(aura::Window* active_window) {
170  if (!active_window)
171    return;
172
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);
178  else
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);
185}
186
187void MruWindowTracker::OnWindowActivated(aura::Window* gained_active,
188                                         aura::Window* lost_active) {
189  if (!ignore_window_activations_)
190    SetActiveWindow(gained_active);
191}
192
193void MruWindowTracker::OnWindowDestroying(aura::Window* window) {
194  mru_windows_.remove(window);
195  window->RemoveObserver(this);
196}
197
198}  // namespace ash
199