Chromium Code Reviews| 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. | |
| 136 if (windows_.size() == 1) { | |
| 137 ::wm::AnimateWindow(windows_[0], ::wm::WINDOW_ANIMATION_TYPE_BOUNCE); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); | |
| 142 | |
| 143 // We're in a valid cycle, so step forward or backward. | |
| 144 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; | |
| 145 | |
| 146 // Wrap to window list size. | |
| 147 current_index_ = (current_index_ + windows_.size()) % windows_.size(); | |
| 148 DCHECK(windows_[current_index_]); | |
| 149 | |
| 150 // Make sure the next window is visible. | |
| 151 showing_window_.reset(new ScopedShowWindow); | |
| 152 showing_window_->Show(windows_[current_index_]); | |
| 153 } | |
| 154 | |
| 155 void WindowCycleList::OnWindowDestroyed(aura::Window* window) { | |
| 156 window->RemoveObserver(this); | |
| 157 | |
| 158 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); | |
| 159 DCHECK(i != windows_.end()); | |
| 160 int removed_index = static_cast<int>(i - windows_.begin()); | |
| 161 windows_.erase(i); | |
| 162 if (current_index_ > removed_index || | |
| 163 current_index_ == static_cast<int>(windows_.size())) | |
| 164 current_index_--; | |
| 165 // If the first window was destroyed, move the index to the new first window. | |
|
flackr
2014/05/05 13:58:56
If the first window was destroyed, either current_
Nina
2014/05/05 15:44:29
Hmm this is true, and if there are no windows call
| |
| 166 if (current_index_ == -1) | |
| 167 current_index_ = 0; | |
| 168 } | |
| 169 | |
| 170 } // namespace ash | |
| OLD | NEW |