| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/mru_window_tracker.h" | 5 #include "ash/wm/mru_window_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/aura/wm_window_aura.h" | |
| 10 #include "ash/common/shell_window_ids.h" | 9 #include "ash/common/shell_window_ids.h" |
| 10 #include "ash/common/wm/focus_rules.h" |
| 11 #include "ash/common/wm/switchable_windows.h" | 11 #include "ash/common/wm/switchable_windows.h" |
| 12 #include "ash/common/wm/window_state.h" | 12 #include "ash/common/wm/window_state.h" |
| 13 #include "ash/session/session_state_delegate.h" | 13 #include "ash/common/wm_shell.h" |
| 14 #include "ash/shell.h" | 14 #include "ash/common/wm_window.h" |
| 15 #include "ash/wm/ash_focus_rules.h" | |
| 16 #include "ash/wm/window_util.h" | |
| 17 #include "ash/wm/workspace_controller.h" | |
| 18 #include "base/bind.h" | 15 #include "base/bind.h" |
| 19 #include "ui/aura/window_event_dispatcher.h" | |
| 20 #include "ui/events/event.h" | |
| 21 #include "ui/events/event_handler.h" | |
| 22 #include "ui/wm/public/activation_client.h" | |
| 23 | 16 |
| 24 namespace ash { | 17 namespace ash { |
| 25 | 18 |
| 26 namespace { | 19 namespace { |
| 27 | 20 |
| 28 typedef base::Callback<bool(aura::Window*)> CanActivateWindowPredicate; | 21 using CanActivateWindowPredicate = base::Callback<bool(WmWindow*)>; |
| 22 |
| 23 bool CallCanActivate(WmWindow* window) { |
| 24 return window->CanActivate(); |
| 25 } |
| 29 | 26 |
| 30 // Adds the windows that can be cycled through for the specified window id to | 27 // Adds the windows that can be cycled through for the specified window id to |
| 31 // |windows|. | 28 // |windows|. |
| 32 void AddTrackedWindows(aura::Window* root, | 29 void AddTrackedWindows(WmWindow* root, |
| 33 int container_id, | 30 int container_id, |
| 34 MruWindowTracker::WindowList* windows) { | 31 MruWindowTracker::WindowList* windows) { |
| 35 aura::Window* container = Shell::GetContainer(root, container_id); | 32 WmWindow* container = root->GetChildByShellWindowId(container_id); |
| 36 const MruWindowTracker::WindowList& children(container->children()); | 33 const MruWindowTracker::WindowList children(container->GetChildren()); |
| 37 windows->insert(windows->end(), children.begin(), children.end()); | 34 windows->insert(windows->end(), children.begin(), children.end()); |
| 38 } | 35 } |
| 39 | 36 |
| 40 // Returns whether |w1| should be considered less recently used than |w2|. This | 37 // Returns whether |w1| should be considered less recently used than |w2|. This |
| 41 // is used for a stable sort to move minimized windows to the LRU end of the | 38 // is used for a stable sort to move minimized windows to the LRU end of the |
| 42 // list. | 39 // list. |
| 43 bool CompareWindowState(aura::Window* w1, aura::Window* w2) { | 40 bool CompareWindowState(WmWindow* w1, WmWindow* w2) { |
| 44 return ash::wm::IsWindowMinimized(w1) && !ash::wm::IsWindowMinimized(w2); | 41 return w1->GetWindowState()->IsMinimized() && |
| 42 !w2->GetWindowState()->IsMinimized(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 // Returns a list of windows ordered by their stacking order. | 45 // Returns a list of windows ordered by their stacking order. |
| 48 // If |mru_windows| is passed, these windows are moved to the front of the list. | 46 // If |mru_windows| is passed, these windows are moved to the front of the list. |
| 49 // It uses the given |should_include_window_predicate| to determine whether to | 47 // It uses the given |should_include_window_predicate| to determine whether to |
| 50 // include a window in the returned list or not. | 48 // include a window in the returned list or not. |
| 51 MruWindowTracker::WindowList BuildWindowListInternal( | 49 MruWindowTracker::WindowList BuildWindowListInternal( |
| 52 const std::list<aura::Window*>* mru_windows, | 50 const std::list<WmWindow*>* mru_windows, |
| 53 const CanActivateWindowPredicate& should_include_window_predicate) { | 51 const CanActivateWindowPredicate& should_include_window_predicate) { |
| 54 MruWindowTracker::WindowList windows; | 52 MruWindowTracker::WindowList windows; |
| 55 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | 53 WmWindow* active_root = WmShell::Get()->GetRootWindowForNewWindows(); |
| 56 | 54 for (WmWindow* window : WmShell::Get()->GetAllRootWindows()) { |
| 57 aura::Window* active_root = Shell::GetTargetRootWindow(); | 55 if (window == active_root) |
| 58 for (aura::Window::Windows::const_iterator iter = root_windows.begin(); | |
| 59 iter != root_windows.end(); ++iter) { | |
| 60 if (*iter == active_root) | |
| 61 continue; | 56 continue; |
| 62 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i) | 57 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i) |
| 63 AddTrackedWindows(*iter, wm::kSwitchableWindowContainerIds[i], &windows); | 58 AddTrackedWindows(window, wm::kSwitchableWindowContainerIds[i], &windows); |
| 64 } | 59 } |
| 65 | 60 |
| 66 // Add windows in the active root windows last so that the topmost window | 61 // Add windows in the active root windows last so that the topmost window |
| 67 // in the active root window becomes the front of the list. | 62 // in the active root window becomes the front of the list. |
| 68 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i) | 63 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i) |
| 69 AddTrackedWindows(active_root, wm::kSwitchableWindowContainerIds[i], | 64 AddTrackedWindows(active_root, wm::kSwitchableWindowContainerIds[i], |
| 70 &windows); | 65 &windows); |
| 71 | 66 |
| 72 // Removes unfocusable windows. | 67 // Removes unfocusable windows. |
| 73 std::vector<aura::Window*>::iterator itr = windows.begin(); | 68 std::vector<WmWindow*>::iterator itr = windows.begin(); |
| 74 while (itr != windows.end()) { | 69 while (itr != windows.end()) { |
| 75 if (!should_include_window_predicate.Run(*itr)) | 70 if (!should_include_window_predicate.Run(*itr)) |
| 76 itr = windows.erase(itr); | 71 itr = windows.erase(itr); |
| 77 else | 72 else |
| 78 ++itr; | 73 ++itr; |
| 79 } | 74 } |
| 80 | 75 |
| 81 // Put the windows in the mru_windows list at the head, if it's available. | 76 // Put the windows in the mru_windows list at the head, if it's available. |
| 82 if (mru_windows) { | 77 if (mru_windows) { |
| 83 // Iterate through the list backwards, so that we can move each window to | 78 // Iterate through the list backwards, so that we can move each window to |
| 84 // the front of the windows list as we find them. | 79 // the front of the windows list as we find them. |
| 85 for (std::list<aura::Window*>::const_reverse_iterator ix = | 80 for (auto ix = mru_windows->rbegin(); ix != mru_windows->rend(); ++ix) { |
| 86 mru_windows->rbegin(); | |
| 87 ix != mru_windows->rend(); ++ix) { | |
| 88 // Exclude windows in non-switchable containers and those which cannot | 81 // Exclude windows in non-switchable containers and those which cannot |
| 89 // be activated. | 82 // be activated. |
| 90 if (!wm::IsSwitchableContainer(WmWindowAura::Get((*ix)->parent())) || | 83 if (!wm::IsSwitchableContainer((*ix)->GetParent()) || |
| 91 !should_include_window_predicate.Run(*ix)) { | 84 !should_include_window_predicate.Run(*ix)) { |
| 92 continue; | 85 continue; |
| 93 } | 86 } |
| 94 | 87 |
| 95 MruWindowTracker::WindowList::iterator window = | 88 MruWindowTracker::WindowList::iterator window = |
| 96 std::find(windows.begin(), windows.end(), *ix); | 89 std::find(windows.begin(), windows.end(), *ix); |
| 97 if (window != windows.end()) { | 90 if (window != windows.end()) { |
| 98 windows.erase(window); | 91 windows.erase(window); |
| 99 windows.push_back(*ix); | 92 windows.push_back(*ix); |
| 100 } | 93 } |
| 101 } | 94 } |
| 102 } | 95 } |
| 103 | 96 |
| 104 // Move minimized windows to the beginning (LRU end) of the list. | 97 // Move minimized windows to the beginning (LRU end) of the list. |
| 105 std::stable_sort(windows.begin(), windows.end(), CompareWindowState); | 98 std::stable_sort(windows.begin(), windows.end(), CompareWindowState); |
| 106 | 99 |
| 107 // Window cycling expects the topmost window at the front of the list. | 100 // Window cycling expects the topmost window at the front of the list. |
| 108 std::reverse(windows.begin(), windows.end()); | 101 std::reverse(windows.begin(), windows.end()); |
| 109 | 102 |
| 110 return windows; | 103 return windows; |
| 111 } | 104 } |
| 112 | 105 |
| 113 } // namespace | 106 } // namespace |
| 114 | 107 |
| 115 ////////////////////////////////////////////////////////////////////////////// | 108 ////////////////////////////////////////////////////////////////////////////// |
| 116 // MruWindowTracker, public: | 109 // MruWindowTracker, public: |
| 117 | 110 |
| 118 MruWindowTracker::MruWindowTracker( | 111 MruWindowTracker::MruWindowTracker() : ignore_window_activations_(false) { |
| 119 aura::client::ActivationClient* activation_client, | 112 WmShell::Get()->AddActivationObserver(this); |
| 120 ash::wm::AshFocusRules* focus_rules) | |
| 121 : activation_client_(activation_client), | |
| 122 focus_rules_(focus_rules), | |
| 123 ignore_window_activations_(false) { | |
| 124 activation_client_->AddObserver(this); | |
| 125 } | 113 } |
| 126 | 114 |
| 127 MruWindowTracker::~MruWindowTracker() { | 115 MruWindowTracker::~MruWindowTracker() { |
| 128 for (std::list<aura::Window*>::iterator iter = mru_windows_.begin(); | 116 WmShell::Get()->RemoveActivationObserver(this); |
| 129 iter != mru_windows_.end(); ++iter) { | 117 for (WmWindow* window : mru_windows_) |
| 130 (*iter)->RemoveObserver(this); | 118 window->RemoveObserver(this); |
| 131 } | |
| 132 | |
| 133 activation_client_->RemoveObserver(this); | |
| 134 } | 119 } |
| 135 | 120 |
| 136 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() const { | 121 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() const { |
| 137 return BuildWindowListInternal(&mru_windows_, | 122 return BuildWindowListInternal(&mru_windows_, base::Bind(&CallCanActivate)); |
| 138 base::Bind(&ash::wm::CanActivateWindow)); | |
| 139 } | 123 } |
| 140 | 124 |
| 141 MruWindowTracker::WindowList | 125 MruWindowTracker::WindowList |
| 142 MruWindowTracker::BuildWindowListIgnoreModal() const { | 126 MruWindowTracker::BuildWindowListIgnoreModal() const { |
| 143 return BuildWindowListInternal( | 127 return BuildWindowListInternal(nullptr, |
| 144 NULL, | 128 base::Bind(&IsWindowConsideredActivatable)); |
| 145 base::Bind(&MruWindowTracker::IsWindowConsideredActivateable, | |
| 146 base::Unretained(this))); | |
| 147 } | 129 } |
| 148 | 130 |
| 149 void MruWindowTracker::SetIgnoreActivations(bool ignore) { | 131 void MruWindowTracker::SetIgnoreActivations(bool ignore) { |
| 150 ignore_window_activations_ = ignore; | 132 ignore_window_activations_ = ignore; |
| 151 | 133 |
| 152 // If no longer ignoring window activations, move currently active window | 134 // If no longer ignoring window activations, move currently active window |
| 153 // to front. | 135 // to front. |
| 154 if (!ignore) | 136 if (!ignore) |
| 155 SetActiveWindow(wm::GetActiveWindow()); | 137 SetActiveWindow(WmShell::Get()->GetActiveWindow()); |
| 156 } | 138 } |
| 157 | 139 |
| 158 ////////////////////////////////////////////////////////////////////////////// | 140 ////////////////////////////////////////////////////////////////////////////// |
| 159 // MruWindowTracker, private: | 141 // MruWindowTracker, private: |
| 160 | 142 |
| 161 void MruWindowTracker::SetActiveWindow(aura::Window* active_window) { | 143 void MruWindowTracker::SetActiveWindow(WmWindow* active_window) { |
| 162 if (!active_window) | 144 if (!active_window) |
| 163 return; | 145 return; |
| 164 | 146 |
| 165 std::list<aura::Window*>::iterator iter = | 147 std::list<WmWindow*>::iterator iter = |
| 166 std::find(mru_windows_.begin(), mru_windows_.end(), active_window); | 148 std::find(mru_windows_.begin(), mru_windows_.end(), active_window); |
| 167 // Observe all newly tracked windows. | 149 // Observe all newly tracked windows. |
| 168 if (iter == mru_windows_.end()) | 150 if (iter == mru_windows_.end()) |
| 169 active_window->AddObserver(this); | 151 active_window->AddObserver(this); |
| 170 else | 152 else |
| 171 mru_windows_.erase(iter); | 153 mru_windows_.erase(iter); |
| 172 // TODO(flackr): Remove this check if this doesn't fire for a while. This | |
| 173 // should verify that all tracked windows start with a layer, see | |
| 174 // http://crbug.com/291354. | |
| 175 CHECK(active_window->layer()); | |
| 176 mru_windows_.push_front(active_window); | 154 mru_windows_.push_front(active_window); |
| 177 } | 155 } |
| 178 | 156 |
| 179 void MruWindowTracker::OnWindowActivated( | 157 void MruWindowTracker::OnWindowActivated(WmWindow* gained_active, |
| 180 aura::client::ActivationChangeObserver::ActivationReason reason, | 158 WmWindow* lost_active) { |
| 181 aura::Window* gained_active, | |
| 182 aura::Window* lost_active) { | |
| 183 if (!ignore_window_activations_) | 159 if (!ignore_window_activations_) |
| 184 SetActiveWindow(gained_active); | 160 SetActiveWindow(gained_active); |
| 185 } | 161 } |
| 186 | 162 |
| 187 void MruWindowTracker::OnWindowDestroyed(aura::Window* window) { | 163 void MruWindowTracker::OnWindowDestroyed(WmWindow* window) { |
| 188 // It's possible for OnWindowActivated() to be called after | 164 // It's possible for OnWindowActivated() to be called after |
| 189 // OnWindowDestroying(). This means we need to override OnWindowDestroyed() | 165 // OnWindowDestroying(). This means we need to override OnWindowDestroyed() |
| 190 // else we may end up with a deleted window in |mru_windows_|. | 166 // else we may end up with a deleted window in |mru_windows_|. |
| 191 mru_windows_.remove(window); | 167 mru_windows_.remove(window); |
| 192 window->RemoveObserver(this); | 168 window->RemoveObserver(this); |
| 193 } | 169 } |
| 194 | 170 |
| 195 bool MruWindowTracker::IsWindowConsideredActivateable( | |
| 196 aura::Window* window) const { | |
| 197 return focus_rules_->IsWindowConsideredActivatable(window); | |
| 198 } | |
| 199 | |
| 200 } // namespace ash | 171 } // namespace ash |
| OLD | NEW |