Support modal windows
[chromium-blink-merge.git] / athena / wm / window_manager_impl.cc
blobf0020c47bc14f2917b393ef529fa78e96819cb0a
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"
7 #include <algorithm>
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"
32 namespace athena {
33 namespace {
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);
43 } // namespace
45 class AthenaContainerLayoutManager : public aura::LayoutManager {
46 public:
47 AthenaContainerLayoutManager();
48 virtual ~AthenaContainerLayoutManager();
50 private:
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();
77 gfx::Size split_size;
78 if (is_splitview) {
79 CHECK(instance->split_view_controller_->left_window());
80 split_size =
81 instance->split_view_controller_->left_window()->bounds().size();
84 for (aura::Window::Windows::const_iterator iter = list.begin();
85 iter != list.end();
86 ++iter) {
87 aura::Window* window = *iter;
88 if (is_splitview) {
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())
92 window->SetBounds(
93 gfx::Rect(gfx::Point(split_size.width(), 0), split_size));
94 else
95 window->SetBounds(gfx::Rect(work_area));
96 } else {
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
104 // account.
105 if (wm::GetTransientParent(child)) {
106 wm::TransientWindowManager::Get(child)
107 ->set_parent_controls_visibility(true);
110 if (!instance->window_list_provider_->IsValidWindow(child))
111 return;
113 if (instance->split_view_controller_->IsSplitViewModeActive() &&
114 !instance->IsOverviewModeActive()) {
115 instance->split_view_controller_->ReplaceWindow(
116 instance->split_view_controller_->left_window(), child);
117 } else {
118 gfx::Size size =
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(
139 aura::Window* child,
140 bool visible) {
143 void AthenaContainerLayoutManager::SetChildBounds(
144 aura::Window* child,
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));
172 instance = this;
173 InstallAccelerators();
176 WindowManagerImpl::~WindowManagerImpl() {
177 overview_.reset();
178 RemoveObserver(split_view_controller_.get());
179 split_view_controller_.reset();
180 window_list_provider_.reset();
181 if (container_) {
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();
187 container_.reset();
188 instance = NULL;
191 void WindowManagerImpl::ToggleSplitView() {
192 if (IsOverviewModeActive())
193 return;
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,
202 observers_,
203 OnSplitViewModeEnter());
204 split_view_controller_->ActivateSplitMode(NULL, NULL, NULL);
208 void WindowManagerImpl::EnterOverview() {
209 if (IsOverviewModeActive())
210 return;
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())
225 return;
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() {
242 return overview_;
245 void WindowManagerImpl::ExitOverviewNoActivate() {
246 if (!IsOverviewModeActive())
247 return;
249 bezel_controller_->set_left_right_delegate(split_view_controller_.get());
250 overview_.reset();
251 FOR_EACH_OBSERVER(WindowManagerObserver, observers_, OnOverviewModeExit());
252 AcceleratorManager::Get()->UnregisterAccelerator(kEscAcceleratorData, this);
255 void WindowManagerImpl::InstallAccelerators() {
256 const AcceleratorData accelerator_data[] = {
257 {TRIGGER_ON_PRESS,
258 ui::VKEY_F6,
259 ui::EF_NONE,
260 CMD_TOGGLE_OVERVIEW,
261 AF_NONE},
262 {TRIGGER_ON_PRESS,
263 ui::VKEY_F6,
264 ui::EF_CONTROL_DOWN,
265 CMD_TOGGLE_SPLIT_VIEW,
266 AF_NONE},
267 // Debug
268 {TRIGGER_ON_PRESS,
269 ui::VKEY_6,
270 ui::EF_NONE,
271 CMD_TOGGLE_OVERVIEW,
272 AF_NONE | AF_DEBUG},
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() {
287 ToggleSplitView();
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.
299 window->Show();
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
308 // resized.
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),
325 desired_bounds,
326 gfx::Transform())));
327 window->SetTransform(transform);
331 void WindowManagerImpl::OnSelectSplitViewWindow(aura::Window* left,
332 aura::Window* right,
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_)
341 container_.reset();
344 bool WindowManagerImpl::IsCommandEnabled(int command_id) const {
345 return true;
348 bool WindowManagerImpl::OnAcceleratorFired(int command_id,
349 const ui::Accelerator& accelerator) {
350 switch (command_id) {
351 case CMD_EXIT_OVERVIEW:
352 ExitOverview();
353 break;
354 case CMD_TOGGLE_OVERVIEW:
355 if (IsOverviewModeActive())
356 ExitOverview();
357 else
358 EnterOverview();
359 break;
360 case CMD_TOGGLE_SPLIT_VIEW:
361 ToggleSplitView();
362 break;
364 return true;
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());
372 ++iter;
373 aura::Window* behind = NULL;
374 if (iter != windows.rend())
375 behind = *iter++;
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;
385 return behind;
388 void WindowManagerImpl::OnTitleDragStarted(aura::Window* window) {
389 aura::Window* next_window = GetWindowBehind(window);
390 if (!next_window)
391 return;
392 // Make sure |window| is active.
393 wm::ActivateWindow(window);
395 // Make sure |next_window| is visibile.
396 next_window->Show();
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;
404 if (dx) {
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);
413 if (!next_window)
414 return;
415 if (split_view_controller_->IsSplitViewModeActive()) {
416 split_view_controller_->ReplaceWindow(window, next_window);
417 } else {
418 ui::ScopedLayerAnimationSettings
419 settings(next_window->layer()->GetAnimator());
420 settings.AddObserver(new ui::ClosureAnimationObserver(
421 base::Bind(&SetWindowState,
422 base::Unretained(next_window),
423 window->bounds(),
424 gfx::Transform())));
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);
434 window->Hide();
437 void WindowManagerImpl::OnTitleDragCanceled(aura::Window* window) {
438 aura::Window* next_window = GetWindowBehind(window);
439 if (!next_window)
440 return;
441 next_window->SetTransform(gfx::Transform());
442 next_window->Hide();
445 // static
446 WindowManager* WindowManager::Create() {
447 DCHECK(!instance);
448 new WindowManagerImpl;
449 DCHECK(instance);
450 return instance;
453 // static
454 void WindowManager::Shutdown() {
455 DCHECK(instance);
456 delete instance;
457 DCHECK(!instance);
460 // static
461 WindowManager* WindowManager::Get() {
462 DCHECK(instance);
463 return instance;
466 } // namespace athena