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/touch/touch_hud_debug.h"
32 #include "ash/touch/touch_hud_projection.h"
33 #include "ash/touch/touch_observer_hud.h"
34 #include "ash/wm/always_on_top_controller.h"
35 #include "ash/wm/dock/docked_window_layout_manager.h"
36 #include "ash/wm/panels/attached_panel_window_targeter.h"
37 #include "ash/wm/panels/panel_layout_manager.h"
38 #include "ash/wm/panels/panel_window_event_handler.h"
39 #include "ash/wm/root_window_layout_manager.h"
40 #include "ash/wm/screen_dimmer.h"
41 #include "ash/wm/stacking_controller.h"
42 #include "ash/wm/status_area_layout_manager.h"
43 #include "ash/wm/system_background_controller.h"
44 #include "ash/wm/system_modal_container_layout_manager.h"
45 #include "ash/wm/window_properties.h"
46 #include "ash/wm/window_state.h"
47 #include "ash/wm/window_util.h"
48 #include "ash/wm/workspace/workspace_layout_manager.h"
49 #include "ash/wm/workspace_controller.h"
50 #include "base/command_line.h"
51 #include "base/time/time.h"
52 #include "ui/aura/client/aura_constants.h"
53 #include "ui/aura/client/screen_position_client.h"
54 #include "ui/aura/window.h"
55 #include "ui/aura/window_delegate.h"
56 #include "ui/aura/window_event_dispatcher.h"
57 #include "ui/aura/window_observer.h"
58 #include "ui/aura/window_tracker.h"
59 #include "ui/base/hit_test.h"
60 #include "ui/base/models/menu_model.h"
61 #include "ui/gfx/display.h"
62 #include "ui/gfx/screen.h"
63 #include "ui/keyboard/keyboard_controller.h"
64 #include "ui/keyboard/keyboard_util.h"
65 #include "ui/views/controls/menu/menu_runner.h"
66 #include "ui/views/view_model.h"
67 #include "ui/views/view_model_utils.h"
68 #include "ui/wm/core/capture_controller.h"
69 #include "ui/wm/core/easy_resize_window_targeter.h"
70 #include "ui/wm/core/visibility_controller.h"
71 #include "ui/wm/core/window_util.h"
72 #include "ui/wm/public/drag_drop_client.h"
73 #include "ui/wm/public/tooltip_client.h"
74 #include "ui/wm/public/window_types.h"
76 #if defined(OS_CHROMEOS)
77 #include "ash/wm/boot_splash_screen_chromeos.h"
83 #if defined(OS_CHROMEOS)
84 // Duration for the animation that hides the boot splash screen, in
85 // milliseconds. This should be short enough in relation to
86 // wm/window_animation.cc's brightness/grayscale fade animation that the login
87 // background image animation isn't hidden by the splash screen animation.
88 const int kBootSplashScreenHideDurationMs
= 500;
91 // Creates a new window for use as a container.
92 aura::Window
* CreateContainer(int window_id
,
94 aura::Window
* parent
) {
95 aura::Window
* container
= new aura::Window(NULL
);
96 container
->set_id(window_id
);
97 container
->SetName(name
);
98 container
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
99 parent
->AddChild(container
);
100 if (window_id
!= kShellWindowId_UnparentedControlContainer
)
105 float ToRelativeValue(int value
, int src
, int dst
) {
106 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
109 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
110 const gfx::Size
& dst_size
,
111 gfx::Rect
* bounds_in_out
) {
112 gfx::Point origin
= bounds_in_out
->origin();
113 bounds_in_out
->set_origin(gfx::Point(
114 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
115 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
118 // Reparents |window| to |new_parent|.
119 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
120 const gfx::Size src_size
= window
->parent()->bounds().size();
121 const gfx::Size dst_size
= new_parent
->bounds().size();
122 // Update the restore bounds to make it relative to the display.
123 wm::WindowState
* state
= wm::GetWindowState(window
);
124 gfx::Rect restore_bounds
;
125 bool has_restore_bounds
= state
->HasRestoreBounds();
127 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
128 new_parent
->id() != kShellWindowId_DockedContainer
;
129 gfx::Rect local_bounds
;
131 local_bounds
= state
->window()->bounds();
132 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
135 if (has_restore_bounds
) {
136 restore_bounds
= state
->GetRestoreBoundsInParent();
137 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
140 new_parent
->AddChild(window
);
142 // Docked windows have bounds handled by the layout manager in AddChild().
144 window
->SetBounds(local_bounds
);
146 if (has_restore_bounds
)
147 state
->SetRestoreBoundsInParent(restore_bounds
);
150 // Reparents the appropriate set of windows from |src| to |dst|.
151 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
152 // Set of windows to move.
153 const int kContainerIdsToMove
[] = {
154 kShellWindowId_DefaultContainer
,
155 kShellWindowId_DockedContainer
,
156 kShellWindowId_PanelContainer
,
157 kShellWindowId_AlwaysOnTopContainer
,
158 kShellWindowId_SystemModalContainer
,
159 kShellWindowId_LockSystemModalContainer
,
160 kShellWindowId_UnparentedControlContainer
, };
161 for (size_t i
= 0; i
< arraysize(kContainerIdsToMove
); i
++) {
162 int id
= kContainerIdsToMove
[i
];
163 aura::Window
* src_container
= Shell::GetContainer(src
, id
);
164 aura::Window
* dst_container
= Shell::GetContainer(dst
, id
);
165 while (!src_container
->children().empty()) {
166 // Restart iteration from the source container windows each time as they
167 // may change as a result of moving other windows.
168 aura::Window::Windows::const_iterator iter
=
169 src_container
->children().begin();
170 while (iter
!= src_container
->children().end() &&
171 SystemModalContainerLayoutManager::IsModalBackground(*iter
)) {
174 // If the entire window list is modal background windows then stop.
175 if (iter
== src_container
->children().end())
177 ReparentWindow(*iter
, dst_container
);
182 // Mark the container window so that a widget added to this container will
183 // use the virtual screeen coordinates instead of parent.
184 void SetUsesScreenCoordinates(aura::Window
* container
) {
185 container
->SetProperty(kUsesScreenCoordinatesKey
, true);
188 // Mark the container window so that a widget added to this container will
189 // say in the same root window regardless of the bounds specified.
190 void DescendantShouldStayInSameRootWindow(aura::Window
* container
) {
191 container
->SetProperty(kStayInSameRootWindowKey
, true);
194 void SetUsesEasyResizeTargeter(aura::Window
* container
) {
195 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
196 -kResizeOutsideBoundsSize
,
197 -kResizeOutsideBoundsSize
,
198 -kResizeOutsideBoundsSize
);
199 gfx::Insets touch_extend
= mouse_extend
.Scale(
200 kResizeOutsideBoundsScaleForTouch
);
201 container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
202 new ::wm::EasyResizeWindowTargeter(container
, mouse_extend
,
206 // A window delegate which does nothing. Used to create a window that
207 // is a event target, but do nothing.
208 class EmptyWindowDelegate
: public aura::WindowDelegate
{
210 EmptyWindowDelegate() {}
211 virtual ~EmptyWindowDelegate() {}
213 // aura::WindowDelegate overrides:
214 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
217 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
220 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
221 const gfx::Rect
& new_bounds
) OVERRIDE
{
223 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
224 return gfx::kNullCursor
;
226 virtual int GetNonClientComponent(
227 const gfx::Point
& point
) const OVERRIDE
{
230 virtual bool ShouldDescendIntoChildForEventHandling(
232 const gfx::Point
& location
) OVERRIDE
{
235 virtual bool CanFocus() OVERRIDE
{
238 virtual void OnCaptureLost() OVERRIDE
{
240 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
242 virtual void OnDeviceScaleFactorChanged(
243 float device_scale_factor
) OVERRIDE
{
245 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
246 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{
249 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
251 virtual bool HasHitTestMask() const OVERRIDE
{
254 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
257 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
262 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost
* host
) {
263 RootWindowController
* controller
= new RootWindowController(host
);
264 controller
->Init(RootWindowController::PRIMARY
,
265 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
268 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost
* host
) {
269 RootWindowController
* controller
= new RootWindowController(host
);
270 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
273 void RootWindowController::CreateForVirtualKeyboardDisplay(
274 AshWindowTreeHost
* host
) {
275 RootWindowController
* controller
= new RootWindowController(host
);
276 controller
->Init(RootWindowController::VIRTUAL_KEYBOARD
,
277 false /* first run */);
281 RootWindowController
* RootWindowController::ForShelf(aura::Window
* window
) {
282 return GetRootWindowController(window
->GetRootWindow());
286 RootWindowController
* RootWindowController::ForWindow(
287 const aura::Window
* window
) {
288 return GetRootWindowController(window
->GetRootWindow());
292 RootWindowController
* RootWindowController::ForTargetRootWindow() {
293 return GetRootWindowController(Shell::GetTargetRootWindow());
297 aura::Window
* RootWindowController::GetContainerForWindow(
298 aura::Window
* window
) {
299 aura::Window
* container
= window
->parent();
300 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
301 container
= container
->parent();
305 RootWindowController::~RootWindowController() {
308 // The CaptureClient needs to be around for as long as the RootWindow is
310 capture_client_
.reset();
313 aura::WindowTreeHost
* RootWindowController::GetHost() {
314 return ash_host_
->AsWindowTreeHost();
317 const aura::WindowTreeHost
* RootWindowController::GetHost() const {
318 return ash_host_
->AsWindowTreeHost();
321 aura::Window
* RootWindowController::GetRootWindow() {
322 return GetHost()->window();
325 const aura::Window
* RootWindowController::GetRootWindow() const {
326 return GetHost()->window();
329 void RootWindowController::SetWallpaperController(
330 DesktopBackgroundWidgetController
* controller
) {
331 wallpaper_controller_
.reset(controller
);
334 void RootWindowController::SetAnimatingWallpaperController(
335 AnimatingDesktopController
* controller
) {
336 if (animating_wallpaper_controller_
.get())
337 animating_wallpaper_controller_
->StopAnimating();
338 animating_wallpaper_controller_
.reset(controller
);
341 void RootWindowController::Shutdown() {
342 Shell::GetInstance()->RemoveShellObserver(this);
344 if (animating_wallpaper_controller_
.get())
345 animating_wallpaper_controller_
->StopAnimating();
346 wallpaper_controller_
.reset();
347 animating_wallpaper_controller_
.reset();
348 aura::Window
* root_window
= GetRootWindow();
349 // Change the target root window before closing child windows. If any child
350 // being removed triggers a relayout of the shelf it will try to build a
351 // window list adding windows from the target root window's containers which
352 // may have already gone away.
353 if (Shell::GetTargetRootWindow() == root_window
) {
354 Shell::GetInstance()->set_target_root_window(
355 Shell::GetPrimaryRootWindow() == root_window
357 : Shell::GetPrimaryRootWindow());
361 GetRootWindowSettings(root_window
)->controller
= NULL
;
362 screen_dimmer_
.reset();
363 workspace_controller_
.reset();
364 // Forget with the display ID so that display lookup
365 // ends up with invalid display.
366 GetRootWindowSettings(root_window
)->display_id
=
367 gfx::Display::kInvalidDisplayID
;
368 GetRootWindowSettings(root_window
)->shutdown
= true;
370 system_background_
.reset();
371 aura::client::SetScreenPositionClient(root_window
, NULL
);
374 SystemModalContainerLayoutManager
*
375 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
376 aura::Window
* modal_container
= NULL
;
378 aura::Window
* window_container
= GetContainerForWindow(window
);
379 if (window_container
&&
380 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
381 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
383 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
386 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
387 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
388 kShellWindowId_SystemModalContainer
;
389 modal_container
= GetContainer(modal_window_id
);
391 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
392 modal_container
->layout_manager()) : NULL
;
395 aura::Window
* RootWindowController::GetContainer(int container_id
) {
396 return GetRootWindow()->GetChildById(container_id
);
399 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
400 return ash_host_
->AsWindowTreeHost()->window()->GetChildById(container_id
);
403 void RootWindowController::ShowShelf() {
404 if (!shelf_
->shelf())
406 shelf_
->shelf()->SetVisible(true);
407 shelf_
->status_area_widget()->Show();
410 void RootWindowController::OnShelfCreated() {
411 if (panel_layout_manager_
)
412 panel_layout_manager_
->SetShelf(shelf_
->shelf());
413 if (docked_layout_manager_
) {
414 docked_layout_manager_
->SetShelf(shelf_
->shelf());
415 if (shelf_
->shelf_layout_manager())
416 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
419 // Notify shell observers that the shelf has been created.
420 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
423 void RootWindowController::UpdateAfterLoginStatusChange(
424 user::LoginStatus status
) {
425 if (status
!= user::LOGGED_IN_NONE
)
426 mouse_event_target_
.reset();
427 if (shelf_
->status_area_widget())
428 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
431 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
432 #if defined(OS_CHROMEOS)
433 if (CommandLine::ForCurrentProcess()->HasSwitch(
434 switches::kAshAnimateFromBootSplashScreen
) &&
435 boot_splash_screen_
.get()) {
436 // Make the splash screen fade out so it doesn't obscure the desktop
437 // wallpaper's brightness/grayscale animation.
438 boot_splash_screen_
->StartHideAnimation(
439 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
444 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
445 // Make sure the wallpaper is visible.
446 system_background_
->SetColor(SK_ColorBLACK
);
447 #if defined(OS_CHROMEOS)
448 boot_splash_screen_
.reset();
451 Shell::GetInstance()->user_wallpaper_delegate()->
452 OnWallpaperAnimationFinished();
453 // Only removes old component when wallpaper animation finished. If we
454 // remove the old one before the new wallpaper is done fading in there will
455 // be a white flash during the animation.
456 if (animating_wallpaper_controller()) {
457 DesktopBackgroundWidgetController
* controller
=
458 animating_wallpaper_controller()->GetController(true);
459 // |desktop_widget_| should be the same animating widget we try to move
460 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
461 // before move it to |kDesktopController|.
462 DCHECK_EQ(controller
->widget(), widget
);
463 // Release the old controller and close its background widget.
464 SetWallpaperController(controller
);
468 void RootWindowController::CloseChildWindows() {
469 mouse_event_target_
.reset();
471 // Deactivate keyboard container before closing child windows and shutting
472 // down associated layout managers.
473 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
475 // panel_layout_manager_ needs to be shut down before windows are destroyed.
476 if (panel_layout_manager_
) {
477 panel_layout_manager_
->Shutdown();
478 panel_layout_manager_
= NULL
;
480 // docked_layout_manager_ needs to be shut down before windows are destroyed.
481 if (docked_layout_manager_
) {
482 if (shelf_
&& shelf_
->shelf_layout_manager())
483 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
484 docked_layout_manager_
->Shutdown();
485 docked_layout_manager_
= NULL
;
487 aura::Window
* root_window
= GetRootWindow();
488 aura::client::SetDragDropClient(root_window
, NULL
);
490 // TODO(harrym): Remove when Status Area Widget is a child view.
492 shelf_
->ShutdownStatusAreaWidget();
494 if (shelf_
->shelf_layout_manager())
495 shelf_
->shelf_layout_manager()->PrepareForShutdown();
498 // Close background widget first as it depends on tooltip.
499 wallpaper_controller_
.reset();
500 animating_wallpaper_controller_
.reset();
502 workspace_controller_
.reset();
503 aura::client::SetTooltipClient(root_window
, NULL
);
505 // Explicitly destroy top level windows. We do this as during part of
506 // destruction such windows may query the RootWindow for state.
507 std::queue
<aura::Window
*> non_toplevel_windows
;
508 non_toplevel_windows
.push(root_window
);
509 while (!non_toplevel_windows
.empty()) {
510 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
511 non_toplevel_windows
.pop();
512 aura::WindowTracker toplevel_windows
;
513 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
514 aura::Window
* child
= non_toplevel_window
->children()[i
];
515 if (!child
->owned_by_parent())
517 if (child
->delegate())
518 toplevel_windows
.Add(child
);
520 non_toplevel_windows
.push(child
);
522 while (!toplevel_windows
.windows().empty())
523 delete *toplevel_windows
.windows().begin();
525 // And then remove the containers.
526 while (!root_window
->children().empty()) {
527 aura::Window
* window
= root_window
->children()[0];
528 if (window
->owned_by_parent()) {
531 root_window
->RemoveChild(window
);
538 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
539 // Forget the shelf early so that shelf don't update itself using wrong
541 workspace_controller_
->SetShelf(NULL
);
542 ReparentAllWindows(GetRootWindow(), dst
);
545 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
546 return shelf_
->shelf_layout_manager();
549 SystemTray
* RootWindowController::GetSystemTray() {
550 // We assume in throughout the code that this will not return NULL. If code
551 // triggers this for valid reasons, it should test status_area_widget first.
552 CHECK(shelf_
->status_area_widget());
553 return shelf_
->status_area_widget()->system_tray();
556 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
557 ui::MenuSourceType source_type
) {
558 DCHECK(Shell::GetInstance()->delegate());
559 scoped_ptr
<ui::MenuModel
> menu_model(
560 Shell::GetInstance()->delegate()->CreateContextMenu(
561 GetRootWindow(), NULL
, NULL
));
565 // Background controller may not be set yet if user clicked on status are
566 // before initial animation completion. See crbug.com/222218
567 if (!wallpaper_controller_
.get())
570 views::MenuRunner
menu_runner(menu_model
.get());
571 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
573 gfx::Rect(location_in_screen
, gfx::Size()),
574 views::MENU_ANCHOR_TOPLEFT
,
576 views::MenuRunner::CONTEXT_MENU
) ==
577 views::MenuRunner::MENU_DELETED
) {
581 Shell::GetInstance()->UpdateShelfVisibility();
584 void RootWindowController::UpdateShelfVisibility() {
585 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
588 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
589 const aura::Window
* topmost_window
= NULL
;
590 const aura::Window
* active_window
= wm::GetActiveWindow();
591 if (active_window
&& active_window
->GetRootWindow() == GetRootWindow() &&
592 IsSwitchableContainer(active_window
->parent())) {
593 // Use the active window when it is on the current root window to determine
594 // the fullscreen state to allow temporarily using a panel or docked window
595 // (which are always above the default container) while a fullscreen
596 // window is open. We only use the active window when in a switchable
597 // container as the launcher should not exit fullscreen mode.
598 topmost_window
= active_window
;
600 // Otherwise, use the topmost window on the root window's default container
601 // when there is no active window on this root window.
602 const aura::Window::Windows
& windows
=
603 GetContainer(kShellWindowId_DefaultContainer
)->children();
604 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
605 iter
!= windows
.rend(); ++iter
) {
606 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
607 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
608 (*iter
)->layer()->GetTargetVisibility()) {
609 topmost_window
= *iter
;
614 while (topmost_window
) {
615 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
616 return topmost_window
;
617 topmost_window
= ::wm::GetTransientParent(topmost_window
);
622 void RootWindowController::ActivateKeyboard(
623 keyboard::KeyboardController
* keyboard_controller
) {
624 if (!keyboard::IsKeyboardEnabled() ||
625 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
628 DCHECK(keyboard_controller
);
629 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
630 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
631 keyboard_controller
->AddObserver(panel_layout_manager_
);
632 keyboard_controller
->AddObserver(docked_layout_manager_
);
633 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
635 aura::Window
* parent
= GetContainer(
636 kShellWindowId_VirtualKeyboardParentContainer
);
638 aura::Window
* keyboard_container
=
639 keyboard_controller
->GetContainerWindow();
640 keyboard_container
->set_id(kShellWindowId_VirtualKeyboardContainer
);
641 parent
->AddChild(keyboard_container
);
642 // TODO(oshima): Bounds of keyboard container should be handled by
643 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
644 keyboard_container
->SetBounds(parent
->bounds());
647 void RootWindowController::DeactivateKeyboard(
648 keyboard::KeyboardController
* keyboard_controller
) {
649 if (!keyboard_controller
||
650 !keyboard_controller
->keyboard_container_initialized()) {
653 aura::Window
* keyboard_container
=
654 keyboard_controller
->GetContainerWindow();
655 if (keyboard_container
->GetRootWindow() == GetRootWindow()) {
656 aura::Window
* parent
= GetContainer(
657 kShellWindowId_VirtualKeyboardParentContainer
);
659 parent
->RemoveChild(keyboard_container
);
660 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
661 // Virtual keyboard may be deactivated while still showing, notify all
662 // observers that keyboard bounds changed to 0 before remove them.
663 keyboard_controller
->NotifyKeyboardBoundsChanging(gfx::Rect());
664 keyboard_controller
->RemoveObserver(shelf()->shelf_layout_manager());
665 keyboard_controller
->RemoveObserver(panel_layout_manager_
);
666 keyboard_controller
->RemoveObserver(docked_layout_manager_
);
667 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
672 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window
* window
) {
673 aura::Window
* parent
= GetContainer(
674 kShellWindowId_VirtualKeyboardParentContainer
);
675 return parent
? parent
->Contains(window
) : false;
678 ////////////////////////////////////////////////////////////////////////////////
679 // RootWindowController, private:
681 RootWindowController::RootWindowController(AshWindowTreeHost
* ash_host
)
682 : ash_host_(ash_host
),
683 root_window_layout_(NULL
),
684 docked_layout_manager_(NULL
),
685 panel_layout_manager_(NULL
),
686 touch_hud_debug_(NULL
),
687 touch_hud_projection_(NULL
) {
688 aura::Window
* root_window
= GetRootWindow();
689 GetRootWindowSettings(root_window
)->controller
= this;
690 screen_dimmer_
.reset(new ScreenDimmer(root_window
));
692 stacking_controller_
.reset(new StackingController
);
693 aura::client::SetWindowTreeClient(root_window
, stacking_controller_
.get());
694 capture_client_
.reset(new ::wm::ScopedCaptureClient(root_window
));
697 void RootWindowController::Init(RootWindowType root_window_type
,
698 bool first_run_after_boot
) {
699 aura::Window
* root_window
= GetRootWindow();
700 Shell
* shell
= Shell::GetInstance();
701 shell
->InitRootWindow(root_window
);
703 ash_host_
->AsWindowTreeHost()->SetCursor(ui::kCursorPointer
);
704 CreateContainersInRootWindow(root_window
);
706 if (root_window_type
== VIRTUAL_KEYBOARD
) {
707 aura::Window
* virtual_keyboard_parent_container
= GetContainer(
708 kShellWindowId_VirtualKeyboardParentContainer
);
709 virtual_keyboard_parent_container
->SetBounds(root_window
->bounds());
710 shell
->InitKeyboard();
714 CreateSystemBackground(first_run_after_boot
);
716 InitLayoutManagers();
719 if (Shell::GetPrimaryRootWindowController()->
720 GetSystemModalLayoutManager(NULL
)->has_modal_background()) {
721 GetSystemModalLayoutManager(NULL
)->CreateModalBackground();
724 shell
->AddShellObserver(this);
726 if (root_window_type
== PRIMARY
) {
727 root_window_layout()->OnWindowResized();
728 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
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
);
743 void RootWindowController::InitLayoutManagers() {
744 aura::Window
* root_window
= GetRootWindow();
745 root_window_layout_
= new RootWindowLayoutManager(root_window
);
746 root_window
->SetLayoutManager(root_window_layout_
);
748 aura::Window
* default_container
=
749 GetContainer(kShellWindowId_DefaultContainer
);
750 // Workspace manager has its own layout managers.
751 workspace_controller_
.reset(
752 new WorkspaceController(default_container
));
754 aura::Window
* always_on_top_container
=
755 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
756 always_on_top_container
->SetLayoutManager(
757 new WorkspaceLayoutManager(always_on_top_container
));
758 always_on_top_controller_
.reset(new AlwaysOnTopController
);
759 always_on_top_controller_
->SetAlwaysOnTopContainer(always_on_top_container
);
761 DCHECK(!shelf_
.get());
762 aura::Window
* shelf_container
= GetContainer(kShellWindowId_ShelfContainer
);
763 // TODO(harrym): Remove when status area is view.
764 aura::Window
* status_container
= GetContainer(kShellWindowId_StatusContainer
);
765 shelf_
.reset(new ShelfWidget(
766 shelf_container
, status_container
, workspace_controller()));
768 if (!Shell::GetInstance()->session_state_delegate()->
769 IsActiveUserSessionStarted()) {
770 // This window exists only to be a event target on login screen.
771 // It does not have to handle events, nor be visible.
772 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
773 mouse_event_target_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
775 aura::Window
* lock_background_container
=
776 GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
777 lock_background_container
->AddChild(mouse_event_target_
.get());
778 mouse_event_target_
->Show();
781 // Create Docked windows layout manager
782 aura::Window
* docked_container
= GetContainer(kShellWindowId_DockedContainer
);
783 docked_layout_manager_
=
784 new DockedWindowLayoutManager(docked_container
, workspace_controller());
785 docked_container
->SetLayoutManager(docked_layout_manager_
);
787 // Create Panel layout manager
788 aura::Window
* panel_container
= GetContainer(kShellWindowId_PanelContainer
);
789 panel_layout_manager_
= new PanelLayoutManager(panel_container
);
790 panel_container
->SetLayoutManager(panel_layout_manager_
);
791 panel_container_handler_
.reset(new PanelWindowEventHandler
);
792 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
794 // Install an AttachedPanelWindowTargeter on the panel container to make it
795 // easier to correctly target shelf buttons with touch.
796 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
797 -kResizeOutsideBoundsSize
,
798 -kResizeOutsideBoundsSize
,
799 -kResizeOutsideBoundsSize
);
800 gfx::Insets touch_extend
= mouse_extend
.Scale(
801 kResizeOutsideBoundsScaleForTouch
);
802 panel_container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
803 new AttachedPanelWindowTargeter(panel_container
,
806 panel_layout_manager_
)));
809 void RootWindowController::InitTouchHuds() {
810 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
811 if (command_line
->HasSwitch(switches::kAshTouchHud
))
812 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
813 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
814 EnableTouchHudProjection();
817 void RootWindowController::CreateSystemBackground(
818 bool is_first_run_after_boot
) {
819 SkColor color
= SK_ColorBLACK
;
820 #if defined(OS_CHROMEOS)
821 if (is_first_run_after_boot
)
822 color
= kChromeOsBootColor
;
824 system_background_
.reset(
825 new SystemBackgroundController(GetRootWindow(), color
));
827 #if defined(OS_CHROMEOS)
828 // Make a copy of the system's boot splash screen so we can composite it
829 // onscreen until the desktop background is ready.
830 if (is_first_run_after_boot
&&
831 (CommandLine::ForCurrentProcess()->HasSwitch(
832 switches::kAshCopyHostBackgroundAtBoot
) ||
833 CommandLine::ForCurrentProcess()->HasSwitch(
834 switches::kAshAnimateFromBootSplashScreen
)))
835 boot_splash_screen_
.reset(new BootSplashScreen(GetHost()));
839 void RootWindowController::CreateContainersInRootWindow(
840 aura::Window
* root_window
) {
841 // These containers are just used by PowerButtonController to animate groups
842 // of containers simultaneously without messing up the current transformations
843 // on those containers. These are direct children of the root window; all of
844 // the other containers are their children.
846 // The desktop background container is not part of the lock animation, so it
847 // is not included in those animate groups.
848 // When screen is locked desktop background is moved to lock screen background
849 // container (moved back on unlock). We want to make sure that there's an
850 // opaque layer occluding the non-lock-screen layers.
851 aura::Window
* desktop_background_container
= CreateContainer(
852 kShellWindowId_DesktopBackgroundContainer
,
853 "DesktopBackgroundContainer",
855 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container
);
857 aura::Window
* non_lock_screen_containers
= CreateContainer(
858 kShellWindowId_NonLockScreenContainersContainer
,
859 "NonLockScreenContainersContainer",
862 aura::Window
* lock_background_containers
= CreateContainer(
863 kShellWindowId_LockScreenBackgroundContainer
,
864 "LockScreenBackgroundContainer",
866 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers
);
868 aura::Window
* lock_screen_containers
= CreateContainer(
869 kShellWindowId_LockScreenContainersContainer
,
870 "LockScreenContainersContainer",
872 aura::Window
* lock_screen_related_containers
= CreateContainer(
873 kShellWindowId_LockScreenRelatedContainersContainer
,
874 "LockScreenRelatedContainersContainer",
877 CreateContainer(kShellWindowId_UnparentedControlContainer
,
878 "UnparentedControlContainer",
879 non_lock_screen_containers
);
881 aura::Window
* default_container
= CreateContainer(
882 kShellWindowId_DefaultContainer
,
884 non_lock_screen_containers
);
885 ::wm::SetChildWindowVisibilityChangesAnimated(default_container
);
886 SetUsesScreenCoordinates(default_container
);
887 SetUsesEasyResizeTargeter(default_container
);
889 aura::Window
* always_on_top_container
= CreateContainer(
890 kShellWindowId_AlwaysOnTopContainer
,
891 "AlwaysOnTopContainer",
892 non_lock_screen_containers
);
893 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container
);
894 SetUsesScreenCoordinates(always_on_top_container
);
896 aura::Window
* docked_container
= CreateContainer(
897 kShellWindowId_DockedContainer
,
899 non_lock_screen_containers
);
900 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container
);
901 SetUsesScreenCoordinates(docked_container
);
902 SetUsesEasyResizeTargeter(docked_container
);
904 aura::Window
* shelf_container
=
905 CreateContainer(kShellWindowId_ShelfContainer
,
907 non_lock_screen_containers
);
908 SetUsesScreenCoordinates(shelf_container
);
909 DescendantShouldStayInSameRootWindow(shelf_container
);
911 aura::Window
* panel_container
= CreateContainer(
912 kShellWindowId_PanelContainer
,
914 non_lock_screen_containers
);
915 SetUsesScreenCoordinates(panel_container
);
917 aura::Window
* shelf_bubble_container
=
918 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
919 "ShelfBubbleContainer",
920 non_lock_screen_containers
);
921 SetUsesScreenCoordinates(shelf_bubble_container
);
922 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
924 aura::Window
* app_list_container
=
925 CreateContainer(kShellWindowId_AppListContainer
,
927 non_lock_screen_containers
);
928 SetUsesScreenCoordinates(app_list_container
);
930 aura::Window
* modal_container
= CreateContainer(
931 kShellWindowId_SystemModalContainer
,
932 "SystemModalContainer",
933 non_lock_screen_containers
);
934 modal_container
->SetLayoutManager(
935 new SystemModalContainerLayoutManager(modal_container
));
936 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container
);
937 SetUsesScreenCoordinates(modal_container
);
938 SetUsesEasyResizeTargeter(modal_container
);
940 // TODO(beng): Figure out if we can make this use
941 // SystemModalContainerEventFilter instead of stops_event_propagation.
942 aura::Window
* lock_container
= CreateContainer(
943 kShellWindowId_LockScreenContainer
,
944 "LockScreenContainer",
945 lock_screen_containers
);
946 lock_container
->SetLayoutManager(new WorkspaceLayoutManager(lock_container
));
947 SetUsesScreenCoordinates(lock_container
);
948 // TODO(beng): stopsevents
950 aura::Window
* lock_modal_container
= CreateContainer(
951 kShellWindowId_LockSystemModalContainer
,
952 "LockSystemModalContainer",
953 lock_screen_containers
);
954 lock_modal_container
->SetLayoutManager(
955 new SystemModalContainerLayoutManager(lock_modal_container
));
956 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
957 SetUsesScreenCoordinates(lock_modal_container
);
958 SetUsesEasyResizeTargeter(lock_modal_container
);
960 aura::Window
* status_container
=
961 CreateContainer(kShellWindowId_StatusContainer
,
963 lock_screen_related_containers
);
964 SetUsesScreenCoordinates(status_container
);
965 DescendantShouldStayInSameRootWindow(status_container
);
967 aura::Window
* settings_bubble_container
= CreateContainer(
968 kShellWindowId_SettingBubbleContainer
,
969 "SettingBubbleContainer",
970 lock_screen_related_containers
);
971 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container
);
972 SetUsesScreenCoordinates(settings_bubble_container
);
973 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
975 aura::Window
* menu_container
= CreateContainer(
976 kShellWindowId_MenuContainer
,
978 lock_screen_related_containers
);
979 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container
);
980 SetUsesScreenCoordinates(menu_container
);
982 aura::Window
* drag_drop_container
= CreateContainer(
983 kShellWindowId_DragImageAndTooltipContainer
,
984 "DragImageAndTooltipContainer",
985 lock_screen_related_containers
);
986 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
987 SetUsesScreenCoordinates(drag_drop_container
);
989 aura::Window
* overlay_container
= CreateContainer(
990 kShellWindowId_OverlayContainer
,
992 lock_screen_related_containers
);
993 SetUsesScreenCoordinates(overlay_container
);
995 aura::Window
* virtual_keyboard_parent_container
= CreateContainer(
996 kShellWindowId_VirtualKeyboardParentContainer
,
997 "VirtualKeyboardParentContainer",
999 SetUsesScreenCoordinates(virtual_keyboard_parent_container
);
1001 #if defined(OS_CHROMEOS)
1002 aura::Window
* mouse_cursor_container
= CreateContainer(
1003 kShellWindowId_MouseCursorContainer
,
1004 "MouseCursorContainer",
1006 SetUsesScreenCoordinates(mouse_cursor_container
);
1009 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
1010 "PowerButtonAnimationContainer", root_window
);
1013 void RootWindowController::EnableTouchHudProjection() {
1014 if (touch_hud_projection_
)
1016 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1019 void RootWindowController::DisableTouchHudProjection() {
1020 if (!touch_hud_projection_
)
1022 touch_hud_projection_
->Remove();
1025 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
1026 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
1029 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
1031 EnableTouchHudProjection();
1033 DisableTouchHudProjection();
1036 RootWindowController
* GetRootWindowController(
1037 const aura::Window
* root_window
) {
1038 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;