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
|
// Copyright 2014 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 "athena/wm/split_view_controller.h"
#include <cmath>
#include "athena/wm/public/window_list_provider.h"
#include "athena/wm/public/window_manager.h"
#include "base/bind.h"
#include "ui/aura/window.h"
#include "ui/compositor/closure_animation_observer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/events/event_handler.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/wm/core/window_util.h"
namespace athena {
SplitViewController::SplitViewController(
aura::Window* container,
WindowListProvider* window_list_provider)
: state_(INACTIVE),
container_(container),
window_list_provider_(window_list_provider),
left_window_(NULL),
right_window_(NULL),
separator_position_(0),
weak_factory_(this) {
}
SplitViewController::~SplitViewController() {
}
bool SplitViewController::IsSplitViewModeActive() const {
return state_ == ACTIVE;
}
void SplitViewController::ActivateSplitMode(aura::Window* left,
aura::Window* right) {
aura::Window::Windows windows = window_list_provider_->GetWindowList();
aura::Window::Windows::reverse_iterator iter = windows.rbegin();
if (state_ == ACTIVE) {
if (left_window_ == right)
left_window_ = left;
if (right_window_ == left)
right_window_ = right;
if (!left)
left = left_window_;
if (!right)
right = right_window_;
}
if (!left && iter != windows.rend()) {
left = *iter;
iter++;
if (left == right && iter != windows.rend()) {
left = *iter;
iter++;
}
}
if (!right && iter != windows.rend()) {
right = *iter;
iter++;
if (right == left && iter != windows.rend()) {
right = *iter;
iter++;
}
}
state_ = ACTIVE;
if (right_window_ != right) {
right_window_ = right;
container_->StackChildAtTop(right_window_);
}
if (left_window_ != left) {
left_window_ = left;
container_->StackChildAtTop(left_window_);
}
UpdateLayout(true);
}
void SplitViewController::ReplaceWindow(aura::Window* window,
aura::Window* replace_with) {
CHECK(IsSplitViewModeActive());
CHECK(replace_with);
CHECK(window == left_window_ || window == right_window_);
CHECK(replace_with != left_window_ && replace_with != right_window_);
#if !defined(NDEBUG)
aura::Window::Windows windows = window_list_provider_->GetWindowList();
DCHECK(std::find(windows.begin(), windows.end(), replace_with) !=
windows.end());
#endif
replace_with->SetBounds(window->bounds());
replace_with->SetTransform(gfx::Transform());
if (window == left_window_)
left_window_ = replace_with;
else
right_window_ = replace_with;
wm::ActivateWindow(replace_with);
window->SetTransform(gfx::Transform());
}
void SplitViewController::DeactivateSplitMode() {
CHECK_NE(SCROLLING, state_);
state_ = INACTIVE;
left_window_ = right_window_ = NULL;
}
void SplitViewController::UpdateLayout(bool animate) {
if (!left_window_)
return;
CHECK(right_window_);
gfx::Transform left_transform;
gfx::Transform right_transform;
int container_width = container_->GetBoundsInScreen().width();
if (state_ == ACTIVE) {
// This method should only be called once in ACTIVE state when
// the left and rightwindows are still full screen and need to be resized.
CHECK_EQ(left_window_->bounds().width(), container_width);
CHECK_EQ(right_window_->bounds().width(), container_width);
// Windows should be resized via an animation when entering the ACTIVE
// state.
CHECK(animate);
// We scale the windows here, but when the animation finishes, we reset
// the scaling and update the window bounds to the proper size - see
// OnAnimationCompleted().
left_transform.Scale(.5, 1);
right_transform.Scale(.5, 1);
right_transform.Translate(container_width, 0);
} else {
left_transform.Translate(separator_position_ - container_width, 0);
right_transform.Translate(separator_position_, 0);
}
left_window_->Show();
right_window_->Show();
SetWindowTransform(left_window_, left_transform, animate);
SetWindowTransform(right_window_, right_transform, animate);
}
void SplitViewController::SetWindowTransform(aura::Window* window,
const gfx::Transform& transform,
bool animate) {
if (animate) {
scoped_refptr<ui::LayerAnimator> animator = window->layer()->GetAnimator();
ui::ScopedLayerAnimationSettings settings(animator);
settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
settings.AddObserver(new ui::ClosureAnimationObserver(
base::Bind(&SplitViewController::OnAnimationCompleted,
weak_factory_.GetWeakPtr(),
window)));
window->SetTransform(transform);
} else {
window->SetTransform(transform);
}
}
void SplitViewController::OnAnimationCompleted(aura::Window* window) {
// Animation can be cancelled when deactivated.
if (left_window_ == NULL)
return;
DCHECK(window == left_window_ || window == right_window_);
if (state_ == ACTIVE) {
int container_width = container_->bounds().width();
gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
gfx::Rect window_bounds(container_width / 2, display.work_area().height());
window->SetTransform(gfx::Transform());
if (window == left_window_) {
left_window_->SetBounds(window_bounds);
} else {
window_bounds.set_x(container_width / 2);
right_window_->SetBounds(window_bounds);
}
} else {
int container_width = container_->bounds().width();
window->SetTransform(gfx::Transform());
if (window == left_window_) {
if (separator_position_ == 0)
left_window_->Hide();
if (state_ == INACTIVE)
left_window_ = NULL;
} else {
if (separator_position_ == container_width)
right_window_->Hide();
if (state_ == INACTIVE)
right_window_ = NULL;
}
}
}
void SplitViewController::UpdateSeparatorPositionFromScrollDelta(float delta) {
gfx::Screen* screen = gfx::Screen::GetScreenFor(container_);
const gfx::Rect& display_bounds =
screen->GetDisplayNearestWindow(container_).bounds();
gfx::Rect container_bounds = container_->GetBoundsInScreen();
separator_position_ =
delta > 0 ? ((int)delta) + display_bounds.x() - container_bounds.x()
: display_bounds.right() - container_bounds.x() + delta;
}
///////////////////////////////////////////////////////////////////////////////
// BezelController::ScrollDelegate:
void SplitViewController::ScrollBegin(BezelController::Bezel bezel,
float delta) {
if (!CanScroll())
return;
state_ = SCROLLING;
aura::Window::Windows windows = window_list_provider_->GetWindowList();
CHECK(windows.size() >= 2);
aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
aura::Window* current_window = *(iter);
CHECK(wm::IsActiveWindow(current_window));
if (delta > 0) {
right_window_ = current_window;
left_window_ = *(iter + 1);
} else {
left_window_ = current_window;
right_window_ = *(iter + 1);
}
CHECK(left_window_);
CHECK(right_window_);
UpdateSeparatorPositionFromScrollDelta(delta);
UpdateLayout(false);
}
void SplitViewController::ScrollEnd() {
if (state_ != SCROLLING)
return;
// Max distance from the scroll end position to the middle of the screen where
// we would go into the split view mode.
const int kMaxDistanceFromMiddle = 120;
int container_width = container_->GetBoundsInScreen().width();
if (std::abs(container_width / 2 - separator_position_) <=
kMaxDistanceFromMiddle) {
state_ = ACTIVE;
separator_position_ = container_width / 2;
} else if (separator_position_ < container_width / 2) {
separator_position_ = 0;
state_ = INACTIVE;
wm::ActivateWindow(right_window_);
} else {
separator_position_ = container_width;
state_ = INACTIVE;
wm::ActivateWindow(left_window_);
}
UpdateLayout(true);
}
void SplitViewController::ScrollUpdate(float delta) {
if (state_ != SCROLLING)
return;
UpdateSeparatorPositionFromScrollDelta(delta);
UpdateLayout(false);
}
bool SplitViewController::CanScroll() {
// TODO(mfomitchev): return false in vertical orientation, in full screen.
bool result = (!IsSplitViewModeActive() &&
window_list_provider_->GetWindowList().size() >= 2);
return result;
}
} // namespace athena
|