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
|
// 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/panel_layout_manager.h"
#include <algorithm>
#include "ash/launcher/launcher.h"
#include "ash/shell.h"
#include "ash/wm/frame_painter.h"
#include "ash/wm/property_util.h"
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/aura/client/activation_client.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/rect.h"
#include "ui/views/background.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace internal {
namespace {
const int kPanelMarginEdge = 4;
const int kPanelMarginMiddle = 8;
const int kMinimizedHeight = 24;
const float kMaxHeightFactor = .80f;
const float kMaxWidthFactor = .50f;
// Callout arrow dimensions.
const int kArrowWidth = 20;
const int kArrowHeight = 10;
class CalloutWidgetBackground : public views::Background {
public:
CalloutWidgetBackground() {}
virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
SkPath path;
// TODO(dcheng): Verify if this results in off by one errors.
path.moveTo(SkIntToScalar(0), SkIntToScalar(0));
path.lineTo(SkIntToScalar(kArrowWidth / 2), SkIntToScalar(kArrowHeight));
path.lineTo(SkIntToScalar(kArrowWidth), SkIntToScalar(0));
// Use the same opacity and colors as the header for now.
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setColor(
SkColorSetARGB(FramePainter::kActiveWindowOpacity, 189, 189, 189));
canvas->DrawPath(path, paint);
}
private:
DISALLOW_COPY_AND_ASSIGN(CalloutWidgetBackground);
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// PanelLayoutManager public implementation:
PanelLayoutManager::PanelLayoutManager(aura::Window* panel_container)
: panel_container_(panel_container),
in_layout_(false),
dragged_panel_(NULL),
launcher_(NULL),
last_active_panel_(NULL),
callout_widget_(new views::Widget),
weak_factory_(this) {
DCHECK(panel_container);
views::Widget::InitParams params;
params.type = views::Widget::InitParams::TYPE_POPUP;
params.transparent = true;
params.can_activate = false;
params.keep_on_top = true;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = panel_container_;
params.bounds.set_width(kArrowWidth);
params.bounds.set_height(kArrowHeight);
// Why do we need this and can_activate = false?
callout_widget_->set_focus_on_creation(false);
callout_widget_->Init(params);
views::View* content_view = new views::View;
content_view->set_background(new CalloutWidgetBackground);
callout_widget_->SetContentsView(content_view);
aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
AddObserver(this);
}
PanelLayoutManager::~PanelLayoutManager() {
if (launcher_)
launcher_->RemoveIconObserver(this);
aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
RemoveObserver(this);
}
void PanelLayoutManager::StartDragging(aura::Window* panel) {
DCHECK(!dragged_panel_);
DCHECK(panel->parent() == panel_container_);
dragged_panel_ = panel;
}
void PanelLayoutManager::FinishDragging() {
DCHECK(dragged_panel_);
dragged_panel_ = NULL;
Relayout();
}
void PanelLayoutManager::SetLauncher(ash::Launcher* launcher) {
launcher_ = launcher;
launcher_->AddIconObserver(this);
}
void PanelLayoutManager::ToggleMinimize(aura::Window* panel) {
DCHECK(panel->parent() == panel_container_);
if (panel->GetProperty(aura::client::kShowStateKey) ==
ui::SHOW_STATE_MINIMIZED) {
const gfx::Rect& old_bounds = panel->bounds();
panel->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
gfx::Rect new_bounds(old_bounds);
const gfx::Rect* restore_bounds = GetRestoreBounds(panel);
if (restore_bounds) {
new_bounds.set_height(restore_bounds->height());
new_bounds.set_y(old_bounds.bottom() - restore_bounds->height());
SetChildBounds(panel, new_bounds);
ClearRestoreBounds(panel);
}
} else {
const gfx::Rect& old_bounds = panel->bounds();
panel->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
SetRestoreBounds(panel, old_bounds);
SetChildBounds(panel,
gfx::Rect(old_bounds.x(),
old_bounds.bottom() - kMinimizedHeight,
old_bounds.width(),
kMinimizedHeight));
}
Relayout();
}
////////////////////////////////////////////////////////////////////////////////
// PanelLayoutManager, aura::LayoutManager implementation:
void PanelLayoutManager::OnWindowResized() {
Relayout();
}
void PanelLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
if (child == callout_widget_->GetNativeWindow())
return;
panel_windows_.push_back(child);
Relayout();
}
void PanelLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
PanelList::iterator found =
std::find(panel_windows_.begin(), panel_windows_.end(), child);
if (found != panel_windows_.end())
panel_windows_.erase(found);
if (dragged_panel_ == child)
dragged_panel_ = NULL;
if (last_active_panel_ == child)
last_active_panel_ = NULL;
Relayout();
}
void PanelLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {
}
void PanelLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) {
Relayout();
}
void PanelLayoutManager::SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) {
gfx::Rect bounds(requested_bounds);
const gfx::Rect& max_bounds = panel_container_->GetRootWindow()->bounds();
const int max_width = max_bounds.width() * kMaxWidthFactor;
const int max_height = max_bounds.height() * kMaxHeightFactor;
if (bounds.width() > max_width)
bounds.set_width(max_width);
if (bounds.height() > max_height)
bounds.set_height(max_height);
// Reposition dragged panel in the panel order.
if (dragged_panel_ == child) {
PanelList::iterator dragged_panel_iter =
std::find(panel_windows_.begin(), panel_windows_.end(), dragged_panel_);
DCHECK(dragged_panel_iter != panel_windows_.end());
PanelList::iterator new_position;
for (new_position = panel_windows_.begin();
new_position != panel_windows_.end();
++new_position) {
const gfx::Rect& bounds = (*new_position)->bounds();
if (bounds.x() + bounds.width()/2 <= requested_bounds.x()) break;
}
if (new_position != dragged_panel_iter) {
panel_windows_.erase(dragged_panel_iter);
panel_windows_.insert(new_position, dragged_panel_);
}
}
SetChildBoundsDirect(child, bounds);
Relayout();
}
////////////////////////////////////////////////////////////////////////////////
// PanelLayoutManager, ash::LauncherIconObserver implementation:
void PanelLayoutManager::OnLauncherIconPositionsChanged() {
Relayout();
}
////////////////////////////////////////////////////////////////////////////////
// PanelLayoutManager, aura::client::ActivationChangeObserver implementation:
void PanelLayoutManager::OnWindowActivated(aura::Window* active,
aura::Window* old_active) {
if (active && active->type() == aura::client::WINDOW_TYPE_PANEL) {
UpdateStacking(active);
UpdateCallout(active);
} else {
UpdateCallout(NULL);
}
}
////////////////////////////////////////////////////////////////////////////////
// PanelLayoutManager private implementation:
void PanelLayoutManager::Relayout() {
if (in_layout_)
return;
AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
int launcher_top = launcher_->widget()->GetWindowScreenBounds().y();
aura::Window* active_panel = NULL;
for (PanelList::iterator iter = panel_windows_.begin();
iter != panel_windows_.end(); ++iter) {
aura::Window* panel = *iter;
if (!panel->IsVisible() || panel == dragged_panel_)
continue;
gfx::Rect icon_bounds =
launcher_->GetScreenBoundsOfItemIconForWindow(panel);
// An empty rect indicates that there is no icon for the panel in the
// launcher. Just use the current bounds, as there's no icon to draw the
// panel above.
// TODO(dcheng): Need to anchor to overflow icon.
if (icon_bounds.IsEmpty())
continue;
if (panel->HasFocus()) {
DCHECK(!active_panel);
active_panel = panel;
}
gfx::Point icon_origin = icon_bounds.origin();
aura::Window::ConvertPointToWindow(panel_container_->GetRootWindow(),
panel_container_, &icon_origin);
// TODO(dcheng): Need to clamp to screen edges.
gfx::Rect bounds = panel->bounds();
bounds.set_x(
icon_origin.x() + icon_bounds.width() / 2 - bounds.width() / 2);
bounds.set_y(launcher_top - bounds.height());
SetChildBoundsDirect(panel, bounds);
}
UpdateStacking(active_panel);
UpdateCallout(active_panel);
}
void PanelLayoutManager::UpdateStacking(aura::Window* active_panel) {
if (!active_panel) {
if (!last_active_panel_)
return;
active_panel = last_active_panel_;
}
// We want to to stack the panels like a deck of cards:
// ,--,--,--,-------.--.--.
// | | | | | | |
// | | | | | | |
//
// We use the middle of each panel to figure out how to stack the panels. This
// allows us to update the stacking when a panel is being dragged around by
// the titlebar--even though it doesn't update the launcher icon positions, we
// still want the visual effect.
std::map<int, aura::Window*> window_ordering;
for (PanelList::const_iterator it = panel_windows_.begin();
it != panel_windows_.end(); ++it) {
gfx::Rect bounds = (*it)->bounds();
window_ordering.insert(std::make_pair(bounds.x() + bounds.width() / 2,
*it));
}
aura::Window* previous_panel = NULL;
for (std::map<int, aura::Window*>::const_iterator it =
window_ordering.begin();
it != window_ordering.end() && it->second != active_panel; ++it) {
if (previous_panel)
panel_container_->StackChildAbove(it->second, previous_panel);
previous_panel = it->second;
}
previous_panel = NULL;
for (std::map<int, aura::Window*>::const_reverse_iterator it =
window_ordering.rbegin();
it != window_ordering.rend() && it->second != active_panel; ++it) {
if (previous_panel)
panel_container_->StackChildAbove(it->second, previous_panel);
previous_panel = it->second;
}
panel_container_->StackChildAtTop(active_panel);
last_active_panel_ = active_panel;
}
void PanelLayoutManager::UpdateCallout(aura::Window* active_panel) {
weak_factory_.InvalidateWeakPtrs();
// TODO(dcheng): This doesn't account for panels in overflow. They should have
// a callout as well.
if (!active_panel ||
launcher_->GetScreenBoundsOfItemIconForWindow(active_panel).IsEmpty()) {
callout_widget_->Hide();
return;
}
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PanelLayoutManager::ShowCalloutHelper,
weak_factory_.GetWeakPtr(),
active_panel));
}
void PanelLayoutManager::ShowCalloutHelper(aura::Window* active_panel) {
DCHECK(active_panel);
gfx::Rect bounds = active_panel->GetBoundsInRootWindow();
gfx::Rect callout_bounds = callout_widget_->GetWindowScreenBounds();
callout_bounds.set_x(
bounds.x() + (bounds.width() - callout_bounds.width()) / 2);
callout_bounds.set_y(bounds.bottom());
SetChildBoundsDirect(callout_widget_->GetNativeWindow(), callout_bounds);
panel_container_->StackChildAtTop(callout_widget_->GetNativeWindow());
callout_widget_->Show();
}
} // namespace internal
} // namespace ash
|