Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1176)

Side by Side Diff: ash/wm/mru_window_tracker.cc

Issue 2045793002: Moves MruWindowTracker into common (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mru_window_tracker
Patch Set: not relative Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/wm/mru_window_tracker.h ('k') | ash/wm/mru_window_tracker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/wm/mru_window_tracker.h"
6
7 #include <algorithm>
8
9 #include "ash/common/shell_window_ids.h"
10 #include "ash/common/wm/focus_rules.h"
11 #include "ash/common/wm/switchable_windows.h"
12 #include "ash/common/wm/window_state.h"
13 #include "ash/common/wm_shell.h"
14 #include "ash/common/wm_window.h"
15 #include "base/bind.h"
16
17 namespace ash {
18
19 namespace {
20
21 using CanActivateWindowPredicate = base::Callback<bool(WmWindow*)>;
22
23 bool CallCanActivate(WmWindow* window) {
24 return window->CanActivate();
25 }
26
27 // Adds the windows that can be cycled through for the specified window id to
28 // |windows|.
29 void AddTrackedWindows(WmWindow* root,
30 int container_id,
31 MruWindowTracker::WindowList* windows) {
32 WmWindow* container = root->GetChildByShellWindowId(container_id);
33 const MruWindowTracker::WindowList children(container->GetChildren());
34 windows->insert(windows->end(), children.begin(), children.end());
35 }
36
37 // Returns whether |w1| should be considered less recently used than |w2|. This
38 // is used for a stable sort to move minimized windows to the LRU end of the
39 // list.
40 bool CompareWindowState(WmWindow* w1, WmWindow* w2) {
41 return w1->GetWindowState()->IsMinimized() &&
42 !w2->GetWindowState()->IsMinimized();
43 }
44
45 // Returns a list of windows ordered by their stacking order.
46 // If |mru_windows| is passed, these windows are moved to the front of the list.
47 // It uses the given |should_include_window_predicate| to determine whether to
48 // include a window in the returned list or not.
49 MruWindowTracker::WindowList BuildWindowListInternal(
50 const std::list<WmWindow*>* mru_windows,
51 const CanActivateWindowPredicate& should_include_window_predicate) {
52 MruWindowTracker::WindowList windows;
53 WmWindow* active_root = WmShell::Get()->GetRootWindowForNewWindows();
54 for (WmWindow* window : WmShell::Get()->GetAllRootWindows()) {
55 if (window == active_root)
56 continue;
57 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i)
58 AddTrackedWindows(window, wm::kSwitchableWindowContainerIds[i], &windows);
59 }
60
61 // Add windows in the active root windows last so that the topmost window
62 // in the active root window becomes the front of the list.
63 for (size_t i = 0; i < wm::kSwitchableWindowContainerIdsLength; ++i)
64 AddTrackedWindows(active_root, wm::kSwitchableWindowContainerIds[i],
65 &windows);
66
67 // Removes unfocusable windows.
68 std::vector<WmWindow*>::iterator itr = windows.begin();
69 while (itr != windows.end()) {
70 if (!should_include_window_predicate.Run(*itr))
71 itr = windows.erase(itr);
72 else
73 ++itr;
74 }
75
76 // Put the windows in the mru_windows list at the head, if it's available.
77 if (mru_windows) {
78 // Iterate through the list backwards, so that we can move each window to
79 // the front of the windows list as we find them.
80 for (auto ix = mru_windows->rbegin(); ix != mru_windows->rend(); ++ix) {
81 // Exclude windows in non-switchable containers and those which cannot
82 // be activated.
83 if (!wm::IsSwitchableContainer((*ix)->GetParent()) ||
84 !should_include_window_predicate.Run(*ix)) {
85 continue;
86 }
87
88 MruWindowTracker::WindowList::iterator window =
89 std::find(windows.begin(), windows.end(), *ix);
90 if (window != windows.end()) {
91 windows.erase(window);
92 windows.push_back(*ix);
93 }
94 }
95 }
96
97 // Move minimized windows to the beginning (LRU end) of the list.
98 std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
99
100 // Window cycling expects the topmost window at the front of the list.
101 std::reverse(windows.begin(), windows.end());
102
103 return windows;
104 }
105
106 } // namespace
107
108 //////////////////////////////////////////////////////////////////////////////
109 // MruWindowTracker, public:
110
111 MruWindowTracker::MruWindowTracker() : ignore_window_activations_(false) {
112 WmShell::Get()->AddActivationObserver(this);
113 }
114
115 MruWindowTracker::~MruWindowTracker() {
116 WmShell::Get()->RemoveActivationObserver(this);
117 for (WmWindow* window : mru_windows_)
118 window->RemoveObserver(this);
119 }
120
121 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() const {
122 return BuildWindowListInternal(&mru_windows_, base::Bind(&CallCanActivate));
123 }
124
125 MruWindowTracker::WindowList
126 MruWindowTracker::BuildWindowListIgnoreModal() const {
127 return BuildWindowListInternal(nullptr,
128 base::Bind(&IsWindowConsideredActivatable));
129 }
130
131 void MruWindowTracker::SetIgnoreActivations(bool ignore) {
132 ignore_window_activations_ = ignore;
133
134 // If no longer ignoring window activations, move currently active window
135 // to front.
136 if (!ignore)
137 SetActiveWindow(WmShell::Get()->GetActiveWindow());
138 }
139
140 //////////////////////////////////////////////////////////////////////////////
141 // MruWindowTracker, private:
142
143 void MruWindowTracker::SetActiveWindow(WmWindow* active_window) {
144 if (!active_window)
145 return;
146
147 std::list<WmWindow*>::iterator iter =
148 std::find(mru_windows_.begin(), mru_windows_.end(), active_window);
149 // Observe all newly tracked windows.
150 if (iter == mru_windows_.end())
151 active_window->AddObserver(this);
152 else
153 mru_windows_.erase(iter);
154 mru_windows_.push_front(active_window);
155 }
156
157 void MruWindowTracker::OnWindowActivated(WmWindow* gained_active,
158 WmWindow* lost_active) {
159 if (!ignore_window_activations_)
160 SetActiveWindow(gained_active);
161 }
162
163 void MruWindowTracker::OnWindowDestroyed(WmWindow* window) {
164 // It's possible for OnWindowActivated() to be called after
165 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
166 // else we may end up with a deleted window in |mru_windows_|.
167 mru_windows_.remove(window);
168 window->RemoveObserver(this);
169 }
170
171 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/mru_window_tracker.h ('k') | ash/wm/mru_window_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698