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/virtual_keyboard_container_layout_manager.h"
48 #include "ash/wm/window_properties.h"
49 #include "ash/wm/window_state.h"
50 #include "ash/wm/window_util.h"
51 #include "ash/wm/workspace/workspace_layout_manager.h"
52 #include "ash/wm/workspace_controller.h"
53 #include "base/command_line.h"
54 #include "base/time/time.h"
55 #include "ui/aura/client/aura_constants.h"
56 #include "ui/aura/client/screen_position_client.h"
57 #include "ui/aura/window.h"
58 #include "ui/aura/window_delegate.h"
59 #include "ui/aura/window_event_dispatcher.h"
60 #include "ui/aura/window_observer.h"
61 #include "ui/aura/window_tracker.h"
62 #include "ui/base/hit_test.h"
63 #include "ui/base/models/menu_model.h"
64 #include "ui/gfx/display.h"
65 #include "ui/gfx/screen.h"
66 #include "ui/keyboard/keyboard_controller.h"
67 #include "ui/keyboard/keyboard_util.h"
68 #include "ui/views/controls/menu/menu_runner.h"
69 #include "ui/views/view_model.h"
70 #include "ui/views/view_model_utils.h"
71 #include "ui/wm/core/capture_controller.h"
72 #include "ui/wm/core/easy_resize_window_targeter.h"
73 #include "ui/wm/core/visibility_controller.h"
74 #include "ui/wm/core/window_util.h"
75 #include "ui/wm/public/drag_drop_client.h"
76 #include "ui/wm/public/tooltip_client.h"
77 #include "ui/wm/public/window_types.h"
79 #if defined(OS_CHROMEOS)
80 #include "ash/ash_touch_exploration_manager_chromeos.h"
81 #include "ash/wm/boot_splash_screen_chromeos.h"
82 #include "ui/chromeos/touch_exploration_controller.h"
88 #if defined(OS_CHROMEOS)
89 // Duration for the animation that hides the boot splash screen, in
90 // milliseconds. This should be short enough in relation to
91 // wm/window_animation.cc's brightness/grayscale fade animation that the login
92 // background image animation isn't hidden by the splash screen animation.
93 const int kBootSplashScreenHideDurationMs
= 500;
96 // Creates a new window for use as a container.
97 aura::Window
* CreateContainer(int window_id
,
99 aura::Window
* parent
) {
100 aura::Window
* container
= new aura::Window(NULL
);
101 container
->set_id(window_id
);
102 container
->SetName(name
);
103 container
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
104 parent
->AddChild(container
);
105 if (window_id
!= kShellWindowId_UnparentedControlContainer
)
110 float ToRelativeValue(int value
, int src
, int dst
) {
111 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
114 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
115 const gfx::Size
& dst_size
,
116 gfx::Rect
* bounds_in_out
) {
117 gfx::Point origin
= bounds_in_out
->origin();
118 bounds_in_out
->set_origin(gfx::Point(
119 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
120 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
123 // Reparents |window| to |new_parent|.
124 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
125 const gfx::Size src_size
= window
->parent()->bounds().size();
126 const gfx::Size dst_size
= new_parent
->bounds().size();
127 // Update the restore bounds to make it relative to the display.
128 wm::WindowState
* state
= wm::GetWindowState(window
);
129 gfx::Rect restore_bounds
;
130 bool has_restore_bounds
= state
->HasRestoreBounds();
132 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
133 new_parent
->id() != kShellWindowId_DockedContainer
;
134 gfx::Rect local_bounds
;
136 local_bounds
= state
->window()->bounds();
137 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
140 if (has_restore_bounds
) {
141 restore_bounds
= state
->GetRestoreBoundsInParent();
142 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
145 new_parent
->AddChild(window
);
147 // Docked windows have bounds handled by the layout manager in AddChild().
149 window
->SetBounds(local_bounds
);
151 if (has_restore_bounds
)
152 state
->SetRestoreBoundsInParent(restore_bounds
);
155 // Reparents the appropriate set of windows from |src| to |dst|.
156 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
157 // Set of windows to move.
158 const int kContainerIdsToMove
[] = {
159 kShellWindowId_DefaultContainer
,
160 kShellWindowId_DockedContainer
,
161 kShellWindowId_PanelContainer
,
162 kShellWindowId_AlwaysOnTopContainer
,
163 kShellWindowId_SystemModalContainer
,
164 kShellWindowId_LockSystemModalContainer
,
165 kShellWindowId_UnparentedControlContainer
, };
166 for (size_t i
= 0; i
< arraysize(kContainerIdsToMove
); i
++) {
167 int id
= kContainerIdsToMove
[i
];
168 aura::Window
* src_container
= Shell::GetContainer(src
, id
);
169 aura::Window
* dst_container
= Shell::GetContainer(dst
, id
);
170 while (!src_container
->children().empty()) {
171 // Restart iteration from the source container windows each time as they
172 // may change as a result of moving other windows.
173 aura::Window::Windows::const_iterator iter
=
174 src_container
->children().begin();
175 while (iter
!= src_container
->children().end() &&
176 SystemModalContainerLayoutManager::IsModalBackground(*iter
)) {
179 // If the entire window list is modal background windows then stop.
180 if (iter
== src_container
->children().end())
182 ReparentWindow(*iter
, dst_container
);
187 // Mark the container window so that a widget added to this container will
188 // use the virtual screeen coordinates instead of parent.
189 void SetUsesScreenCoordinates(aura::Window
* container
) {
190 container
->SetProperty(kUsesScreenCoordinatesKey
, true);
193 // Mark the container window so that a widget added to this container will
194 // say in the same root window regardless of the bounds specified.
195 void DescendantShouldStayInSameRootWindow(aura::Window
* container
) {
196 container
->SetProperty(kStayInSameRootWindowKey
, true);
199 void SetUsesEasyResizeTargeter(aura::Window
* container
) {
200 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
201 -kResizeOutsideBoundsSize
,
202 -kResizeOutsideBoundsSize
,
203 -kResizeOutsideBoundsSize
);
204 gfx::Insets touch_extend
= mouse_extend
.Scale(
205 kResizeOutsideBoundsScaleForTouch
);
206 container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
207 new ::wm::EasyResizeWindowTargeter(container
, mouse_extend
,
211 // A window delegate which does nothing. Used to create a window that
212 // is a event target, but do nothing.
213 class EmptyWindowDelegate
: public aura::WindowDelegate
{
215 EmptyWindowDelegate() {}
216 virtual ~EmptyWindowDelegate() {}
218 // aura::WindowDelegate overrides:
219 virtual gfx::Size
GetMinimumSize() const override
{
222 virtual gfx::Size
GetMaximumSize() const override
{
225 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
226 const gfx::Rect
& new_bounds
) override
{
228 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
229 return gfx::kNullCursor
;
231 virtual int GetNonClientComponent(
232 const gfx::Point
& point
) const override
{
235 virtual bool ShouldDescendIntoChildForEventHandling(
237 const gfx::Point
& location
) override
{
240 virtual bool CanFocus() override
{
243 virtual void OnCaptureLost() override
{
245 virtual void OnPaint(gfx::Canvas
* canvas
) override
{
247 virtual void OnDeviceScaleFactorChanged(
248 float device_scale_factor
) override
{
250 virtual void OnWindowDestroying(aura::Window
* window
) override
{}
251 virtual void OnWindowDestroyed(aura::Window
* window
) override
{
254 virtual void OnWindowTargetVisibilityChanged(bool visible
) override
{
256 virtual bool HasHitTestMask() const override
{
259 virtual void GetHitTestMask(gfx::Path
* mask
) const override
{}
262 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
267 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost
* host
) {
268 RootWindowController
* controller
= new RootWindowController(host
);
269 controller
->Init(RootWindowController::PRIMARY
,
270 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
273 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost
* host
) {
274 RootWindowController
* controller
= new RootWindowController(host
);
275 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
279 RootWindowController
* RootWindowController::ForShelf(
280 const aura::Window
* window
) {
281 CHECK(Shell::HasInstance());
282 return GetRootWindowController(window
->GetRootWindow());
286 RootWindowController
* RootWindowController::ForWindow(
287 const aura::Window
* window
) {
288 CHECK(Shell::HasInstance());
289 return GetRootWindowController(window
->GetRootWindow());
293 RootWindowController
* RootWindowController::ForTargetRootWindow() {
294 CHECK(Shell::HasInstance());
295 return GetRootWindowController(Shell::GetTargetRootWindow());
299 aura::Window
* RootWindowController::GetContainerForWindow(
300 aura::Window
* window
) {
301 aura::Window
* container
= window
->parent();
302 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
303 container
= container
->parent();
307 RootWindowController::~RootWindowController() {
310 // The CaptureClient needs to be around for as long as the RootWindow is
312 capture_client_
.reset();
315 aura::WindowTreeHost
* RootWindowController::GetHost() {
316 return ash_host_
->AsWindowTreeHost();
319 const aura::WindowTreeHost
* RootWindowController::GetHost() const {
320 return ash_host_
->AsWindowTreeHost();
323 aura::Window
* RootWindowController::GetRootWindow() {
324 return GetHost()->window();
327 const aura::Window
* RootWindowController::GetRootWindow() const {
328 return GetHost()->window();
331 void RootWindowController::SetWallpaperController(
332 DesktopBackgroundWidgetController
* controller
) {
333 wallpaper_controller_
.reset(controller
);
336 void RootWindowController::SetAnimatingWallpaperController(
337 AnimatingDesktopController
* controller
) {
338 if (animating_wallpaper_controller_
.get())
339 animating_wallpaper_controller_
->StopAnimating();
340 animating_wallpaper_controller_
.reset(controller
);
343 void RootWindowController::Shutdown() {
344 Shell
* shell
= Shell::GetInstance();
345 shell
->RemoveShellObserver(this);
347 #if defined(OS_CHROMEOS)
348 if (touch_exploration_manager_
) {
349 touch_exploration_manager_
.reset();
353 if (animating_wallpaper_controller_
.get())
354 animating_wallpaper_controller_
->StopAnimating();
355 wallpaper_controller_
.reset();
356 animating_wallpaper_controller_
.reset();
357 aura::Window
* root_window
= GetRootWindow();
358 // Change the target root window before closing child windows. If any child
359 // being removed triggers a relayout of the shelf it will try to build a
360 // window list adding windows from the target root window's containers which
361 // may have already gone away.
362 if (Shell::GetTargetRootWindow() == root_window
) {
363 shell
->set_target_root_window(
364 Shell::GetPrimaryRootWindow() == root_window
366 : Shell::GetPrimaryRootWindow());
370 GetRootWindowSettings(root_window
)->controller
= NULL
;
371 screen_dimmer_
.reset();
372 workspace_controller_
.reset();
373 // Forget with the display ID so that display lookup
374 // ends up with invalid display.
375 GetRootWindowSettings(root_window
)->display_id
=
376 gfx::Display::kInvalidDisplayID
;
377 ash_host_
->PrepareForShutdown();
379 system_background_
.reset();
380 aura::client::SetScreenPositionClient(root_window
, NULL
);
383 SystemModalContainerLayoutManager
*
384 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
385 aura::Window
* modal_container
= NULL
;
387 aura::Window
* window_container
= GetContainerForWindow(window
);
388 if (window_container
&&
389 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
390 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
392 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
395 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
396 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
397 kShellWindowId_SystemModalContainer
;
398 modal_container
= GetContainer(modal_window_id
);
400 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
401 modal_container
->layout_manager()) : NULL
;
404 aura::Window
* RootWindowController::GetContainer(int container_id
) {
405 return GetRootWindow()->GetChildById(container_id
);
408 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
409 return ash_host_
->AsWindowTreeHost()->window()->GetChildById(container_id
);
412 void RootWindowController::ShowShelf() {
413 if (!shelf_
->shelf())
415 shelf_
->shelf()->SetVisible(true);
416 shelf_
->status_area_widget()->Show();
419 void RootWindowController::OnShelfCreated() {
420 if (panel_layout_manager_
)
421 panel_layout_manager_
->SetShelf(shelf_
->shelf());
422 if (docked_layout_manager_
) {
423 docked_layout_manager_
->SetShelf(shelf_
->shelf());
424 if (shelf_
->shelf_layout_manager())
425 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
428 // Notify shell observers that the shelf has been created.
429 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
432 void RootWindowController::UpdateAfterLoginStatusChange(
433 user::LoginStatus status
) {
434 if (status
!= user::LOGGED_IN_NONE
)
435 mouse_event_target_
.reset();
436 if (shelf_
->status_area_widget())
437 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
440 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
441 #if defined(OS_CHROMEOS)
442 if (CommandLine::ForCurrentProcess()->HasSwitch(
443 switches::kAshAnimateFromBootSplashScreen
) &&
444 boot_splash_screen_
.get()) {
445 // Make the splash screen fade out so it doesn't obscure the desktop
446 // wallpaper's brightness/grayscale animation.
447 boot_splash_screen_
->StartHideAnimation(
448 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
453 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
454 // Make sure the wallpaper is visible.
455 system_background_
->SetColor(SK_ColorBLACK
);
456 #if defined(OS_CHROMEOS)
457 boot_splash_screen_
.reset();
460 Shell::GetInstance()->user_wallpaper_delegate()->
461 OnWallpaperAnimationFinished();
462 // Only removes old component when wallpaper animation finished. If we
463 // remove the old one before the new wallpaper is done fading in there will
464 // be a white flash during the animation.
465 if (animating_wallpaper_controller()) {
466 DesktopBackgroundWidgetController
* controller
=
467 animating_wallpaper_controller()->GetController(true);
468 // |desktop_widget_| should be the same animating widget we try to move
469 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
470 // before move it to |kDesktopController|.
471 DCHECK_EQ(controller
->widget(), widget
);
472 // Release the old controller and close its background widget.
473 SetWallpaperController(controller
);
477 void RootWindowController::CloseChildWindows() {
478 mouse_event_target_
.reset();
480 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
481 // to fire notifications.
482 if (docked_layout_manager_
&& shelf_
&& shelf_
->shelf_layout_manager())
483 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
485 // Deactivate keyboard container before closing child windows and shutting
486 // down associated layout managers.
487 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
489 // panel_layout_manager_ needs to be shut down before windows are destroyed.
490 if (panel_layout_manager_
) {
491 panel_layout_manager_
->Shutdown();
492 panel_layout_manager_
= NULL
;
494 // docked_layout_manager_ needs to be shut down before windows are destroyed.
495 if (docked_layout_manager_
) {
496 docked_layout_manager_
->Shutdown();
497 docked_layout_manager_
= NULL
;
499 aura::Window
* root_window
= GetRootWindow();
500 aura::client::SetDragDropClient(root_window
, NULL
);
502 // TODO(harrym): Remove when Status Area Widget is a child view.
504 shelf_
->ShutdownStatusAreaWidget();
506 if (shelf_
->shelf_layout_manager())
507 shelf_
->shelf_layout_manager()->PrepareForShutdown();
510 // Close background widget first as it depends on tooltip.
511 wallpaper_controller_
.reset();
512 animating_wallpaper_controller_
.reset();
514 workspace_controller_
.reset();
515 aura::client::SetTooltipClient(root_window
, NULL
);
517 // Explicitly destroy top level windows. We do this as during part of
518 // destruction such windows may query the RootWindow for state.
519 std::queue
<aura::Window
*> non_toplevel_windows
;
520 non_toplevel_windows
.push(root_window
);
521 while (!non_toplevel_windows
.empty()) {
522 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
523 non_toplevel_windows
.pop();
524 aura::WindowTracker toplevel_windows
;
525 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
526 aura::Window
* child
= non_toplevel_window
->children()[i
];
527 if (!child
->owned_by_parent())
529 if (child
->delegate())
530 toplevel_windows
.Add(child
);
532 non_toplevel_windows
.push(child
);
534 while (!toplevel_windows
.windows().empty())
535 delete *toplevel_windows
.windows().begin();
537 // And then remove the containers.
538 while (!root_window
->children().empty()) {
539 aura::Window
* window
= root_window
->children()[0];
540 if (window
->owned_by_parent()) {
543 root_window
->RemoveChild(window
);
550 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
551 // Forget the shelf early so that shelf don't update itself using wrong
553 workspace_controller_
->SetShelf(NULL
);
554 ReparentAllWindows(GetRootWindow(), dst
);
557 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
558 return shelf_
->shelf_layout_manager();
561 SystemTray
* RootWindowController::GetSystemTray() {
562 // We assume in throughout the code that this will not return NULL. If code
563 // triggers this for valid reasons, it should test status_area_widget first.
564 CHECK(shelf_
->status_area_widget());
565 return shelf_
->status_area_widget()->system_tray();
568 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
569 ui::MenuSourceType source_type
) {
570 DCHECK(Shell::GetInstance()->delegate());
571 scoped_ptr
<ui::MenuModel
> menu_model(
572 Shell::GetInstance()->delegate()->CreateContextMenu(
573 GetRootWindow(), NULL
, NULL
));
577 // Background controller may not be set yet if user clicked on status are
578 // before initial animation completion. See crbug.com/222218
579 if (!wallpaper_controller_
.get())
582 views::MenuRunner
menu_runner(menu_model
.get(),
583 views::MenuRunner::CONTEXT_MENU
);
584 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
586 gfx::Rect(location_in_screen
, gfx::Size()),
587 views::MENU_ANCHOR_TOPLEFT
,
588 source_type
) == views::MenuRunner::MENU_DELETED
) {
592 Shell::GetInstance()->UpdateShelfVisibility();
595 void RootWindowController::UpdateShelfVisibility() {
596 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
599 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
600 const aura::Window
* topmost_window
= NULL
;
601 const aura::Window
* active_window
= wm::GetActiveWindow();
602 if (active_window
&& active_window
->GetRootWindow() == GetRootWindow() &&
603 IsSwitchableContainer(active_window
->parent())) {
604 // Use the active window when it is on the current root window to determine
605 // the fullscreen state to allow temporarily using a panel or docked window
606 // (which are always above the default container) while a fullscreen
607 // window is open. We only use the active window when in a switchable
608 // container as the launcher should not exit fullscreen mode.
609 topmost_window
= active_window
;
611 // Otherwise, use the topmost window on the root window's default container
612 // when there is no active window on this root window.
613 const aura::Window::Windows
& windows
=
614 GetContainer(kShellWindowId_DefaultContainer
)->children();
615 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
616 iter
!= windows
.rend(); ++iter
) {
617 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
618 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
619 (*iter
)->layer()->GetTargetVisibility()) {
620 topmost_window
= *iter
;
625 while (topmost_window
) {
626 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
627 return topmost_window
;
628 topmost_window
= ::wm::GetTransientParent(topmost_window
);
633 void RootWindowController::ActivateKeyboard(
634 keyboard::KeyboardController
* keyboard_controller
) {
635 if (!keyboard::IsKeyboardEnabled() ||
636 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
639 DCHECK(keyboard_controller
);
640 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
641 keyboard_controller
->AddObserver(panel_layout_manager_
);
642 keyboard_controller
->AddObserver(docked_layout_manager_
);
643 keyboard_controller
->AddObserver(workspace_controller_
->layout_manager());
644 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
645 aura::Window
* parent
= GetContainer(kShellWindowId_ImeWindowParentContainer
);
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
=
666 GetContainer(kShellWindowId_ImeWindowParentContainer
);
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(kShellWindowId_ImeWindowParentContainer
);
683 return parent
? parent
->Contains(window
) : false;
686 ////////////////////////////////////////////////////////////////////////////////
687 // RootWindowController, private:
689 RootWindowController::RootWindowController(AshWindowTreeHost
* ash_host
)
690 : ash_host_(ash_host
),
691 root_window_layout_(NULL
),
692 docked_layout_manager_(NULL
),
693 panel_layout_manager_(NULL
),
694 touch_hud_debug_(NULL
),
695 touch_hud_projection_(NULL
) {
696 aura::Window
* root_window
= GetRootWindow();
697 GetRootWindowSettings(root_window
)->controller
= this;
698 screen_dimmer_
.reset(new ScreenDimmer(root_window
));
700 stacking_controller_
.reset(new StackingController
);
701 aura::client::SetWindowTreeClient(root_window
, stacking_controller_
.get());
702 capture_client_
.reset(new ::wm::ScopedCaptureClient(root_window
));
705 void RootWindowController::Init(RootWindowType root_window_type
,
706 bool first_run_after_boot
) {
707 aura::Window
* root_window
= GetRootWindow();
708 Shell
* shell
= Shell::GetInstance();
709 shell
->InitRootWindow(root_window
);
711 ash_host_
->AsWindowTreeHost()->SetCursor(ui::kCursorPointer
);
712 CreateContainersInRootWindow(root_window
);
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 shell
->InitKeyboard();
730 root_window_layout()->OnWindowResized();
731 ash_host_
->AsWindowTreeHost()->Show();
733 // Create a shelf if a user is already logged in.
734 if (shell
->session_state_delegate()->NumberOfLoggedInUsers())
735 shelf()->CreateShelf();
737 // Notify shell observers about new root window.
738 shell
->OnRootWindowAdded(root_window
);
741 #if defined(OS_CHROMEOS)
742 if (!CommandLine::ForCurrentProcess()->HasSwitch(
743 switches::kAshDisableTouchExplorationMode
)) {
744 touch_exploration_manager_
.reset(new AshTouchExplorationManager(this));
749 void RootWindowController::InitLayoutManagers() {
750 aura::Window
* root_window
= GetRootWindow();
751 root_window_layout_
= new RootWindowLayoutManager(root_window
);
752 root_window
->SetLayoutManager(root_window_layout_
);
754 aura::Window
* default_container
=
755 GetContainer(kShellWindowId_DefaultContainer
);
756 // Workspace manager has its own layout managers.
757 workspace_controller_
.reset(
758 new WorkspaceController(default_container
));
760 aura::Window
* always_on_top_container
=
761 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
762 always_on_top_container
->SetLayoutManager(
763 new WorkspaceLayoutManager(always_on_top_container
));
764 always_on_top_controller_
.reset(new AlwaysOnTopController
);
765 always_on_top_controller_
->SetAlwaysOnTopContainer(always_on_top_container
);
767 DCHECK(!shelf_
.get());
768 aura::Window
* shelf_container
= GetContainer(kShellWindowId_ShelfContainer
);
769 // TODO(harrym): Remove when status area is view.
770 aura::Window
* status_container
= GetContainer(kShellWindowId_StatusContainer
);
771 shelf_
.reset(new ShelfWidget(
772 shelf_container
, status_container
, workspace_controller()));
774 if (!Shell::GetInstance()->session_state_delegate()->
775 IsActiveUserSessionStarted()) {
776 // This window exists only to be a event target on login screen.
777 // It does not have to handle events, nor be visible.
778 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
779 mouse_event_target_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
781 aura::Window
* lock_background_container
=
782 GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
783 lock_background_container
->AddChild(mouse_event_target_
.get());
784 mouse_event_target_
->Show();
787 // Create Docked windows layout manager
788 aura::Window
* docked_container
= GetContainer(kShellWindowId_DockedContainer
);
789 docked_layout_manager_
=
790 new DockedWindowLayoutManager(docked_container
, workspace_controller());
791 docked_container
->SetLayoutManager(docked_layout_manager_
);
793 // Installs SnapLayoutManager to containers who set the
794 // |kSnapsChildrenToPhysicalPixelBoundary| property.
795 wm::InstallSnapLayoutManagerToContainers(root_window
);
797 // Create Panel layout manager
798 aura::Window
* panel_container
= GetContainer(kShellWindowId_PanelContainer
);
799 panel_layout_manager_
= new PanelLayoutManager(panel_container
);
800 panel_container
->SetLayoutManager(panel_layout_manager_
);
801 panel_container_handler_
.reset(new PanelWindowEventHandler
);
802 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
804 // Install an AttachedPanelWindowTargeter on the panel container to make it
805 // easier to correctly target shelf buttons with touch.
806 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
807 -kResizeOutsideBoundsSize
,
808 -kResizeOutsideBoundsSize
,
809 -kResizeOutsideBoundsSize
);
810 gfx::Insets touch_extend
= mouse_extend
.Scale(
811 kResizeOutsideBoundsScaleForTouch
);
812 panel_container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
813 new AttachedPanelWindowTargeter(panel_container
,
816 panel_layout_manager_
)));
819 void RootWindowController::InitTouchHuds() {
820 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
821 if (command_line
->HasSwitch(switches::kAshTouchHud
))
822 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
823 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
824 EnableTouchHudProjection();
827 void RootWindowController::CreateSystemBackground(
828 bool is_first_run_after_boot
) {
829 SkColor color
= SK_ColorBLACK
;
830 #if defined(OS_CHROMEOS)
831 if (is_first_run_after_boot
)
832 color
= kChromeOsBootColor
;
834 system_background_
.reset(
835 new SystemBackgroundController(GetRootWindow(), color
));
837 #if defined(OS_CHROMEOS)
838 // Make a copy of the system's boot splash screen so we can composite it
839 // onscreen until the desktop background is ready.
840 if (is_first_run_after_boot
&&
841 (CommandLine::ForCurrentProcess()->HasSwitch(
842 switches::kAshCopyHostBackgroundAtBoot
) ||
843 CommandLine::ForCurrentProcess()->HasSwitch(
844 switches::kAshAnimateFromBootSplashScreen
)))
845 boot_splash_screen_
.reset(new BootSplashScreen(GetHost()));
849 void RootWindowController::CreateContainersInRootWindow(
850 aura::Window
* root_window
) {
851 // These containers are just used by PowerButtonController to animate groups
852 // of containers simultaneously without messing up the current transformations
853 // on those containers. These are direct children of the root window; all of
854 // the other containers are their children.
856 // The desktop background container is not part of the lock animation, so it
857 // is not included in those animate groups.
858 // When screen is locked desktop background is moved to lock screen background
859 // container (moved back on unlock). We want to make sure that there's an
860 // opaque layer occluding the non-lock-screen layers.
861 aura::Window
* desktop_background_container
= CreateContainer(
862 kShellWindowId_DesktopBackgroundContainer
,
863 "DesktopBackgroundContainer",
865 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container
);
867 aura::Window
* non_lock_screen_containers
= CreateContainer(
868 kShellWindowId_NonLockScreenContainersContainer
,
869 "NonLockScreenContainersContainer",
872 aura::Window
* lock_background_containers
= CreateContainer(
873 kShellWindowId_LockScreenBackgroundContainer
,
874 "LockScreenBackgroundContainer",
876 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers
);
878 aura::Window
* lock_screen_containers
= CreateContainer(
879 kShellWindowId_LockScreenContainersContainer
,
880 "LockScreenContainersContainer",
882 aura::Window
* lock_screen_related_containers
= CreateContainer(
883 kShellWindowId_LockScreenRelatedContainersContainer
,
884 "LockScreenRelatedContainersContainer",
887 CreateContainer(kShellWindowId_UnparentedControlContainer
,
888 "UnparentedControlContainer",
889 non_lock_screen_containers
);
891 aura::Window
* default_container
= CreateContainer(
892 kShellWindowId_DefaultContainer
,
894 non_lock_screen_containers
);
895 ::wm::SetChildWindowVisibilityChangesAnimated(default_container
);
896 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container
);
897 SetUsesScreenCoordinates(default_container
);
898 SetUsesEasyResizeTargeter(default_container
);
900 aura::Window
* always_on_top_container
= CreateContainer(
901 kShellWindowId_AlwaysOnTopContainer
,
902 "AlwaysOnTopContainer",
903 non_lock_screen_containers
);
904 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container
);
905 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container
);
906 SetUsesScreenCoordinates(always_on_top_container
);
908 aura::Window
* docked_container
= CreateContainer(
909 kShellWindowId_DockedContainer
,
911 non_lock_screen_containers
);
912 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container
);
913 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container
);
914 SetUsesScreenCoordinates(docked_container
);
915 SetUsesEasyResizeTargeter(docked_container
);
917 aura::Window
* shelf_container
=
918 CreateContainer(kShellWindowId_ShelfContainer
,
920 non_lock_screen_containers
);
921 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container
);
922 SetUsesScreenCoordinates(shelf_container
);
923 DescendantShouldStayInSameRootWindow(shelf_container
);
925 aura::Window
* panel_container
= CreateContainer(
926 kShellWindowId_PanelContainer
,
928 non_lock_screen_containers
);
929 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container
);
930 SetUsesScreenCoordinates(panel_container
);
932 aura::Window
* shelf_bubble_container
=
933 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
934 "ShelfBubbleContainer",
935 non_lock_screen_containers
);
936 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container
);
937 SetUsesScreenCoordinates(shelf_bubble_container
);
938 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
940 aura::Window
* app_list_container
=
941 CreateContainer(kShellWindowId_AppListContainer
,
943 non_lock_screen_containers
);
944 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container
);
945 SetUsesScreenCoordinates(app_list_container
);
947 aura::Window
* modal_container
= CreateContainer(
948 kShellWindowId_SystemModalContainer
,
949 "SystemModalContainer",
950 non_lock_screen_containers
);
951 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container
);
952 modal_container
->SetLayoutManager(
953 new SystemModalContainerLayoutManager(modal_container
));
954 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container
);
955 SetUsesScreenCoordinates(modal_container
);
956 SetUsesEasyResizeTargeter(modal_container
);
958 // TODO(beng): Figure out if we can make this use
959 // SystemModalContainerEventFilter instead of stops_event_propagation.
960 aura::Window
* lock_container
= CreateContainer(
961 kShellWindowId_LockScreenContainer
,
962 "LockScreenContainer",
963 lock_screen_containers
);
964 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container
);
965 if (CommandLine::ForCurrentProcess()->HasSwitch(
966 switches::kAshDisableLockLayoutManager
)) {
967 lock_container
->SetLayoutManager(
968 new WorkspaceLayoutManager(lock_container
));
970 lock_container
->SetLayoutManager(new LockLayoutManager(lock_container
));
972 SetUsesScreenCoordinates(lock_container
);
973 // TODO(beng): stopsevents
975 aura::Window
* lock_modal_container
= CreateContainer(
976 kShellWindowId_LockSystemModalContainer
,
977 "LockSystemModalContainer",
978 lock_screen_containers
);
979 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container
);
980 lock_modal_container
->SetLayoutManager(
981 new SystemModalContainerLayoutManager(lock_modal_container
));
982 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
983 SetUsesScreenCoordinates(lock_modal_container
);
984 SetUsesEasyResizeTargeter(lock_modal_container
);
986 aura::Window
* status_container
=
987 CreateContainer(kShellWindowId_StatusContainer
,
989 lock_screen_related_containers
);
990 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container
);
991 SetUsesScreenCoordinates(status_container
);
992 DescendantShouldStayInSameRootWindow(status_container
);
994 aura::Window
* settings_bubble_container
= CreateContainer(
995 kShellWindowId_SettingBubbleContainer
,
996 "SettingBubbleContainer",
997 lock_screen_related_containers
);
998 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container
);
999 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container
);
1000 SetUsesScreenCoordinates(settings_bubble_container
);
1001 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
1003 aura::Window
* virtual_keyboard_parent_container
=
1004 CreateContainer(kShellWindowId_ImeWindowParentContainer
,
1005 "VirtualKeyboardParentContainer",
1006 lock_screen_related_containers
);
1007 wm::SetSnapsChildrenToPhysicalPixelBoundary(
1008 virtual_keyboard_parent_container
);
1009 virtual_keyboard_parent_container
->SetLayoutManager(
1010 new VirtualKeyboardContainerLayoutManager(
1011 virtual_keyboard_parent_container
));
1012 SetUsesScreenCoordinates(virtual_keyboard_parent_container
);
1014 aura::Window
* menu_container
= CreateContainer(
1015 kShellWindowId_MenuContainer
,
1017 lock_screen_related_containers
);
1018 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container
);
1019 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container
);
1020 SetUsesScreenCoordinates(menu_container
);
1022 aura::Window
* drag_drop_container
= CreateContainer(
1023 kShellWindowId_DragImageAndTooltipContainer
,
1024 "DragImageAndTooltipContainer",
1025 lock_screen_related_containers
);
1026 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
1027 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container
);
1028 SetUsesScreenCoordinates(drag_drop_container
);
1030 aura::Window
* overlay_container
= CreateContainer(
1031 kShellWindowId_OverlayContainer
,
1033 lock_screen_related_containers
);
1034 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container
);
1035 SetUsesScreenCoordinates(overlay_container
);
1037 #if defined(OS_CHROMEOS)
1038 aura::Window
* mouse_cursor_container
= CreateContainer(
1039 kShellWindowId_MouseCursorContainer
,
1040 "MouseCursorContainer",
1042 SetUsesScreenCoordinates(mouse_cursor_container
);
1045 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
1046 "PowerButtonAnimationContainer", root_window
);
1049 void RootWindowController::EnableTouchHudProjection() {
1050 if (touch_hud_projection_
)
1052 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1055 void RootWindowController::DisableTouchHudProjection() {
1056 if (!touch_hud_projection_
)
1058 touch_hud_projection_
->Remove();
1061 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
1062 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
1065 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
1067 EnableTouchHudProjection();
1069 DisableTouchHudProjection();
1072 RootWindowController
* GetRootWindowController(
1073 const aura::Window
* root_window
) {
1074 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;