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/root_window_settings.h"
19 #include "ash/session_state_delegate.h"
20 #include "ash/shelf/shelf_layout_manager.h"
21 #include "ash/shelf/shelf_types.h"
22 #include "ash/shelf/shelf_widget.h"
23 #include "ash/shell.h"
24 #include "ash/shell_delegate.h"
25 #include "ash/shell_factory.h"
26 #include "ash/shell_window_ids.h"
27 #include "ash/switchable_windows.h"
28 #include "ash/system/status_area_widget.h"
29 #include "ash/system/tray/system_tray_delegate.h"
30 #include "ash/touch/touch_hud_debug.h"
31 #include "ash/touch/touch_hud_projection.h"
32 #include "ash/touch/touch_observer_hud.h"
33 #include "ash/wm/always_on_top_controller.h"
34 #include "ash/wm/dock/docked_window_layout_manager.h"
35 #include "ash/wm/panels/panel_layout_manager.h"
36 #include "ash/wm/panels/panel_window_event_handler.h"
37 #include "ash/wm/root_window_layout_manager.h"
38 #include "ash/wm/screen_dimmer.h"
39 #include "ash/wm/stacking_controller.h"
40 #include "ash/wm/status_area_layout_manager.h"
41 #include "ash/wm/system_background_controller.h"
42 #include "ash/wm/system_modal_container_layout_manager.h"
43 #include "ash/wm/window_properties.h"
44 #include "ash/wm/window_state.h"
45 #include "ash/wm/window_util.h"
46 #include "ash/wm/workspace/workspace_layout_manager.h"
47 #include "ash/wm/workspace_controller.h"
48 #include "base/command_line.h"
49 #include "base/time/time.h"
50 #include "ui/aura/client/aura_constants.h"
51 #include "ui/aura/client/drag_drop_client.h"
52 #include "ui/aura/client/tooltip_client.h"
53 #include "ui/aura/window.h"
54 #include "ui/aura/window_delegate.h"
55 #include "ui/aura/window_event_dispatcher.h"
56 #include "ui/aura/window_observer.h"
57 #include "ui/aura/window_tracker.h"
58 #include "ui/base/hit_test.h"
59 #include "ui/base/models/menu_model.h"
60 #include "ui/gfx/display.h"
61 #include "ui/gfx/screen.h"
62 #include "ui/keyboard/keyboard_controller.h"
63 #include "ui/keyboard/keyboard_util.h"
64 #include "ui/views/controls/menu/menu_runner.h"
65 #include "ui/views/corewm/capture_controller.h"
66 #include "ui/views/corewm/visibility_controller.h"
67 #include "ui/views/corewm/window_util.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/public/easy_resize_window_targeter.h"
71 #include "ui/wm/public/window_types.h"
73 #if defined(OS_CHROMEOS)
74 #include "ash/wm/boot_splash_screen_chromeos.h"
80 #if defined(OS_CHROMEOS)
81 // Duration for the animation that hides the boot splash screen, in
82 // milliseconds. This should be short enough in relation to
83 // wm/window_animation.cc's brightness/grayscale fade animation that the login
84 // background image animation isn't hidden by the splash screen animation.
85 const int kBootSplashScreenHideDurationMs
= 500;
88 // Creates a new window for use as a container.
89 aura::Window
* CreateContainer(int window_id
,
91 aura::Window
* parent
) {
92 aura::Window
* container
= new aura::Window(NULL
);
93 container
->set_id(window_id
);
94 container
->SetName(name
);
95 container
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
96 parent
->AddChild(container
);
97 if (window_id
!= internal::kShellWindowId_UnparentedControlContainer
)
102 float ToRelativeValue(int value
, int src
, int dst
) {
103 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
106 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
107 const gfx::Size
& dst_size
,
108 gfx::Rect
* bounds_in_out
) {
109 gfx::Point origin
= bounds_in_out
->origin();
110 bounds_in_out
->set_origin(gfx::Point(
111 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
112 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
115 // Reparents |window| to |new_parent|.
116 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
117 const gfx::Size src_size
= window
->parent()->bounds().size();
118 const gfx::Size dst_size
= new_parent
->bounds().size();
119 // Update the restore bounds to make it relative to the display.
120 wm::WindowState
* state
= wm::GetWindowState(window
);
121 gfx::Rect restore_bounds
;
122 bool has_restore_bounds
= state
->HasRestoreBounds();
124 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
125 new_parent
->id() != internal::kShellWindowId_DockedContainer
;
126 gfx::Rect local_bounds
;
128 local_bounds
= state
->window()->bounds();
129 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
132 if (has_restore_bounds
) {
133 restore_bounds
= state
->GetRestoreBoundsInParent();
134 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
137 new_parent
->AddChild(window
);
139 // Docked windows have bounds handled by the layout manager in AddChild().
141 window
->SetBounds(local_bounds
);
143 if (has_restore_bounds
)
144 state
->SetRestoreBoundsInParent(restore_bounds
);
147 // Reparents the appropriate set of windows from |src| to |dst|.
148 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
149 // Set of windows to move.
150 const int kContainerIdsToMove
[] = {
151 internal::kShellWindowId_DefaultContainer
,
152 internal::kShellWindowId_DockedContainer
,
153 internal::kShellWindowId_PanelContainer
,
154 internal::kShellWindowId_AlwaysOnTopContainer
,
155 internal::kShellWindowId_SystemModalContainer
,
156 internal::kShellWindowId_LockSystemModalContainer
,
157 internal::kShellWindowId_InputMethodContainer
,
158 internal::kShellWindowId_UnparentedControlContainer
,
160 for (size_t i
= 0; i
< arraysize(kContainerIdsToMove
); i
++) {
161 int id
= kContainerIdsToMove
[i
];
162 aura::Window
* src_container
= Shell::GetContainer(src
, id
);
163 aura::Window
* dst_container
= Shell::GetContainer(dst
, id
);
164 while (!src_container
->children().empty()) {
165 // Restart iteration from the source container windows each time as they
166 // may change as a result of moving other windows.
167 aura::Window::Windows::const_iterator iter
=
168 src_container
->children().begin();
169 while (iter
!= src_container
->children().end() &&
170 internal::SystemModalContainerLayoutManager::IsModalBackground(
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(internal::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(internal::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() OVERRIDE
{}
246 virtual void OnWindowDestroyed() OVERRIDE
{
249 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
251 virtual bool HasHitTestMask() const OVERRIDE
{
254 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
255 virtual void DidRecreateLayer(ui::Layer
* old_layer
,
256 ui::Layer
* new_layer
) OVERRIDE
{}
259 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
266 void RootWindowController::CreateForPrimaryDisplay(
267 aura::WindowEventDispatcher
* dispatcher
) {
268 RootWindowController
* controller
= new RootWindowController(dispatcher
);
269 controller
->Init(RootWindowController::PRIMARY
,
270 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
273 void RootWindowController::CreateForSecondaryDisplay(
274 aura::WindowEventDispatcher
* dispatcher
) {
275 RootWindowController
* controller
= new RootWindowController(dispatcher
);
276 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
279 void RootWindowController::CreateForVirtualKeyboardDisplay(
280 aura::WindowEventDispatcher
* dispatcher
) {
281 RootWindowController
* controller
= new RootWindowController(dispatcher
);
282 controller
->Init(RootWindowController::VIRTUAL_KEYBOARD
,
283 false /* first run */);
287 RootWindowController
* RootWindowController::ForShelf(aura::Window
* window
) {
288 return GetRootWindowController(window
->GetRootWindow());
292 RootWindowController
* RootWindowController::ForWindow(
293 const aura::Window
* window
) {
294 return GetRootWindowController(window
->GetRootWindow());
298 RootWindowController
* RootWindowController::ForTargetRootWindow() {
299 return internal::GetRootWindowController(Shell::GetTargetRootWindow());
303 aura::Window
* RootWindowController::GetContainerForWindow(
304 aura::Window
* window
) {
305 aura::Window
* container
= window
->parent();
306 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
307 container
= container
->parent();
311 RootWindowController::~RootWindowController() {
314 // The CaptureClient needs to be around for as long as the RootWindow is
316 capture_client_
.reset();
319 void RootWindowController::SetWallpaperController(
320 DesktopBackgroundWidgetController
* controller
) {
321 wallpaper_controller_
.reset(controller
);
324 void RootWindowController::SetAnimatingWallpaperController(
325 AnimatingDesktopController
* controller
) {
326 if (animating_wallpaper_controller_
.get())
327 animating_wallpaper_controller_
->StopAnimating();
328 animating_wallpaper_controller_
.reset(controller
);
331 void RootWindowController::Shutdown() {
332 Shell::GetInstance()->RemoveShellObserver(this);
334 if (animating_wallpaper_controller_
.get())
335 animating_wallpaper_controller_
->StopAnimating();
336 wallpaper_controller_
.reset();
337 animating_wallpaper_controller_
.reset();
339 // Change the target root window before closing child windows. If any child
340 // being removed triggers a relayout of the shelf it will try to build a
341 // window list adding windows from the target root window's containers which
342 // may have already gone away.
343 if (Shell::GetTargetRootWindow() == root_window()) {
344 Shell::GetInstance()->set_target_root_window(
345 Shell::GetPrimaryRootWindow() == root_window() ?
346 NULL
: Shell::GetPrimaryRootWindow());
350 GetRootWindowSettings(root_window())->controller
= NULL
;
351 screen_dimmer_
.reset();
352 workspace_controller_
.reset();
353 // Forget with the display ID so that display lookup
354 // ends up with invalid display.
355 internal::GetRootWindowSettings(root_window())->display_id
=
356 gfx::Display::kInvalidDisplayID
;
357 // And this root window should no longer process events.
358 dispatcher_
->PrepareForShutdown();
360 system_background_
.reset();
363 SystemModalContainerLayoutManager
*
364 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
365 aura::Window
* modal_container
= NULL
;
367 aura::Window
* window_container
= GetContainerForWindow(window
);
368 if (window_container
&&
369 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
370 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
372 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
375 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
376 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
377 kShellWindowId_SystemModalContainer
;
378 modal_container
= GetContainer(modal_window_id
);
380 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
381 modal_container
->layout_manager()) : NULL
;
384 aura::Window
* RootWindowController::GetContainer(int container_id
) {
385 return root_window()->GetChildById(container_id
);
388 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
389 return dispatcher_
->window()->GetChildById(container_id
);
392 void RootWindowController::ShowShelf() {
393 if (!shelf_
->shelf())
395 shelf_
->shelf()->SetVisible(true);
396 shelf_
->status_area_widget()->Show();
399 void RootWindowController::OnShelfCreated() {
400 if (panel_layout_manager_
)
401 panel_layout_manager_
->SetShelf(shelf_
->shelf());
402 if (docked_layout_manager_
) {
403 docked_layout_manager_
->SetShelf(shelf_
->shelf());
404 if (shelf_
->shelf_layout_manager())
405 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
409 void RootWindowController::UpdateAfterLoginStatusChange(
410 user::LoginStatus status
) {
411 if (status
!= user::LOGGED_IN_NONE
)
412 mouse_event_target_
.reset();
413 if (shelf_
->status_area_widget())
414 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
417 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
418 #if defined(OS_CHROMEOS)
419 if (CommandLine::ForCurrentProcess()->HasSwitch(
420 switches::kAshAnimateFromBootSplashScreen
) &&
421 boot_splash_screen_
.get()) {
422 // Make the splash screen fade out so it doesn't obscure the desktop
423 // wallpaper's brightness/grayscale animation.
424 boot_splash_screen_
->StartHideAnimation(
425 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
430 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
431 // Make sure the wallpaper is visible.
432 system_background_
->SetColor(SK_ColorBLACK
);
433 #if defined(OS_CHROMEOS)
434 boot_splash_screen_
.reset();
437 Shell::GetInstance()->user_wallpaper_delegate()->
438 OnWallpaperAnimationFinished();
439 // Only removes old component when wallpaper animation finished. If we
440 // remove the old one before the new wallpaper is done fading in there will
441 // be a white flash during the animation.
442 if (animating_wallpaper_controller()) {
443 DesktopBackgroundWidgetController
* controller
=
444 animating_wallpaper_controller()->GetController(true);
445 // |desktop_widget_| should be the same animating widget we try to move
446 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
447 // before move it to |kDesktopController|.
448 DCHECK_EQ(controller
->widget(), widget
);
449 // Release the old controller and close its background widget.
450 SetWallpaperController(controller
);
454 void RootWindowController::CloseChildWindows() {
455 mouse_event_target_
.reset();
457 // Deactivate keyboard container before closing child windows and shutting
458 // down associated layout managers.
459 DeactivateKeyboard(Shell::GetInstance()->keyboard_controller());
461 // panel_layout_manager_ needs to be shut down before windows are destroyed.
462 if (panel_layout_manager_
) {
463 panel_layout_manager_
->Shutdown();
464 panel_layout_manager_
= NULL
;
466 // docked_layout_manager_ needs to be shut down before windows are destroyed.
467 if (docked_layout_manager_
) {
468 if (shelf_
&& shelf_
->shelf_layout_manager())
469 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
470 docked_layout_manager_
->Shutdown();
471 docked_layout_manager_
= NULL
;
474 aura::client::SetDragDropClient(root_window(), NULL
);
476 // TODO(harrym): Remove when Status Area Widget is a child view.
478 shelf_
->ShutdownStatusAreaWidget();
480 if (shelf_
->shelf_layout_manager())
481 shelf_
->shelf_layout_manager()->PrepareForShutdown();
484 // Close background widget first as it depends on tooltip.
485 wallpaper_controller_
.reset();
486 animating_wallpaper_controller_
.reset();
488 workspace_controller_
.reset();
489 aura::client::SetTooltipClient(root_window(), NULL
);
491 // Explicitly destroy top level windows. We do this as during part of
492 // destruction such windows may query the RootWindow for state.
493 std::queue
<aura::Window
*> non_toplevel_windows
;
494 non_toplevel_windows
.push(root_window());
495 while (!non_toplevel_windows
.empty()) {
496 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
497 non_toplevel_windows
.pop();
498 aura::WindowTracker toplevel_windows
;
499 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
500 aura::Window
* child
= non_toplevel_window
->children()[i
];
501 if (!child
->owned_by_parent())
503 if (child
->delegate())
504 toplevel_windows
.Add(child
);
506 non_toplevel_windows
.push(child
);
508 while (!toplevel_windows
.windows().empty())
509 delete *toplevel_windows
.windows().begin();
511 // And then remove the containers.
512 while (!root_window()->children().empty()) {
513 aura::Window
* window
= root_window()->children()[0];
514 if (window
->owned_by_parent()) {
517 root_window()->RemoveChild(window
);
524 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
525 // Forget the shelf early so that shelf don't update itself using wrong
527 workspace_controller_
->SetShelf(NULL
);
528 ReparentAllWindows(root_window(), dst
);
531 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
532 return shelf_
->shelf_layout_manager();
535 SystemTray
* RootWindowController::GetSystemTray() {
536 // We assume in throughout the code that this will not return NULL. If code
537 // triggers this for valid reasons, it should test status_area_widget first.
538 CHECK(shelf_
->status_area_widget());
539 return shelf_
->status_area_widget()->system_tray();
542 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
543 ui::MenuSourceType source_type
) {
544 DCHECK(Shell::GetInstance()->delegate());
545 scoped_ptr
<ui::MenuModel
> menu_model(
546 Shell::GetInstance()->delegate()->CreateContextMenu(root_window(),
552 // Background controller may not be set yet if user clicked on status are
553 // before initial animation completion. See crbug.com/222218
554 if (!wallpaper_controller_
.get())
557 views::MenuRunner
menu_runner(menu_model
.get());
558 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
559 NULL
, gfx::Rect(location_in_screen
, gfx::Size()),
560 views::MenuItemView::TOPLEFT
, source_type
,
561 views::MenuRunner::CONTEXT_MENU
) ==
562 views::MenuRunner::MENU_DELETED
) {
566 Shell::GetInstance()->UpdateShelfVisibility();
569 void RootWindowController::UpdateShelfVisibility() {
570 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
573 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
574 const aura::Window
* topmost_window
= NULL
;
575 const aura::Window
* active_window
= wm::GetActiveWindow();
576 if (active_window
&& active_window
->GetRootWindow() == root_window() &&
577 IsSwitchableContainer(active_window
->parent())) {
578 // Use the active window when it is on the current root window to determine
579 // the fullscreen state to allow temporarily using a panel or docked window
580 // (which are always above the default container) while a fullscreen
581 // window is open. We only use the active window when in a switchable
582 // container as the launcher should not exit fullscreen mode.
583 topmost_window
= active_window
;
585 // Otherwise, use the topmost window on the root window's default container
586 // when there is no active window on this root window.
587 const aura::Window::Windows
& windows
=
588 GetContainer(kShellWindowId_DefaultContainer
)->children();
589 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
590 iter
!= windows
.rend(); ++iter
) {
591 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
592 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
593 (*iter
)->layer()->GetTargetVisibility()) {
594 topmost_window
= *iter
;
599 while (topmost_window
) {
600 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
601 return topmost_window
;
602 topmost_window
= views::corewm::GetTransientParent(topmost_window
);
607 void RootWindowController::ActivateKeyboard(
608 keyboard::KeyboardController
* keyboard_controller
) {
609 if (!keyboard::IsKeyboardEnabled() ||
610 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
613 DCHECK(keyboard_controller
);
614 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
615 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
616 keyboard_controller
->AddObserver(panel_layout_manager_
);
617 keyboard_controller
->AddObserver(docked_layout_manager_
);
619 aura::Window
* parent
= root_window();
620 aura::Window
* keyboard_container
=
621 keyboard_controller
->GetContainerWindow();
622 keyboard_container
->set_id(kShellWindowId_VirtualKeyboardContainer
);
623 parent
->AddChild(keyboard_container
);
624 // TODO(oshima): Bounds of keyboard container should be handled by
625 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
626 keyboard_container
->SetBounds(parent
->bounds());
629 void RootWindowController::DeactivateKeyboard(
630 keyboard::KeyboardController
* keyboard_controller
) {
631 if (!keyboard_controller
||
632 !keyboard_controller
->keyboard_container_initialized()) {
635 aura::Window
* keyboard_container
=
636 keyboard_controller
->GetContainerWindow();
637 if (keyboard_container
->GetRootWindow() == root_window()) {
638 root_window()->RemoveChild(keyboard_container
);
639 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
640 // Virtual keyboard may be deactivated while still showing, notify all
641 // observers that keyboard bounds changed to 0 before remove them.
642 keyboard_controller
->NotifyKeyboardBoundsChanging(gfx::Rect());
643 keyboard_controller
->RemoveObserver(shelf()->shelf_layout_manager());
644 keyboard_controller
->RemoveObserver(panel_layout_manager_
);
645 keyboard_controller
->RemoveObserver(docked_layout_manager_
);
650 ////////////////////////////////////////////////////////////////////////////////
651 // RootWindowController, private:
653 RootWindowController::RootWindowController(
654 aura::WindowEventDispatcher
* dispatcher
)
655 : dispatcher_(dispatcher
),
656 root_window_layout_(NULL
),
657 docked_layout_manager_(NULL
),
658 panel_layout_manager_(NULL
),
659 touch_hud_debug_(NULL
),
660 touch_hud_projection_(NULL
) {
661 GetRootWindowSettings(dispatcher_
->window())->controller
= this;
662 screen_dimmer_
.reset(new ScreenDimmer(dispatcher_
->window()));
664 stacking_controller_
.reset(new StackingController
);
665 aura::client::SetWindowTreeClient(dispatcher_
->window(),
666 stacking_controller_
.get());
667 capture_client_
.reset(
668 new views::corewm::ScopedCaptureClient(dispatcher_
->window()));
671 void RootWindowController::Init(RootWindowType root_window_type
,
672 bool first_run_after_boot
) {
673 Shell
* shell
= Shell::GetInstance();
674 shell
->InitRootWindow(root_window());
676 dispatcher_
->host()->SetCursor(ui::kCursorPointer
);
677 CreateContainersInRootWindow(dispatcher_
->window());
679 if (root_window_type
== VIRTUAL_KEYBOARD
) {
680 shell
->InitKeyboard();
684 CreateSystemBackground(first_run_after_boot
);
686 InitLayoutManagers();
689 if (Shell::GetPrimaryRootWindowController()->
690 GetSystemModalLayoutManager(NULL
)->has_modal_background()) {
691 GetSystemModalLayoutManager(NULL
)->CreateModalBackground();
694 shell
->AddShellObserver(this);
696 if (root_window_type
== PRIMARY
) {
697 root_window_layout()->OnWindowResized();
698 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
699 shell
->InitKeyboard();
701 root_window_layout()->OnWindowResized();
702 shell
->desktop_background_controller()->OnRootWindowAdded(root_window());
703 shell
->high_contrast_controller()->OnRootWindowAdded(dispatcher_
->window());
704 dispatcher_
->host()->Show();
706 // Create a shelf if a user is already logged in.
707 if (shell
->session_state_delegate()->NumberOfLoggedInUsers())
708 shelf()->CreateShelf();
712 void RootWindowController::InitLayoutManagers() {
713 root_window_layout_
= new RootWindowLayoutManager(root_window());
714 root_window()->SetLayoutManager(root_window_layout_
);
716 aura::Window
* default_container
=
717 GetContainer(kShellWindowId_DefaultContainer
);
718 // Workspace manager has its own layout managers.
719 workspace_controller_
.reset(
720 new WorkspaceController(default_container
));
722 aura::Window
* always_on_top_container
=
723 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
724 always_on_top_container
->SetLayoutManager(
725 new internal::WorkspaceLayoutManager(
726 always_on_top_container
));
727 always_on_top_controller_
.reset(new internal::AlwaysOnTopController
);
728 always_on_top_controller_
->SetAlwaysOnTopContainer(always_on_top_container
);
730 DCHECK(!shelf_
.get());
731 aura::Window
* shelf_container
=
732 GetContainer(internal::kShellWindowId_ShelfContainer
);
733 // TODO(harrym): Remove when status area is view.
734 aura::Window
* status_container
=
735 GetContainer(internal::kShellWindowId_StatusContainer
);
736 shelf_
.reset(new ShelfWidget(
737 shelf_container
, status_container
, workspace_controller()));
739 if (!Shell::GetInstance()->session_state_delegate()->
740 IsActiveUserSessionStarted()) {
741 // This window exists only to be a event target on login screen.
742 // It does not have to handle events, nor be visible.
743 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
744 mouse_event_target_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
746 aura::Window
* lock_background_container
=
747 GetContainer(internal::kShellWindowId_LockScreenBackgroundContainer
);
748 lock_background_container
->AddChild(mouse_event_target_
.get());
749 mouse_event_target_
->Show();
752 // Create Docked windows layout manager
753 aura::Window
* docked_container
= GetContainer(
754 internal::kShellWindowId_DockedContainer
);
755 docked_layout_manager_
=
756 new internal::DockedWindowLayoutManager(docked_container
,
757 workspace_controller());
758 docked_container
->SetLayoutManager(docked_layout_manager_
);
760 // Create Panel layout manager
761 aura::Window
* panel_container
= GetContainer(
762 internal::kShellWindowId_PanelContainer
);
763 panel_layout_manager_
=
764 new internal::PanelLayoutManager(panel_container
);
765 panel_container
->SetLayoutManager(panel_layout_manager_
);
766 panel_container_handler_
.reset(new PanelWindowEventHandler
);
767 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
770 void RootWindowController::InitTouchHuds() {
771 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
772 if (command_line
->HasSwitch(switches::kAshTouchHud
))
773 set_touch_hud_debug(new TouchHudDebug(root_window()));
774 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
775 EnableTouchHudProjection();
778 void RootWindowController::CreateSystemBackground(
779 bool is_first_run_after_boot
) {
780 SkColor color
= SK_ColorBLACK
;
781 #if defined(OS_CHROMEOS)
782 if (is_first_run_after_boot
)
783 color
= kChromeOsBootColor
;
785 system_background_
.reset(
786 new SystemBackgroundController(root_window(), color
));
788 #if defined(OS_CHROMEOS)
789 // Make a copy of the system's boot splash screen so we can composite it
790 // onscreen until the desktop background is ready.
791 if (is_first_run_after_boot
&&
792 (CommandLine::ForCurrentProcess()->HasSwitch(
793 switches::kAshCopyHostBackgroundAtBoot
) ||
794 CommandLine::ForCurrentProcess()->HasSwitch(
795 switches::kAshAnimateFromBootSplashScreen
)))
796 boot_splash_screen_
.reset(new BootSplashScreen(dispatcher_
.get()));
800 void RootWindowController::CreateContainersInRootWindow(
801 aura::Window
* root_window
) {
802 // These containers are just used by PowerButtonController to animate groups
803 // of containers simultaneously without messing up the current transformations
804 // on those containers. These are direct children of the root window; all of
805 // the other containers are their children.
807 // The desktop background container is not part of the lock animation, so it
808 // is not included in those animate groups.
809 // When screen is locked desktop background is moved to lock screen background
810 // container (moved back on unlock). We want to make sure that there's an
811 // opaque layer occluding the non-lock-screen layers.
812 aura::Window
* desktop_background_container
= CreateContainer(
813 kShellWindowId_DesktopBackgroundContainer
,
814 "DesktopBackgroundContainer",
816 views::corewm::SetChildWindowVisibilityChangesAnimated(
817 desktop_background_container
);
819 aura::Window
* non_lock_screen_containers
= CreateContainer(
820 kShellWindowId_NonLockScreenContainersContainer
,
821 "NonLockScreenContainersContainer",
824 aura::Window
* lock_background_containers
= CreateContainer(
825 kShellWindowId_LockScreenBackgroundContainer
,
826 "LockScreenBackgroundContainer",
828 views::corewm::SetChildWindowVisibilityChangesAnimated(
829 lock_background_containers
);
831 aura::Window
* lock_screen_containers
= CreateContainer(
832 kShellWindowId_LockScreenContainersContainer
,
833 "LockScreenContainersContainer",
835 aura::Window
* lock_screen_related_containers
= CreateContainer(
836 kShellWindowId_LockScreenRelatedContainersContainer
,
837 "LockScreenRelatedContainersContainer",
840 CreateContainer(kShellWindowId_UnparentedControlContainer
,
841 "UnparentedControlContainer",
842 non_lock_screen_containers
);
844 aura::Window
* default_container
= CreateContainer(
845 kShellWindowId_DefaultContainer
,
847 non_lock_screen_containers
);
848 views::corewm::SetChildWindowVisibilityChangesAnimated(default_container
);
849 SetUsesScreenCoordinates(default_container
);
850 SetUsesEasyResizeTargeter(default_container
);
852 aura::Window
* always_on_top_container
= CreateContainer(
853 kShellWindowId_AlwaysOnTopContainer
,
854 "AlwaysOnTopContainer",
855 non_lock_screen_containers
);
856 views::corewm::SetChildWindowVisibilityChangesAnimated(
857 always_on_top_container
);
858 SetUsesScreenCoordinates(always_on_top_container
);
860 aura::Window
* docked_container
= CreateContainer(
861 kShellWindowId_DockedContainer
,
863 non_lock_screen_containers
);
864 views::corewm::SetChildWindowVisibilityChangesAnimated(docked_container
);
865 SetUsesScreenCoordinates(docked_container
);
866 SetUsesEasyResizeTargeter(docked_container
);
868 aura::Window
* shelf_container
=
869 CreateContainer(kShellWindowId_ShelfContainer
,
871 non_lock_screen_containers
);
872 SetUsesScreenCoordinates(shelf_container
);
873 DescendantShouldStayInSameRootWindow(shelf_container
);
875 aura::Window
* panel_container
= CreateContainer(
876 kShellWindowId_PanelContainer
,
878 non_lock_screen_containers
);
879 SetUsesScreenCoordinates(panel_container
);
880 SetUsesEasyResizeTargeter(panel_container
);
882 aura::Window
* shelf_bubble_container
=
883 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
884 "ShelfBubbleContainer",
885 non_lock_screen_containers
);
886 SetUsesScreenCoordinates(shelf_bubble_container
);
887 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
889 aura::Window
* app_list_container
=
890 CreateContainer(kShellWindowId_AppListContainer
,
892 non_lock_screen_containers
);
893 SetUsesScreenCoordinates(app_list_container
);
895 aura::Window
* modal_container
= CreateContainer(
896 kShellWindowId_SystemModalContainer
,
897 "SystemModalContainer",
898 non_lock_screen_containers
);
899 modal_container
->SetLayoutManager(
900 new SystemModalContainerLayoutManager(modal_container
));
901 views::corewm::SetChildWindowVisibilityChangesAnimated(modal_container
);
902 SetUsesScreenCoordinates(modal_container
);
903 SetUsesEasyResizeTargeter(modal_container
);
905 aura::Window
* input_method_container
= CreateContainer(
906 kShellWindowId_InputMethodContainer
,
907 "InputMethodContainer",
908 non_lock_screen_containers
);
909 views::corewm::SetChildWindowVisibilityChangesAnimated(
910 input_method_container
);
911 SetUsesScreenCoordinates(input_method_container
);
913 // TODO(beng): Figure out if we can make this use
914 // SystemModalContainerEventFilter instead of stops_event_propagation.
915 aura::Window
* lock_container
= CreateContainer(
916 kShellWindowId_LockScreenContainer
,
917 "LockScreenContainer",
918 lock_screen_containers
);
919 lock_container
->SetLayoutManager(
920 new internal::WorkspaceLayoutManager(lock_container
));
921 SetUsesScreenCoordinates(lock_container
);
922 // TODO(beng): stopsevents
924 aura::Window
* lock_modal_container
= CreateContainer(
925 kShellWindowId_LockSystemModalContainer
,
926 "LockSystemModalContainer",
927 lock_screen_containers
);
928 lock_modal_container
->SetLayoutManager(
929 new SystemModalContainerLayoutManager(lock_modal_container
));
930 views::corewm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
931 SetUsesScreenCoordinates(lock_modal_container
);
932 SetUsesEasyResizeTargeter(lock_modal_container
);
934 aura::Window
* status_container
=
935 CreateContainer(kShellWindowId_StatusContainer
,
937 lock_screen_related_containers
);
938 SetUsesScreenCoordinates(status_container
);
939 DescendantShouldStayInSameRootWindow(status_container
);
941 aura::Window
* settings_bubble_container
= CreateContainer(
942 kShellWindowId_SettingBubbleContainer
,
943 "SettingBubbleContainer",
944 lock_screen_related_containers
);
945 views::corewm::SetChildWindowVisibilityChangesAnimated(
946 settings_bubble_container
);
947 SetUsesScreenCoordinates(settings_bubble_container
);
948 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
950 aura::Window
* menu_container
= CreateContainer(
951 kShellWindowId_MenuContainer
,
953 lock_screen_related_containers
);
954 views::corewm::SetChildWindowVisibilityChangesAnimated(menu_container
);
955 SetUsesScreenCoordinates(menu_container
);
957 aura::Window
* drag_drop_container
= CreateContainer(
958 kShellWindowId_DragImageAndTooltipContainer
,
959 "DragImageAndTooltipContainer",
960 lock_screen_related_containers
);
961 views::corewm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
962 SetUsesScreenCoordinates(drag_drop_container
);
964 aura::Window
* overlay_container
= CreateContainer(
965 kShellWindowId_OverlayContainer
,
967 lock_screen_related_containers
);
968 SetUsesScreenCoordinates(overlay_container
);
970 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
971 "PowerButtonAnimationContainer", root_window
) ;
974 void RootWindowController::EnableTouchHudProjection() {
975 if (touch_hud_projection_
)
977 set_touch_hud_projection(new TouchHudProjection(root_window()));
980 void RootWindowController::DisableTouchHudProjection() {
981 if (!touch_hud_projection_
)
983 touch_hud_projection_
->Remove();
986 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
987 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
990 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
992 EnableTouchHudProjection();
994 DisableTouchHudProjection();
997 RootWindowController
* GetRootWindowController(
998 const aura::Window
* root_window
) {
999 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;
1002 } // namespace internal