[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / wm / window_cycle_list.cc
blob7ea875b11beea8b9e35c78978a25a2374b9fe236
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"
10 namespace ash {
12 WindowCycleList::WindowCycleList(const WindowList& windows)
13 : windows_(windows),
14 current_index_(-1) {
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();
23 ++i) {
24 (*i)->AddObserver(this);
28 WindowCycleList::~WindowCycleList() {
29 for (WindowList::const_iterator i = windows_.begin(); i != windows_.end();
30 ++i) {
31 (*i)->RemoveObserver(this);
35 void WindowCycleList::Step(Direction direction) {
36 if (windows_.empty())
37 return;
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);
43 } else {
44 if (windows_.size() == 1)
45 return;
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());
71 windows_.erase(i);
72 if (current_index_ > removed_index)
73 current_index_--;
74 else if (current_index_ == static_cast<int>(windows_.size()))
75 current_index_--;
78 } // namespace ash