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

Side by Side Diff: ash/wm/overview/window_selector.cc

Issue 260883005: Separated alt-tab window cycle from overview mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed another unused function Created 6 years, 7 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/overview/window_selector.h ('k') | ash/wm/overview/window_selector_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/overview/window_selector.h" 5 #include "ash/wm/overview/window_selector.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ash/ash_switches.h" 9 #include "ash/ash_switches.h"
10 #include "ash/root_window_controller.h" 10 #include "ash/root_window_controller.h"
11 #include "ash/shell.h" 11 #include "ash/shell.h"
12 #include "ash/switchable_windows.h" 12 #include "ash/switchable_windows.h"
13 #include "ash/wm/overview/window_overview.h" 13 #include "ash/wm/overview/window_overview.h"
14 #include "ash/wm/overview/window_selector_delegate.h" 14 #include "ash/wm/overview/window_selector_delegate.h"
15 #include "ash/wm/overview/window_selector_panels.h" 15 #include "ash/wm/overview/window_selector_panels.h"
16 #include "ash/wm/overview/window_selector_window.h" 16 #include "ash/wm/overview/window_selector_window.h"
17 #include "ash/wm/window_state.h" 17 #include "ash/wm/window_state.h"
18 #include "base/auto_reset.h" 18 #include "base/auto_reset.h"
19 #include "base/command_line.h" 19 #include "base/command_line.h"
20 #include "base/metrics/histogram.h" 20 #include "base/metrics/histogram.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "ui/aura/client/focus_client.h" 22 #include "ui/aura/client/focus_client.h"
23 #include "ui/aura/window.h" 23 #include "ui/aura/window.h"
24 #include "ui/aura/window_event_dispatcher.h"
25 #include "ui/aura/window_observer.h" 24 #include "ui/aura/window_observer.h"
26 #include "ui/events/event.h"
27 #include "ui/events/event_handler.h"
28 #include "ui/wm/core/window_util.h" 25 #include "ui/wm/core/window_util.h"
29 #include "ui/wm/public/activation_client.h" 26 #include "ui/wm/public/activation_client.h"
30 27
31 namespace ash { 28 namespace ash {
32 29
33 namespace { 30 namespace {
34 31
35 // A comparator for locating a given selectable window. 32 // A comparator for locating a given selectable window.
36 struct WindowSelectorItemComparator 33 struct WindowSelectorItemComparator
37 : public std::unary_function<WindowSelectorItem*, bool> { 34 : public std::unary_function<WindowSelectorItem*, bool> {
(...skipping 29 matching lines...) Expand all
67 : root_window(root) { 64 : root_window(root) {
68 } 65 }
69 66
70 bool operator()(WindowSelectorItem* item) const { 67 bool operator()(WindowSelectorItem* item) const {
71 return item->GetRootWindow() == root_window; 68 return item->GetRootWindow() == root_window;
72 } 69 }
73 70
74 const aura::Window* root_window; 71 const aura::Window* root_window;
75 }; 72 };
76 73
77 // Filter to watch for the termination of a keyboard gesture to cycle through
78 // multiple windows.
79 class WindowSelectorEventFilter : public ui::EventHandler {
80 public:
81 WindowSelectorEventFilter(WindowSelector* selector);
82 virtual ~WindowSelectorEventFilter();
83
84 // Overridden from ui::EventHandler:
85 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
86
87 private:
88 // A weak pointer to the WindowSelector which owns this instance.
89 WindowSelector* selector_;
90
91 DISALLOW_COPY_AND_ASSIGN(WindowSelectorEventFilter);
92 };
93
94 // Watch for all keyboard events by filtering the root window.
95 WindowSelectorEventFilter::WindowSelectorEventFilter(WindowSelector* selector)
96 : selector_(selector) {
97 Shell::GetInstance()->AddPreTargetHandler(this);
98 }
99
100 WindowSelectorEventFilter::~WindowSelectorEventFilter() {
101 Shell::GetInstance()->RemovePreTargetHandler(this);
102 }
103
104 void WindowSelectorEventFilter::OnKeyEvent(ui::KeyEvent* event) {
105 // Views uses VKEY_MENU for both left and right Alt keys.
106 if (event->key_code() == ui::VKEY_MENU &&
107 event->type() == ui::ET_KEY_RELEASED) {
108 selector_->SelectWindow();
109 // Warning: |this| will be deleted from here on.
110 }
111 }
112
113 // Triggers a shelf visibility update on all root window controllers. 74 // Triggers a shelf visibility update on all root window controllers.
114 void UpdateShelfVisibility() { 75 void UpdateShelfVisibility() {
115 Shell::RootWindowControllerList root_window_controllers = 76 Shell::RootWindowControllerList root_window_controllers =
116 Shell::GetInstance()->GetAllRootWindowControllers(); 77 Shell::GetInstance()->GetAllRootWindowControllers();
117 for (Shell::RootWindowControllerList::iterator iter = 78 for (Shell::RootWindowControllerList::iterator iter =
118 root_window_controllers.begin(); 79 root_window_controllers.begin();
119 iter != root_window_controllers.end(); ++iter) { 80 iter != root_window_controllers.end(); ++iter) {
120 (*iter)->UpdateShelfVisibility(); 81 (*iter)->UpdateShelfVisibility();
121 } 82 }
122 } 83 }
123 84
124 // Returns the window immediately below |window| in the current container.
125 aura::Window* GetWindowBelow(aura::Window* window) {
126 aura::Window* parent = window->parent();
127 if (!parent)
128 return NULL;
129 aura::Window* below = NULL;
130 for (aura::Window::Windows::const_iterator iter = parent->children().begin();
131 iter != parent->children().end(); ++iter) {
132 if (*iter == window)
133 return below;
134 below = *iter;
135 }
136 NOTREACHED();
137 return NULL;
138 }
139
140 } // namespace 85 } // namespace
141 86
142 // This class restores and moves a window to the front of the stacking order for
143 // the duration of the class's scope.
144 class ScopedShowWindow : public aura::WindowObserver {
145 public:
146 ScopedShowWindow();
147 virtual ~ScopedShowWindow();
148
149 // Show |window| at the top of the stacking order.
150 void Show(aura::Window* window);
151
152 // Cancel restoring the window on going out of scope.
153 void CancelRestore();
154
155 aura::Window* window() { return window_; }
156
157 // aura::WindowObserver:
158 virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE;
159
160 private:
161 // The window being shown.
162 aura::Window* window_;
163
164 // The window immediately below where window_ belongs.
165 aura::Window* stack_window_above_;
166
167 // If true, minimize window_ on going out of scope.
168 bool minimized_;
169
170 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow);
171 };
172
173 ScopedShowWindow::ScopedShowWindow()
174 : window_(NULL),
175 stack_window_above_(NULL),
176 minimized_(false) {
177 }
178
179 void ScopedShowWindow::Show(aura::Window* window) {
180 DCHECK(!window_);
181 window_ = window;
182 stack_window_above_ = GetWindowBelow(window);
183 minimized_ = wm::GetWindowState(window)->IsMinimized();
184 window_->parent()->AddObserver(this);
185 window_->Show();
186 wm::GetWindowState(window_)->Activate();
187 }
188
189 ScopedShowWindow::~ScopedShowWindow() {
190 if (window_) {
191 window_->parent()->RemoveObserver(this);
192
193 // Restore window's stacking position.
194 if (stack_window_above_)
195 window_->parent()->StackChildAbove(window_, stack_window_above_);
196 else
197 window_->parent()->StackChildAtBottom(window_);
198
199 // Restore minimized state.
200 if (minimized_)
201 wm::GetWindowState(window_)->Minimize();
202 }
203 }
204
205 void ScopedShowWindow::CancelRestore() {
206 if (!window_)
207 return;
208 window_->parent()->RemoveObserver(this);
209 window_ = stack_window_above_ = NULL;
210 }
211
212 void ScopedShowWindow::OnWillRemoveWindow(aura::Window* window) {
213 if (window == window_) {
214 CancelRestore();
215 } else if (window == stack_window_above_) {
216 // If the window this window was above is removed, use the next window down
217 // as the restore marker.
218 stack_window_above_ = GetWindowBelow(stack_window_above_);
219 }
220 }
221
222 WindowSelector::WindowSelector(const WindowList& windows, 87 WindowSelector::WindowSelector(const WindowList& windows,
223 WindowSelector::Mode mode,
224 WindowSelectorDelegate* delegate) 88 WindowSelectorDelegate* delegate)
225 : mode_(mode), 89 : delegate_(delegate),
226 delegate_(delegate),
227 selected_window_(0),
228 restore_focus_window_(aura::client::GetFocusClient( 90 restore_focus_window_(aura::client::GetFocusClient(
229 Shell::GetPrimaryRootWindow())->GetFocusedWindow()), 91 Shell::GetPrimaryRootWindow())->GetFocusedWindow()),
230 ignore_activations_(false) { 92 ignore_activations_(false) {
231 DCHECK(delegate_); 93 DCHECK(delegate_);
232 94
233 if (restore_focus_window_) 95 if (restore_focus_window_)
234 restore_focus_window_->AddObserver(this); 96 restore_focus_window_->AddObserver(this);
235 97
236 std::vector<WindowSelectorPanels*> panels_items; 98 std::vector<WindowSelectorPanels*> panels_items;
237 for (size_t i = 0; i < windows.size(); ++i) { 99 for (size_t i = 0; i < windows.size(); ++i) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 for (aura::Window::Windows::const_iterator iter = root_windows.begin(); 135 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
274 iter != root_windows.end(); ++iter) { 136 iter != root_windows.end(); ++iter) {
275 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) { 137 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
276 aura::Window* container = Shell::GetContainer(*iter, 138 aura::Window* container = Shell::GetContainer(*iter,
277 kSwitchableWindowContainerIds[i]); 139 kSwitchableWindowContainerIds[i]);
278 container->AddObserver(this); 140 container->AddObserver(this);
279 observed_windows_.insert(container); 141 observed_windows_.insert(container);
280 } 142 }
281 } 143 }
282 144
283 if (mode == WindowSelector::CYCLE) { 145 StartOverview();
284 cycle_start_time_ = base::Time::Now();
285 event_handler_.reset(new WindowSelectorEventFilter(this));
286 } else {
287 StartOverview();
288 }
289 } 146 }
290 147
291 WindowSelector::~WindowSelector() { 148 WindowSelector::~WindowSelector() {
292 ResetFocusRestoreWindow(true); 149 ResetFocusRestoreWindow(true);
293 for (std::set<aura::Window*>::iterator iter = observed_windows_.begin(); 150 for (std::set<aura::Window*>::iterator iter = observed_windows_.begin();
294 iter != observed_windows_.end(); ++iter) { 151 iter != observed_windows_.end(); ++iter) {
295 (*iter)->RemoveObserver(this); 152 (*iter)->RemoveObserver(this);
296 } 153 }
297 Shell::GetInstance()->activation_client()->RemoveObserver(this); 154 Shell::GetInstance()->activation_client()->RemoveObserver(this);
298 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 155 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
299 window_overview_.reset(); 156 window_overview_.reset();
300 // Clearing the window list resets the ignored_by_shelf flag on the windows. 157 // Clearing the window list resets the ignored_by_shelf flag on the windows.
301 windows_.clear(); 158 windows_.clear();
302 UpdateShelfVisibility(); 159 UpdateShelfVisibility();
303
304 if (!cycle_start_time_.is_null()) {
305 UMA_HISTOGRAM_MEDIUM_TIMES("Ash.WindowSelector.CycleTime",
306 base::Time::Now() - cycle_start_time_);
307 }
308 }
309
310 void WindowSelector::Step(WindowSelector::Direction direction) {
311 DCHECK(!windows_.empty());
312 // Upgrade to CYCLE mode if currently in OVERVIEW mode.
313 if (mode_ != CYCLE) {
314 event_handler_.reset(new WindowSelectorEventFilter(this));
315 DCHECK(window_overview_);
316 // Set the initial selection window to animate to the new selection.
317 window_overview_->SetSelection(selected_window_);
318 window_overview_->MoveToSingleRootWindow(
319 windows_[selected_window_]->GetRootWindow());
320 mode_ = CYCLE;
321 }
322
323 selected_window_ = (selected_window_ + windows_.size() +
324 (direction == WindowSelector::FORWARD ? 1 : -1)) % windows_.size();
325 if (window_overview_) {
326 window_overview_->SetSelection(selected_window_);
327 } else {
328 base::AutoReset<bool> restoring_focus(&ignore_activations_, true);
329 showing_window_.reset(new ScopedShowWindow);
330 showing_window_->Show(windows_[selected_window_]->SelectionWindow());
331 }
332 }
333
334 void WindowSelector::SelectWindow() {
335 SelectWindow(windows_[selected_window_]->SelectionWindow());
336 } 160 }
337 161
338 void WindowSelector::SelectWindow(aura::Window* window) { 162 void WindowSelector::SelectWindow(aura::Window* window) {
339 ResetFocusRestoreWindow(false); 163 ResetFocusRestoreWindow(false);
340 if (showing_window_ && showing_window_->window() == window)
341 showing_window_->CancelRestore();
342 ScopedVector<WindowSelectorItem>::iterator iter = 164 ScopedVector<WindowSelectorItem>::iterator iter =
343 std::find_if(windows_.begin(), windows_.end(), 165 std::find_if(windows_.begin(), windows_.end(),
344 WindowSelectorItemTargetComparator(window)); 166 WindowSelectorItemTargetComparator(window));
345 DCHECK(iter != windows_.end()); 167 DCHECK(iter != windows_.end());
346 // The selected window should not be minimized when window selection is 168 // The selected window should not be minimized when window selection is
347 // ended. 169 // ended.
348 (*iter)->RestoreWindowOnExit(window); 170 (*iter)->RestoreWindowOnExit(window);
349 delegate_->OnWindowSelected(window); 171 delegate_->OnWindowSelected(window);
350 } 172 }
351 173
(...skipping 29 matching lines...) Expand all
381 restore_focus_window_ = NULL; 203 restore_focus_window_ = NULL;
382 if (iter == windows_.end()) 204 if (iter == windows_.end())
383 return; 205 return;
384 206
385 (*iter)->RemoveWindow(window); 207 (*iter)->RemoveWindow(window);
386 // If there are still windows in this selector entry then the overview is 208 // If there are still windows in this selector entry then the overview is
387 // still active and the active selection remains the same. 209 // still active and the active selection remains the same.
388 if (!(*iter)->empty()) 210 if (!(*iter)->empty())
389 return; 211 return;
390 212
391 size_t deleted_index = iter - windows_.begin();
392 windows_.erase(iter); 213 windows_.erase(iter);
393 if (windows_.empty()) { 214 if (windows_.empty()) {
394 CancelSelection(); 215 CancelSelection();
395 return; 216 return;
396 } 217 }
397 if (window_overview_) 218 if (window_overview_)
398 window_overview_->OnWindowsChanged(); 219 window_overview_->OnWindowsChanged();
399 if (mode_ == CYCLE && selected_window_ >= deleted_index) {
400 if (selected_window_ > deleted_index)
401 selected_window_--;
402 selected_window_ = selected_window_ % windows_.size();
403 if (window_overview_)
404 window_overview_->SetSelection(selected_window_);
405 }
406 } 220 }
407 221
408 void WindowSelector::OnWindowBoundsChanged(aura::Window* window, 222 void WindowSelector::OnWindowBoundsChanged(aura::Window* window,
409 const gfx::Rect& old_bounds, 223 const gfx::Rect& old_bounds,
410 const gfx::Rect& new_bounds) { 224 const gfx::Rect& new_bounds) {
411 if (!window_overview_) 225 if (!window_overview_)
412 return; 226 return;
413 227
414 ScopedVector<WindowSelectorItem>::iterator iter = 228 ScopedVector<WindowSelectorItem>::iterator iter =
415 std::find_if(windows_.begin(), windows_.end(), 229 std::find_if(windows_.begin(), windows_.end(),
(...skipping 26 matching lines...) Expand all
442 ResetFocusRestoreWindow(false); 256 ResetFocusRestoreWindow(false);
443 CancelSelection(); 257 CancelSelection();
444 } 258 }
445 259
446 void WindowSelector::StartOverview() { 260 void WindowSelector::StartOverview() {
447 DCHECK(!window_overview_); 261 DCHECK(!window_overview_);
448 // Remove focus from active window before entering overview. 262 // Remove focus from active window before entering overview.
449 aura::client::GetFocusClient( 263 aura::client::GetFocusClient(
450 Shell::GetPrimaryRootWindow())->FocusWindow(NULL); 264 Shell::GetPrimaryRootWindow())->FocusWindow(NULL);
451 265
452 aura::Window* overview_root = NULL; 266 window_overview_.reset(new WindowOverview(this, &windows_));
453 if (mode_ == CYCLE)
454 overview_root = windows_[selected_window_]->GetRootWindow();
455 window_overview_.reset(new WindowOverview(this, &windows_, overview_root));
456 if (mode_ == CYCLE)
457 window_overview_->SetSelection(selected_window_);
458 UpdateShelfVisibility(); 267 UpdateShelfVisibility();
459 } 268 }
460 269
461 void WindowSelector::ResetFocusRestoreWindow(bool focus) { 270 void WindowSelector::ResetFocusRestoreWindow(bool focus) {
462 if (!restore_focus_window_) 271 if (!restore_focus_window_)
463 return; 272 return;
464 if (focus) { 273 if (focus) {
465 base::AutoReset<bool> restoring_focus(&ignore_activations_, true); 274 base::AutoReset<bool> restoring_focus(&ignore_activations_, true);
466 restore_focus_window_->Focus(); 275 restore_focus_window_->Focus();
467 } 276 }
468 // If the window is in the observed_windows_ list it needs to continue to be 277 // If the window is in the observed_windows_ list it needs to continue to be
469 // observed. 278 // observed.
470 if (observed_windows_.find(restore_focus_window_) == 279 if (observed_windows_.find(restore_focus_window_) ==
471 observed_windows_.end()) { 280 observed_windows_.end()) {
472 restore_focus_window_->RemoveObserver(this); 281 restore_focus_window_->RemoveObserver(this);
473 } 282 }
474 restore_focus_window_ = NULL; 283 restore_focus_window_ = NULL;
475 } 284 }
476 285
477 } // namespace ash 286 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/overview/window_selector.h ('k') | ash/wm/overview/window_selector_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698