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/panels/attached_panel_window_targeter.h"
38 #include "ash/wm/panels/panel_layout_manager.h"
39 #include "ash/wm/panels/panel_window_event_handler.h"
40 #include "ash/wm/root_window_layout_manager.h"
41 #include "ash/wm/screen_dimmer.h"
42 #include "ash/wm/stacking_controller.h"
43 #include "ash/wm/status_area_layout_manager.h"
44 #include "ash/wm/system_background_controller.h"
45 #include "ash/wm/system_modal_container_layout_manager.h"
46 #include "ash/wm/window_properties.h"
47 #include "ash/wm/window_state.h"
48 #include "ash/wm/window_util.h"
49 #include "ash/wm/workspace/workspace_layout_manager.h"
50 #include "ash/wm/workspace_controller.h"
51 #include "base/command_line.h"
52 #include "base/time/time.h"
53 #include "ui/aura/client/aura_constants.h"
54 #include "ui/aura/client/screen_position_client.h"
55 #include "ui/aura/window.h"
56 #include "ui/aura/window_delegate.h"
57 #include "ui/aura/window_event_dispatcher.h"
58 #include "ui/aura/window_observer.h"
59 #include "ui/aura/window_tracker.h"
60 #include "ui/base/hit_test.h"
61 #include "ui/base/models/menu_model.h"
62 #include "ui/gfx/display.h"
63 #include "ui/gfx/screen.h"
64 #include "ui/keyboard/keyboard_controller.h"
65 #include "ui/keyboard/keyboard_util.h"
66 #include "ui/views/controls/menu/menu_runner.h"
67 #include "ui/views/view_model.h"
68 #include "ui/views/view_model_utils.h"
69 #include "ui/wm/core/capture_controller.h"
70 #include "ui/wm/core/easy_resize_window_targeter.h"
71 #include "ui/wm/core/visibility_controller.h"
72 #include "ui/wm/core/window_util.h"
73 #include "ui/wm/public/drag_drop_client.h"
74 #include "ui/wm/public/tooltip_client.h"
75 #include "ui/wm/public/window_types.h"
77 #if defined(OS_CHROMEOS)
78 #include "ash/system/tray_accessibility.h"
79 #include "ash/wm/boot_splash_screen_chromeos.h"
80 #include "ui/chromeos/touch_exploration_controller.h"
86 #if defined(OS_CHROMEOS)
87 // Duration for the animation that hides the boot splash screen, in
88 // milliseconds. This should be short enough in relation to
89 // wm/window_animation.cc's brightness/grayscale fade animation that the login
90 // background image animation isn't hidden by the splash screen animation.
91 const int kBootSplashScreenHideDurationMs
= 500;
94 // Creates a new window for use as a container.
95 aura::Window
* CreateContainer(int window_id
,
97 aura::Window
* parent
) {
98 aura::Window
* container
= new aura::Window(NULL
);
99 container
->set_id(window_id
);
100 container
->SetName(name
);
101 container
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
102 parent
->AddChild(container
);
103 if (window_id
!= kShellWindowId_UnparentedControlContainer
)
108 float ToRelativeValue(int value
, int src
, int dst
) {
109 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
112 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
113 const gfx::Size
& dst_size
,
114 gfx::Rect
* bounds_in_out
) {
115 gfx::Point origin
= bounds_in_out
->origin();
116 bounds_in_out
->set_origin(gfx::Point(
117 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
118 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
121 // Reparents |window| to |new_parent|.
122 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
123 const gfx::Size src_size
= window
->parent()->bounds().size();
124 const gfx::Size dst_size
= new_parent
->bounds().size();
125 // Update the restore bounds to make it relative to the display.
126 wm::WindowState
* state
= wm::GetWindowState(window
);
127 gfx::Rect restore_bounds
;
128 bool has_restore_bounds
= state
->HasRestoreBounds();
130 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
131 new_parent
->id() != kShellWindowId_DockedContainer
;
132 gfx::Rect local_bounds
;
134 local_bounds
= state
->window()->bounds();
135 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
138 if (has_restore_bounds
) {
139 restore_bounds
= state
->GetRestoreBoundsInParent();
140 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
143 new_parent
->AddChild(window
);
145 // Docked windows have bounds handled by the layout manager in AddChild().
147 window
->SetBounds(local_bounds
);
149 if (has_restore_bounds
)
150 state
->SetRestoreBoundsInParent(restore_bounds
);
153 // Reparents the appropriate set of windows from |src| to |dst|.
154 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
155 // Set of windows to move.
156 const int kContainerIdsToMove
[] = {
157 kShellWindowId_DefaultContainer
,
158 kShellWindowId_DockedContainer
,
159 kShellWindowId_PanelContainer
,
160 kShellWindowId_AlwaysOnTopContainer
,
161 kShellWindowId_SystemModalContainer
,
162 kShellWindowId_LockSystemModalContainer
,
163 kShellWindowId_UnparentedControlContainer
, };
164 for (size_t i
= 0; i
< arraysize(kContainerIdsToMove
); i
++) {
165 int id
= kContainerIdsToMove
[i
];
166 aura::Window
* src_container
= Shell::GetContainer(src
, id
);
167 aura::Window
* dst_container
= Shell::GetContainer(dst
, id
);
168 while (!src_container
->children().empty()) {
169 // Restart iteration from the source container windows each time as they
170 // may change as a result of moving other windows.
171 aura::Window::Windows::const_iterator iter
=
172 src_container
->children().begin();
173 while (iter
!= src_container
->children().end() &&
174 SystemModalContainerLayoutManager::IsModalBackground(*iter
)) {
177 // If the entire window list is modal background windows then stop.
178 if (iter
== src_container
->children().end())
180 ReparentWindow(*iter
, dst_container
);
185 // Mark the container window so that a widget added to this container will
186 // use the virtual screeen coordinates instead of parent.
187 void SetUsesScreenCoordinates(aura::Window
* container
) {
188 container
->SetProperty(kUsesScreenCoordinatesKey
, true);
191 // Mark the container window so that a widget added to this container will
192 // say in the same root window regardless of the bounds specified.
193 void DescendantShouldStayInSameRootWindow(aura::Window
* container
) {
194 container
->SetProperty(kStayInSameRootWindowKey
, true);
197 void SetUsesEasyResizeTargeter(aura::Window
* container
) {
198 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
199 -kResizeOutsideBoundsSize
,
200 -kResizeOutsideBoundsSize
,
201 -kResizeOutsideBoundsSize
);
202 gfx::Insets touch_extend
= mouse_extend
.Scale(
203 kResizeOutsideBoundsScaleForTouch
);
204 container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
205 new ::wm::EasyResizeWindowTargeter(container
, mouse_extend
,
209 // A window delegate which does nothing. Used to create a window that
210 // is a event target, but do nothing.
211 class EmptyWindowDelegate
: public aura::WindowDelegate
{
213 EmptyWindowDelegate() {}
214 virtual ~EmptyWindowDelegate() {}
216 // aura::WindowDelegate overrides:
217 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
220 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
223 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
224 const gfx::Rect
& new_bounds
) OVERRIDE
{
226 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
227 return gfx::kNullCursor
;
229 virtual int GetNonClientComponent(
230 const gfx::Point
& point
) const OVERRIDE
{
233 virtual bool ShouldDescendIntoChildForEventHandling(
235 const gfx::Point
& location
) OVERRIDE
{
238 virtual bool CanFocus() OVERRIDE
{
241 virtual void OnCaptureLost() OVERRIDE
{
243 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
245 virtual void OnDeviceScaleFactorChanged(
246 float device_scale_factor
) OVERRIDE
{
248 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
249 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{
252 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{
254 virtual bool HasHitTestMask() const OVERRIDE
{
257 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
260 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
263 #if defined(OS_CHROMEOS)
264 // Responsible for initializing TouchExplorationController when spoken
266 class CrosAccessibilityObserver
: public AccessibilityObserver
{
268 explicit CrosAccessibilityObserver(
269 RootWindowController
* root_window_controller
)
270 : root_window_controller_(root_window_controller
) {
271 Shell::GetInstance()->system_tray_notifier()->
272 AddAccessibilityObserver(this);
273 UpdateTouchExplorationState();
276 virtual ~CrosAccessibilityObserver() {
277 SystemTrayNotifier
* system_tray_notifier
=
278 Shell::GetInstance()->system_tray_notifier();
279 if (system_tray_notifier
)
280 system_tray_notifier
->RemoveAccessibilityObserver(this);
284 void UpdateTouchExplorationState() {
285 AccessibilityDelegate
* delegate
=
286 Shell::GetInstance()->accessibility_delegate();
287 bool enabled
= delegate
->IsSpokenFeedbackEnabled();
289 if (enabled
&& !touch_exploration_controller_
.get()) {
290 touch_exploration_controller_
.reset(
291 new ui::TouchExplorationController(
292 root_window_controller_
->GetRootWindow()));
293 } else if (!enabled
) {
294 touch_exploration_controller_
.reset();
298 // Overridden from AccessibilityObserver.
299 virtual void OnAccessibilityModeChanged(
300 AccessibilityNotificationVisibility notify
) OVERRIDE
{
301 UpdateTouchExplorationState();
304 scoped_ptr
<ui::TouchExplorationController
> touch_exploration_controller_
;
305 RootWindowController
* root_window_controller_
;
307 DISALLOW_COPY_AND_ASSIGN(CrosAccessibilityObserver
);
309 #endif // OS_CHROMEOS
313 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost
* host
) {
314 RootWindowController
* controller
= new RootWindowController(host
);
315 controller
->Init(RootWindowController::PRIMARY
,
316 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
319 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost
* host
) {
320 RootWindowController
* controller
= new RootWindowController(host
);
321 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
324 void RootWindowController::CreateForVirtualKeyboardDisplay(
325 AshWindowTreeHost
* host
) {
326 RootWindowController
* controller
= new RootWindowController(host
);
327 controller
->Init(RootWindowController::VIRTUAL_KEYBOARD
,
328 false /* first run */);
332 RootWindowController
* RootWindowController::ForShelf(aura::Window
* window
) {
333 return GetRootWindowController(window
->GetRootWindow());
337 RootWindowController
* RootWindowController::ForWindow(
338 const aura::Window
* window
) {
339 return GetRootWindowController(window
->GetRootWindow());
343 RootWindowController
* RootWindowController::ForTargetRootWindow() {
344 return GetRootWindowController(Shell::GetTargetRootWindow());
348 aura::Window
* RootWindowController::GetContainerForWindow(
349 aura::Window
* window
) {
350 aura::Window
* container
= window
->parent();
351 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
352 container
= container
->parent();
356 RootWindowController::~RootWindowController() {
359 // The CaptureClient needs to be around for as long as the RootWindow is
361 capture_client_
.reset();
364 aura::WindowTreeHost
* RootWindowController::GetHost() {
365 return ash_host_
->AsWindowTreeHost();
368 const aura::WindowTreeHost
* RootWindowController::GetHost() const {
369 return ash_host_
->AsWindowTreeHost();
372 aura::Window
* RootWindowController::GetRootWindow() {
373 return GetHost()->window();
376 const aura::Window
* RootWindowController::GetRootWindow() const {
377 return GetHost()->window();
380 void RootWindowController::SetWallpaperController(
381 DesktopBackgroundWidgetController
* controller
) {
382 wallpaper_controller_
.reset(controller
);
385 void RootWindowController::SetAnimatingWallpaperController(
386 AnimatingDesktopController
* controller
) {
387 if (animating_wallpaper_controller_
.get())
388 animating_wallpaper_controller_
->StopAnimating();
389 animating_wallpaper_controller_
.reset(controller
);
392 void RootWindowController::Shutdown() {
393 Shell
* shell
= Shell::GetInstance();
394 shell
->RemoveShellObserver(this);
396 #if defined(OS_CHROMEOS)
397 if (cros_accessibility_observer_
) {
398 cros_accessibility_observer_
.reset();
402 if (animating_wallpaper_controller_
.get())
403 animating_wallpaper_controller_
->StopAnimating();
404 wallpaper_controller_
.reset();
405 animating_wallpaper_controller_
.reset();
406 aura::Window
* root_window
= GetRootWindow();
407 // Change the target root window before closing child windows. If any child
408 // being removed triggers a relayout of the shelf it will try to build a
409 // window list adding windows from the target root window's containers which
410 // may have already gone away.
411 if (Shell::GetTargetRootWindow() == root_window
) {
412 shell
->set_target_root_window(
413 Shell::GetPrimaryRootWindow() == root_window
415 : Shell::GetPrimaryRootWindow());
419 GetRootWindowSettings(root_window
)->controller
= NULL
;
420 screen_dimmer_
.reset();
421 workspace_controller_
.reset();
422 // Forget with the display ID so that display lookup
423 // ends up with invalid display.
424 GetRootWindowSettings(root_window
)->display_id
=
425 gfx::Display::kInvalidDisplayID
;
426 GetRootWindowSettings(root_window
)->shutdown
= true;
428 system_background_
.reset();
429 aura::client::SetScreenPositionClient(root_window
, NULL
);
432 SystemModalContainerLayoutManager
*
433 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
434 aura::Window
* modal_container
= NULL
;
436 aura::Window
* window_container
= GetContainerForWindow(window
);
437 if (window_container
&&
438 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
439 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
441 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
444 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
445 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
446 kShellWindowId_SystemModalContainer
;
447 modal_container
= GetContainer(modal_window_id
);
449 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
450 modal_container
->layout_manager()) : NULL
;
453 aura::Window
* RootWindowController::GetContainer(int container_id
) {
454 return GetRootWindow()->GetChildById(container_id
);
457 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
458 return ash_host_
->AsWindowTreeHost()->window()->GetChildById(container_id
);
461 void RootWindowController::ShowShelf() {
462 if (!shelf_
->shelf())
464 shelf_
->shelf()->SetVisible(true);
465 shelf_
->status_area_widget()->Show();
468 void RootWindowController::OnShelfCreated() {
469 if (panel_layout_manager_
)
470 panel_layout_manager_
->SetShelf(shelf_
->shelf());
471 if (docked_layout_manager_
) {
472 docked_layout_manager_
->SetShelf(shelf_
->shelf());
473 if (shelf_
->shelf_layout_manager())
474 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
477 // Notify shell observers that the shelf has been created.
478 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
481 void RootWindowController::UpdateAfterLoginStatusChange(
482 user::LoginStatus status
) {
483 if (status
!= user::LOGGED_IN_NONE
)
484 mouse_event_target_
.reset();
485 if (shelf_
->status_area_widget())
486 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
489 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
490 #if defined(OS_CHROMEOS)
491 if (CommandLine::ForCurrentProcess()->HasSwitch(
492 switches::kAshAnimateFromBootSplashScreen
) &&
493 boot_splash_screen_
.get()) {
494 // Make the splash screen fade out so it doesn't obscure the desktop
495 // wallpaper's brightness/grayscale animation.
496 boot_splash_screen_
->StartHideAnimation(
497 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
502 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
503 // Make sure the wallpaper is visible.
504 system_background_
->SetColor(SK_ColorBLACK
);
505 #if defined(OS_CHROMEOS)
506 boot_splash_screen_
.reset();
509 Shell::GetInstance()->user_wallpaper_delegate()->
510 OnWallpaperAnimationFinished();
511 // Only removes old component when wallpaper animation finished. If we
512 // remove the old one before the new wallpaper is done fading in there will
513 // be a white flash during the animation.
514 if (animating_wallpaper_controller()) {
515 DesktopBackgroundWidgetController
* controller
=
516 animating_wallpaper_controller()->GetController(true);
517 // |desktop_widget_| should be the same animating widget we try to move
518 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
519 // before move it to |kDesktopController|.
520 DCHECK_EQ(controller
->widget(), widget
);
521 // Release the old controller and close its background widget.
522 SetWallpaperController(controller
);
526 void RootWindowController::CloseChildWindows() {
527 mouse_event_target_
.reset();
529 // Deactivate keyboard container before closing child windows and shutting
530 // down associated layout managers.
531 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
533 // panel_layout_manager_ needs to be shut down before windows are destroyed.
534 if (panel_layout_manager_
) {
535 panel_layout_manager_
->Shutdown();
536 panel_layout_manager_
= NULL
;
538 // docked_layout_manager_ needs to be shut down before windows are destroyed.
539 if (docked_layout_manager_
) {
540 if (shelf_
&& shelf_
->shelf_layout_manager())
541 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
542 docked_layout_manager_
->Shutdown();
543 docked_layout_manager_
= NULL
;
545 aura::Window
* root_window
= GetRootWindow();
546 aura::client::SetDragDropClient(root_window
, NULL
);
548 // TODO(harrym): Remove when Status Area Widget is a child view.
550 shelf_
->ShutdownStatusAreaWidget();
552 if (shelf_
->shelf_layout_manager())
553 shelf_
->shelf_layout_manager()->PrepareForShutdown();
556 // Close background widget first as it depends on tooltip.
557 wallpaper_controller_
.reset();
558 animating_wallpaper_controller_
.reset();
560 workspace_controller_
.reset();
561 aura::client::SetTooltipClient(root_window
, NULL
);
563 // Explicitly destroy top level windows. We do this as during part of
564 // destruction such windows may query the RootWindow for state.
565 std::queue
<aura::Window
*> non_toplevel_windows
;
566 non_toplevel_windows
.push(root_window
);
567 while (!non_toplevel_windows
.empty()) {
568 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
569 non_toplevel_windows
.pop();
570 aura::WindowTracker toplevel_windows
;
571 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
572 aura::Window
* child
= non_toplevel_window
->children()[i
];
573 if (!child
->owned_by_parent())
575 if (child
->delegate())
576 toplevel_windows
.Add(child
);
578 non_toplevel_windows
.push(child
);
580 while (!toplevel_windows
.windows().empty())
581 delete *toplevel_windows
.windows().begin();
583 // And then remove the containers.
584 while (!root_window
->children().empty()) {
585 aura::Window
* window
= root_window
->children()[0];
586 if (window
->owned_by_parent()) {
589 root_window
->RemoveChild(window
);
596 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
597 // Forget the shelf early so that shelf don't update itself using wrong
599 workspace_controller_
->SetShelf(NULL
);
600 ReparentAllWindows(GetRootWindow(), dst
);
603 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
604 return shelf_
->shelf_layout_manager();
607 SystemTray
* RootWindowController::GetSystemTray() {
608 // We assume in throughout the code that this will not return NULL. If code
609 // triggers this for valid reasons, it should test status_area_widget first.
610 CHECK(shelf_
->status_area_widget());
611 return shelf_
->status_area_widget()->system_tray();
614 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
615 ui::MenuSourceType source_type
) {
616 DCHECK(Shell::GetInstance()->delegate());
617 scoped_ptr
<ui::MenuModel
> menu_model(
618 Shell::GetInstance()->delegate()->CreateContextMenu(
619 GetRootWindow(), NULL
, NULL
));
623 // Background controller may not be set yet if user clicked on status are
624 // before initial animation completion. See crbug.com/222218
625 if (!wallpaper_controller_
.get())
628 views::MenuRunner
menu_runner(menu_model
.get());
629 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
631 gfx::Rect(location_in_screen
, gfx::Size()),
632 views::MENU_ANCHOR_TOPLEFT
,
634 views::MenuRunner::CONTEXT_MENU
) ==
635 views::MenuRunner::MENU_DELETED
) {
639 Shell::GetInstance()->UpdateShelfVisibility();
642 void RootWindowController::UpdateShelfVisibility() {
643 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
646 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
647 const aura::Window
* topmost_window
= NULL
;
648 const aura::Window
* active_window
= wm::GetActiveWindow();
649 if (active_window
&& active_window
->GetRootWindow() == GetRootWindow() &&
650 IsSwitchableContainer(active_window
->parent())) {
651 // Use the active window when it is on the current root window to determine
652 // the fullscreen state to allow temporarily using a panel or docked window
653 // (which are always above the default container) while a fullscreen
654 // window is open. We only use the active window when in a switchable
655 // container as the launcher should not exit fullscreen mode.
656 topmost_window
= active_window
;
658 // Otherwise, use the topmost window on the root window's default container
659 // when there is no active window on this root window.
660 const aura::Window::Windows
& windows
=
661 GetContainer(kShellWindowId_DefaultContainer
)->children();
662 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
663 iter
!= windows
.rend(); ++iter
) {
664 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
665 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
666 (*iter
)->layer()->GetTargetVisibility()) {
667 topmost_window
= *iter
;
672 while (topmost_window
) {
673 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
674 return topmost_window
;
675 topmost_window
= ::wm::GetTransientParent(topmost_window
);
680 void RootWindowController::ActivateKeyboard(
681 keyboard::KeyboardController
* keyboard_controller
) {
682 if (!keyboard::IsKeyboardEnabled() ||
683 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
686 DCHECK(keyboard_controller
);
687 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
688 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
689 keyboard_controller
->AddObserver(panel_layout_manager_
);
690 keyboard_controller
->AddObserver(docked_layout_manager_
);
691 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
693 aura::Window
* parent
= GetContainer(
694 kShellWindowId_VirtualKeyboardParentContainer
);
696 aura::Window
* keyboard_container
=
697 keyboard_controller
->GetContainerWindow();
698 keyboard_container
->set_id(kShellWindowId_VirtualKeyboardContainer
);
699 parent
->AddChild(keyboard_container
);
700 // TODO(oshima): Bounds of keyboard container should be handled by
701 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
702 keyboard_container
->SetBounds(parent
->bounds());
705 void RootWindowController::DeactivateKeyboard(
706 keyboard::KeyboardController
* keyboard_controller
) {
707 if (!keyboard_controller
||
708 !keyboard_controller
->keyboard_container_initialized()) {
711 aura::Window
* keyboard_container
=
712 keyboard_controller
->GetContainerWindow();
713 if (keyboard_container
->GetRootWindow() == GetRootWindow()) {
714 aura::Window
* parent
= GetContainer(
715 kShellWindowId_VirtualKeyboardParentContainer
);
717 parent
->RemoveChild(keyboard_container
);
718 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
719 // Virtual keyboard may be deactivated while still showing, notify all
720 // observers that keyboard bounds changed to 0 before remove them.
721 keyboard_controller
->NotifyKeyboardBoundsChanging(gfx::Rect());
722 keyboard_controller
->RemoveObserver(shelf()->shelf_layout_manager());
723 keyboard_controller
->RemoveObserver(panel_layout_manager_
);
724 keyboard_controller
->RemoveObserver(docked_layout_manager_
);
725 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
730 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window
* window
) {
731 aura::Window
* parent
= GetContainer(
732 kShellWindowId_VirtualKeyboardParentContainer
);
733 return parent
? parent
->Contains(window
) : false;
736 ////////////////////////////////////////////////////////////////////////////////
737 // RootWindowController, private:
739 RootWindowController::RootWindowController(AshWindowTreeHost
* ash_host
)
740 : ash_host_(ash_host
),
741 root_window_layout_(NULL
),
742 docked_layout_manager_(NULL
),
743 panel_layout_manager_(NULL
),
744 touch_hud_debug_(NULL
),
745 touch_hud_projection_(NULL
) {
746 aura::Window
* root_window
= GetRootWindow();
747 GetRootWindowSettings(root_window
)->controller
= this;
748 screen_dimmer_
.reset(new ScreenDimmer(root_window
));
750 stacking_controller_
.reset(new StackingController
);
751 aura::client::SetWindowTreeClient(root_window
, stacking_controller_
.get());
752 capture_client_
.reset(new ::wm::ScopedCaptureClient(root_window
));
755 void RootWindowController::Init(RootWindowType root_window_type
,
756 bool first_run_after_boot
) {
757 aura::Window
* root_window
= GetRootWindow();
758 Shell
* shell
= Shell::GetInstance();
759 shell
->InitRootWindow(root_window
);
761 ash_host_
->AsWindowTreeHost()->SetCursor(ui::kCursorPointer
);
762 CreateContainersInRootWindow(root_window
);
764 if (root_window_type
== VIRTUAL_KEYBOARD
) {
765 aura::Window
* virtual_keyboard_parent_container
= GetContainer(
766 kShellWindowId_VirtualKeyboardParentContainer
);
767 virtual_keyboard_parent_container
->SetBounds(root_window
->bounds());
768 shell
->InitKeyboard();
772 CreateSystemBackground(first_run_after_boot
);
774 InitLayoutManagers();
777 if (Shell::GetPrimaryRootWindowController()->
778 GetSystemModalLayoutManager(NULL
)->has_modal_background()) {
779 GetSystemModalLayoutManager(NULL
)->CreateModalBackground();
782 shell
->AddShellObserver(this);
784 if (root_window_type
== PRIMARY
) {
785 root_window_layout()->OnWindowResized();
786 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
787 shell
->InitKeyboard();
789 root_window_layout()->OnWindowResized();
790 ash_host_
->AsWindowTreeHost()->Show();
792 // Create a shelf if a user is already logged in.
793 if (shell
->session_state_delegate()->NumberOfLoggedInUsers())
794 shelf()->CreateShelf();
796 // Notify shell observers about new root window.
797 shell
->OnRootWindowAdded(root_window
);
800 #if defined(OS_CHROMEOS)
801 if (CommandLine::ForCurrentProcess()->HasSwitch(
802 switches::kAshEnableTouchExplorationMode
)) {
803 cros_accessibility_observer_
.reset(new CrosAccessibilityObserver(this));
808 void RootWindowController::InitLayoutManagers() {
809 aura::Window
* root_window
= GetRootWindow();
810 root_window_layout_
= new RootWindowLayoutManager(root_window
);
811 root_window
->SetLayoutManager(root_window_layout_
);
813 aura::Window
* default_container
=
814 GetContainer(kShellWindowId_DefaultContainer
);
815 // Workspace manager has its own layout managers.
816 workspace_controller_
.reset(
817 new WorkspaceController(default_container
));
819 aura::Window
* always_on_top_container
=
820 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
821 always_on_top_container
->SetLayoutManager(
822 new WorkspaceLayoutManager(always_on_top_container
));
823 always_on_top_controller_
.reset(new AlwaysOnTopController
);
824 always_on_top_controller_
->SetAlwaysOnTopContainer(always_on_top_container
);
826 DCHECK(!shelf_
.get());
827 aura::Window
* shelf_container
= GetContainer(kShellWindowId_ShelfContainer
);
828 // TODO(harrym): Remove when status area is view.
829 aura::Window
* status_container
= GetContainer(kShellWindowId_StatusContainer
);
830 shelf_
.reset(new ShelfWidget(
831 shelf_container
, status_container
, workspace_controller()));
833 if (!Shell::GetInstance()->session_state_delegate()->
834 IsActiveUserSessionStarted()) {
835 // This window exists only to be a event target on login screen.
836 // It does not have to handle events, nor be visible.
837 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
838 mouse_event_target_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
840 aura::Window
* lock_background_container
=
841 GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
842 lock_background_container
->AddChild(mouse_event_target_
.get());
843 mouse_event_target_
->Show();
846 // Create Docked windows layout manager
847 aura::Window
* docked_container
= GetContainer(kShellWindowId_DockedContainer
);
848 docked_layout_manager_
=
849 new DockedWindowLayoutManager(docked_container
, workspace_controller());
850 docked_container
->SetLayoutManager(docked_layout_manager_
);
852 // Create Panel layout manager
853 aura::Window
* panel_container
= GetContainer(kShellWindowId_PanelContainer
);
854 panel_layout_manager_
= new PanelLayoutManager(panel_container
);
855 panel_container
->SetLayoutManager(panel_layout_manager_
);
856 panel_container_handler_
.reset(new PanelWindowEventHandler
);
857 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
859 // Install an AttachedPanelWindowTargeter on the panel container to make it
860 // easier to correctly target shelf buttons with touch.
861 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
862 -kResizeOutsideBoundsSize
,
863 -kResizeOutsideBoundsSize
,
864 -kResizeOutsideBoundsSize
);
865 gfx::Insets touch_extend
= mouse_extend
.Scale(
866 kResizeOutsideBoundsScaleForTouch
);
867 panel_container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
868 new AttachedPanelWindowTargeter(panel_container
,
871 panel_layout_manager_
)));
874 void RootWindowController::InitTouchHuds() {
875 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
876 if (command_line
->HasSwitch(switches::kAshTouchHud
))
877 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
878 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
879 EnableTouchHudProjection();
882 void RootWindowController::CreateSystemBackground(
883 bool is_first_run_after_boot
) {
884 SkColor color
= SK_ColorBLACK
;
885 #if defined(OS_CHROMEOS)
886 if (is_first_run_after_boot
)
887 color
= kChromeOsBootColor
;
889 system_background_
.reset(
890 new SystemBackgroundController(GetRootWindow(), color
));
892 #if defined(OS_CHROMEOS)
893 // Make a copy of the system's boot splash screen so we can composite it
894 // onscreen until the desktop background is ready.
895 if (is_first_run_after_boot
&&
896 (CommandLine::ForCurrentProcess()->HasSwitch(
897 switches::kAshCopyHostBackgroundAtBoot
) ||
898 CommandLine::ForCurrentProcess()->HasSwitch(
899 switches::kAshAnimateFromBootSplashScreen
)))
900 boot_splash_screen_
.reset(new BootSplashScreen(GetHost()));
904 void RootWindowController::CreateContainersInRootWindow(
905 aura::Window
* root_window
) {
906 // These containers are just used by PowerButtonController to animate groups
907 // of containers simultaneously without messing up the current transformations
908 // on those containers. These are direct children of the root window; all of
909 // the other containers are their children.
911 // The desktop background container is not part of the lock animation, so it
912 // is not included in those animate groups.
913 // When screen is locked desktop background is moved to lock screen background
914 // container (moved back on unlock). We want to make sure that there's an
915 // opaque layer occluding the non-lock-screen layers.
916 aura::Window
* desktop_background_container
= CreateContainer(
917 kShellWindowId_DesktopBackgroundContainer
,
918 "DesktopBackgroundContainer",
920 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container
);
922 aura::Window
* non_lock_screen_containers
= CreateContainer(
923 kShellWindowId_NonLockScreenContainersContainer
,
924 "NonLockScreenContainersContainer",
927 aura::Window
* lock_background_containers
= CreateContainer(
928 kShellWindowId_LockScreenBackgroundContainer
,
929 "LockScreenBackgroundContainer",
931 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers
);
933 aura::Window
* lock_screen_containers
= CreateContainer(
934 kShellWindowId_LockScreenContainersContainer
,
935 "LockScreenContainersContainer",
937 aura::Window
* lock_screen_related_containers
= CreateContainer(
938 kShellWindowId_LockScreenRelatedContainersContainer
,
939 "LockScreenRelatedContainersContainer",
942 CreateContainer(kShellWindowId_UnparentedControlContainer
,
943 "UnparentedControlContainer",
944 non_lock_screen_containers
);
946 aura::Window
* default_container
= CreateContainer(
947 kShellWindowId_DefaultContainer
,
949 non_lock_screen_containers
);
950 ::wm::SetChildWindowVisibilityChangesAnimated(default_container
);
951 SetUsesScreenCoordinates(default_container
);
952 SetUsesEasyResizeTargeter(default_container
);
954 aura::Window
* always_on_top_container
= CreateContainer(
955 kShellWindowId_AlwaysOnTopContainer
,
956 "AlwaysOnTopContainer",
957 non_lock_screen_containers
);
958 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container
);
959 SetUsesScreenCoordinates(always_on_top_container
);
961 aura::Window
* docked_container
= CreateContainer(
962 kShellWindowId_DockedContainer
,
964 non_lock_screen_containers
);
965 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container
);
966 SetUsesScreenCoordinates(docked_container
);
967 SetUsesEasyResizeTargeter(docked_container
);
969 aura::Window
* shelf_container
=
970 CreateContainer(kShellWindowId_ShelfContainer
,
972 non_lock_screen_containers
);
973 SetUsesScreenCoordinates(shelf_container
);
974 DescendantShouldStayInSameRootWindow(shelf_container
);
976 aura::Window
* panel_container
= CreateContainer(
977 kShellWindowId_PanelContainer
,
979 non_lock_screen_containers
);
980 SetUsesScreenCoordinates(panel_container
);
982 aura::Window
* shelf_bubble_container
=
983 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
984 "ShelfBubbleContainer",
985 non_lock_screen_containers
);
986 SetUsesScreenCoordinates(shelf_bubble_container
);
987 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
989 aura::Window
* app_list_container
=
990 CreateContainer(kShellWindowId_AppListContainer
,
992 non_lock_screen_containers
);
993 SetUsesScreenCoordinates(app_list_container
);
995 aura::Window
* modal_container
= CreateContainer(
996 kShellWindowId_SystemModalContainer
,
997 "SystemModalContainer",
998 non_lock_screen_containers
);
999 modal_container
->SetLayoutManager(
1000 new SystemModalContainerLayoutManager(modal_container
));
1001 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container
);
1002 SetUsesScreenCoordinates(modal_container
);
1003 SetUsesEasyResizeTargeter(modal_container
);
1005 // TODO(beng): Figure out if we can make this use
1006 // SystemModalContainerEventFilter instead of stops_event_propagation.
1007 aura::Window
* lock_container
= CreateContainer(
1008 kShellWindowId_LockScreenContainer
,
1009 "LockScreenContainer",
1010 lock_screen_containers
);
1011 lock_container
->SetLayoutManager(new WorkspaceLayoutManager(lock_container
));
1012 SetUsesScreenCoordinates(lock_container
);
1013 // TODO(beng): stopsevents
1015 aura::Window
* lock_modal_container
= CreateContainer(
1016 kShellWindowId_LockSystemModalContainer
,
1017 "LockSystemModalContainer",
1018 lock_screen_containers
);
1019 lock_modal_container
->SetLayoutManager(
1020 new SystemModalContainerLayoutManager(lock_modal_container
));
1021 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
1022 SetUsesScreenCoordinates(lock_modal_container
);
1023 SetUsesEasyResizeTargeter(lock_modal_container
);
1025 aura::Window
* status_container
=
1026 CreateContainer(kShellWindowId_StatusContainer
,
1028 lock_screen_related_containers
);
1029 SetUsesScreenCoordinates(status_container
);
1030 DescendantShouldStayInSameRootWindow(status_container
);
1032 aura::Window
* settings_bubble_container
= CreateContainer(
1033 kShellWindowId_SettingBubbleContainer
,
1034 "SettingBubbleContainer",
1035 lock_screen_related_containers
);
1036 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container
);
1037 SetUsesScreenCoordinates(settings_bubble_container
);
1038 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
1040 aura::Window
* menu_container
= CreateContainer(
1041 kShellWindowId_MenuContainer
,
1043 lock_screen_related_containers
);
1044 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container
);
1045 SetUsesScreenCoordinates(menu_container
);
1047 aura::Window
* drag_drop_container
= CreateContainer(
1048 kShellWindowId_DragImageAndTooltipContainer
,
1049 "DragImageAndTooltipContainer",
1050 lock_screen_related_containers
);
1051 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
1052 SetUsesScreenCoordinates(drag_drop_container
);
1054 aura::Window
* overlay_container
= CreateContainer(
1055 kShellWindowId_OverlayContainer
,
1057 lock_screen_related_containers
);
1058 SetUsesScreenCoordinates(overlay_container
);
1060 aura::Window
* virtual_keyboard_parent_container
= CreateContainer(
1061 kShellWindowId_VirtualKeyboardParentContainer
,
1062 "VirtualKeyboardParentContainer",
1064 SetUsesScreenCoordinates(virtual_keyboard_parent_container
);
1066 #if defined(OS_CHROMEOS)
1067 aura::Window
* mouse_cursor_container
= CreateContainer(
1068 kShellWindowId_MouseCursorContainer
,
1069 "MouseCursorContainer",
1071 SetUsesScreenCoordinates(mouse_cursor_container
);
1074 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
1075 "PowerButtonAnimationContainer", root_window
);
1078 void RootWindowController::EnableTouchHudProjection() {
1079 if (touch_hud_projection_
)
1081 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1084 void RootWindowController::DisableTouchHudProjection() {
1085 if (!touch_hud_projection_
)
1087 touch_hud_projection_
->Remove();
1090 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
1091 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
1094 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
1096 EnableTouchHudProjection();
1098 DisableTouchHudProjection();
1101 RootWindowController
* GetRootWindowController(
1102 const aura::Window
* root_window
) {
1103 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;