1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/root_window_controller.h"
10 #include "ash/ash_constants.h"
11 #include "ash/ash_switches.h"
12 #include "ash/desktop_background/desktop_background_controller.h"
13 #include "ash/desktop_background/desktop_background_widget_controller.h"
14 #include "ash/desktop_background/user_wallpaper_delegate.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/focus_cycler.h"
17 #include "ash/high_contrast/high_contrast_controller.h"
18 #include "ash/host/ash_window_tree_host.h"
19 #include "ash/root_window_settings.h"
20 #include "ash/session/session_state_delegate.h"
21 #include "ash/shelf/shelf_layout_manager.h"
22 #include "ash/shelf/shelf_types.h"
23 #include "ash/shelf/shelf_widget.h"
24 #include "ash/shell.h"
25 #include "ash/shell_delegate.h"
26 #include "ash/shell_factory.h"
27 #include "ash/shell_window_ids.h"
28 #include "ash/switchable_windows.h"
29 #include "ash/system/status_area_widget.h"
30 #include "ash/system/tray/system_tray_delegate.h"
31 #include "ash/system/tray/system_tray_notifier.h"
32 #include "ash/touch/touch_hud_debug.h"
33 #include "ash/touch/touch_hud_projection.h"
34 #include "ash/touch/touch_observer_hud.h"
35 #include "ash/wm/always_on_top_controller.h"
36 #include "ash/wm/dock/docked_window_layout_manager.h"
37 #include "ash/wm/lock_layout_manager.h"
38 #include "ash/wm/panels/attached_panel_window_targeter.h"
39 #include "ash/wm/panels/panel_layout_manager.h"
40 #include "ash/wm/panels/panel_window_event_handler.h"
41 #include "ash/wm/root_window_layout_manager.h"
42 #include "ash/wm/screen_dimmer.h"
43 #include "ash/wm/stacking_controller.h"
44 #include "ash/wm/status_area_layout_manager.h"
45 #include "ash/wm/system_background_controller.h"
46 #include "ash/wm/system_modal_container_layout_manager.h"
47 #include "ash/wm/window_properties.h"
48 #include "ash/wm/window_state.h"
49 #include "ash/wm/window_util.h"
50 #include "ash/wm/workspace/workspace_layout_manager.h"
51 #include "ash/wm/workspace_controller.h"
52 #include "base/command_line.h"
53 #include "base/time/time.h"
54 #include "ui/aura/client/aura_constants.h"
55 #include "ui/aura/client/screen_position_client.h"
56 #include "ui/aura/window.h"
57 #include "ui/aura/window_delegate.h"
58 #include "ui/aura/window_event_dispatcher.h"
59 #include "ui/aura/window_observer.h"
60 #include "ui/aura/window_tracker.h"
61 #include "ui/base/hit_test.h"
62 #include "ui/base/models/menu_model.h"
63 #include "ui/gfx/display.h"
64 #include "ui/gfx/screen.h"
65 #include "ui/keyboard/keyboard_controller.h"
66 #include "ui/keyboard/keyboard_util.h"
67 #include "ui/views/controls/menu/menu_runner.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/core/capture_controller.h"
71 #include "ui/wm/core/easy_resize_window_targeter.h"
72 #include "ui/wm/core/visibility_controller.h"
73 #include "ui/wm/core/window_util.h"
74 #include "ui/wm/public/drag_drop_client.h"
75 #include "ui/wm/public/tooltip_client.h"
76 #include "ui/wm/public/window_types.h"
78 #if defined(OS_CHROMEOS)
79 #include "ash/ash_touch_exploration_manager_chromeos.h"
80 #include "ash/wm/boot_splash_screen_chromeos.h"
81 #include "ui/chromeos/touch_exploration_controller.h"
87 #if defined(OS_CHROMEOS)
88 // Duration for the animation that hides the boot splash screen, in
89 // milliseconds. This should be short enough in relation to
90 // wm/window_animation.cc's brightness/grayscale fade animation that the login
91 // background image animation isn't hidden by the splash screen animation.
92 const int kBootSplashScreenHideDurationMs
= 500;
95 // Creates a new window for use as a container.
96 aura::Window
* CreateContainer(int window_id
,
98 aura::Window
* parent
) {
99 aura::Window
* container
= new aura::Window(NULL
);
100 container
->set_id(window_id
);
101 container
->SetName(name
);
102 container
->Init(ui::LAYER_NOT_DRAWN
);
103 parent
->AddChild(container
);
104 if (window_id
!= kShellWindowId_UnparentedControlContainer
)
109 float ToRelativeValue(int value
, int src
, int dst
) {
110 return static_cast<float>(value
) / static_cast<float>(src
) * dst
;
113 void MoveOriginRelativeToSize(const gfx::Size
& src_size
,
114 const gfx::Size
& dst_size
,
115 gfx::Rect
* bounds_in_out
) {
116 gfx::Point origin
= bounds_in_out
->origin();
117 bounds_in_out
->set_origin(gfx::Point(
118 ToRelativeValue(origin
.x(), src_size
.width(), dst_size
.width()),
119 ToRelativeValue(origin
.y(), src_size
.height(), dst_size
.height())));
122 // Reparents |window| to |new_parent|.
123 void ReparentWindow(aura::Window
* window
, aura::Window
* new_parent
) {
124 const gfx::Size src_size
= window
->parent()->bounds().size();
125 const gfx::Size dst_size
= new_parent
->bounds().size();
126 // Update the restore bounds to make it relative to the display.
127 wm::WindowState
* state
= wm::GetWindowState(window
);
128 gfx::Rect restore_bounds
;
129 bool has_restore_bounds
= state
->HasRestoreBounds();
131 bool update_bounds
= (state
->IsNormalOrSnapped() || state
->IsMinimized()) &&
132 new_parent
->id() != kShellWindowId_DockedContainer
;
133 gfx::Rect local_bounds
;
135 local_bounds
= state
->window()->bounds();
136 MoveOriginRelativeToSize(src_size
, dst_size
, &local_bounds
);
139 if (has_restore_bounds
) {
140 restore_bounds
= state
->GetRestoreBoundsInParent();
141 MoveOriginRelativeToSize(src_size
, dst_size
, &restore_bounds
);
144 new_parent
->AddChild(window
);
146 // Docked windows have bounds handled by the layout manager in AddChild().
148 window
->SetBounds(local_bounds
);
150 if (has_restore_bounds
)
151 state
->SetRestoreBoundsInParent(restore_bounds
);
154 // Reparents the appropriate set of windows from |src| to |dst|.
155 void ReparentAllWindows(aura::Window
* src
, aura::Window
* dst
) {
156 // Set of windows to move.
157 const int kContainerIdsToMove
[] = {
158 kShellWindowId_DefaultContainer
,
159 kShellWindowId_DockedContainer
,
160 kShellWindowId_PanelContainer
,
161 kShellWindowId_AlwaysOnTopContainer
,
162 kShellWindowId_SystemModalContainer
,
163 kShellWindowId_LockSystemModalContainer
,
164 kShellWindowId_UnparentedControlContainer
,
165 kShellWindowId_OverlayContainer
, };
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 ~EmptyWindowDelegate() override
{}
218 // aura::WindowDelegate overrides:
219 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
220 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
221 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
222 const gfx::Rect
& new_bounds
) override
{}
223 ui::TextInputClient
* GetFocusedTextInputClient() override
{ return nullptr; }
224 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
225 return gfx::kNullCursor
;
227 int GetNonClientComponent(const gfx::Point
& point
) const override
{
230 bool ShouldDescendIntoChildForEventHandling(
232 const gfx::Point
& location
) override
{
235 bool CanFocus() override
{ return false; }
236 void OnCaptureLost() override
{}
237 void OnPaint(const ui::PaintContext
& context
) override
{}
238 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
239 void OnWindowDestroying(aura::Window
* window
) override
{}
240 void OnWindowDestroyed(aura::Window
* window
) override
{ delete this; }
241 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
242 bool HasHitTestMask() const override
{ return false; }
243 void GetHitTestMask(gfx::Path
* mask
) const override
{}
246 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate
);
251 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost
* host
) {
252 RootWindowController
* controller
= new RootWindowController(host
);
253 controller
->Init(RootWindowController::PRIMARY
,
254 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
257 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost
* host
) {
258 RootWindowController
* controller
= new RootWindowController(host
);
259 controller
->Init(RootWindowController::SECONDARY
, false /* first run */);
263 RootWindowController
* RootWindowController::ForShelf(
264 const aura::Window
* window
) {
265 CHECK(Shell::HasInstance());
266 return GetRootWindowController(window
->GetRootWindow());
270 RootWindowController
* RootWindowController::ForWindow(
271 const aura::Window
* window
) {
272 CHECK(Shell::HasInstance());
273 return GetRootWindowController(window
->GetRootWindow());
277 RootWindowController
* RootWindowController::ForTargetRootWindow() {
278 CHECK(Shell::HasInstance());
279 return GetRootWindowController(Shell::GetTargetRootWindow());
283 aura::Window
* RootWindowController::GetContainerForWindow(
284 aura::Window
* window
) {
285 aura::Window
* container
= window
->parent();
286 while (container
&& container
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
)
287 container
= container
->parent();
291 RootWindowController::~RootWindowController() {
294 // The CaptureClient needs to be around for as long as the RootWindow is
296 capture_client_
.reset();
299 aura::WindowTreeHost
* RootWindowController::GetHost() {
300 return ash_host_
->AsWindowTreeHost();
303 const aura::WindowTreeHost
* RootWindowController::GetHost() const {
304 return ash_host_
->AsWindowTreeHost();
307 aura::Window
* RootWindowController::GetRootWindow() {
308 return GetHost()->window();
311 const aura::Window
* RootWindowController::GetRootWindow() const {
312 return GetHost()->window();
315 void RootWindowController::SetWallpaperController(
316 DesktopBackgroundWidgetController
* controller
) {
317 wallpaper_controller_
.reset(controller
);
320 void RootWindowController::SetAnimatingWallpaperController(
321 AnimatingDesktopController
* controller
) {
322 if (animating_wallpaper_controller_
.get())
323 animating_wallpaper_controller_
->StopAnimating();
324 animating_wallpaper_controller_
.reset(controller
);
327 void RootWindowController::Shutdown() {
328 Shell
* shell
= Shell::GetInstance();
329 shell
->RemoveShellObserver(this);
331 #if defined(OS_CHROMEOS)
332 if (touch_exploration_manager_
) {
333 touch_exploration_manager_
.reset();
337 if (animating_wallpaper_controller_
.get())
338 animating_wallpaper_controller_
->StopAnimating();
339 wallpaper_controller_
.reset();
340 animating_wallpaper_controller_
.reset();
341 aura::Window
* root_window
= GetRootWindow();
342 // Change the target root window before closing child windows. If any child
343 // being removed triggers a relayout of the shelf it will try to build a
344 // window list adding windows from the target root window's containers which
345 // may have already gone away.
346 if (Shell::GetTargetRootWindow() == root_window
) {
347 shell
->set_target_root_window(
348 Shell::GetPrimaryRootWindow() == root_window
350 : Shell::GetPrimaryRootWindow());
354 GetRootWindowSettings(root_window
)->controller
= NULL
;
355 screen_dimmer_
.reset();
356 workspace_controller_
.reset();
357 // Forget with the display ID so that display lookup
358 // ends up with invalid display.
359 GetRootWindowSettings(root_window
)->display_id
=
360 gfx::Display::kInvalidDisplayID
;
361 ash_host_
->PrepareForShutdown();
363 system_background_
.reset();
364 aura::client::SetScreenPositionClient(root_window
, NULL
);
367 SystemModalContainerLayoutManager
*
368 RootWindowController::GetSystemModalLayoutManager(aura::Window
* window
) {
369 aura::Window
* modal_container
= NULL
;
371 aura::Window
* window_container
= GetContainerForWindow(window
);
372 if (window_container
&&
373 window_container
->id() >= kShellWindowId_LockScreenContainer
) {
374 modal_container
= GetContainer(kShellWindowId_LockSystemModalContainer
);
376 modal_container
= GetContainer(kShellWindowId_SystemModalContainer
);
379 int modal_window_id
= Shell::GetInstance()->session_state_delegate()
380 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer
:
381 kShellWindowId_SystemModalContainer
;
382 modal_container
= GetContainer(modal_window_id
);
384 return modal_container
? static_cast<SystemModalContainerLayoutManager
*>(
385 modal_container
->layout_manager()) : NULL
;
388 aura::Window
* RootWindowController::GetContainer(int container_id
) {
389 return GetRootWindow()->GetChildById(container_id
);
392 const aura::Window
* RootWindowController::GetContainer(int container_id
) const {
393 return ash_host_
->AsWindowTreeHost()->window()->GetChildById(container_id
);
396 void RootWindowController::ShowShelf() {
397 if (!shelf_
->shelf())
399 shelf_
->shelf()->SetVisible(true);
400 shelf_
->status_area_widget()->Show();
403 void RootWindowController::OnShelfCreated() {
404 if (panel_layout_manager_
)
405 panel_layout_manager_
->SetShelf(shelf_
->shelf());
406 if (docked_layout_manager_
) {
407 docked_layout_manager_
->SetShelf(shelf_
->shelf());
408 if (shelf_
->shelf_layout_manager())
409 docked_layout_manager_
->AddObserver(shelf_
->shelf_layout_manager());
412 // Notify shell observers that the shelf has been created.
413 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
416 void RootWindowController::UpdateAfterLoginStatusChange(
417 user::LoginStatus status
) {
418 if (status
!= user::LOGGED_IN_NONE
)
419 mouse_event_target_
.reset();
420 if (shelf_
->status_area_widget())
421 shelf_
->status_area_widget()->UpdateAfterLoginStatusChange(status
);
424 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
425 #if defined(OS_CHROMEOS)
426 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
427 switches::kAshAnimateFromBootSplashScreen
) &&
428 boot_splash_screen_
.get()) {
429 // Make the splash screen fade out so it doesn't obscure the desktop
430 // wallpaper's brightness/grayscale animation.
431 boot_splash_screen_
->StartHideAnimation(
432 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs
));
437 void RootWindowController::OnWallpaperAnimationFinished(views::Widget
* widget
) {
438 // Make sure the wallpaper is visible.
439 system_background_
->SetColor(SK_ColorBLACK
);
440 #if defined(OS_CHROMEOS)
441 boot_splash_screen_
.reset();
444 Shell::GetInstance()->user_wallpaper_delegate()->
445 OnWallpaperAnimationFinished();
446 // Only removes old component when wallpaper animation finished. If we
447 // remove the old one before the new wallpaper is done fading in there will
448 // be a white flash during the animation.
449 if (animating_wallpaper_controller()) {
450 DesktopBackgroundWidgetController
* controller
=
451 animating_wallpaper_controller()->GetController(true);
452 // |desktop_widget_| should be the same animating widget we try to move
453 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
454 // before move it to |kDesktopController|.
455 DCHECK_EQ(controller
->widget(), widget
);
456 // Release the old controller and close its background widget.
457 SetWallpaperController(controller
);
461 void RootWindowController::CloseChildWindows() {
462 mouse_event_target_
.reset();
464 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
465 // to fire notifications.
466 if (docked_layout_manager_
&& shelf_
&& shelf_
->shelf_layout_manager())
467 docked_layout_manager_
->RemoveObserver(shelf_
->shelf_layout_manager());
469 // Deactivate keyboard container before closing child windows and shutting
470 // down associated layout managers.
471 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
473 // panel_layout_manager_ needs to be shut down before windows are destroyed.
474 if (panel_layout_manager_
) {
475 panel_layout_manager_
->Shutdown();
476 panel_layout_manager_
= NULL
;
478 // docked_layout_manager_ needs to be shut down before windows are destroyed.
479 if (docked_layout_manager_
) {
480 docked_layout_manager_
->Shutdown();
481 docked_layout_manager_
= NULL
;
483 aura::Window
* root_window
= GetRootWindow();
484 aura::client::SetDragDropClient(root_window
, NULL
);
486 // TODO(harrym): Remove when Status Area Widget is a child view.
488 shelf_
->ShutdownStatusAreaWidget();
490 if (shelf_
->shelf_layout_manager())
491 shelf_
->shelf_layout_manager()->PrepareForShutdown();
494 // Close background widget first as it depends on tooltip.
495 wallpaper_controller_
.reset();
496 animating_wallpaper_controller_
.reset();
498 workspace_controller_
.reset();
499 aura::client::SetTooltipClient(root_window
, NULL
);
501 // Explicitly destroy top level windows. We do this as during part of
502 // destruction such windows may query the RootWindow for state.
503 std::queue
<aura::Window
*> non_toplevel_windows
;
504 non_toplevel_windows
.push(root_window
);
505 while (!non_toplevel_windows
.empty()) {
506 aura::Window
* non_toplevel_window
= non_toplevel_windows
.front();
507 non_toplevel_windows
.pop();
508 aura::WindowTracker toplevel_windows
;
509 for (size_t i
= 0; i
< non_toplevel_window
->children().size(); ++i
) {
510 aura::Window
* child
= non_toplevel_window
->children()[i
];
511 if (!child
->owned_by_parent())
513 if (child
->delegate())
514 toplevel_windows
.Add(child
);
516 non_toplevel_windows
.push(child
);
518 while (!toplevel_windows
.windows().empty())
519 delete *toplevel_windows
.windows().begin();
521 // And then remove the containers.
522 while (!root_window
->children().empty()) {
523 aura::Window
* window
= root_window
->children()[0];
524 if (window
->owned_by_parent()) {
527 root_window
->RemoveChild(window
);
534 void RootWindowController::MoveWindowsTo(aura::Window
* dst
) {
535 // Forget the shelf early so that shelf don't update itself using wrong
537 workspace_controller_
->SetShelf(NULL
);
538 ReparentAllWindows(GetRootWindow(), dst
);
541 ShelfLayoutManager
* RootWindowController::GetShelfLayoutManager() {
542 return shelf_
->shelf_layout_manager();
545 SystemTray
* RootWindowController::GetSystemTray() {
546 // We assume in throughout the code that this will not return NULL. If code
547 // triggers this for valid reasons, it should test status_area_widget first.
548 CHECK(shelf_
->status_area_widget());
549 return shelf_
->status_area_widget()->system_tray();
552 void RootWindowController::ShowContextMenu(const gfx::Point
& location_in_screen
,
553 ui::MenuSourceType source_type
) {
554 DCHECK(Shell::GetInstance()->delegate());
555 scoped_ptr
<ui::MenuModel
> menu_model(
556 Shell::GetInstance()->delegate()->CreateContextMenu(
557 GetRootWindow(), NULL
, NULL
));
561 // Background controller may not be set yet if user clicked on status are
562 // before initial animation completion. See crbug.com/222218
563 if (!wallpaper_controller_
.get())
566 views::MenuRunner
menu_runner(menu_model
.get(),
567 views::MenuRunner::CONTEXT_MENU
);
568 if (menu_runner
.RunMenuAt(wallpaper_controller_
->widget(),
570 gfx::Rect(location_in_screen
, gfx::Size()),
571 views::MENU_ANCHOR_TOPLEFT
,
572 source_type
) == views::MenuRunner::MENU_DELETED
) {
576 Shell::GetInstance()->UpdateShelfVisibility();
579 void RootWindowController::UpdateShelfVisibility() {
580 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
583 const aura::Window
* RootWindowController::GetWindowForFullscreenMode() const {
584 const aura::Window
* topmost_window
= NULL
;
585 const aura::Window
* active_window
= wm::GetActiveWindow();
586 if (active_window
&& active_window
->GetRootWindow() == GetRootWindow() &&
587 IsSwitchableContainer(active_window
->parent())) {
588 // Use the active window when it is on the current root window to determine
589 // the fullscreen state to allow temporarily using a panel or docked window
590 // (which are always above the default container) while a fullscreen
591 // window is open. We only use the active window when in a switchable
592 // container as the launcher should not exit fullscreen mode.
593 topmost_window
= active_window
;
595 // Otherwise, use the topmost window on the root window's default container
596 // when there is no active window on this root window.
597 const aura::Window::Windows
& windows
=
598 GetContainer(kShellWindowId_DefaultContainer
)->children();
599 for (aura::Window::Windows::const_reverse_iterator iter
= windows
.rbegin();
600 iter
!= windows
.rend(); ++iter
) {
601 if (((*iter
)->type() == ui::wm::WINDOW_TYPE_NORMAL
||
602 (*iter
)->type() == ui::wm::WINDOW_TYPE_PANEL
) &&
603 (*iter
)->layer()->GetTargetVisibility()) {
604 topmost_window
= *iter
;
609 while (topmost_window
) {
610 if (wm::GetWindowState(topmost_window
)->IsFullscreen())
611 return topmost_window
;
612 topmost_window
= ::wm::GetTransientParent(topmost_window
);
617 void RootWindowController::ActivateKeyboard(
618 keyboard::KeyboardController
* keyboard_controller
) {
619 if (!keyboard::IsKeyboardEnabled() ||
620 GetContainer(kShellWindowId_VirtualKeyboardContainer
)) {
623 DCHECK(keyboard_controller
);
624 keyboard_controller
->AddObserver(shelf()->shelf_layout_manager());
625 keyboard_controller
->AddObserver(panel_layout_manager_
);
626 keyboard_controller
->AddObserver(docked_layout_manager_
);
627 keyboard_controller
->AddObserver(workspace_controller_
->layout_manager());
628 keyboard_controller
->AddObserver(
629 always_on_top_controller_
->GetLayoutManager());
630 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
631 aura::Window
* parent
= GetContainer(kShellWindowId_ImeWindowParentContainer
);
633 aura::Window
* keyboard_container
=
634 keyboard_controller
->GetContainerWindow();
635 keyboard_container
->set_id(kShellWindowId_VirtualKeyboardContainer
);
636 parent
->AddChild(keyboard_container
);
639 void RootWindowController::DeactivateKeyboard(
640 keyboard::KeyboardController
* keyboard_controller
) {
641 if (!keyboard_controller
||
642 !keyboard_controller
->keyboard_container_initialized()) {
645 aura::Window
* keyboard_container
=
646 keyboard_controller
->GetContainerWindow();
647 if (keyboard_container
->GetRootWindow() == GetRootWindow()) {
648 aura::Window
* parent
=
649 GetContainer(kShellWindowId_ImeWindowParentContainer
);
651 parent
->RemoveChild(keyboard_container
);
652 // Virtual keyboard may be deactivated while still showing, notify all
653 // observers that keyboard bounds changed to 0 before remove them.
654 keyboard_controller
->NotifyKeyboardBoundsChanging(gfx::Rect());
655 keyboard_controller
->RemoveObserver(shelf()->shelf_layout_manager());
656 keyboard_controller
->RemoveObserver(panel_layout_manager_
);
657 keyboard_controller
->RemoveObserver(docked_layout_manager_
);
658 keyboard_controller
->RemoveObserver(
659 workspace_controller_
->layout_manager());
660 keyboard_controller
->RemoveObserver(
661 always_on_top_controller_
->GetLayoutManager());
662 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
666 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window
* window
) {
667 aura::Window
* parent
= GetContainer(kShellWindowId_ImeWindowParentContainer
);
668 return parent
? parent
->Contains(window
) : false;
671 ////////////////////////////////////////////////////////////////////////////////
672 // RootWindowController, private:
674 RootWindowController::RootWindowController(AshWindowTreeHost
* ash_host
)
675 : ash_host_(ash_host
),
676 root_window_layout_(NULL
),
677 docked_layout_manager_(NULL
),
678 panel_layout_manager_(NULL
),
679 touch_hud_debug_(NULL
),
680 touch_hud_projection_(NULL
) {
681 aura::Window
* root_window
= GetRootWindow();
682 GetRootWindowSettings(root_window
)->controller
= this;
683 screen_dimmer_
.reset(new ScreenDimmer(root_window
));
685 stacking_controller_
.reset(new StackingController
);
686 aura::client::SetWindowTreeClient(root_window
, stacking_controller_
.get());
687 capture_client_
.reset(new ::wm::ScopedCaptureClient(root_window
));
690 void RootWindowController::Init(RootWindowType root_window_type
,
691 bool first_run_after_boot
) {
692 aura::Window
* root_window
= GetRootWindow();
693 Shell
* shell
= Shell::GetInstance();
694 shell
->InitRootWindow(root_window
);
696 ash_host_
->AsWindowTreeHost()->SetCursor(ui::kCursorPointer
);
697 CreateContainersInRootWindow(root_window
);
699 CreateSystemBackground(first_run_after_boot
);
701 InitLayoutManagers();
704 if (Shell::GetPrimaryRootWindowController()->
705 GetSystemModalLayoutManager(NULL
)->has_modal_background()) {
706 GetSystemModalLayoutManager(NULL
)->CreateModalBackground();
709 shell
->AddShellObserver(this);
711 if (root_window_type
== PRIMARY
) {
712 root_window_layout()->OnWindowResized();
713 shell
->InitKeyboard();
715 root_window_layout()->OnWindowResized();
716 ash_host_
->AsWindowTreeHost()->Show();
718 // Create a shelf if a user is already logged in.
719 if (shell
->session_state_delegate()->NumberOfLoggedInUsers())
720 shelf()->CreateShelf();
722 // Notify shell observers about new root window.
723 shell
->OnRootWindowAdded(root_window
);
726 #if defined(OS_CHROMEOS)
727 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
728 switches::kAshDisableTouchExplorationMode
)) {
729 touch_exploration_manager_
.reset(new AshTouchExplorationManager(this));
734 void RootWindowController::InitLayoutManagers() {
735 aura::Window
* root_window
= GetRootWindow();
736 root_window_layout_
= new RootWindowLayoutManager(root_window
);
737 root_window
->SetLayoutManager(root_window_layout_
);
739 aura::Window
* default_container
=
740 GetContainer(kShellWindowId_DefaultContainer
);
741 // Workspace manager has its own layout managers.
742 workspace_controller_
.reset(
743 new WorkspaceController(default_container
));
745 aura::Window
* always_on_top_container
=
746 GetContainer(kShellWindowId_AlwaysOnTopContainer
);
747 always_on_top_controller_
.reset(
748 new AlwaysOnTopController(always_on_top_container
));
750 DCHECK(!shelf_
.get());
751 aura::Window
* shelf_container
= GetContainer(kShellWindowId_ShelfContainer
);
752 // TODO(harrym): Remove when status area is view.
753 aura::Window
* status_container
= GetContainer(kShellWindowId_StatusContainer
);
754 shelf_
.reset(new ShelfWidget(
755 shelf_container
, status_container
, workspace_controller()));
757 if (!Shell::GetInstance()->session_state_delegate()->
758 IsActiveUserSessionStarted()) {
759 // This window exists only to be a event target on login screen.
760 // It does not have to handle events, nor be visible.
761 mouse_event_target_
.reset(new aura::Window(new EmptyWindowDelegate
));
762 mouse_event_target_
->Init(ui::LAYER_NOT_DRAWN
);
764 aura::Window
* lock_background_container
=
765 GetContainer(kShellWindowId_LockScreenBackgroundContainer
);
766 lock_background_container
->AddChild(mouse_event_target_
.get());
767 mouse_event_target_
->Show();
770 // Create Docked windows layout manager
771 aura::Window
* docked_container
= GetContainer(kShellWindowId_DockedContainer
);
772 docked_layout_manager_
=
773 new DockedWindowLayoutManager(docked_container
, workspace_controller());
774 docked_container
->SetLayoutManager(docked_layout_manager_
);
776 // Installs SnapLayoutManager to containers who set the
777 // |kSnapsChildrenToPhysicalPixelBoundary| property.
778 wm::InstallSnapLayoutManagerToContainers(root_window
);
780 // Create Panel layout manager
781 aura::Window
* panel_container
= GetContainer(kShellWindowId_PanelContainer
);
782 panel_layout_manager_
= new PanelLayoutManager(panel_container
);
783 panel_container
->SetLayoutManager(panel_layout_manager_
);
784 panel_container_handler_
.reset(new PanelWindowEventHandler
);
785 panel_container
->AddPreTargetHandler(panel_container_handler_
.get());
787 // Install an AttachedPanelWindowTargeter on the panel container to make it
788 // easier to correctly target shelf buttons with touch.
789 gfx::Insets
mouse_extend(-kResizeOutsideBoundsSize
,
790 -kResizeOutsideBoundsSize
,
791 -kResizeOutsideBoundsSize
,
792 -kResizeOutsideBoundsSize
);
793 gfx::Insets touch_extend
= mouse_extend
.Scale(
794 kResizeOutsideBoundsScaleForTouch
);
795 panel_container
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
796 new AttachedPanelWindowTargeter(panel_container
,
799 panel_layout_manager_
)));
802 void RootWindowController::InitTouchHuds() {
803 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
804 if (command_line
->HasSwitch(switches::kAshTouchHud
))
805 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
806 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
807 EnableTouchHudProjection();
810 void RootWindowController::CreateSystemBackground(
811 bool is_first_run_after_boot
) {
812 SkColor color
= SK_ColorBLACK
;
813 #if defined(OS_CHROMEOS)
814 if (is_first_run_after_boot
)
815 color
= kChromeOsBootColor
;
817 system_background_
.reset(
818 new SystemBackgroundController(GetRootWindow(), color
));
820 #if defined(OS_CHROMEOS)
821 // Make a copy of the system's boot splash screen so we can composite it
822 // onscreen until the desktop background is ready.
823 if (is_first_run_after_boot
&&
824 (base::CommandLine::ForCurrentProcess()->HasSwitch(
825 switches::kAshCopyHostBackgroundAtBoot
) ||
826 base::CommandLine::ForCurrentProcess()->HasSwitch(
827 switches::kAshAnimateFromBootSplashScreen
)))
828 boot_splash_screen_
.reset(new BootSplashScreen(GetHost()));
832 void RootWindowController::CreateContainersInRootWindow(
833 aura::Window
* root_window
) {
834 // These containers are just used by PowerButtonController to animate groups
835 // of containers simultaneously without messing up the current transformations
836 // on those containers. These are direct children of the root window; all of
837 // the other containers are their children.
839 // The desktop background container is not part of the lock animation, so it
840 // is not included in those animate groups.
841 // When screen is locked desktop background is moved to lock screen background
842 // container (moved back on unlock). We want to make sure that there's an
843 // opaque layer occluding the non-lock-screen layers.
844 aura::Window
* desktop_background_container
= CreateContainer(
845 kShellWindowId_DesktopBackgroundContainer
,
846 "DesktopBackgroundContainer",
848 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container
);
850 aura::Window
* non_lock_screen_containers
= CreateContainer(
851 kShellWindowId_NonLockScreenContainersContainer
,
852 "NonLockScreenContainersContainer",
855 aura::Window
* lock_background_containers
= CreateContainer(
856 kShellWindowId_LockScreenBackgroundContainer
,
857 "LockScreenBackgroundContainer",
859 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers
);
861 aura::Window
* lock_screen_containers
= CreateContainer(
862 kShellWindowId_LockScreenContainersContainer
,
863 "LockScreenContainersContainer",
865 aura::Window
* lock_screen_related_containers
= CreateContainer(
866 kShellWindowId_LockScreenRelatedContainersContainer
,
867 "LockScreenRelatedContainersContainer",
870 CreateContainer(kShellWindowId_UnparentedControlContainer
,
871 "UnparentedControlContainer",
872 non_lock_screen_containers
);
874 aura::Window
* default_container
= CreateContainer(
875 kShellWindowId_DefaultContainer
,
877 non_lock_screen_containers
);
878 ::wm::SetChildWindowVisibilityChangesAnimated(default_container
);
879 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container
);
880 SetUsesScreenCoordinates(default_container
);
881 SetUsesEasyResizeTargeter(default_container
);
883 aura::Window
* always_on_top_container
= CreateContainer(
884 kShellWindowId_AlwaysOnTopContainer
,
885 "AlwaysOnTopContainer",
886 non_lock_screen_containers
);
887 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container
);
888 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container
);
889 SetUsesScreenCoordinates(always_on_top_container
);
891 aura::Window
* docked_container
= CreateContainer(
892 kShellWindowId_DockedContainer
,
894 non_lock_screen_containers
);
895 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container
);
896 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container
);
897 SetUsesScreenCoordinates(docked_container
);
898 SetUsesEasyResizeTargeter(docked_container
);
900 aura::Window
* shelf_container
=
901 CreateContainer(kShellWindowId_ShelfContainer
,
903 non_lock_screen_containers
);
904 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container
);
905 SetUsesScreenCoordinates(shelf_container
);
906 DescendantShouldStayInSameRootWindow(shelf_container
);
908 aura::Window
* panel_container
= CreateContainer(
909 kShellWindowId_PanelContainer
,
911 non_lock_screen_containers
);
912 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container
);
913 SetUsesScreenCoordinates(panel_container
);
915 aura::Window
* shelf_bubble_container
=
916 CreateContainer(kShellWindowId_ShelfBubbleContainer
,
917 "ShelfBubbleContainer",
918 non_lock_screen_containers
);
919 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container
);
920 SetUsesScreenCoordinates(shelf_bubble_container
);
921 DescendantShouldStayInSameRootWindow(shelf_bubble_container
);
923 aura::Window
* app_list_container
=
924 CreateContainer(kShellWindowId_AppListContainer
,
926 non_lock_screen_containers
);
927 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container
);
928 SetUsesScreenCoordinates(app_list_container
);
930 aura::Window
* modal_container
= CreateContainer(
931 kShellWindowId_SystemModalContainer
,
932 "SystemModalContainer",
933 non_lock_screen_containers
);
934 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container
);
935 modal_container
->SetLayoutManager(
936 new SystemModalContainerLayoutManager(modal_container
));
937 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container
);
938 SetUsesScreenCoordinates(modal_container
);
939 SetUsesEasyResizeTargeter(modal_container
);
941 // TODO(beng): Figure out if we can make this use
942 // SystemModalContainerEventFilter instead of stops_event_propagation.
943 aura::Window
* lock_container
= CreateContainer(
944 kShellWindowId_LockScreenContainer
,
945 "LockScreenContainer",
946 lock_screen_containers
);
947 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container
);
948 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
949 switches::kAshDisableLockLayoutManager
)) {
950 lock_container
->SetLayoutManager(
951 new WorkspaceLayoutManager(lock_container
));
953 lock_container
->SetLayoutManager(new LockLayoutManager(lock_container
));
955 SetUsesScreenCoordinates(lock_container
);
956 // TODO(beng): stopsevents
958 aura::Window
* lock_modal_container
= CreateContainer(
959 kShellWindowId_LockSystemModalContainer
,
960 "LockSystemModalContainer",
961 lock_screen_containers
);
962 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container
);
963 lock_modal_container
->SetLayoutManager(
964 new SystemModalContainerLayoutManager(lock_modal_container
));
965 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container
);
966 SetUsesScreenCoordinates(lock_modal_container
);
967 SetUsesEasyResizeTargeter(lock_modal_container
);
969 aura::Window
* status_container
=
970 CreateContainer(kShellWindowId_StatusContainer
,
972 lock_screen_related_containers
);
973 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container
);
974 SetUsesScreenCoordinates(status_container
);
975 DescendantShouldStayInSameRootWindow(status_container
);
977 aura::Window
* settings_bubble_container
= CreateContainer(
978 kShellWindowId_SettingBubbleContainer
,
979 "SettingBubbleContainer",
980 lock_screen_related_containers
);
981 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container
);
982 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container
);
983 SetUsesScreenCoordinates(settings_bubble_container
);
984 DescendantShouldStayInSameRootWindow(settings_bubble_container
);
986 aura::Window
* virtual_keyboard_parent_container
=
987 CreateContainer(kShellWindowId_ImeWindowParentContainer
,
988 "VirtualKeyboardParentContainer",
989 lock_screen_related_containers
);
990 wm::SetSnapsChildrenToPhysicalPixelBoundary(
991 virtual_keyboard_parent_container
);
992 SetUsesScreenCoordinates(virtual_keyboard_parent_container
);
994 aura::Window
* menu_container
= CreateContainer(
995 kShellWindowId_MenuContainer
,
997 lock_screen_related_containers
);
998 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container
);
999 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container
);
1000 SetUsesScreenCoordinates(menu_container
);
1002 aura::Window
* drag_drop_container
= CreateContainer(
1003 kShellWindowId_DragImageAndTooltipContainer
,
1004 "DragImageAndTooltipContainer",
1005 lock_screen_related_containers
);
1006 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container
);
1007 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container
);
1008 SetUsesScreenCoordinates(drag_drop_container
);
1010 aura::Window
* overlay_container
= CreateContainer(
1011 kShellWindowId_OverlayContainer
,
1013 lock_screen_related_containers
);
1014 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container
);
1015 SetUsesScreenCoordinates(overlay_container
);
1017 #if defined(OS_CHROMEOS)
1018 aura::Window
* mouse_cursor_container
= CreateContainer(
1019 kShellWindowId_MouseCursorContainer
,
1020 "MouseCursorContainer",
1022 SetUsesScreenCoordinates(mouse_cursor_container
);
1025 CreateContainer(kShellWindowId_PowerButtonAnimationContainer
,
1026 "PowerButtonAnimationContainer", root_window
);
1029 void RootWindowController::EnableTouchHudProjection() {
1030 if (touch_hud_projection_
)
1032 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1035 void RootWindowController::DisableTouchHudProjection() {
1036 if (!touch_hud_projection_
)
1038 touch_hud_projection_
->Remove();
1041 void RootWindowController::OnLoginStateChanged(user::LoginStatus status
) {
1042 shelf_
->shelf_layout_manager()->UpdateVisibilityState();
1045 void RootWindowController::OnTouchHudProjectionToggled(bool enabled
) {
1047 EnableTouchHudProjection();
1049 DisableTouchHudProjection();
1052 RootWindowController
* GetRootWindowController(
1053 const aura::Window
* root_window
) {
1054 return root_window
? GetRootWindowSettings(root_window
)->controller
: NULL
;