Chromium Code Reviews| Index: ash/wm/window_cycle_list.cc |
| diff --git a/ash/wm/window_cycle_list.cc b/ash/wm/window_cycle_list.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10ff69e9476d260c56f6649c4880c6ab0fe41323 |
| --- /dev/null |
| +++ b/ash/wm/window_cycle_list.cc |
| @@ -0,0 +1,168 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/wm/window_cycle_list.h" |
| + |
| +#include "ash/shell.h" |
| +#include "ash/wm/mru_window_tracker.h" |
| +#include "ash/wm/window_animations.h" |
| +#include "ash/wm/window_state.h" |
| +#include "ash/wm/window_util.h" |
| +#include "ui/aura/window.h" |
| + |
| +namespace ash { |
| + |
| +// Returns the window immediately below |window| in the current container. |
| +aura::Window* GetWindowBelow(aura::Window* window) { |
| + aura::Window* parent = window->parent(); |
| + if (!parent) |
| + return NULL; |
| + aura::Window* below = NULL; |
| + for (aura::Window::Windows::const_iterator iter = parent->children().begin(); |
| + iter != parent->children().end(); |
| + ++iter) { |
| + if (*iter == window) |
| + return below; |
| + below = *iter; |
| + } |
| + NOTREACHED(); |
| + return NULL; |
| +} |
| + |
| +// This class restores and moves a window to the front of the stacking order for |
| +// the duration of the class's scope. |
| +class ScopedShowWindow : public aura::WindowObserver { |
| + public: |
| + ScopedShowWindow(); |
| + virtual ~ScopedShowWindow(); |
| + |
| + // Show |window| at the top of the stacking order. |
| + void Show(aura::Window* window); |
| + |
| + // Cancel restoring the window on going out of scope. |
| + void CancelRestore(); |
| + |
| + aura::Window* window() { return window_; } |
| + |
| + // aura::WindowObserver: |
| + virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE; |
| + |
| + private: |
| + // The window being shown. |
| + aura::Window* window_; |
| + |
| + // The window immediately below where window_ belongs. |
| + aura::Window* stack_window_above_; |
| + |
| + // If true, minimize window_ on going out of scope. |
| + bool minimized_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow); |
| +}; |
| + |
| +ScopedShowWindow::ScopedShowWindow() |
| + : window_(NULL), stack_window_above_(NULL), minimized_(false) { |
| +} |
| + |
| +void ScopedShowWindow::Show(aura::Window* window) { |
| + DCHECK(!window_); |
| + window_ = window; |
| + stack_window_above_ = GetWindowBelow(window); |
| + minimized_ = wm::GetWindowState(window)->IsMinimized(); |
| + window_->parent()->AddObserver(this); |
| + window_->Show(); |
| + wm::GetWindowState(window_)->Activate(); |
| +} |
| + |
| +ScopedShowWindow::~ScopedShowWindow() { |
| + if (window_) { |
| + window_->parent()->RemoveObserver(this); |
| + |
| + // Restore window's stacking position. |
| + if (stack_window_above_) |
| + window_->parent()->StackChildAbove(window_, stack_window_above_); |
| + else |
| + window_->parent()->StackChildAtBottom(window_); |
| + |
| + // Restore minimized state. |
| + if (minimized_) |
| + wm::GetWindowState(window_)->Minimize(); |
| + } |
| +} |
| + |
| +void ScopedShowWindow::CancelRestore() { |
| + if (!window_) |
| + return; |
| + window_->parent()->RemoveObserver(this); |
| + window_ = stack_window_above_ = NULL; |
| +} |
| + |
| +void ScopedShowWindow::OnWillRemoveWindow(aura::Window* window) { |
| + if (window == window_) { |
| + CancelRestore(); |
| + } else if (window == stack_window_above_) { |
| + // If the window this window was above is removed, use the next window down |
| + // as the restore marker. |
| + stack_window_above_ = GetWindowBelow(stack_window_above_); |
| + } |
| +} |
| + |
| +WindowCycleList::WindowCycleList(const WindowList& windows) |
| + : windows_(windows), current_index_(0) { |
| + ash::Shell::GetInstance()->mru_window_tracker()->SetIgnoreActivations(true); |
| + |
| + for (WindowList::const_iterator i = windows_.begin(); i != windows_.end(); |
| + ++i) |
|
tdanderson
2014/05/01 21:58:33
{} around loop body since multiple lines
Nina
2014/05/02 13:27:34
Done.
|
| + (*i)->AddObserver(this); |
| +} |
| + |
| +WindowCycleList::~WindowCycleList() { |
| + ash::Shell::GetInstance()->mru_window_tracker()->SetIgnoreActivations(false); |
| + for (WindowList::const_iterator i = windows_.begin(); i != windows_.end(); |
| + ++i) { |
| + (*i)->RemoveObserver(this); |
| + } |
| + if (showing_window_) |
| + showing_window_->CancelRestore(); |
| +} |
| + |
| +void WindowCycleList::Step(WindowCycleController::Direction direction) { |
| + if (windows_.empty()) |
| + return; |
| + |
| + // When there is only one window, we should give feedback to the user. |
| + if (windows_.size() == 1) { |
| + ::wm::AnimateWindow(windows_[0], ::wm::WINDOW_ANIMATION_TYPE_BOUNCE); |
| + return; |
| + } |
| + |
| + DCHECK(static_cast<size_t>(current_index_) < windows_.size()); |
| + |
| + // We're in a valid cycle, so step forward or backward. |
| + current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; |
| + |
| + // Wrap to window list size. |
| + current_index_ = (current_index_ + windows_.size()) % windows_.size(); |
| + DCHECK(windows_[current_index_]); |
|
tdanderson
2014/05/01 21:58:33
new line after line 147
Nina
2014/05/02 13:27:34
Done.
|
| + // Make sure the next window is visible. |
| + showing_window_.reset(new ScopedShowWindow); |
| + showing_window_->Show(windows_[current_index_]); |
| +} |
| + |
| +void WindowCycleList::OnWindowDestroyed(aura::Window* window) { |
| + window->RemoveObserver(this); |
| + |
| + WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); |
| + DCHECK(i != windows_.end()); |
| + int removed_index = static_cast<int>(i - windows_.begin()); |
| + windows_.erase(i); |
| + if (current_index_ > removed_index || |
| + current_index_ == static_cast<int>(windows_.size())) |
| + current_index_--; |
| + // If the first window was destroyed, move the index to the new first window. |
| + if (current_index_ == -1) |
| + current_index_ = 0; |
| +} |
| + |
| +} // namespace ash |