1 // Copyright (c) 2012 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 "ash/root_window_controller.h"
10 #include "ash/ash_constants.h"
11 #include "ash/ash_switches.h"
12 #include "ash/desktop_background/desktop_background_controller.h"
13 #include "ash/desktop_background/desktop_background_widget_controller.h"
14 #include "ash/desktop_background/user_wallpaper_delegate.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/focus_cycler.h"
17 #include "ash/high_contrast/high_contrast_controller.h"
18 #include "ash/host/ash_window_tree_host.h"
19 #include "ash/root_window_settings.h"
20 #include "ash/session/session_state_delegate.h"
21 #include "ash/shelf/shelf_layout_manager.h"
22 #include "ash/shelf/shelf_types.h"
23 #include "ash/shelf/shelf_widget.h"
24 #include "ash/shell.h"
25 #include "ash/shell_delegate.h"
26 #include "ash/shell_factory.h"
27 #include "ash/shell_window_ids.h"
28 #include "ash/switchable_windows.h"
29 #include "ash/system/status_area_widget.h"
30 #include "ash/system/tray/system_tray_delegate.h"
31 #include "ash/system/tray/system_tray_notifier.h"
32 #include "ash/touch/touch_hud_debug.h"
33 #include "ash/touch/touch_hud_projection.h"
34 #include "ash/touch/touch_observer_hud.h"
35 #include "ash/wm/always_on_top_controller.h"
36 #include "ash/wm/dock/docked_window_layout_manager.h"
37 #include "ash/wm/lock_layout_manager.h"
38 #include "ash/wm/panels/attached_panel_window_targeter.h"
39 #include "ash/wm/panels/panel_layout_manager.h"
40 #include "ash/wm/panels/panel_window_event_handler.h"
41 #include "ash/wm/root_window_layout_manager.h"
42 #include "ash/wm/screen_dimmer.h"
43 #include "ash/wm/stacking_controller.h"
44 #include "ash/wm/status_area_layout_manager.h"
45 #include "ash/wm/system_background_controller.h"
46 #include "ash/wm/system_modal_container_layout_manager.h"
47 #include "ash/wm/window_properties.h"
48 #include "ash/wm/window_state.h"
49 #include "ash/wm/window_util.h"
50 #include "ash/wm/workspace/workspace_layout_manager.h"
51 #include "ash/wm/workspace_controller.h"
52 #include "base/command_line.h"
53 #include "base/time/time.h"
54 #include "ui/aura/client/aura_constants.h"
55 #include "ui/aura/client/screen_position_client.h"
56 #include "ui/aura/window.h"
57 #include "ui/aura/window_delegate.h"
58 #include "ui/aura/window_event_dispatcher.h"
59 #include "ui/aura/window_observer.h"
60 #include "ui/aura/window_tracker.h"
61 #include "ui/base/hit_test.h"
62 #include "ui/base/models/menu_model.h"
63 #include "ui/gfx/display.h"
64 #include "ui/gfx/screen.h"
65 #include "ui/keyboard/keyboard_controller.h"
66 #include "ui/keyboard/keyboard_util.h"
67 #include "ui/views/controls/menu/menu_runner.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/core/capture_controller.h"
71 #include "ui/wm/core/easy_resize_window_targeter.h"
72 #include "ui/wm/core/visibility_controller.h"
73 #include "ui/wm/core/window_util.h"
74 #include "ui/wm/public/drag_drop_client.h"
75 #include "ui/wm/public/tooltip_client.h"
76 #include "ui/wm/public/window_types.h"
78 #if defined(OS_CHROMEOS)
79 #include "ash/ash_touch_exploration_manager_chromeos.h"
80 #include "ash/wm/boot_splash_screen_chromeos.h"
81 #include "ui/chromeos/touch_exploration_controller.h"
87 #if defined(OS_CHROMEOS)
88 // Duration for the animation that hides the boot splash screen, in
89 // milliseconds. This should be short enough in relation to
90 // wm/window_animation.cc's brightness/grayscale fade animation that the login
91 // background image animation isn't hidden by the splash screen animation.
92 const int kBootSplashScreenHideDurationMs
= 500;
95 // Creates a new window for use as a container.
96 aura::Window
* CreateContainer(int window_id
,
98 aura::Window
* parent
) {
99 aura::Window
* container
= new aura::Window(NULL
);
100 container
->set_id(window_id
);
101 container
->SetName(name
);
102 container
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
103 parent
->AddChild(container
);
104 if (window_id
!= kShellWindowId_UnparentedControlContainer
)
109 float ToRelativeValue(int value
, int src
, int dst
) {
110 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
113 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
114 const gfx::Size
& dst_size
,
115 gfx::Rect
* bounds_in_out
) {
116 gfx::Point origin
= bounds_in_out
->origin();
117 bounds_in_out
->set_origin(gfx::Point(
118 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
119 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
122 // Reparents |window| to |new_parent|.
123 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
124 const gfx::Size src_size
= window
->parent()->bounds().size();
125 const gfx::Size dst_size
= new_parent
->bounds().size();
126 // Update the restore bounds to make it relative to the display.
127 wm::WindowState
* state
= wm::GetWindowState(window
);
128 gfx::Rect restore_bounds
;
129 bool has_restore_bounds
= state
->HasRestoreBounds();
131 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
132 new_parent
->id() != kShellWindowId_DockedContainer
;
133 gfx::Rect local_bounds
;
135 local_bounds
= state
->window()->bounds();
136 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
139 if (has_restore_bounds
) {
140 restore_bounds
= state
->GetRestoreBoundsInParent();
141 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
144 new_parent
->AddChild(window
);
146 // Docked windows have bounds handled by the layout manager in AddChild().
148 window
->SetBounds(local_bounds
);
150 if (has_restore_bounds
)
151 state
->SetRestoreBoundsInParent(restore_bounds
);
154 // Reparents the appropriate set of windows from |src| to |dst|.
155 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
156 // Set of windows to move.
157 const int kContainerIdsToMove
[] = {
158 kShellWindowId_DefaultContainer
,
159 kShellWindowId_DockedContainer
,
160 kShellWindowId_PanelContainer
,
161 kShellWindowId_AlwaysOnTopContainer
,
162 kShellWindowId_SystemModalContainer
,
163 kShellWindowId_LockSystemModalContainer
,
164 kShellWindowId_UnparentedControlContainer
, };
165 for (size_t i
= 0; i
< arraysize(kContainerIdsToMove
); i
++) {
166 int id
= kContainerIdsToMove
[i
];
167 aura::Window
* src_container
= Shell::GetContainer(src
, id
);
168 aura::Window
* dst_container
= Shell::GetContainer(dst
, id
);
169 while (!src_container
->children().empty()) {
170 // Restart iteration from the source container windows each time as they
171 // may change as a result of moving other windows.
172 aura::Window::Windows::const_iterator iter
=
173 src_container
->children().begin();
174 while (iter
!= src_container
->children().end() &&
175 SystemModalContainerLayoutManager::IsModalBackground(*iter
)) {
178 // If the entire window list is modal background windows then stop.
179 if (iter
== src_container
->children().end())
181 ReparentWindow(*iter
, dst_container
);
186 // Mark the container window so that a widget added to this container will
187 // use the virtual screeen coordinates instead of parent.
188 void SetUsesScreenCoordinates(aura::Window
* container
) {
189 container
->SetProperty(kUsesScreenCoordinatesKey
, true);
192 // Mark the container window so that a widget added to this container will
193 // say in the same root window regardless of the bounds specified.
194 void DescendantShouldStayInSameRootWindow(aura::Window
* container
) {
195 container
->SetProperty(kStayInSameRootWindowKey
, true);
198 void SetUsesEasyResizeTargeter(aura::Window
* container
) {
199 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
200 -kResizeOutsideBoundsSize
,
201 -kResizeOutsideBoundsSize
,
202 -kResizeOutsideBoundsSize
);
203 gfx::Insets touch_extend
= mouse_extend
.Scale(
204 kResizeOutsideBoundsScaleForTouch
);
205 container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
206 new ::wm::EasyResizeWindowTargeter(container
, mouse_extend
,
210 // A window delegate which does nothing. Used to create a window that
211 // is a event target, but do nothing.
212 class EmptyWindowDelegate
: public aura::WindowDelegate
{
214 EmptyWindowDelegate() {}
215 virtual ~EmptyWindowDelegate() {}
217 // aura::WindowDelegate overrides:
218 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
221 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
224 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
225 const gfx::Rect
& new_bounds
) OVERRIDE
{
227 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
228 return gfx::kNullCursor
;
230 virtual int GetNonClientComponent(
231 const gfx::Point
& point
) const OVERRIDE
{
234 virtual bool ShouldDescendIntoChildForEventHandling(
236 const gfx::Point
& location
) OVERRIDE
{
239 virtual bool CanFocus() OVERRIDE
{
242 virtual void OnCaptureLost() OVERRIDE
{
244 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
246 virtual void OnDeviceScaleFactorChanged(
247 float device_scale_factor
) OVERRIDE
{
249 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
250 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{
253 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
255 virtual bool HasHitTestMask() const OVERRIDE
{
258 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
261 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
266 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost
* host
) {
267 RootWindowController
* controller
= new RootWindowController(host
);
268 controller
->Init(RootWindowController::PRIMARY
,
269 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
272 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost
* host
) {
273 RootWindowController
* controller
= new RootWindowController(host
);
274 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
278 RootWindowController
* RootWindowController::ForShelf(
279 const aura::Window
* window
) {
280 CHECK(Shell::HasInstance());
281 return GetRootWindowController(window
->GetRootWindow());
285 RootWindowController
* RootWindowController::ForWindow(
286 const aura::Window
* window
) {
287 CHECK(Shell::HasInstance());
288 return GetRootWindowController(window
->GetRootWindow());
292 RootWindowController
* RootWindowController::ForTargetRootWindow() {
293 CHECK(Shell::HasInstance());
294 return GetRootWindowController(Shell::GetTargetRootWindow());
298 aura::Window
* RootWindowController::GetContainerForWindow(
299 aura::Window
* window
) {
300 aura::Window
* container
= window
->parent();
301 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
302 container
= container
->parent();
306 RootWindowController::~RootWindowController() {
309 // The CaptureClient needs to be around for as long as the RootWindow is
311 capture_client_
.reset();
314 aura::WindowTreeHost
* RootWindowController::GetHost() {
315 return ash_host_
->AsWindowTreeHost();
318 const aura::WindowTreeHost
* RootWindowController::GetHost() const {
319 return ash_host_
->AsWindowTreeHost();
322 aura::Window
* RootWindowController::GetRootWindow() {
323 return GetHost()->window();
326 const aura::Window
* RootWindowController::GetRootWindow() const {
327 return GetHost()->window();
330 void RootWindowController::SetWallpaperController(
331 DesktopBackgroundWidgetController
* controller
) {
332 wallpaper_controller_
.reset(controller
);
335 void RootWindowController::SetAnimatingWallpaperController(
336 AnimatingDesktopController
* controller
) {
337 if (animating_wallpaper_controller_
.get())
338 animating_wallpaper_controller_
->StopAnimating();
339 animating_wallpaper_controller_
.reset(controller
);
342 void RootWindowController::Shutdown() {
343 Shell
* shell
= Shell::GetInstance();
344 shell
->RemoveShellObserver(this);
346 #if defined(OS_CHROMEOS)
347 if (touch_exploration_manager_
) {
348 touch_exploration_manager_
.reset();
352 if (animating_wallpaper_controller_
.get())
353 animating_wallpaper_controller_
->StopAnimating();
354 wallpaper_controller_
.reset();
355 animating_wallpaper_controller_
.reset();
356 aura::Window
* root_window
= GetRootWindow();
357 // Change the target root window before closing child windows. If any child
358 // being removed triggers a relayout of the shelf it will try to build a
359 // window list adding windows from the target root window's containers which
360 // may have already gone away.
361 if (Shell::GetTargetRootWindow() == root_window
) {
362 shell
->set_target_root_window(
363 Shell::GetPrimaryRootWindow() == root_window
365 : Shell::GetPrimaryRootWindow());
369 GetRootWindowSettings(root_window
)->controller
= NULL
;
370 screen_dimmer_
.reset();
371 workspace_controller_
.reset();
372 // Forget with the display ID so that display lookup
373 // ends up with invalid display.
374 GetRootWindowSettings(root_window
)->display_id
=
375 gfx::Display::kInvalidDisplayID
;
376 ash_host_
->PrepareForShutdown();
378 system_background_
.reset();
379 aura::client::SetScreenPositionClient(root_window
, NULL
);
382 SystemModalContainerLayoutManager
*
383 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
384 aura::Window
* modal_container
= NULL
;
386 aura::Window
* window_container
= GetContainerForWindow(window
);
387 if (window_container
&&
388 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
389 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
391 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
394 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
395 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
396 kShellWindowId_SystemModalContainer
;
397 modal_container
= GetContainer(modal_window_id
);
399 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
400 modal_container
->layout_manager()) : NULL
;
403 aura::Window
* RootWindowController::GetContainer(int container_id
) {
404 return GetRootWindow()->GetChildById(container_id
);
407 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
408 return ash_host_
->AsWindowTreeHost()->window()->GetChildById(container_id
);
411 void RootWindowController::ShowShelf() {
412 if (!shelf_
->shelf())
414 shelf_
->shelf()->SetVisible(true);
415 shelf_
->status_area_widget()->Show();
418 void RootWindowController::OnShelfCreated() {
419 if (panel_layout_manager_
)
420 panel_layout_manager_
->SetShelf(shelf_
->shelf());
421 if (docked_layout_manager_
) {
422 docked_layout_manager_
->SetShelf(shelf_
->shelf());
423 if (shelf_
->shelf_layout_manager())
424 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
427 // Notify shell observers that the shelf has been created.
428 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
431 void RootWindowController::UpdateAfterLoginStatusChange(
432 user::LoginStatus status
) {
433 if (status
!= user::LOGGED_IN_NONE
)
434 mouse_event_target_
.reset();
435 if (shelf_
->status_area_widget())
436 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
439 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
440 #if defined(OS_CHROMEOS)
441 if (CommandLine::ForCurrentProcess()->HasSwitch(
442 switches::kAshAnimateFromBootSplashScreen
) &&
443 boot_splash_screen_
.get()) {
444 // Make the splash screen fade out so it doesn't obscure the desktop
445 // wallpaper's brightness/grayscale animation.
446 boot_splash_screen_
->StartHideAnimation(
447 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
452 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
453 // Make sure the wallpaper is visible.
454 system_background_
->SetColor(SK_ColorBLACK
);
455 #if defined(OS_CHROMEOS)
456 boot_splash_screen_
.reset();
459 Shell::GetInstance()->user_wallpaper_delegate()->
460 OnWallpaperAnimationFinished();
461 // Only removes old component when wallpaper animation finished. If we
462 // remove the old one before the new wallpaper is done fading in there will
463 // be a white flash during the animation.
464 if (animating_wallpaper_controller()) {
465 DesktopBackgroundWidgetController
* controller
=
466 animating_wallpaper_controller()->GetController(true);
467 // |desktop_widget_| should be the same animating widget we try to move
468 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
469 // before move it to |kDesktopController|.
470 DCHECK_EQ(controller
->widget(), widget
);
471 // Release the old controller and close its background widget.
472 SetWallpaperController(controller
);
476 void RootWindowController::CloseChildWindows() {
477 mouse_event_target_
.reset();
479 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
480 // to fire notifications.
481 if (docked_layout_manager_
&& shelf_
&& shelf_
->shelf_layout_manager())
482 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
484 // Deactivate keyboard container before closing child windows and shutting
485 // down associated layout managers.
486 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
488 // panel_layout_manager_ needs to be shut down before windows are destroyed.
489 if (panel_layout_manager_
) {
490 panel_layout_manager_
->Shutdown();
491 panel_layout_manager_
= NULL
;
493 // docked_layout_manager_ needs to be shut down before windows are destroyed.
494 if (docked_layout_manager_
) {
495 docked_layout_manager_
->Shutdown();
496 docked_layout_manager_
= NULL
;
498 aura::Window
* root_window
= GetRootWindow();
499 aura::client::SetDragDropClient(root_window
, NULL
);
501 // TODO(harrym): Remove when Status Area Widget is a child view.
503 shelf_
->ShutdownStatusAreaWidget();
505 if (shelf_
->shelf_layout_manager())
506 shelf_
->shelf_layout_manager()->PrepareForShutdown();
509 // Close background widget first as it depends on tooltip.
510 wallpaper_controller_
.reset();
511 animating_wallpaper_controller_
.reset();
513 workspace_controller_
.reset();
514 aura::client::SetTooltipClient(root_window
, NULL
);
516 // Explicitly destroy top level windows. We do this as during part of
517 // destruction such windows may query the RootWindow for state.
518 std::queue
<aura::Window
*> non_toplevel_windows
;
519 non_toplevel_windows
.push(root_window
);
520 while (!non_toplevel_windows
.empty()) {
521 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
522 non_toplevel_windows
.pop();
523 aura::WindowTracker toplevel_windows
;
524 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
525 aura::Window
* child
= non_toplevel_window
->children()[i
];
526 if (!child
->owned_by_parent())
528 if (child
->delegate())
529 toplevel_windows
.Add(child
);
531 non_toplevel_windows
.push(child
);
533 while (!toplevel_windows
.windows().empty())
534 delete *toplevel_windows
.windows().begin();
536 // And then remove the containers.
537 while (!root_window
->children().empty()) {
538 aura::Window
* window
= root_window
->children()[0];
539 if (window
->owned_by_parent()) {
542 root_window
->RemoveChild(window
);
549 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
550 // Forget the shelf early so that shelf don't update itself using wrong
552 workspace_controller_
->SetShelf(NULL
);
553 ReparentAllWindows(GetRootWindow(), dst
);
556 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
557 return shelf_
->shelf_layout_manager();
560 SystemTray
* RootWindowController::GetSystemTray() {
561 // We assume in throughout the code that this will not return NULL. If code
562 // triggers this for valid reasons, it should test status_area_widget first.
563 CHECK(shelf_
->status_area_widget());
564 return shelf_
->status_area_widget()->system_tray();
567 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
568 ui::MenuSourceType source_type
) {
569 DCHECK(Shell::GetInstance()->delegate());
570 scoped_ptr
<ui::MenuModel
> menu_model(
571 Shell::GetInstance()->delegate()->CreateContextMenu(
572 GetRootWindow(), NULL
, NULL
));
576 // Background controller may not be set yet if user clicked on status are
577 // before initial animation completion. See crbug.com/222218
578 if (!wallpaper_controller_
.get())
581 views::MenuRunner
menu_runner(menu_model
.get(),
582 views::MenuRunner::CONTEXT_MENU
);
583 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
585 gfx::Rect(location_in_screen
, gfx::Size()),
586 views::MENU_ANCHOR_TOPLEFT
,
587 source_type
) == views::MenuRunner::MENU_DELETED
) {
591 Shell::GetInstance()->UpdateShelfVisibility();
594 void RootWindowController::UpdateShelfVisibility() {
595 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
598 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
599 const aura::Window
* topmost_window
= NULL
;
600 const aura::Window
* active_window
= wm::GetActiveWindow();
601 if (active_window
&& active_window
->GetRootWindow() == GetRootWindow() &&
602 IsSwitchableContainer(active_window
->parent())) {
603 // Use the active window when it is on the current root window to determine
604 // the fullscreen state to allow temporarily using a panel or docked window
605 // (which are always above the default container) while a fullscreen
606 // window is open. We only use the active window when in a switchable
607 // container as the launcher should not exit fullscreen mode.
608 topmost_window
= active_window
;
610 // Otherwise, use the topmost window on the root window's default container
611 // when there is no active window on this root window.
612 const aura::Window::Windows
& windows
=
613 GetContainer(kShellWindowId_DefaultContainer
)->children();
614 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
615 iter
!= windows
.rend(); ++iter
) {
616 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
617 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
618 (*iter
)->layer()->GetTargetVisibility()) {
619 topmost_window
= *iter
;
624 while (topmost_window
) {
625 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
626 return topmost_window
;
627 topmost_window
= ::wm::GetTransientParent(topmost_window
);
632 void RootWindowController::ActivateKeyboard(
633 keyboard::KeyboardController
* keyboard_controller
) {
634 if (!keyboard::IsKeyboardEnabled() ||
635 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
638 DCHECK(keyboard_controller
);
639 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
640 keyboard_controller
->AddObserver(panel_layout_manager_
);
641 keyboard_controller
->AddObserver(docked_layout_manager_
);
642 keyboard_controller
->AddObserver(workspace_controller_
->layout_manager());
643 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
644 aura::Window
* parent
= GetContainer(
645 kShellWindowId_VirtualKeyboardParentContainer
);
647 aura::Window
* keyboard_container
=
648 keyboard_controller
->GetContainerWindow();
649 keyboard_container
->set_id(kShellWindowId_VirtualKeyboardContainer
);
650 parent
->AddChild(keyboard_container
);
651 // TODO(oshima): Bounds of keyboard container should be handled by
652 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
653 keyboard_container
->SetBounds(parent
->bounds());
656 void RootWindowController::DeactivateKeyboard(
657 keyboard::KeyboardController
* keyboard_controller
) {
658 if (!keyboard_controller
||
659 !keyboard_controller
->keyboard_container_initialized()) {
662 aura::Window
* keyboard_container
=
663 keyboard_controller
->GetContainerWindow();
664 if (keyboard_container
->GetRootWindow() == GetRootWindow()) {
665 aura::Window
* parent
= GetContainer(
666 kShellWindowId_VirtualKeyboardParentContainer
);
668 parent
->RemoveChild(keyboard_container
);
669 // Virtual keyboard may be deactivated while still showing, notify all
670 // observers that keyboard bounds changed to 0 before remove them.
671 keyboard_controller
->NotifyKeyboardBoundsChanging(gfx::Rect());
672 keyboard_controller
->RemoveObserver(shelf()->shelf_layout_manager());
673 keyboard_controller
->RemoveObserver(panel_layout_manager_
);
674 keyboard_controller
->RemoveObserver(docked_layout_manager_
);
675 keyboard_controller
->RemoveObserver(
676 workspace_controller_
->layout_manager());
677 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
681 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window
* window
) {
682 aura::Window
* parent
= GetContainer(
683 kShellWindowId_VirtualKeyboardParentContainer
);
684 return parent
? parent
->Contains(window
) : false;
687 ////////////////////////////////////////////////////////////////////////////////
688 // RootWindowController, private:
690 RootWindowController::RootWindowController(AshWindowTreeHost
* ash_host
)
691 : ash_host_(ash_host
),
692 root_window_layout_(NULL
),
693 docked_layout_manager_(NULL
),
694 panel_layout_manager_(NULL
),
695 touch_hud_debug_(NULL
),
696 touch_hud_projection_(NULL
) {
697 aura::Window
* root_window
= GetRootWindow();
698 GetRootWindowSettings(root_window
)->controller
= this;
699 screen_dimmer_
.reset(new ScreenDimmer(root_window
));
701 stacking_controller_
.reset(new StackingController
);
702 aura::client::SetWindowTreeClient(root_window
, stacking_controller_
.get());
703 capture_client_
.reset(new ::wm::ScopedCaptureClient(root_window
));
706 void RootWindowController::Init(RootWindowType root_window_type
,
707 bool first_run_after_boot
) {
708 aura::Window
* root_window
= GetRootWindow();
709 Shell
* shell
= Shell::GetInstance();
710 shell
->InitRootWindow(root_window
);
712 ash_host_
->AsWindowTreeHost()->SetCursor(ui::kCursorPointer
);
713 CreateContainersInRootWindow(root_window
);
715 CreateSystemBackground(first_run_after_boot
);
717 InitLayoutManagers();
720 if (Shell::GetPrimaryRootWindowController()->
721 GetSystemModalLayoutManager(NULL
)->has_modal_background()) {
722 GetSystemModalLayoutManager(NULL
)->CreateModalBackground();
725 shell
->AddShellObserver(this);
727 if (root_window_type
== PRIMARY
) {
728 root_window_layout()->OnWindowResized();
729 shell
->InitKeyboard();
731 root_window_layout()->OnWindowResized();
732 ash_host_
->AsWindowTreeHost()->Show();
734 // Create a shelf if a user is already logged in.
735 if (shell
->session_state_delegate()->NumberOfLoggedInUsers())
736 shelf()->CreateShelf();
738 // Notify shell observers about new root window.
739 shell
->OnRootWindowAdded(root_window
);
742 #if defined(OS_CHROMEOS)
743 if (!CommandLine::ForCurrentProcess()->HasSwitch(
744 switches::kAshDisableTouchExplorationMode
)) {
745 touch_exploration_manager_
.reset(new AshTouchExplorationManager(this));
750 void RootWindowController::InitLayoutManagers() {
751 aura::Window
* root_window
= GetRootWindow();
752 root_window_layout_
= new RootWindowLayoutManager(root_window
);
753 root_window
->SetLayoutManager(root_window_layout_
);
755 aura::Window
* default_container
=
756 GetContainer(kShellWindowId_DefaultContainer
);
757 // Workspace manager has its own layout managers.
758 workspace_controller_
.reset(
759 new WorkspaceController(default_container
));
761 aura::Window
* always_on_top_container
=
762 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
763 always_on_top_container
->SetLayoutManager(
764 new WorkspaceLayoutManager(always_on_top_container
));
765 always_on_top_controller_
.reset(new AlwaysOnTopController
);
766 always_on_top_controller_
->SetAlwaysOnTopContainer(always_on_top_container
);
768 DCHECK(!shelf_
.get());
769 aura::Window
* shelf_container
= GetContainer(kShellWindowId_ShelfContainer
);
770 // TODO(harrym): Remove when status area is view.
771 aura::Window
* status_container
= GetContainer(kShellWindowId_StatusContainer
);
772 shelf_
.reset(new ShelfWidget(
773 shelf_container
, status_container
, workspace_controller()));
775 if (!Shell::GetInstance()->session_state_delegate()->
776 IsActiveUserSessionStarted()) {
777 // This window exists only to be a event target on login screen.
778 // It does not have to handle events, nor be visible.
779 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
780 mouse_event_target_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
782 aura::Window
* lock_background_container
=
783 GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
784 lock_background_container
->AddChild(mouse_event_target_
.get());
785 mouse_event_target_
->Show();
788 // Create Docked windows layout manager
789 aura::Window
* docked_container
= GetContainer(kShellWindowId_DockedContainer
);
790 docked_layout_manager_
=
791 new DockedWindowLayoutManager(docked_container
, workspace_controller());
792 docked_container
->SetLayoutManager(docked_layout_manager_
);
794 // Installs SnapLayoutManager to containers who set the
795 // |kSnapsChildrenToPhysicalPixelBoundary| property.
796 wm::InstallSnapLayoutManagerToContainers(root_window
);
798 // Create Panel layout manager
799 aura::Window
* panel_container
= GetContainer(kShellWindowId_PanelContainer
);
800 panel_layout_manager_
= new PanelLayoutManager(panel_container
);
801 panel_container
->SetLayoutManager(panel_layout_manager_
);
802 panel_container_handler_
.reset(new PanelWindowEventHandler
);
803 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
805 // Install an AttachedPanelWindowTargeter on the panel container to make it
806 // easier to correctly target shelf buttons with touch.
807 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
808 -kResizeOutsideBoundsSize
,
809 -kResizeOutsideBoundsSize
,
810 -kResizeOutsideBoundsSize
);
811 gfx::Insets touch_extend
= mouse_extend
.Scale(
812 kResizeOutsideBoundsScaleForTouch
);
813 panel_container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
814 new AttachedPanelWindowTargeter(panel_container
,
817 panel_layout_manager_
)));
820 void RootWindowController::InitTouchHuds() {
821 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
822 if (command_line
->HasSwitch(switches::kAshTouchHud
))
823 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
824 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
825 EnableTouchHudProjection();
828 void RootWindowController::CreateSystemBackground(
829 bool is_first_run_after_boot
) {
830 SkColor color
= SK_ColorBLACK
;
831 #if defined(OS_CHROMEOS)
832 if (is_first_run_after_boot
)
833 color
= kChromeOsBootColor
;
835 system_background_
.reset(
836 new SystemBackgroundController(GetRootWindow(), color
));
838 #if defined(OS_CHROMEOS)
839 // Make a copy of the system's boot splash screen so we can composite it
840 // onscreen until the desktop background is ready.
841 if (is_first_run_after_boot
&&
842 (CommandLine::ForCurrentProcess()->HasSwitch(
843 switches::kAshCopyHostBackgroundAtBoot
) ||
844 CommandLine::ForCurrentProcess()->HasSwitch(
845 switches::kAshAnimateFromBootSplashScreen
)))
846 boot_splash_screen_
.reset(new BootSplashScreen(GetHost()));
850 void RootWindowController::CreateContainersInRootWindow(
851 aura::Window
* root_window
) {
852 // These containers are just used by PowerButtonController to animate groups
853 // of containers simultaneously without messing up the current transformations
854 // on those containers. These are direct children of the root window; all of
855 // the other containers are their children.
857 // The desktop background container is not part of the lock animation, so it
858 // is not included in those animate groups.
859 // When screen is locked desktop background is moved to lock screen background
860 // container (moved back on unlock). We want to make sure that there's an
861 // opaque layer occluding the non-lock-screen layers.
862 aura::Window
* desktop_background_container
= CreateContainer(
863 kShellWindowId_DesktopBackgroundContainer
,
864 "DesktopBackgroundContainer",
866 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container
);
868 aura::Window
* non_lock_screen_containers
= CreateContainer(
869 kShellWindowId_NonLockScreenContainersContainer
,
870 "NonLockScreenContainersContainer",
873 aura::Window
* lock_background_containers
= CreateContainer(
874 kShellWindowId_LockScreenBackgroundContainer
,
875 "LockScreenBackgroundContainer",
877 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers
);
879 aura::Window
* lock_screen_containers
= CreateContainer(
880 kShellWindowId_LockScreenContainersContainer
,
881 "LockScreenContainersContainer",
883 aura::Window
* lock_screen_related_containers
= CreateContainer(
884 kShellWindowId_LockScreenRelatedContainersContainer
,
885 "LockScreenRelatedContainersContainer",
888 CreateContainer(kShellWindowId_UnparentedControlContainer
,
889 "UnparentedControlContainer",
890 non_lock_screen_containers
);
892 aura::Window
* default_container
= CreateContainer(
893 kShellWindowId_DefaultContainer
,
895 non_lock_screen_containers
);
896 ::wm::SetChildWindowVisibilityChangesAnimated(default_container
);
897 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container
);
898 SetUsesScreenCoordinates(default_container
);
899 SetUsesEasyResizeTargeter(default_container
);
901 aura::Window
* always_on_top_container
= CreateContainer(
902 kShellWindowId_AlwaysOnTopContainer
,
903 "AlwaysOnTopContainer",
904 non_lock_screen_containers
);
905 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container
);
906 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container
);
907 SetUsesScreenCoordinates(always_on_top_container
);
909 aura::Window
* docked_container
= CreateContainer(
910 kShellWindowId_DockedContainer
,
912 non_lock_screen_containers
);
913 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container
);
914 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container
);
915 SetUsesScreenCoordinates(docked_container
);
916 SetUsesEasyResizeTargeter(docked_container
);
918 aura::Window
* shelf_container
=
919 CreateContainer(kShellWindowId_ShelfContainer
,
921 non_lock_screen_containers
);
922 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container
);
923 SetUsesScreenCoordinates(shelf_container
);
924 DescendantShouldStayInSameRootWindow(shelf_container
);
926 aura::Window
* panel_container
= CreateContainer(
927 kShellWindowId_PanelContainer
,
929 non_lock_screen_containers
);
930 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container
);
931 SetUsesScreenCoordinates(panel_container
);
933 aura::Window
* shelf_bubble_container
=
934 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
935 "ShelfBubbleContainer",
936 non_lock_screen_containers
);
937 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container
);
938 SetUsesScreenCoordinates(shelf_bubble_container
);
939 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
941 aura::Window
* app_list_container
=
942 CreateContainer(kShellWindowId_AppListContainer
,
944 non_lock_screen_containers
);
945 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container
);
946 SetUsesScreenCoordinates(app_list_container
);
948 aura::Window
* modal_container
= CreateContainer(
949 kShellWindowId_SystemModalContainer
,
950 "SystemModalContainer",
951 non_lock_screen_containers
);
952 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container
);
953 modal_container
->SetLayoutManager(
954 new SystemModalContainerLayoutManager(modal_container
));
955 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container
);
956 SetUsesScreenCoordinates(modal_container
);
957 SetUsesEasyResizeTargeter(modal_container
);
959 // TODO(beng): Figure out if we can make this use
960 // SystemModalContainerEventFilter instead of stops_event_propagation.
961 aura::Window
* lock_container
= CreateContainer(
962 kShellWindowId_LockScreenContainer
,
963 "LockScreenContainer",
964 lock_screen_containers
);
965 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container
);
966 if (CommandLine::ForCurrentProcess()->HasSwitch(
967 switches::kAshDisableLockLayoutManager
)) {
968 lock_container
->SetLayoutManager(
969 new WorkspaceLayoutManager(lock_container
));
971 lock_container
->SetLayoutManager(new LockLayoutManager(lock_container
));
973 SetUsesScreenCoordinates(lock_container
);
974 // TODO(beng): stopsevents
976 aura::Window
* lock_modal_container
= CreateContainer(
977 kShellWindowId_LockSystemModalContainer
,
978 "LockSystemModalContainer",
979 lock_screen_containers
);
980 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container
);
981 lock_modal_container
->SetLayoutManager(
982 new SystemModalContainerLayoutManager(lock_modal_container
));
983 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
984 SetUsesScreenCoordinates(lock_modal_container
);
985 SetUsesEasyResizeTargeter(lock_modal_container
);
987 aura::Window
* status_container
=
988 CreateContainer(kShellWindowId_StatusContainer
,
990 lock_screen_related_containers
);
991 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container
);
992 SetUsesScreenCoordinates(status_container
);
993 DescendantShouldStayInSameRootWindow(status_container
);
995 aura::Window
* settings_bubble_container
= CreateContainer(
996 kShellWindowId_SettingBubbleContainer
,
997 "SettingBubbleContainer",
998 lock_screen_related_containers
);
999 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container
);
1000 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container
);
1001 SetUsesScreenCoordinates(settings_bubble_container
);
1002 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
1004 aura::Window
* virtual_keyboard_parent_container
=
1005 CreateContainer(kShellWindowId_VirtualKeyboardParentContainer
,
1006 "VirtualKeyboardParentContainer",
1007 lock_screen_related_containers
);
1008 wm::SetSnapsChildrenToPhysicalPixelBoundary(
1009 virtual_keyboard_parent_container
);
1010 SetUsesScreenCoordinates(virtual_keyboard_parent_container
);
1012 aura::Window
* menu_container
= CreateContainer(
1013 kShellWindowId_MenuContainer
,
1015 lock_screen_related_containers
);
1016 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container
);
1017 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container
);
1018 SetUsesScreenCoordinates(menu_container
);
1020 aura::Window
* drag_drop_container
= CreateContainer(
1021 kShellWindowId_DragImageAndTooltipContainer
,
1022 "DragImageAndTooltipContainer",
1023 lock_screen_related_containers
);
1024 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
1025 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container
);
1026 SetUsesScreenCoordinates(drag_drop_container
);
1028 aura::Window
* overlay_container
= CreateContainer(
1029 kShellWindowId_OverlayContainer
,
1031 lock_screen_related_containers
);
1032 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container
);
1033 SetUsesScreenCoordinates(overlay_container
);
1035 #if defined(OS_CHROMEOS)
1036 aura::Window
* mouse_cursor_container
= CreateContainer(
1037 kShellWindowId_MouseCursorContainer
,
1038 "MouseCursorContainer",
1040 SetUsesScreenCoordinates(mouse_cursor_container
);
1043 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
1044 "PowerButtonAnimationContainer", root_window
);
1047 void RootWindowController::EnableTouchHudProjection() {
1048 if (touch_hud_projection_
)
1050 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1053 void RootWindowController::DisableTouchHudProjection() {
1054 if (!touch_hud_projection_
)
1056 touch_hud_projection_
->Remove();
1059 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
1060 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
1063 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
1065 EnableTouchHudProjection();
1067 DisableTouchHudProjection();
1070 RootWindowController
* GetRootWindowController(
1071 const aura::Window
* root_window
) {
1072 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;