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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
// Copyright (c) 2012 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/shelf_layout_manager.h"
#include "ash/launcher/launcher.h"
#include "ash/screen_ash.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/shell_window_ids.h"
#include "ash/system/tray/system_tray.h"
#include "ash/wm/workspace/workspace_manager.h"
#include "base/auto_reset.h"
#include "ui/aura/client/activation_client.h"
#include "ui/aura/event.h"
#include "ui/aura/event_filter.h"
#include "ui/aura/root_window.h"
#include "ui/gfx/compositor/layer.h"
#include "ui/gfx/compositor/layer_animation_observer.h"
#include "ui/gfx/compositor/layer_animator.h"
#include "ui/gfx/compositor/scoped_layer_animation_settings.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace internal {
namespace {
// Delay before showing the launcher. This is after the mouse stops moving.
const int kAutoHideDelayMS = 200;
ui::Layer* GetLayer(views::Widget* widget) {
return widget->GetNativeView()->layer();
}
} // namespace
// static
const int ShelfLayoutManager::kWorkspaceAreaBottomInset = 2;
// static
const int ShelfLayoutManager::kAutoHideHeight = 2;
// Notifies ShelfLayoutManager any time the mouse moves.
class ShelfLayoutManager::AutoHideEventFilter : public aura::EventFilter {
public:
explicit AutoHideEventFilter(ShelfLayoutManager* shelf);
virtual ~AutoHideEventFilter();
// Overridden from aura::EventFilter:
virtual bool PreHandleKeyEvent(aura::Window* target,
aura::KeyEvent* event) OVERRIDE;
virtual bool PreHandleMouseEvent(aura::Window* target,
aura::MouseEvent* event) OVERRIDE;
virtual ui::TouchStatus PreHandleTouchEvent(aura::Window* target,
aura::TouchEvent* event) OVERRIDE;
virtual ui::GestureStatus PreHandleGestureEvent(
aura::Window* target,
aura::GestureEvent* event) OVERRIDE;
private:
ShelfLayoutManager* shelf_;
DISALLOW_COPY_AND_ASSIGN(AutoHideEventFilter);
};
ShelfLayoutManager::AutoHideEventFilter::AutoHideEventFilter(
ShelfLayoutManager* shelf)
: shelf_(shelf) {
Shell::GetInstance()->AddRootWindowEventFilter(this);
}
ShelfLayoutManager::AutoHideEventFilter::~AutoHideEventFilter() {
Shell::GetInstance()->RemoveRootWindowEventFilter(this);
}
bool ShelfLayoutManager::AutoHideEventFilter::PreHandleKeyEvent(
aura::Window* target,
aura::KeyEvent* event) {
return false; // Always let the event propagate.
}
bool ShelfLayoutManager::AutoHideEventFilter::PreHandleMouseEvent(
aura::Window* target,
aura::MouseEvent* event) {
if (event->type() == ui::ET_MOUSE_MOVED)
shelf_->UpdateAutoHideState();
return false; // Not handled.
}
ui::TouchStatus ShelfLayoutManager::AutoHideEventFilter::PreHandleTouchEvent(
aura::Window* target,
aura::TouchEvent* event) {
return ui::TOUCH_STATUS_UNKNOWN; // Not handled.
}
ui::GestureStatus
ShelfLayoutManager::AutoHideEventFilter::PreHandleGestureEvent(
aura::Window* target,
aura::GestureEvent* event) {
return ui::GESTURE_STATUS_UNKNOWN; // Not handled.
}
////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, public:
ShelfLayoutManager::ShelfLayoutManager(views::Widget* status)
: root_window_(Shell::GetInstance()->GetRootWindow()),
in_layout_(false),
auto_hide_behavior_(SHELF_AUTO_HIDE_BEHAVIOR_DEFAULT),
shelf_height_(status->GetWindowScreenBounds().height()),
launcher_(NULL),
status_(status),
workspace_manager_(NULL),
window_overlaps_shelf_(false) {
root_window_->AddObserver(this);
}
ShelfLayoutManager::~ShelfLayoutManager() {
root_window_->RemoveObserver(this);
}
void ShelfLayoutManager::SetAutoHideBehavior(ShelfAutoHideBehavior behavior) {
if (auto_hide_behavior_ == behavior)
return;
auto_hide_behavior_ = behavior;
UpdateVisibilityState();
}
bool ShelfLayoutManager::IsVisible() const {
return state_.visibility_state == VISIBLE ||
(state_.visibility_state == AUTO_HIDE &&
state_.auto_hide_state == AUTO_HIDE_SHOWN);
}
gfx::Rect ShelfLayoutManager::GetMaximizedWindowBounds(
aura::Window* window) const {
// TODO: needs to be multi-mon aware.
gfx::Rect bounds(gfx::Screen::GetMonitorAreaNearestWindow(window));
if (auto_hide_behavior_ == SHELF_AUTO_HIDE_BEHAVIOR_DEFAULT ||
auto_hide_behavior_ == SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS) {
bounds.set_height(bounds.height() - kAutoHideHeight);
return bounds;
}
// SHELF_AUTO_HIDE_BEHAVIOR_NEVER maximized windows don't get any taller.
return GetUnmaximizedWorkAreaBounds(window);
}
gfx::Rect ShelfLayoutManager::GetUnmaximizedWorkAreaBounds(
aura::Window* window) const {
// TODO: needs to be multi-mon aware.
gfx::Rect bounds(gfx::Screen::GetMonitorAreaNearestWindow(window));
bounds.set_height(bounds.height() - shelf_height_);
return bounds;
}
void ShelfLayoutManager::SetLauncher(Launcher* launcher) {
if (launcher == launcher_)
return;
launcher_ = launcher;
shelf_height_ =
std::max(status_->GetWindowScreenBounds().height(),
launcher_widget()->GetWindowScreenBounds().height());
LayoutShelf();
}
void ShelfLayoutManager::LayoutShelf() {
AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
StopAnimating();
TargetBounds target_bounds;
CalculateTargetBounds(state_, &target_bounds);
if (launcher_widget()) {
GetLayer(launcher_widget())->SetOpacity(target_bounds.opacity);
launcher_widget()->SetBounds(target_bounds.launcher_bounds);
launcher_->SetStatusWidth(
target_bounds.status_bounds.width());
}
GetLayer(status_)->SetOpacity(target_bounds.opacity);
status_->SetBounds(target_bounds.status_bounds);
Shell::GetInstance()->SetMonitorWorkAreaInsets(
Shell::GetRootWindow(),
target_bounds.work_area_insets);
UpdateHitTestBounds();
}
void ShelfLayoutManager::UpdateVisibilityState() {
ShellDelegate* delegate = Shell::GetInstance()->delegate();
if (delegate && delegate->IsScreenLocked()) {
SetState(VISIBLE);
} else {
WorkspaceManager::WindowState window_state(
workspace_manager_->GetWindowState());
switch (window_state) {
case WorkspaceManager::WINDOW_STATE_FULL_SCREEN:
SetState(HIDDEN);
break;
case WorkspaceManager::WINDOW_STATE_MAXIMIZED:
SetState(auto_hide_behavior_ != SHELF_AUTO_HIDE_BEHAVIOR_NEVER ?
AUTO_HIDE : VISIBLE);
break;
case WorkspaceManager::WINDOW_STATE_WINDOW_OVERLAPS_SHELF:
case WorkspaceManager::WINDOW_STATE_DEFAULT:
SetState(auto_hide_behavior_ == SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS ?
AUTO_HIDE : VISIBLE);
SetWindowOverlapsShelf(window_state ==
WorkspaceManager::WINDOW_STATE_WINDOW_OVERLAPS_SHELF);
}
}
}
void ShelfLayoutManager::UpdateAutoHideState() {
AutoHideState auto_hide_state =
CalculateAutoHideState(state_.visibility_state);
if (auto_hide_state != state_.auto_hide_state) {
if (auto_hide_state == AUTO_HIDE_HIDDEN) {
// Hides happen immediately.
SetState(state_.visibility_state);
} else {
auto_hide_timer_.Stop();
auto_hide_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kAutoHideDelayMS),
this, &ShelfLayoutManager::UpdateAutoHideStateNow);
}
} else {
auto_hide_timer_.Stop();
}
}
void ShelfLayoutManager::SetWindowOverlapsShelf(bool value) {
window_overlaps_shelf_ = value;
UpdateShelfBackground(internal::BackgroundAnimator::CHANGE_ANIMATE);
}
////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, aura::LayoutManager implementation:
void ShelfLayoutManager::OnWindowResized() {
LayoutShelf();
}
void ShelfLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
}
void ShelfLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
}
void ShelfLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) {
}
void ShelfLayoutManager::SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) {
SetChildBoundsDirect(child, requested_bounds);
if (!in_layout_)
LayoutShelf();
}
void ShelfLayoutManager::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (key == aura::client::kRootWindowActiveWindowKey)
UpdateAutoHideStateNow();
}
////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, private:
void ShelfLayoutManager::SetState(VisibilityState visibility_state) {
State state;
state.visibility_state = visibility_state;
state.auto_hide_state = CalculateAutoHideState(visibility_state);
if (state_.Equals(state))
return; // Nothing changed.
if (state.visibility_state == AUTO_HIDE) {
// When state is AUTO_HIDE we need to track when the mouse is over the
// launcher to unhide the shelf. AutoHideEventFilter does that for us.
if (!event_filter_.get())
event_filter_.reset(new AutoHideEventFilter(this));
} else {
event_filter_.reset(NULL);
}
auto_hide_timer_.Stop();
// Animating the background when transitioning from auto-hide & hidden to
// visibile is janking. Update the background immediately in this case.
internal::BackgroundAnimator::ChangeType change_type =
(state_.visibility_state == AUTO_HIDE &&
state_.auto_hide_state == AUTO_HIDE_HIDDEN &&
state.visibility_state == VISIBLE) ?
internal::BackgroundAnimator::CHANGE_IMMEDIATE :
internal::BackgroundAnimator::CHANGE_ANIMATE;
StopAnimating();
state_ = state;
TargetBounds target_bounds;
CalculateTargetBounds(state_, &target_bounds);
if (launcher_widget()) {
ui::ScopedLayerAnimationSettings launcher_animation_setter(
GetLayer(launcher_widget())->GetAnimator());
launcher_animation_setter.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(130));
launcher_animation_setter.SetTweenType(ui::Tween::EASE_OUT);
GetLayer(launcher_widget())->SetBounds(target_bounds.launcher_bounds);
GetLayer(launcher_widget())->SetOpacity(target_bounds.opacity);
}
ui::ScopedLayerAnimationSettings status_animation_setter(
GetLayer(status_)->GetAnimator());
status_animation_setter.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(130));
status_animation_setter.SetTweenType(ui::Tween::EASE_OUT);
GetLayer(status_)->SetBounds(target_bounds.status_bounds);
GetLayer(status_)->SetOpacity(target_bounds.opacity);
Shell::GetInstance()->SetMonitorWorkAreaInsets(
Shell::GetRootWindow(),
target_bounds.work_area_insets);
UpdateHitTestBounds();
UpdateShelfBackground(change_type);
}
void ShelfLayoutManager::StopAnimating() {
if (launcher_widget())
GetLayer(launcher_widget())->GetAnimator()->StopAnimating();
GetLayer(status_)->GetAnimator()->StopAnimating();
}
void ShelfLayoutManager::CalculateTargetBounds(
const State& state,
TargetBounds* target_bounds) const {
const gfx::Rect& available_bounds(
status_->GetNativeView()->GetRootWindow()->bounds());
int y = available_bounds.bottom();
int shelf_height = 0;
if (state.visibility_state == VISIBLE ||
(state.visibility_state == AUTO_HIDE &&
state.auto_hide_state == AUTO_HIDE_SHOWN))
shelf_height = shelf_height_;
else if (state.visibility_state == AUTO_HIDE &&
state.auto_hide_state == AUTO_HIDE_HIDDEN)
shelf_height = kAutoHideHeight;
y -= shelf_height;
gfx::Rect status_bounds(status_->GetWindowScreenBounds());
// The status widget should extend to the bottom and right edges.
target_bounds->status_bounds = gfx::Rect(
available_bounds.right() - status_bounds.width(),
y + shelf_height_ - status_bounds.height(),
status_bounds.width(), status_bounds.height());
if (launcher_widget()) {
gfx::Rect launcher_bounds(launcher_widget()->GetWindowScreenBounds());
target_bounds->launcher_bounds = gfx::Rect(
available_bounds.x(),
y + (shelf_height_ - launcher_bounds.height()) / 2,
available_bounds.width(),
launcher_bounds.height());
}
target_bounds->opacity =
(state.visibility_state == VISIBLE ||
state.visibility_state == AUTO_HIDE) ? 1.0f : 0.0f;
int work_area_bottom = 0;
if (state.visibility_state == VISIBLE)
work_area_bottom = shelf_height_;
else if (state.visibility_state == AUTO_HIDE)
work_area_bottom = kAutoHideHeight;
target_bounds->work_area_insets.Set(0, 0, work_area_bottom, 0);
}
void ShelfLayoutManager::UpdateShelfBackground(
BackgroundAnimator::ChangeType type) {
bool launcher_paints = GetLauncherPaintsBackground();
if (launcher_)
launcher_->SetPaintsBackground(launcher_paints, type);
// SystemTray normally draws a background, but we don't want it to draw a
// background when the launcher does.
if (Shell::GetInstance()->tray())
Shell::GetInstance()->tray()->SetPaintsBackground(!launcher_paints, type);
}
bool ShelfLayoutManager::GetLauncherPaintsBackground() const {
return window_overlaps_shelf_ || state_.visibility_state == AUTO_HIDE;
}
void ShelfLayoutManager::UpdateAutoHideStateNow() {
SetState(state_.visibility_state);
}
ShelfLayoutManager::AutoHideState ShelfLayoutManager::CalculateAutoHideState(
VisibilityState visibility_state) const {
if (visibility_state != AUTO_HIDE || !launcher_widget())
return AUTO_HIDE_HIDDEN;
Shell* shell = Shell::GetInstance();
if (shell->tray() && shell->tray()->should_show_launcher())
return AUTO_HIDE_SHOWN;
if (launcher_ && launcher_->IsShowingMenu())
return AUTO_HIDE_SHOWN;
if (launcher_widget()->IsActive() || status_->IsActive())
return AUTO_HIDE_SHOWN;
aura::RootWindow* root = launcher_widget()->GetNativeView()->GetRootWindow();
bool mouse_over_launcher =
launcher_widget()->GetWindowScreenBounds().Contains(
root->last_mouse_location());
return mouse_over_launcher ? AUTO_HIDE_SHOWN : AUTO_HIDE_HIDDEN;
}
void ShelfLayoutManager::UpdateHitTestBounds() {
gfx::Insets insets;
// Only modify the hit test when the shelf is visible, so we don't mess with
// hover hit testing in the auto-hide state.
if (state_.visibility_state == VISIBLE) {
// Let clicks at the very top of the launcher through so windows can be
// resized with the bottom-right corner and bottom edge.
insets.Set(kWorkspaceAreaBottomInset, 0, 0, 0);
}
if (launcher_widget() && launcher_widget()->GetNativeWindow())
launcher_widget()->GetNativeWindow()->set_hit_test_bounds_override_outer(
insets);
status_->GetNativeWindow()->set_hit_test_bounds_override_outer(insets);
}
} // namespace internal
} // namespace ash
|