OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/window_cycle_list.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/wm/mru_window_tracker.h" |
| 9 #include "ash/wm/window_animations.h" |
| 10 #include "ash/wm/window_state.h" |
| 11 #include "ash/wm/window_util.h" |
| 12 #include "ui/aura/window.h" |
| 13 |
| 14 namespace ash { |
| 15 |
| 16 // Returns the window immediately below |window| in the current container. |
| 17 aura::Window* GetWindowBelow(aura::Window* window) { |
| 18 aura::Window* parent = window->parent(); |
| 19 if (!parent) |
| 20 return NULL; |
| 21 aura::Window* below = NULL; |
| 22 for (aura::Window::Windows::const_iterator iter = parent->children().begin(); |
| 23 iter != parent->children().end(); |
| 24 ++iter) { |
| 25 if (*iter == window) |
| 26 return below; |
| 27 below = *iter; |
| 28 } |
| 29 NOTREACHED(); |
| 30 return NULL; |
| 31 } |
| 32 |
| 33 // This class restores and moves a window to the front of the stacking order for |
| 34 // the duration of the class's scope. |
| 35 class ScopedShowWindow : public aura::WindowObserver { |
| 36 public: |
| 37 ScopedShowWindow(); |
| 38 virtual ~ScopedShowWindow(); |
| 39 |
| 40 // Show |window| at the top of the stacking order. |
| 41 void Show(aura::Window* window); |
| 42 |
| 43 // Cancel restoring the window on going out of scope. |
| 44 void CancelRestore(); |
| 45 |
| 46 aura::Window* window() { return window_; } |
| 47 |
| 48 // aura::WindowObserver: |
| 49 virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE; |
| 50 |
| 51 private: |
| 52 // The window being shown. |
| 53 aura::Window* window_; |
| 54 |
| 55 // The window immediately below where window_ belongs. |
| 56 aura::Window* stack_window_above_; |
| 57 |
| 58 // If true, minimize window_ on going out of scope. |
| 59 bool minimized_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow); |
| 62 }; |
| 63 |
| 64 ScopedShowWindow::ScopedShowWindow() |
| 65 : window_(NULL), stack_window_above_(NULL), minimized_(false) { |
| 66 } |
| 67 |
| 68 void ScopedShowWindow::Show(aura::Window* window) { |
| 69 DCHECK(!window_); |
| 70 window_ = window; |
| 71 stack_window_above_ = GetWindowBelow(window); |
| 72 minimized_ = wm::GetWindowState(window)->IsMinimized(); |
| 73 window_->parent()->AddObserver(this); |
| 74 window_->Show(); |
| 75 wm::GetWindowState(window_)->Activate(); |
| 76 } |
| 77 |
| 78 ScopedShowWindow::~ScopedShowWindow() { |
| 79 if (window_) { |
| 80 window_->parent()->RemoveObserver(this); |
| 81 |
| 82 // Restore window's stacking position. |
| 83 if (stack_window_above_) |
| 84 window_->parent()->StackChildAbove(window_, stack_window_above_); |
| 85 else |
| 86 window_->parent()->StackChildAtBottom(window_); |
| 87 |
| 88 // Restore minimized state. |
| 89 if (minimized_) |
| 90 wm::GetWindowState(window_)->Minimize(); |
| 91 } |
| 92 } |
| 93 |
| 94 void ScopedShowWindow::CancelRestore() { |
| 95 if (!window_) |
| 96 return; |
| 97 window_->parent()->RemoveObserver(this); |
| 98 window_ = stack_window_above_ = NULL; |
| 99 } |
| 100 |
| 101 void ScopedShowWindow::OnWillRemoveWindow(aura::Window* window) { |
| 102 if (window == window_) { |
| 103 CancelRestore(); |
| 104 } else if (window == stack_window_above_) { |
| 105 // If the window this window was above is removed, use the next window down |
| 106 // as the restore marker. |
| 107 stack_window_above_ = GetWindowBelow(stack_window_above_); |
| 108 } |
| 109 } |
| 110 |
| 111 WindowCycleList::WindowCycleList(const WindowList& windows) |
| 112 : windows_(windows), current_index_(0) { |
| 113 ash::Shell::GetInstance()->mru_window_tracker()->SetIgnoreActivations(true); |
| 114 |
| 115 for (WindowList::const_iterator i = windows_.begin(); i != windows_.end(); |
| 116 ++i) { |
| 117 (*i)->AddObserver(this); |
| 118 } |
| 119 } |
| 120 |
| 121 WindowCycleList::~WindowCycleList() { |
| 122 ash::Shell::GetInstance()->mru_window_tracker()->SetIgnoreActivations(false); |
| 123 for (WindowList::const_iterator i = windows_.begin(); i != windows_.end(); |
| 124 ++i) { |
| 125 (*i)->RemoveObserver(this); |
| 126 } |
| 127 if (showing_window_) |
| 128 showing_window_->CancelRestore(); |
| 129 } |
| 130 |
| 131 void WindowCycleList::Step(WindowCycleController::Direction direction) { |
| 132 if (windows_.empty()) |
| 133 return; |
| 134 |
| 135 // When there is only one window, we should give feedback to the user. If the |
| 136 // window is minimized, we should also show it. |
| 137 if (windows_.size() == 1) { |
| 138 ::wm::AnimateWindow(windows_[0], ::wm::WINDOW_ANIMATION_TYPE_BOUNCE); |
| 139 windows_[0]->Show(); |
| 140 wm::GetWindowState(windows_[0])->Activate(); |
| 141 return; |
| 142 } |
| 143 |
| 144 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); |
| 145 |
| 146 // We're in a valid cycle, so step forward or backward. |
| 147 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; |
| 148 |
| 149 // Wrap to window list size. |
| 150 current_index_ = (current_index_ + windows_.size()) % windows_.size(); |
| 151 DCHECK(windows_[current_index_]); |
| 152 |
| 153 // Make sure the next window is visible. |
| 154 showing_window_.reset(new ScopedShowWindow); |
| 155 showing_window_->Show(windows_[current_index_]); |
| 156 } |
| 157 |
| 158 void WindowCycleList::OnWindowDestroyed(aura::Window* window) { |
| 159 window->RemoveObserver(this); |
| 160 |
| 161 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); |
| 162 DCHECK(i != windows_.end()); |
| 163 int removed_index = static_cast<int>(i - windows_.begin()); |
| 164 windows_.erase(i); |
| 165 if (current_index_ > removed_index || |
| 166 current_index_ == static_cast<int>(windows_.size())) |
| 167 current_index_--; |
| 168 } |
| 169 |
| 170 } // namespace ash |
OLD | NEW |