1 // Copyright (c) 2012 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/window_cycle_list.h"
7 #include "ash/wm/window_util.h"
8 #include "ui/aura/window.h"
12 WindowCycleList::WindowCycleList(const WindowList
& windows
)
15 // Locate the currently active window in the list to use as our start point.
16 aura::Window
* active_window
= wm::GetActiveWindow();
18 // The active window may not be in the cycle list, which is expected if there
19 // are additional modal windows on the screen.
20 current_index_
= GetWindowIndex(active_window
);
22 for (WindowList::const_iterator i
= windows_
.begin(); i
!= windows_
.end();
24 (*i
)->AddObserver(this);
28 WindowCycleList::~WindowCycleList() {
29 for (WindowList::const_iterator i
= windows_
.begin(); i
!= windows_
.end();
31 (*i
)->RemoveObserver(this);
35 void WindowCycleList::Step(Direction direction
) {
39 if (current_index_
== -1) {
40 // We weren't able to find our active window in the shell delegate's
41 // provided window list. Just switch to the first (or last) one.
42 current_index_
= (direction
== FORWARD
? 0 : windows_
.size() - 1);
44 if (windows_
.size() == 1)
46 // We're in a valid cycle, so step forward or backward.
47 current_index_
+= (direction
== FORWARD
? 1 : -1);
49 // Wrap to window list size.
50 current_index_
= (current_index_
+ windows_
.size()) % windows_
.size();
51 DCHECK(windows_
[current_index_
]);
52 // Make sure the next window is visible.
53 windows_
[current_index_
]->Show();
54 wm::ActivateWindow(windows_
[current_index_
]);
57 int WindowCycleList::GetWindowIndex(aura::Window
* window
) {
58 WindowList::const_iterator it
=
59 std::find(windows_
.begin(), windows_
.end(), window
);
60 if (it
== windows_
.end())
61 return -1; // Not found.
62 return it
- windows_
.begin();
65 void WindowCycleList::OnWindowDestroyed(aura::Window
* window
) {
66 window
->RemoveObserver(this);
68 WindowList::iterator i
= std::find(windows_
.begin(), windows_
.end(), window
);
69 DCHECK(i
!= windows_
.end());
70 int removed_index
= static_cast<int>(i
- windows_
.begin());
72 if (current_index_
> removed_index
)
74 else if (current_index_
== static_cast<int>(windows_
.size()))