1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "athena/wm/window_manager_impl.h"
9 #include "athena/screen/public/screen_manager.h"
10 #include "athena/util/container_priorities.h"
11 #include "athena/wm/bezel_controller.h"
12 #include "athena/wm/public/window_manager_observer.h"
13 #include "athena/wm/split_view_controller.h"
14 #include "athena/wm/title_drag_controller.h"
15 #include "athena/wm/window_list_provider_impl.h"
16 #include "athena/wm/window_overview_mode.h"
17 #include "base/bind.h"
18 #include "base/logging.h"
19 #include "ui/aura/layout_manager.h"
20 #include "ui/aura/window.h"
21 #include "ui/compositor/closure_animation_observer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/display.h"
24 #include "ui/gfx/screen.h"
25 #include "ui/wm/core/shadow_controller.h"
26 #include "ui/wm/core/window_util.h"
27 #include "ui/wm/core/wm_state.h"
28 #include "ui/wm/public/activation_client.h"
29 #include "ui/wm/public/window_types.h"
33 class WindowManagerImpl
* instance
= NULL
;
35 void SetWindowState(aura::Window
* window
,
36 const gfx::Rect
& bounds
,
37 const gfx::Transform
& transform
) {
38 window
->SetBounds(bounds
);
39 window
->SetTransform(transform
);
44 class AthenaContainerLayoutManager
: public aura::LayoutManager
{
46 AthenaContainerLayoutManager();
47 virtual ~AthenaContainerLayoutManager();
50 // aura::LayoutManager:
51 virtual void OnWindowResized() OVERRIDE
;
52 virtual void OnWindowAddedToLayout(aura::Window
* child
) OVERRIDE
;
53 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) OVERRIDE
;
54 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) OVERRIDE
;
55 virtual void OnChildWindowVisibilityChanged(aura::Window
* child
,
56 bool visible
) OVERRIDE
;
57 virtual void SetChildBounds(aura::Window
* child
,
58 const gfx::Rect
& requested_bounds
) OVERRIDE
;
60 DISALLOW_COPY_AND_ASSIGN(AthenaContainerLayoutManager
);
63 AthenaContainerLayoutManager::AthenaContainerLayoutManager() {
66 AthenaContainerLayoutManager::~AthenaContainerLayoutManager() {
69 void AthenaContainerLayoutManager::OnWindowResized() {
70 // Resize all the existing windows.
71 aura::Window::Windows list
= instance
->window_list_provider_
->GetWindowList();
72 const gfx::Size work_area
=
73 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
74 bool is_splitview
= instance
->split_view_controller_
->IsSplitViewModeActive();
77 CHECK(instance
->split_view_controller_
->left_window());
79 instance
->split_view_controller_
->left_window()->bounds().size();
82 for (aura::Window::Windows::const_iterator iter
= list
.begin();
85 aura::Window
* window
= *iter
;
87 if (window
== instance
->split_view_controller_
->left_window())
88 window
->SetBounds(gfx::Rect(split_size
));
89 else if (window
== instance
->split_view_controller_
->right_window())
91 gfx::Rect(gfx::Point(split_size
.width(), 0), split_size
));
93 window
->SetBounds(gfx::Rect(work_area
));
95 window
->SetBounds(gfx::Rect(work_area
));
100 void AthenaContainerLayoutManager::OnWindowAddedToLayout(aura::Window
* child
) {
101 aura::Window::Windows list
= instance
->window_list_provider_
->GetWindowList();
102 if (std::find(list
.begin(), list
.end(), child
) == list
.end())
104 if (instance
->split_view_controller_
->IsSplitViewModeActive()) {
105 instance
->split_view_controller_
->ReplaceWindow(
106 instance
->split_view_controller_
->left_window(), child
);
109 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
110 child
->SetBounds(gfx::Rect(size
));
114 void AthenaContainerLayoutManager::OnWillRemoveWindowFromLayout(
115 aura::Window
* child
) {
118 void AthenaContainerLayoutManager::OnWindowRemovedFromLayout(
119 aura::Window
* child
) {
122 void AthenaContainerLayoutManager::OnChildWindowVisibilityChanged(
127 void AthenaContainerLayoutManager::SetChildBounds(
129 const gfx::Rect
& requested_bounds
) {
130 if (!requested_bounds
.IsEmpty())
131 SetChildBoundsDirect(child
, requested_bounds
);
134 WindowManagerImpl::WindowManagerImpl() {
135 ScreenManager::ContainerParams
params("DefaultContainer", CP_DEFAULT
);
136 params
.can_activate_children
= true;
137 container_
.reset(ScreenManager::Get()->CreateDefaultContainer(params
));
138 container_
->SetLayoutManager(new AthenaContainerLayoutManager
);
139 container_
->AddObserver(this);
140 window_list_provider_
.reset(new WindowListProviderImpl(container_
.get()));
141 bezel_controller_
.reset(new BezelController(container_
.get()));
142 split_view_controller_
.reset(
143 new SplitViewController(container_
.get(), window_list_provider_
.get()));
144 bezel_controller_
->set_left_right_delegate(split_view_controller_
.get());
145 container_
->AddPreTargetHandler(bezel_controller_
.get());
146 title_drag_controller_
.reset(new TitleDragController(container_
.get(), this));
147 wm_state_
.reset(new wm::WMState());
148 aura::client::ActivationClient
* activation_client
=
149 aura::client::GetActivationClient(container_
->GetRootWindow());
150 shadow_controller_
.reset(new wm::ShadowController(activation_client
));
152 InstallAccelerators();
155 WindowManagerImpl::~WindowManagerImpl() {
157 split_view_controller_
.reset();
158 window_list_provider_
.reset();
160 container_
->RemoveObserver(this);
161 container_
->RemovePreTargetHandler(bezel_controller_
.get());
163 // |title_drag_controller_| needs to be reset before |container_|.
164 title_drag_controller_
.reset();
169 void WindowManagerImpl::ToggleOverview() {
170 SetInOverview(overview_
.get() == NULL
);
173 bool WindowManagerImpl::IsOverviewModeActive() {
177 void WindowManagerImpl::SetInOverview(bool active
) {
178 bool in_overview
= !!overview_
;
179 if (active
== in_overview
)
182 bezel_controller_
->set_left_right_delegate(
183 active
? NULL
: split_view_controller_
.get());
185 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnOverviewModeEnter());
187 // Note: The window_list_provider_ resembles the exact window list of the
188 // container, so no re-stacking is required before showing the OverviewMode.
189 overview_
= WindowOverviewMode::Create(
190 container_
.get(), window_list_provider_
.get(),
191 split_view_controller_
.get(), this);
194 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnOverviewModeExit());
198 void WindowManagerImpl::InstallAccelerators() {
199 const AcceleratorData accelerator_data
[] = {
200 {TRIGGER_ON_PRESS
, ui::VKEY_F6
, ui::EF_NONE
, CMD_TOGGLE_OVERVIEW
,
202 {TRIGGER_ON_PRESS
, ui::VKEY_F6
, ui::EF_CONTROL_DOWN
,
203 CMD_TOGGLE_SPLIT_VIEW
, AF_NONE
},
205 AcceleratorManager::Get()->RegisterAccelerators(
206 accelerator_data
, arraysize(accelerator_data
), this);
209 void WindowManagerImpl::AddObserver(WindowManagerObserver
* observer
) {
210 observers_
.AddObserver(observer
);
213 void WindowManagerImpl::RemoveObserver(WindowManagerObserver
* observer
) {
214 observers_
.RemoveObserver(observer
);
217 void WindowManagerImpl::ToggleSplitViewForTest() {
221 WindowListProvider
* WindowManagerImpl::GetWindowListProvider() {
222 return window_list_provider_
.get();
225 void WindowManagerImpl::OnSelectWindow(aura::Window
* window
) {
226 if (split_view_controller_
->IsSplitViewModeActive()) {
227 split_view_controller_
->DeactivateSplitMode();
228 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeExit());
230 wm::ActivateWindow(window
);
231 SetInOverview(false);
232 // If |window| does not have the size of the work-area, then make sure it is
234 const gfx::Size work_area
=
235 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
236 if (window
->GetTargetBounds().size() != work_area
) {
237 const gfx::Rect
& window_bounds
= window
->bounds();
238 const gfx::Rect
desired_bounds(work_area
);
239 gfx::Transform transform
;
240 transform
.Translate(desired_bounds
.x() - window_bounds
.x(),
241 desired_bounds
.y() - window_bounds
.y());
242 transform
.Scale(desired_bounds
.width() / window_bounds
.width(),
243 desired_bounds
.height() / window_bounds
.height());
244 window
->layer()->GetAnimator()->AbortAllAnimations();
245 ui::ScopedLayerAnimationSettings
settings(window
->layer()->GetAnimator());
246 settings
.SetPreemptionStrategy(
247 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
248 settings
.AddObserver(
249 new ui::ClosureAnimationObserver(base::Bind(&SetWindowState
,
250 base::Unretained(window
),
253 window
->SetTransform(transform
);
257 void WindowManagerImpl::OnSplitViewMode(aura::Window
* left
,
258 aura::Window
* right
) {
259 SetInOverview(false);
260 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeEnter());
261 split_view_controller_
->ActivateSplitMode(left
, right
);
264 void WindowManagerImpl::OnWindowAdded(aura::Window
* new_window
) {
265 // TODO(oshima): Creating a new window should updates the ovewview mode
266 // instead of exitting.
267 if (new_window
->type() == ui::wm::WINDOW_TYPE_NORMAL
)
268 SetInOverview(false);
271 void WindowManagerImpl::OnWindowDestroying(aura::Window
* window
) {
272 if (window
== container_
)
276 bool WindowManagerImpl::IsCommandEnabled(int command_id
) const {
280 bool WindowManagerImpl::OnAcceleratorFired(int command_id
,
281 const ui::Accelerator
& accelerator
) {
282 switch (command_id
) {
283 case CMD_TOGGLE_OVERVIEW
:
286 case CMD_TOGGLE_SPLIT_VIEW
:
293 void WindowManagerImpl::ToggleSplitview() {
294 // TODO(oshima): Figure out what to do.
295 if (IsOverviewModeActive())
298 if (split_view_controller_
->IsSplitViewModeActive()) {
299 split_view_controller_
->DeactivateSplitMode();
300 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeExit());
301 // Relayout so that windows are maximzied.
302 container_
->layout_manager()->OnWindowResized();
303 } else if (window_list_provider_
->GetWindowList().size() > 1) {
304 FOR_EACH_OBSERVER(WindowManagerObserver
,
306 OnSplitViewModeEnter());
307 split_view_controller_
->ActivateSplitMode(NULL
, NULL
);
311 aura::Window
* WindowManagerImpl::GetWindowBehind(aura::Window
* window
) {
312 const aura::Window::Windows
& windows
= window_list_provider_
->GetWindowList();
313 aura::Window::Windows::const_reverse_iterator iter
=
314 std::find(windows
.rbegin(), windows
.rend(), window
);
315 CHECK(iter
!= windows
.rend());
317 aura::Window
* behind
= NULL
;
318 if (iter
!= windows
.rend())
321 if (split_view_controller_
->IsSplitViewModeActive()) {
322 aura::Window
* left
= split_view_controller_
->left_window();
323 aura::Window
* right
= split_view_controller_
->right_window();
324 CHECK(window
== left
|| window
== right
);
325 if (behind
== left
|| behind
== right
)
326 behind
= (iter
== windows
.rend()) ? NULL
: *iter
;
332 void WindowManagerImpl::OnTitleDragStarted(aura::Window
* window
) {
333 aura::Window
* next_window
= GetWindowBehind(window
);
336 // Make sure |window| is active.
337 wm::ActivateWindow(window
);
339 // Make sure |next_window| is visibile.
342 // Position |next_window| correctly (left aligned if it's larger than
343 // |window|, and center aligned otherwise).
344 int dx
= window
->bounds().x() - next_window
->bounds().x();
345 if (next_window
->bounds().width() < window
->bounds().width())
346 dx
-= (next_window
->bounds().width() - window
->bounds().width()) / 2;
349 gfx::Transform transform
;
350 transform
.Translate(dx
, 0);
351 next_window
->SetTransform(transform
);
355 void WindowManagerImpl::OnTitleDragCompleted(aura::Window
* window
) {
356 aura::Window
* next_window
= GetWindowBehind(window
);
359 if (split_view_controller_
->IsSplitViewModeActive()) {
360 split_view_controller_
->ReplaceWindow(window
, next_window
);
361 wm::ActivateWindow(next_window
);
363 ui::ScopedLayerAnimationSettings
364 settings(next_window
->layer()->GetAnimator());
365 settings
.AddObserver(new ui::ClosureAnimationObserver(
366 base::Bind(&SetWindowState
,
367 base::Unretained(next_window
),
371 gfx::Transform transform
;
372 transform
.Scale(window
->bounds().width() / next_window
->bounds().width(),
373 window
->bounds().height() / next_window
->bounds().height());
374 transform
.Translate(window
->bounds().x() - next_window
->bounds().x(), 0);
375 next_window
->SetTransform(transform
);
377 wm::ActivateWindow(next_window
);
382 void WindowManagerImpl::OnTitleDragCanceled(aura::Window
* window
) {
383 aura::Window
* next_window
= GetWindowBehind(window
);
386 next_window
->SetTransform(gfx::Transform());
391 WindowManager
* WindowManager::Create() {
393 new WindowManagerImpl
;
399 void WindowManager::Shutdown() {
406 WindowManager
* WindowManager::GetInstance() {
411 } // namespace athena