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
|
// Copyright (c) 2011 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 "ui/aura_shell/workspace/workspace.h"
#include "base/logging.h"
#include "ui/aura/window.h"
#include "ui/aura_shell/workspace/workspace_manager.h"
#include "ui/gfx/compositor/layer.h"
namespace {
// Horizontal margin between windows.
const int kWindowHorizontalMargin = 10;
}
namespace aura_shell {
Workspace::Workspace(WorkspaceManager* manager)
: workspace_manager_(manager) {
workspace_manager_->AddWorkspace(this);
}
Workspace::~Workspace() {
workspace_manager_->RemoveWorkspace(this);
}
void Workspace::SetBounds(const gfx::Rect& bounds) {
bool bounds_changed = bounds_ != bounds;
bounds_ = bounds;
if (bounds_changed)
Layout(NULL);
}
gfx::Rect Workspace::GetWorkAreaBounds() const {
return workspace_manager_->GetWorkAreaBounds(bounds_);
}
bool Workspace::AddWindowAfter(aura::Window* window, aura::Window* after) {
if (!CanAdd(window))
return false;
DCHECK(!Contains(window));
if (!after) { // insert at the end.
windows_.push_back(window);
} else {
DCHECK(Contains(after));
aura::Window::Windows::iterator i =
std::find(windows_.begin(), windows_.end(), after);
windows_.insert(++i, window);
}
Layout(window);
return true;
}
void Workspace::RemoveWindow(aura::Window* window) {
DCHECK(Contains(window));
windows_.erase(std::find(windows_.begin(), windows_.end(), window));
Layout(NULL);
}
bool Workspace::Contains(aura::Window* window) const {
return std::find(windows_.begin(), windows_.end(), window) != windows_.end();
}
void Workspace::Activate() {
workspace_manager_->SetActiveWorkspace(this);
}
void Workspace::Layout(aura::Window* no_animation) {
gfx::Rect work_area = workspace_manager_->GetWorkAreaBounds(bounds_);
int total_width = 0;
for (aura::Window::Windows::const_iterator i = windows_.begin();
i != windows_.end();
i++) {
if (total_width)
total_width += kWindowHorizontalMargin;
// TODO(oshima): use restored bounds.
total_width += (*i)->bounds().width();
}
if (total_width < work_area.width()) {
int dx = (work_area.width() - total_width) / 2;
for (aura::Window::Windows::iterator i = windows_.begin();
i != windows_.end();
i++) {
MoveWindowTo(*i,
gfx::Point(work_area.x() + dx, work_area.y()),
no_animation != *i);
dx += (*i)->bounds().width() + kWindowHorizontalMargin;
}
} else {
DCHECK_LT(windows_.size(), 3U);
// TODO(oshima): Figure out general algorithm to layout more than
// 2 windows.
MoveWindowTo(windows_[0], work_area.origin(), no_animation != windows_[0]);
if (windows_.size() == 2) {
MoveWindowTo(windows_[1],
gfx::Point(work_area.right() - windows_[1]->bounds().width(),
work_area.y()),
no_animation != windows_[1]);
}
}
}
int Workspace::GetIndexOf(aura::Window* window) const {
aura::Window::Windows::const_iterator i =
std::find(windows_.begin(), windows_.end(), window);
return i == windows_.end() ? -1 : i - windows_.begin();
}
bool Workspace::CanAdd(aura::Window* window) const {
// TODO(oshima): This should be based on available space and the
// size of the |window|.
NOTIMPLEMENTED();
return windows_.size() < 2;
}
void Workspace::MoveWindowTo(
aura::Window* window,
const gfx::Point& origin,
bool animate) {
if (window->show_state() == ui::SHOW_STATE_FULLSCREEN)
window->Fullscreen();
else if (window->show_state() == ui::SHOW_STATE_MAXIMIZED)
window->Maximize();
else {
gfx::Rect bounds = window->GetTargetBounds();
bounds.set_origin(origin);
if (animate)
window->layer()->SetAnimation(aura::Window::CreateDefaultAnimation());
window->SetBounds(bounds);
}
}
} // namespace aura_shell
|