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/transient_window_manager.h"
27 #include "ui/wm/core/window_util.h"
28 #include "ui/wm/core/wm_state.h"
29 #include "ui/wm/public/activation_client.h"
30 #include "ui/wm/public/window_types.h"
34 class WindowManagerImpl
* instance
= NULL
;
36 void SetWindowState(aura::Window
* window
,
37 const gfx::Rect
& bounds
,
38 const gfx::Transform
& transform
) {
39 window
->SetBounds(bounds
);
40 window
->SetTransform(transform
);
45 class AthenaContainerLayoutManager
: public aura::LayoutManager
{
47 AthenaContainerLayoutManager();
48 virtual ~AthenaContainerLayoutManager();
51 // aura::LayoutManager:
52 virtual void OnWindowResized() override
;
53 virtual void OnWindowAddedToLayout(aura::Window
* child
) override
;
54 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
;
55 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) override
;
56 virtual void OnChildWindowVisibilityChanged(aura::Window
* child
,
57 bool visible
) override
;
58 virtual void SetChildBounds(aura::Window
* child
,
59 const gfx::Rect
& requested_bounds
) override
;
61 DISALLOW_COPY_AND_ASSIGN(AthenaContainerLayoutManager
);
64 AthenaContainerLayoutManager::AthenaContainerLayoutManager() {
67 AthenaContainerLayoutManager::~AthenaContainerLayoutManager() {
70 void AthenaContainerLayoutManager::OnWindowResized() {
71 // Resize all the existing windows.
72 const aura::Window::Windows
& list
=
73 instance
->window_list_provider_
->GetWindowList();
74 const gfx::Size work_area
=
75 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
76 bool is_splitview
= instance
->split_view_controller_
->IsSplitViewModeActive();
79 CHECK(instance
->split_view_controller_
->left_window());
81 instance
->split_view_controller_
->left_window()->bounds().size();
84 for (aura::Window::Windows::const_iterator iter
= list
.begin();
87 aura::Window
* window
= *iter
;
89 if (window
== instance
->split_view_controller_
->left_window())
90 window
->SetBounds(gfx::Rect(split_size
));
91 else if (window
== instance
->split_view_controller_
->right_window())
93 gfx::Rect(gfx::Point(split_size
.width(), 0), split_size
));
95 window
->SetBounds(gfx::Rect(work_area
));
97 window
->SetBounds(gfx::Rect(work_area
));
102 void AthenaContainerLayoutManager::OnWindowAddedToLayout(aura::Window
* child
) {
103 // TODO(oshima): Split view modes needs to take the transient window into
105 if (wm::GetTransientParent(child
)) {
106 wm::TransientWindowManager::Get(child
)
107 ->set_parent_controls_visibility(true);
110 if (!instance
->window_list_provider_
->IsValidWindow(child
))
113 if (instance
->split_view_controller_
->IsSplitViewModeActive() &&
114 !instance
->IsOverviewModeActive()) {
115 instance
->split_view_controller_
->ReplaceWindow(
116 instance
->split_view_controller_
->left_window(), child
);
119 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
120 child
->SetBounds(gfx::Rect(size
));
123 if (instance
->IsOverviewModeActive()) {
124 // TODO(pkotwicz|oshima). Creating a new window should only exit overview
125 // mode if the new window is activated. crbug.com/415266
126 instance
->OnSelectWindow(child
);
130 void AthenaContainerLayoutManager::OnWillRemoveWindowFromLayout(
131 aura::Window
* child
) {
134 void AthenaContainerLayoutManager::OnWindowRemovedFromLayout(
135 aura::Window
* child
) {
138 void AthenaContainerLayoutManager::OnChildWindowVisibilityChanged(
143 void AthenaContainerLayoutManager::SetChildBounds(
145 const gfx::Rect
& requested_bounds
) {
146 if (!requested_bounds
.IsEmpty())
147 SetChildBoundsDirect(child
, requested_bounds
);
150 WindowManagerImpl::WindowManagerImpl() {
151 ScreenManager::ContainerParams
params("DefaultContainer", CP_DEFAULT
);
152 params
.can_activate_children
= true;
153 params
.default_parent
= true;
154 params
.modal_container_priority
= CP_SYSTEM_MODAL
;
155 container_
.reset(ScreenManager::Get()->CreateContainer(params
));
156 container_
->SetLayoutManager(new AthenaContainerLayoutManager
);
157 container_
->AddObserver(this);
158 window_list_provider_
.reset(new WindowListProviderImpl(container_
.get()));
159 bezel_controller_
.reset(new BezelController(container_
.get()));
160 split_view_controller_
.reset(
161 new SplitViewController(container_
.get(), window_list_provider_
.get()));
162 AddObserver(split_view_controller_
.get());
163 bezel_controller_
->set_left_right_delegate(split_view_controller_
.get());
164 container_
->AddPreTargetHandler(bezel_controller_
.get());
165 title_drag_controller_
.reset(new TitleDragController(container_
.get(), this));
166 wm_state_
.reset(new wm::WMState());
167 aura::client::ActivationClient
* activation_client
=
168 aura::client::GetActivationClient(container_
->GetRootWindow());
169 DCHECK(container_
->GetRootWindow());
170 DCHECK(activation_client
);
171 shadow_controller_
.reset(new wm::ShadowController(activation_client
));
173 InstallAccelerators();
176 WindowManagerImpl::~WindowManagerImpl() {
178 RemoveObserver(split_view_controller_
.get());
179 split_view_controller_
.reset();
180 window_list_provider_
.reset();
182 container_
->RemoveObserver(this);
183 container_
->RemovePreTargetHandler(bezel_controller_
.get());
185 // |title_drag_controller_| needs to be reset before |container_|.
186 title_drag_controller_
.reset();
191 void WindowManagerImpl::ToggleSplitView() {
192 if (IsOverviewModeActive())
195 if (split_view_controller_
->IsSplitViewModeActive()) {
196 split_view_controller_
->DeactivateSplitMode();
197 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeExit());
198 // Relayout so that windows are maximzied.
199 container_
->layout_manager()->OnWindowResized();
200 } else if (split_view_controller_
->CanActivateSplitViewMode()) {
201 FOR_EACH_OBSERVER(WindowManagerObserver
,
203 OnSplitViewModeEnter());
204 split_view_controller_
->ActivateSplitMode(NULL
, NULL
, NULL
);
208 void WindowManagerImpl::EnterOverview() {
209 if (IsOverviewModeActive())
212 bezel_controller_
->set_left_right_delegate(NULL
);
213 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnOverviewModeEnter());
215 // Note: The window_list_provider_ resembles the exact window list of the
216 // container, so no re-stacking is required before showing the OverviewMode.
217 overview_
= WindowOverviewMode::Create(
218 container_
.get(), window_list_provider_
.get(),
219 split_view_controller_
.get(), this);
220 AcceleratorManager::Get()->RegisterAccelerator(kEscAcceleratorData
, this);
223 void WindowManagerImpl::ExitOverview() {
224 if (!IsOverviewModeActive())
227 ExitOverviewNoActivate();
229 // Activate the window which was active prior to entering overview.
230 const aura::Window::Windows windows
= window_list_provider_
->GetWindowList();
231 if (!windows
.empty()) {
232 aura::Window
* window_to_activate
= windows
.back();
234 // Show the window in case the exit overview animation has finished and
235 // |window| was hidden.
236 window_to_activate
->Show();
237 wm::ActivateWindow(window_to_activate
);
241 bool WindowManagerImpl::IsOverviewModeActive() {
245 void WindowManagerImpl::ExitOverviewNoActivate() {
246 if (!IsOverviewModeActive())
249 bezel_controller_
->set_left_right_delegate(split_view_controller_
.get());
251 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnOverviewModeExit());
252 AcceleratorManager::Get()->UnregisterAccelerator(kEscAcceleratorData
, this);
255 void WindowManagerImpl::InstallAccelerators() {
256 const AcceleratorData accelerator_data
[] = {
265 CMD_TOGGLE_SPLIT_VIEW
,
274 AcceleratorManager::Get()->RegisterAccelerators(
275 accelerator_data
, arraysize(accelerator_data
), this);
278 void WindowManagerImpl::AddObserver(WindowManagerObserver
* observer
) {
279 observers_
.AddObserver(observer
);
282 void WindowManagerImpl::RemoveObserver(WindowManagerObserver
* observer
) {
283 observers_
.RemoveObserver(observer
);
286 void WindowManagerImpl::ToggleSplitViewForTest() {
290 WindowListProvider
* WindowManagerImpl::GetWindowListProvider() {
291 return window_list_provider_
.get();
294 void WindowManagerImpl::OnSelectWindow(aura::Window
* window
) {
295 ExitOverviewNoActivate();
297 // Show the window in case the exit overview animation has finished and
298 // |window| was hidden.
301 wm::ActivateWindow(window
);
303 if (split_view_controller_
->IsSplitViewModeActive()) {
304 split_view_controller_
->DeactivateSplitMode();
305 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeExit());
307 // If |window| does not have the size of the work-area, then make sure it is
309 const gfx::Size work_area
=
310 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
311 if (window
->GetTargetBounds().size() != work_area
) {
312 const gfx::Rect
& window_bounds
= window
->bounds();
313 const gfx::Rect
desired_bounds(work_area
);
314 gfx::Transform transform
;
315 transform
.Translate(desired_bounds
.x() - window_bounds
.x(),
316 desired_bounds
.y() - window_bounds
.y());
317 transform
.Scale(desired_bounds
.width() / window_bounds
.width(),
318 desired_bounds
.height() / window_bounds
.height());
319 ui::ScopedLayerAnimationSettings
settings(window
->layer()->GetAnimator());
320 settings
.SetPreemptionStrategy(
321 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
322 settings
.AddObserver(
323 new ui::ClosureAnimationObserver(base::Bind(&SetWindowState
,
324 base::Unretained(window
),
327 window
->SetTransform(transform
);
331 void WindowManagerImpl::OnSelectSplitViewWindow(aura::Window
* left
,
333 aura::Window
* to_activate
) {
334 ExitOverviewNoActivate();
335 FOR_EACH_OBSERVER(WindowManagerObserver
, observers_
, OnSplitViewModeEnter());
336 split_view_controller_
->ActivateSplitMode(left
, right
, to_activate
);
339 void WindowManagerImpl::OnWindowDestroying(aura::Window
* window
) {
340 if (window
== container_
)
344 bool WindowManagerImpl::IsCommandEnabled(int command_id
) const {
348 bool WindowManagerImpl::OnAcceleratorFired(int command_id
,
349 const ui::Accelerator
& accelerator
) {
350 switch (command_id
) {
351 case CMD_EXIT_OVERVIEW
:
354 case CMD_TOGGLE_OVERVIEW
:
355 if (IsOverviewModeActive())
360 case CMD_TOGGLE_SPLIT_VIEW
:
367 aura::Window
* WindowManagerImpl::GetWindowBehind(aura::Window
* window
) {
368 const aura::Window::Windows
& windows
= window_list_provider_
->GetWindowList();
369 aura::Window::Windows::const_reverse_iterator iter
=
370 std::find(windows
.rbegin(), windows
.rend(), window
);
371 CHECK(iter
!= windows
.rend());
373 aura::Window
* behind
= NULL
;
374 if (iter
!= windows
.rend())
377 if (split_view_controller_
->IsSplitViewModeActive()) {
378 aura::Window
* left
= split_view_controller_
->left_window();
379 aura::Window
* right
= split_view_controller_
->right_window();
380 CHECK(window
== left
|| window
== right
);
381 if (behind
== left
|| behind
== right
)
382 behind
= (iter
== windows
.rend()) ? NULL
: *iter
;
388 void WindowManagerImpl::OnTitleDragStarted(aura::Window
* window
) {
389 aura::Window
* next_window
= GetWindowBehind(window
);
392 // Make sure |window| is active.
393 wm::ActivateWindow(window
);
395 // Make sure |next_window| is visibile.
398 // Position |next_window| correctly (left aligned if it's larger than
399 // |window|, and center aligned otherwise).
400 int dx
= window
->bounds().x() - next_window
->bounds().x();
401 if (next_window
->bounds().width() < window
->bounds().width())
402 dx
-= (next_window
->bounds().width() - window
->bounds().width()) / 2;
405 gfx::Transform transform
;
406 transform
.Translate(dx
, 0);
407 next_window
->SetTransform(transform
);
411 void WindowManagerImpl::OnTitleDragCompleted(aura::Window
* window
) {
412 aura::Window
* next_window
= GetWindowBehind(window
);
415 if (split_view_controller_
->IsSplitViewModeActive()) {
416 split_view_controller_
->ReplaceWindow(window
, next_window
);
418 ui::ScopedLayerAnimationSettings
419 settings(next_window
->layer()->GetAnimator());
420 settings
.AddObserver(new ui::ClosureAnimationObserver(
421 base::Bind(&SetWindowState
,
422 base::Unretained(next_window
),
426 gfx::Transform transform
;
427 transform
.Scale(window
->bounds().width() / next_window
->bounds().width(),
428 window
->bounds().height() / next_window
->bounds().height());
429 transform
.Translate(window
->bounds().x() - next_window
->bounds().x(), 0);
430 next_window
->SetTransform(transform
);
432 wm::ActivateWindow(next_window
);
437 void WindowManagerImpl::OnTitleDragCanceled(aura::Window
* window
) {
438 aura::Window
* next_window
= GetWindowBehind(window
);
441 next_window
->SetTransform(gfx::Transform());
446 WindowManager
* WindowManager::Create() {
448 new WindowManagerImpl
;
454 void WindowManager::Shutdown() {
461 WindowManager
* WindowManager::Get() {
466 } // namespace athena