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/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/wm/activation_controller.h" | 10 #include "ash/wm/activation_controller.h" |
11 #include "ash/wm/mru_window_tracker.h" | 11 #include "ash/wm/mru_window_tracker.h" |
12 #include "ash/wm/overview/window_overview.h" | 12 #include "ash/wm/overview/window_overview.h" |
13 #include "ash/wm/overview/window_selector_delegate.h" | 13 #include "ash/wm/overview/window_selector_delegate.h" |
14 #include "ash/wm/overview/window_selector_window.h" | 14 #include "ash/wm/overview/window_selector_window.h" |
15 #include "base/auto_reset.h" | 15 #include "base/auto_reset.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "ui/aura/client/focus_client.h" | 17 #include "ui/aura/client/focus_client.h" |
18 #include "ui/aura/root_window.h" | 18 #include "ui/aura/root_window.h" |
19 #include "ui/aura/window.h" | 19 #include "ui/aura/window.h" |
| 20 #include "ui/base/events/event.h" |
| 21 #include "ui/base/events/event_handler.h" |
20 | 22 |
21 namespace ash { | 23 namespace ash { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 const int kOverviewDelayOnCycleMilliseconds = 300; | 27 const int kOverviewDelayOnCycleMilliseconds = 300; |
26 | 28 |
27 // A comparator for locating a given target window. | 29 // A comparator for locating a given target window. |
28 struct WindowSelectorWindowComparator | 30 struct WindowSelectorWindowComparator |
29 : public std::unary_function<WindowSelectorWindow*, bool> { | 31 : public std::unary_function<WindowSelectorWindow*, bool> { |
30 explicit WindowSelectorWindowComparator(const aura::Window* target_window) | 32 explicit WindowSelectorWindowComparator(const aura::Window* target_window) |
31 : target(target_window) { | 33 : target(target_window) { |
32 } | 34 } |
33 | 35 |
34 bool operator()(const WindowSelectorWindow* window) const { | 36 bool operator()(const WindowSelectorWindow* window) const { |
35 return target == window->window(); | 37 return target == window->window(); |
36 } | 38 } |
37 | 39 |
38 const aura::Window* target; | 40 const aura::Window* target; |
39 }; | 41 }; |
40 | 42 |
| 43 // Filter to watch for the termination of a keyboard gesture to cycle through |
| 44 // multiple windows. |
| 45 class WindowSelectorEventFilter : public ui::EventHandler { |
| 46 public: |
| 47 WindowSelectorEventFilter(WindowSelector* selector); |
| 48 virtual ~WindowSelectorEventFilter(); |
| 49 |
| 50 // Overridden from ui::EventHandler: |
| 51 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; |
| 52 |
| 53 private: |
| 54 // A weak pointer to the WindowSelector which owns this instance. |
| 55 WindowSelector* selector_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(WindowSelectorEventFilter); |
| 58 }; |
| 59 |
| 60 // Watch for all keyboard events by filtering the root window. |
| 61 WindowSelectorEventFilter::WindowSelectorEventFilter(WindowSelector* selector) |
| 62 : selector_(selector) { |
| 63 Shell::GetInstance()->AddPreTargetHandler(this); |
| 64 } |
| 65 |
| 66 WindowSelectorEventFilter::~WindowSelectorEventFilter() { |
| 67 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 68 } |
| 69 |
| 70 void WindowSelectorEventFilter::OnKeyEvent(ui::KeyEvent* event) { |
| 71 // Views uses VKEY_MENU for both left and right Alt keys. |
| 72 if (event->key_code() == ui::VKEY_MENU && |
| 73 event->type() == ui::ET_KEY_RELEASED) { |
| 74 selector_->SelectWindow(); |
| 75 // Warning: |this| will be deleted from here on. |
| 76 } |
| 77 } |
| 78 |
41 } // namespace | 79 } // namespace |
42 | 80 |
43 WindowSelector::WindowSelector(const WindowList& windows, | 81 WindowSelector::WindowSelector(const WindowList& windows, |
44 WindowSelector::Mode mode, | 82 WindowSelector::Mode mode, |
45 WindowSelectorDelegate* delegate) | 83 WindowSelectorDelegate* delegate) |
46 : mode_(mode), | 84 : mode_(mode), |
47 start_overview_timer_(FROM_HERE, | 85 start_overview_timer_(FROM_HERE, |
48 base::TimeDelta::FromMilliseconds(kOverviewDelayOnCycleMilliseconds), | 86 base::TimeDelta::FromMilliseconds(kOverviewDelayOnCycleMilliseconds), |
49 this, &WindowSelector::StartOverview), | 87 this, &WindowSelector::StartOverview), |
50 delegate_(delegate), | 88 delegate_(delegate), |
(...skipping 15 matching lines...) Expand all Loading... |
66 Shell::GetInstance()->activation_client()->AddObserver(this); | 104 Shell::GetInstance()->activation_client()->AddObserver(this); |
67 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | 105 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
68 for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); | 106 for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); |
69 iter != root_windows.end(); ++iter) { | 107 iter != root_windows.end(); ++iter) { |
70 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) { | 108 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) { |
71 Shell::GetContainer(*iter, | 109 Shell::GetContainer(*iter, |
72 kSwitchableWindowContainerIds[i])->AddObserver(this); | 110 kSwitchableWindowContainerIds[i])->AddObserver(this); |
73 } | 111 } |
74 } | 112 } |
75 | 113 |
76 if (mode == WindowSelector::CYCLE) | 114 if (mode == WindowSelector::CYCLE) { |
| 115 event_handler_.reset(new WindowSelectorEventFilter(this)); |
77 start_overview_timer_.Reset(); | 116 start_overview_timer_.Reset(); |
78 else | 117 } else { |
79 StartOverview(); | 118 StartOverview(); |
| 119 } |
80 } | 120 } |
81 | 121 |
82 WindowSelector::~WindowSelector() { | 122 WindowSelector::~WindowSelector() { |
83 ResetFocusRestoreWindow(true); | 123 ResetFocusRestoreWindow(true); |
84 for (size_t i = 0; i < windows_.size(); i++) { | 124 for (size_t i = 0; i < windows_.size(); i++) { |
85 windows_[i]->window()->RemoveObserver(this); | 125 windows_[i]->window()->RemoveObserver(this); |
86 } | 126 } |
87 Shell::GetInstance()->activation_client()->RemoveObserver(this); | 127 Shell::GetInstance()->activation_client()->RemoveObserver(this); |
88 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | 128 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
89 for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); | 129 for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // If the window is in the windows_ list it needs to continue to be observed. | 262 // If the window is in the windows_ list it needs to continue to be observed. |
223 if (std::find_if(windows_.begin(), windows_.end(), | 263 if (std::find_if(windows_.begin(), windows_.end(), |
224 WindowSelectorWindowComparator(restore_focus_window_)) == | 264 WindowSelectorWindowComparator(restore_focus_window_)) == |
225 windows_.end()) { | 265 windows_.end()) { |
226 restore_focus_window_->RemoveObserver(this); | 266 restore_focus_window_->RemoveObserver(this); |
227 } | 267 } |
228 restore_focus_window_ = NULL; | 268 restore_focus_window_ = NULL; |
229 } | 269 } |
230 | 270 |
231 } // namespace ash | 271 } // namespace ash |
OLD | NEW |