1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/overview/window_selector.h"
#include <algorithm>
#include "ash/shell.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/overview/window_overview.h"
#include "ash/wm/overview/window_selector_delegate.h"
#include "ash/wm/overview/window_selector_window.h"
#include "base/auto_reset.h"
#include "base/timer/timer.h"
#include "ui/aura/client/activation_client.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_handler.h"
namespace ash {
namespace {
const int kOverviewDelayOnCycleMilliseconds = 300;
// A comparator for locating a given target window.
struct WindowSelectorWindowComparator
: public std::unary_function<WindowSelectorWindow*, bool> {
explicit WindowSelectorWindowComparator(const aura::Window* target_window)
: target(target_window) {
}
bool operator()(const WindowSelectorWindow* window) const {
return target == window->window();
}
const aura::Window* target;
};
// Filter to watch for the termination of a keyboard gesture to cycle through
// multiple windows.
class WindowSelectorEventFilter : public ui::EventHandler {
public:
WindowSelectorEventFilter(WindowSelector* selector);
virtual ~WindowSelectorEventFilter();
// Overridden from ui::EventHandler:
virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
private:
// A weak pointer to the WindowSelector which owns this instance.
WindowSelector* selector_;
DISALLOW_COPY_AND_ASSIGN(WindowSelectorEventFilter);
};
// Watch for all keyboard events by filtering the root window.
WindowSelectorEventFilter::WindowSelectorEventFilter(WindowSelector* selector)
: selector_(selector) {
Shell::GetInstance()->AddPreTargetHandler(this);
}
WindowSelectorEventFilter::~WindowSelectorEventFilter() {
Shell::GetInstance()->RemovePreTargetHandler(this);
}
void WindowSelectorEventFilter::OnKeyEvent(ui::KeyEvent* event) {
// Views uses VKEY_MENU for both left and right Alt keys.
if (event->key_code() == ui::VKEY_MENU &&
event->type() == ui::ET_KEY_RELEASED) {
selector_->SelectWindow();
// Warning: |this| will be deleted from here on.
}
}
} // namespace
WindowSelector::WindowSelector(const WindowList& windows,
WindowSelector::Mode mode,
WindowSelectorDelegate* delegate)
: mode_(mode),
start_overview_timer_(FROM_HERE,
base::TimeDelta::FromMilliseconds(kOverviewDelayOnCycleMilliseconds),
this, &WindowSelector::StartOverview),
delegate_(delegate),
selected_window_(0),
restore_focus_window_(NULL),
restoring_focus_(false) {
DCHECK(delegate_);
RemoveFocusAndSetRestoreWindow();
for (size_t i = 0; i < windows.size(); ++i) {
// restore_focus_window_ is already observed from the call to
// RemoveFocusAndSetRestoreWindow.
if (windows[i] != restore_focus_window_)
windows[i]->AddObserver(this);
windows_.push_back(new WindowSelectorWindow(windows[i]));
}
// Observe window activations and switchable containers on all root windows
// for newly created windows during overview.
Shell::GetInstance()->activation_client()->AddObserver(this);
Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter) {
for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
Shell::GetContainer(*iter,
kSwitchableWindowContainerIds[i])->AddObserver(this);
}
}
if (mode == WindowSelector::CYCLE) {
event_handler_.reset(new WindowSelectorEventFilter(this));
start_overview_timer_.Reset();
} else {
StartOverview();
}
}
WindowSelector::~WindowSelector() {
ResetFocusRestoreWindow(true);
for (size_t i = 0; i < windows_.size(); i++) {
windows_[i]->window()->RemoveObserver(this);
}
Shell::GetInstance()->activation_client()->RemoveObserver(this);
Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter) {
for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
Shell::GetContainer(*iter,
kSwitchableWindowContainerIds[i])->RemoveObserver(this);
}
}
}
void WindowSelector::Step(WindowSelector::Direction direction) {
DCHECK_EQ(CYCLE, mode_);
DCHECK(!windows_.empty());
selected_window_ = (selected_window_ + windows_.size() +
(direction == WindowSelector::FORWARD ? 1 : -1)) % windows_.size();
if (window_overview_) {
window_overview_->SetSelection(selected_window_);
} else {
aura::Window* current_window = windows_[selected_window_]->window();
current_window->Show();
current_window->SetTransform(gfx::Transform());
current_window->parent()->StackChildAtTop(current_window);
start_overview_timer_.Reset();
}
}
void WindowSelector::SelectWindow() {
ResetFocusRestoreWindow(false);
SelectWindow(windows_[selected_window_]->window());
}
void WindowSelector::SelectWindow(aura::Window* window) {
ScopedVector<WindowSelectorWindow>::iterator iter =
std::find_if(windows_.begin(), windows_.end(),
WindowSelectorWindowComparator(window));
DCHECK(iter != windows_.end());
// The selected window should not be minimized when window selection is
// ended.
(*iter)->RestoreWindowOnExit();
delegate_->OnWindowSelected(window);
}
void WindowSelector::CancelSelection() {
delegate_->OnSelectionCanceled();
}
void WindowSelector::OnWindowAdded(aura::Window* new_window) {
if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL &&
new_window->type() != aura::client::WINDOW_TYPE_PANEL) {
return;
}
for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
if (new_window->parent()->id() == kSwitchableWindowContainerIds[i] &&
!new_window->transient_parent()) {
// The new window is in one of the switchable containers, abort overview.
CancelSelection();
return;
}
}
}
void WindowSelector::OnWindowDestroyed(aura::Window* window) {
ScopedVector<WindowSelectorWindow>::iterator iter =
std::find_if(windows_.begin(), windows_.end(),
WindowSelectorWindowComparator(window));
DCHECK(window == restore_focus_window_ || iter != windows_.end());
window->RemoveObserver(this);
if (window == restore_focus_window_)
restore_focus_window_ = NULL;
if (iter == windows_.end())
return;
size_t deleted_index = iter - windows_.begin();
(*iter)->OnWindowDestroyed();
windows_.erase(iter);
if (windows_.empty()) {
CancelSelection();
return;
}
window_overview_->OnWindowsChanged();
if (mode_ == CYCLE && selected_window_ >= deleted_index) {
if (selected_window_ > deleted_index)
selected_window_--;
selected_window_ = selected_window_ % windows_.size();
if (window_overview_)
window_overview_->SetSelection(selected_window_);
}
}
void WindowSelector::OnWindowActivated(aura::Window* gained_active,
aura::Window* lost_active) {
if (restoring_focus_ || !gained_active)
return;
// Don't restore focus on exit if a window was just activated.
ResetFocusRestoreWindow(false);
CancelSelection();
}
void WindowSelector::OnAttemptToReactivateWindow(aura::Window* request_active,
aura::Window* actual_active) {
if (restoring_focus_)
return;
// Don't restore focus on exit if a window was just activated.
ResetFocusRestoreWindow(false);
CancelSelection();
}
void WindowSelector::StartOverview() {
DCHECK(!window_overview_);
window_overview_.reset(new WindowOverview(this, &windows_,
mode_ == CYCLE ? Shell::GetTargetRootWindow() : NULL));
if (mode_ == CYCLE)
window_overview_->SetSelection(selected_window_);
}
void WindowSelector::RemoveFocusAndSetRestoreWindow() {
aura::client::FocusClient* focus_client = aura::client::GetFocusClient(
Shell::GetPrimaryRootWindow());
DCHECK(!restore_focus_window_);
restore_focus_window_ = focus_client->GetFocusedWindow();
if (restore_focus_window_) {
focus_client->FocusWindow(NULL);
restore_focus_window_->AddObserver(this);
}
}
void WindowSelector::ResetFocusRestoreWindow(bool focus) {
if (!restore_focus_window_)
return;
if (focus) {
base::AutoReset<bool> restoring_focus(&restoring_focus_, true);
restore_focus_window_->Focus();
}
// If the window is in the windows_ list it needs to continue to be observed.
if (std::find_if(windows_.begin(), windows_.end(),
WindowSelectorWindowComparator(restore_focus_window_)) ==
windows_.end()) {
restore_focus_window_->RemoveObserver(this);
}
restore_focus_window_ = NULL;
}
} // namespace ash
|