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/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 Loading... |
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), | 90 selected_window_(0), |
228 restore_focus_window_(aura::client::GetFocusClient( | 91 restore_focus_window_(aura::client::GetFocusClient( |
229 Shell::GetPrimaryRootWindow())->GetFocusedWindow()), | 92 Shell::GetPrimaryRootWindow())->GetFocusedWindow()), |
230 ignore_activations_(false) { | 93 ignore_activations_(false) { |
231 DCHECK(delegate_); | 94 DCHECK(delegate_); |
232 | 95 |
233 if (restore_focus_window_) | 96 if (restore_focus_window_) |
234 restore_focus_window_->AddObserver(this); | 97 restore_focus_window_->AddObserver(this); |
235 | 98 |
236 std::vector<WindowSelectorPanels*> panels_items; | 99 std::vector<WindowSelectorPanels*> panels_items; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 for (aura::Window::Windows::const_iterator iter = root_windows.begin(); | 136 for (aura::Window::Windows::const_iterator iter = root_windows.begin(); |
274 iter != root_windows.end(); ++iter) { | 137 iter != root_windows.end(); ++iter) { |
275 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) { | 138 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) { |
276 aura::Window* container = Shell::GetContainer(*iter, | 139 aura::Window* container = Shell::GetContainer(*iter, |
277 kSwitchableWindowContainerIds[i]); | 140 kSwitchableWindowContainerIds[i]); |
278 container->AddObserver(this); | 141 container->AddObserver(this); |
279 observed_windows_.insert(container); | 142 observed_windows_.insert(container); |
280 } | 143 } |
281 } | 144 } |
282 | 145 |
283 if (mode == WindowSelector::CYCLE) { | 146 StartOverview(); |
284 cycle_start_time_ = base::Time::Now(); | |
285 event_handler_.reset(new WindowSelectorEventFilter(this)); | |
286 } else { | |
287 StartOverview(); | |
288 } | |
289 } | 147 } |
290 | 148 |
291 WindowSelector::~WindowSelector() { | 149 WindowSelector::~WindowSelector() { |
292 ResetFocusRestoreWindow(true); | 150 ResetFocusRestoreWindow(true); |
293 for (std::set<aura::Window*>::iterator iter = observed_windows_.begin(); | 151 for (std::set<aura::Window*>::iterator iter = observed_windows_.begin(); |
294 iter != observed_windows_.end(); ++iter) { | 152 iter != observed_windows_.end(); ++iter) { |
295 (*iter)->RemoveObserver(this); | 153 (*iter)->RemoveObserver(this); |
296 } | 154 } |
297 Shell::GetInstance()->activation_client()->RemoveObserver(this); | 155 Shell::GetInstance()->activation_client()->RemoveObserver(this); |
298 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | 156 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
299 window_overview_.reset(); | 157 window_overview_.reset(); |
300 // Clearing the window list resets the ignored_by_shelf flag on the windows. | 158 // Clearing the window list resets the ignored_by_shelf flag on the windows. |
301 windows_.clear(); | 159 windows_.clear(); |
302 UpdateShelfVisibility(); | 160 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 } | 161 } |
333 | 162 |
334 void WindowSelector::SelectWindow() { | 163 void WindowSelector::SelectWindow() { |
335 SelectWindow(windows_[selected_window_]->SelectionWindow()); | 164 SelectWindow(windows_[selected_window_]->SelectionWindow()); |
336 } | 165 } |
337 | 166 |
338 void WindowSelector::SelectWindow(aura::Window* window) { | 167 void WindowSelector::SelectWindow(aura::Window* window) { |
339 ResetFocusRestoreWindow(false); | 168 ResetFocusRestoreWindow(false); |
340 if (showing_window_ && showing_window_->window() == window) | |
341 showing_window_->CancelRestore(); | |
342 ScopedVector<WindowSelectorItem>::iterator iter = | 169 ScopedVector<WindowSelectorItem>::iterator iter = |
343 std::find_if(windows_.begin(), windows_.end(), | 170 std::find_if(windows_.begin(), windows_.end(), |
344 WindowSelectorItemTargetComparator(window)); | 171 WindowSelectorItemTargetComparator(window)); |
345 DCHECK(iter != windows_.end()); | 172 DCHECK(iter != windows_.end()); |
346 // The selected window should not be minimized when window selection is | 173 // The selected window should not be minimized when window selection is |
347 // ended. | 174 // ended. |
348 (*iter)->RestoreWindowOnExit(window); | 175 (*iter)->RestoreWindowOnExit(window); |
349 delegate_->OnWindowSelected(window); | 176 delegate_->OnWindowSelected(window); |
350 } | 177 } |
351 | 178 |
(...skipping 29 matching lines...) Expand all Loading... |
381 restore_focus_window_ = NULL; | 208 restore_focus_window_ = NULL; |
382 if (iter == windows_.end()) | 209 if (iter == windows_.end()) |
383 return; | 210 return; |
384 | 211 |
385 (*iter)->RemoveWindow(window); | 212 (*iter)->RemoveWindow(window); |
386 // If there are still windows in this selector entry then the overview is | 213 // If there are still windows in this selector entry then the overview is |
387 // still active and the active selection remains the same. | 214 // still active and the active selection remains the same. |
388 if (!(*iter)->empty()) | 215 if (!(*iter)->empty()) |
389 return; | 216 return; |
390 | 217 |
391 size_t deleted_index = iter - windows_.begin(); | |
392 windows_.erase(iter); | 218 windows_.erase(iter); |
393 if (windows_.empty()) { | 219 if (windows_.empty()) { |
394 CancelSelection(); | 220 CancelSelection(); |
395 return; | 221 return; |
396 } | 222 } |
397 if (window_overview_) | 223 if (window_overview_) |
398 window_overview_->OnWindowsChanged(); | 224 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 } | 225 } |
407 | 226 |
408 void WindowSelector::OnWindowBoundsChanged(aura::Window* window, | 227 void WindowSelector::OnWindowBoundsChanged(aura::Window* window, |
409 const gfx::Rect& old_bounds, | 228 const gfx::Rect& old_bounds, |
410 const gfx::Rect& new_bounds) { | 229 const gfx::Rect& new_bounds) { |
411 if (!window_overview_) | 230 if (!window_overview_) |
412 return; | 231 return; |
413 | 232 |
414 ScopedVector<WindowSelectorItem>::iterator iter = | 233 ScopedVector<WindowSelectorItem>::iterator iter = |
415 std::find_if(windows_.begin(), windows_.end(), | 234 std::find_if(windows_.begin(), windows_.end(), |
(...skipping 26 matching lines...) Expand all Loading... |
442 ResetFocusRestoreWindow(false); | 261 ResetFocusRestoreWindow(false); |
443 CancelSelection(); | 262 CancelSelection(); |
444 } | 263 } |
445 | 264 |
446 void WindowSelector::StartOverview() { | 265 void WindowSelector::StartOverview() { |
447 DCHECK(!window_overview_); | 266 DCHECK(!window_overview_); |
448 // Remove focus from active window before entering overview. | 267 // Remove focus from active window before entering overview. |
449 aura::client::GetFocusClient( | 268 aura::client::GetFocusClient( |
450 Shell::GetPrimaryRootWindow())->FocusWindow(NULL); | 269 Shell::GetPrimaryRootWindow())->FocusWindow(NULL); |
451 | 270 |
452 aura::Window* overview_root = NULL; | 271 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(); | 272 UpdateShelfVisibility(); |
459 } | 273 } |
460 | 274 |
461 void WindowSelector::ResetFocusRestoreWindow(bool focus) { | 275 void WindowSelector::ResetFocusRestoreWindow(bool focus) { |
462 if (!restore_focus_window_) | 276 if (!restore_focus_window_) |
463 return; | 277 return; |
464 if (focus) { | 278 if (focus) { |
465 base::AutoReset<bool> restoring_focus(&ignore_activations_, true); | 279 base::AutoReset<bool> restoring_focus(&ignore_activations_, true); |
466 restore_focus_window_->Focus(); | 280 restore_focus_window_->Focus(); |
467 } | 281 } |
468 // If the window is in the observed_windows_ list it needs to continue to be | 282 // If the window is in the observed_windows_ list it needs to continue to be |
469 // observed. | 283 // observed. |
470 if (observed_windows_.find(restore_focus_window_) == | 284 if (observed_windows_.find(restore_focus_window_) == |
471 observed_windows_.end()) { | 285 observed_windows_.end()) { |
472 restore_focus_window_->RemoveObserver(this); | 286 restore_focus_window_->RemoveObserver(this); |
473 } | 287 } |
474 restore_focus_window_ = NULL; | 288 restore_focus_window_ = NULL; |
475 } | 289 } |
476 | 290 |
477 } // namespace ash | 291 } // namespace ash |
OLD | NEW |