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
|
// 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/compact_layout_manager.h"
#include <vector>
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/gfx/compositor/layer.h"
#include "ui/gfx/compositor/layer_animation_sequence.h"
#include "ui/gfx/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace internal {
namespace {
typedef std::vector<aura::Window*> WindowList;
typedef std::vector<aura::Window*>::const_iterator WindowListConstIter;
// Convenience method to get the layer of this container.
ui::Layer* GetDefaultContainerLayer() {
return Shell::GetInstance()->GetContainer(
internal::kShellWindowId_DefaultContainer)->layer();
}
// Whether it is a window that should be animated on entrance.
bool ShouldAnimateOnEntrance(aura::Window* window) {
return window &&
window->type() == aura::client::WINDOW_TYPE_NORMAL &&
wm::IsWindowMaximized(window);
}
// Adjust layer bounds to grow or shrink in |delta_width|.
void AdjustContainerLayerWidth(int delta_width) {
gfx::Rect bounds(GetDefaultContainerLayer()->bounds());
bounds.set_width(bounds.width() + delta_width);
GetDefaultContainerLayer()->SetBounds(bounds);
}
} // namespace
/////////////////////////////////////////////////////////////////////////////
// CompactLayoutManager, public:
CompactLayoutManager::CompactLayoutManager()
: status_area_widget_(NULL),
current_window_(NULL) {
}
CompactLayoutManager::~CompactLayoutManager() {
}
/////////////////////////////////////////////////////////////////////////////
// CompactLayoutManager, LayoutManager overrides:
void CompactLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
// Windows added to this container does not need extra animation.
if (child->type() == aura::client::WINDOW_TYPE_NORMAL)
child->SetProperty(aura::client::kAnimationsDisabledKey, true);
BaseLayoutManager::OnWindowAddedToLayout(child);
UpdateStatusAreaVisibility();
if (windows().size() > 1 &&
child->type() == aura::client::WINDOW_TYPE_NORMAL) {
// The first window is already contained in the current layer,
// add subsequent windows to layer bounds calculation.
AdjustContainerLayerWidth(child->bounds().width());
}
}
void CompactLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
BaseLayoutManager::OnWillRemoveWindowFromLayout(child);
UpdateStatusAreaVisibility();
if (windows().size() > 1 && ShouldAnimateOnEntrance(child))
AdjustContainerLayerWidth(-child->bounds().width());
if (child == current_window_) {
LayoutWindows(current_window_);
SwitchToReplacementWindow();
}
// Allow window to be animated by others.
if (child->type() == aura::client::WINDOW_TYPE_NORMAL)
child->SetProperty(aura::client::kAnimationsDisabledKey, false);
}
void CompactLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) {
BaseLayoutManager::OnChildWindowVisibilityChanged(child, visible);
UpdateStatusAreaVisibility();
if (ShouldAnimateOnEntrance(child)) {
LayoutWindows(visible ? NULL : child);
if (visible) {
current_window_ = child;
AnimateSlideTo(child->bounds().x());
} else if (child == current_window_) {
SwitchToReplacementWindow();
}
}
}
void CompactLayoutManager::SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) {
gfx::Rect child_bounds(requested_bounds);
// Avoid a janky resize on startup by ensuring the initial bounds fill the
// screen.
if (wm::IsWindowMaximized(child))
child_bounds = gfx::Screen::GetMonitorWorkAreaNearestWindow(child);
else if (wm::IsWindowFullscreen(child))
child_bounds = gfx::Screen::GetMonitorAreaNearestWindow(child);
else if (current_window_) {
// All other windows should be offset by the current viewport.
int offset_x = current_window_->bounds().x();
child_bounds.Offset(offset_x, 0);
}
SetChildBoundsDirect(child, child_bounds);
}
/////////////////////////////////////////////////////////////////////////////
// CompactLayoutManager, aura::WindowObserver overrides:
void CompactLayoutManager::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
BaseLayoutManager::OnWindowPropertyChanged(window, key, old);
if (key == aura::client::kShowStateKey)
UpdateStatusAreaVisibility();
}
void CompactLayoutManager::OnWindowStackingChanged(aura::Window* window) {
if (!current_window_ || ShouldAnimateOnEntrance(window)) {
if (current_window_ != window) {
LayoutWindows(current_window_);
current_window_ = window;
} else {
// Same window as |current_window_|, and already animating.
if (GetDefaultContainerLayer()->GetAnimator()->is_animating())
return;
}
// Animate to |window| when there is a stacking change.
AnimateSlideTo(window->bounds().x());
}
}
/////////////////////////////////////////////////////////////////////////////
// CompactLayoutManager, AnimationDelegate overrides:
void CompactLayoutManager::OnImplicitAnimationsCompleted() {
if (!GetDefaultContainerLayer()->GetAnimator()->is_animating())
HideWindows();
}
//////////////////////////////////////////////////////////////////////////////
// CompactLayoutManager, private:
void CompactLayoutManager::UpdateStatusAreaVisibility() {
if (!status_area_widget_)
return;
// Full screen windows should hide the status area widget.
bool has_fullscreen = wm::HasFullscreenWindow(windows());
bool widget_visible = status_area_widget_->IsVisible();
if (has_fullscreen && widget_visible)
status_area_widget_->Hide();
else if (!has_fullscreen && !widget_visible)
status_area_widget_->Show();
}
void CompactLayoutManager::AnimateSlideTo(int offset_x) {
GetDefaultContainerLayer()->GetAnimator()->RemoveObserver(this);
ui::ScopedLayerAnimationSettings settings(
GetDefaultContainerLayer()->GetAnimator());
settings.AddObserver(this);
ui::Transform transform;
transform.ConcatTranslate(-offset_x, 0);
GetDefaultContainerLayer()->SetTransform(transform); // Will be animated!
}
void CompactLayoutManager::LayoutWindows(aura::Window* skip) {
ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate();
const WindowList& windows_list = shell_delegate->GetCycleWindowList(
ShellDelegate::SOURCE_KEYBOARD,
ShellDelegate::ORDER_LINEAR);
int new_x = 0;
for (WindowListConstIter const_it = windows_list.begin();
const_it != windows_list.end();
++const_it) {
if (*const_it != skip) {
gfx::Rect new_bounds((*const_it)->bounds());
new_bounds.set_x(new_x);
SetChildBoundsDirect(*const_it, new_bounds);
(*const_it)->layer()->SetVisible(true);
new_x += (*const_it)->bounds().width();
}
}
}
void CompactLayoutManager::HideWindows() {
// If we do not know which one is the current window, or if the current
// window is not visible, do not attempt to hide the windows.
if (current_window_ == NULL)
return;
// Current window should be visible, if not it is an error and we shouldn't
// proceed.
if (!current_window_->layer()->visible())
NOTREACHED() << "Current window is invisible";
ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate();
const WindowList& windows_list = shell_delegate->GetCycleWindowList(
ShellDelegate::SOURCE_KEYBOARD,
ShellDelegate::ORDER_LINEAR);
for (WindowListConstIter const_it = windows_list.begin();
const_it != windows_list.end();
++const_it) {
if (*const_it != current_window_)
(*const_it)->layer()->SetVisible(false);
}
}
aura::Window* CompactLayoutManager::FindReplacementWindow(
aura::Window* window) {
ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate();
const WindowList& windows_list = shell_delegate->GetCycleWindowList(
ShellDelegate::SOURCE_KEYBOARD,
ShellDelegate::ORDER_LINEAR);
WindowListConstIter const_it = std::find(windows_list.begin(),
windows_list.end(),
window);
if (windows_list.size() > 1 && const_it != windows_list.end()) {
do {
++const_it;
if (const_it == windows_list.end())
const_it = windows_list.begin();
} while (*const_it != window && !(*const_it)->IsVisible());
if (*const_it != window)
return *const_it;
}
return NULL;
}
void CompactLayoutManager::SwitchToReplacementWindow() {
current_window_ = FindReplacementWindow(current_window_);
if (current_window_) {
wm::ActivateWindow(current_window_);
AnimateSlideTo(current_window_->bounds().x());
}
}
} // namespace internal
} // namespace ash
|