Roll ffmpeg 2fa2c9a:202208
[chromium-blink-merge.git] / ash / root_window_controller.cc
blobfdebffa9ca945036f78bc1bbe2c3706d438aa7e4
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"
7 #include <queue>
8 #include <vector>
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"
83 #endif
85 namespace ash {
86 namespace {
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;
94 #endif
96 // Creates a new window for use as a container.
97 aura::Window* CreateContainer(int window_id,
98 const char* name,
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)
106 container->Show();
107 return container;
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;
135 if (update_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().
148 if (update_bounds)
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 kShellWindowId_OverlayContainer, };
167 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
168 int id = kContainerIdsToMove[i];
169 aura::Window* src_container = Shell::GetContainer(src, id);
170 aura::Window* dst_container = Shell::GetContainer(dst, id);
171 while (!src_container->children().empty()) {
172 // Restart iteration from the source container windows each time as they
173 // may change as a result of moving other windows.
174 aura::Window::Windows::const_iterator iter =
175 src_container->children().begin();
176 while (iter != src_container->children().end() &&
177 SystemModalContainerLayoutManager::IsModalBackground(*iter)) {
178 ++iter;
180 // If the entire window list is modal background windows then stop.
181 if (iter == src_container->children().end())
182 break;
183 ReparentWindow(*iter, dst_container);
188 // Mark the container window so that a widget added to this container will
189 // use the virtual screeen coordinates instead of parent.
190 void SetUsesScreenCoordinates(aura::Window* container) {
191 container->SetProperty(kUsesScreenCoordinatesKey, true);
194 // Mark the container window so that a widget added to this container will
195 // say in the same root window regardless of the bounds specified.
196 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
197 container->SetProperty(kStayInSameRootWindowKey, true);
200 void SetUsesEasyResizeTargeter(aura::Window* container) {
201 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
202 -kResizeOutsideBoundsSize,
203 -kResizeOutsideBoundsSize,
204 -kResizeOutsideBoundsSize);
205 gfx::Insets touch_extend = mouse_extend.Scale(
206 kResizeOutsideBoundsScaleForTouch);
207 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
208 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
209 touch_extend)));
212 // A window delegate which does nothing. Used to create a window that
213 // is a event target, but do nothing.
214 class EmptyWindowDelegate : public aura::WindowDelegate {
215 public:
216 EmptyWindowDelegate() {}
217 ~EmptyWindowDelegate() override {}
219 // aura::WindowDelegate overrides:
220 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
221 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
222 void OnBoundsChanged(const gfx::Rect& old_bounds,
223 const gfx::Rect& new_bounds) override {}
224 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
225 return gfx::kNullCursor;
227 int GetNonClientComponent(const gfx::Point& point) const override {
228 return HTNOWHERE;
230 bool ShouldDescendIntoChildForEventHandling(
231 aura::Window* child,
232 const gfx::Point& location) override {
233 return false;
235 bool CanFocus() override { return false; }
236 void OnCaptureLost() override {}
237 void OnPaint(gfx::Canvas* canvas) 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 {}
245 private:
246 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
249 } // namespace
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 */);
262 // static
263 RootWindowController* RootWindowController::ForShelf(
264 const aura::Window* window) {
265 CHECK(Shell::HasInstance());
266 return GetRootWindowController(window->GetRootWindow());
269 // static
270 RootWindowController* RootWindowController::ForWindow(
271 const aura::Window* window) {
272 CHECK(Shell::HasInstance());
273 return GetRootWindowController(window->GetRootWindow());
276 // static
277 RootWindowController* RootWindowController::ForTargetRootWindow() {
278 CHECK(Shell::HasInstance());
279 return GetRootWindowController(Shell::GetTargetRootWindow());
282 // static
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();
288 return container;
291 RootWindowController::~RootWindowController() {
292 Shutdown();
293 ash_host_.reset();
294 // The CaptureClient needs to be around for as long as the RootWindow is
295 // valid.
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();
335 #endif
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
349 ? NULL
350 : Shell::GetPrimaryRootWindow());
353 CloseChildWindows();
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;
370 if (window) {
371 aura::Window* window_container = GetContainerForWindow(window);
372 if (window_container &&
373 window_container->id() >= kShellWindowId_LockScreenContainer) {
374 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
375 } else {
376 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
378 } else {
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())
398 return;
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));
434 #endif
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();
442 #endif
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.
487 if (shelf_) {
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())
512 continue;
513 if (child->delegate())
514 toplevel_windows.Add(child);
515 else
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()) {
525 delete window;
526 } else {
527 root_window->RemoveChild(window);
531 shelf_.reset();
534 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
535 // Forget the shelf early so that shelf don't update itself using wrong
536 // display info.
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));
558 if (!menu_model)
559 return;
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())
564 return;
566 views::MenuRunner menu_runner(menu_model.get(),
567 views::MenuRunner::CONTEXT_MENU);
568 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
569 NULL,
570 gfx::Rect(location_in_screen, gfx::Size()),
571 views::MENU_ANCHOR_TOPLEFT,
572 source_type) == views::MenuRunner::MENU_DELETED) {
573 return;
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;
594 } else {
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;
605 break;
609 while (topmost_window) {
610 if (wm::GetWindowState(topmost_window)->IsFullscreen())
611 return topmost_window;
612 topmost_window = ::wm::GetTransientParent(topmost_window);
614 return NULL;
617 void RootWindowController::ActivateKeyboard(
618 keyboard::KeyboardController* keyboard_controller) {
619 if (!keyboard::IsKeyboardEnabled() ||
620 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
621 return;
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 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
629 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
630 DCHECK(parent);
631 aura::Window* keyboard_container =
632 keyboard_controller->GetContainerWindow();
633 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
634 parent->AddChild(keyboard_container);
635 // TODO(oshima): Bounds of keyboard container should be handled by
636 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
637 keyboard_container->SetBounds(parent->bounds());
640 void RootWindowController::DeactivateKeyboard(
641 keyboard::KeyboardController* keyboard_controller) {
642 if (!keyboard_controller ||
643 !keyboard_controller->keyboard_container_initialized()) {
644 return;
646 aura::Window* keyboard_container =
647 keyboard_controller->GetContainerWindow();
648 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
649 aura::Window* parent =
650 GetContainer(kShellWindowId_ImeWindowParentContainer);
651 DCHECK(parent);
652 parent->RemoveChild(keyboard_container);
653 // Virtual keyboard may be deactivated while still showing, notify all
654 // observers that keyboard bounds changed to 0 before remove them.
655 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
656 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
657 keyboard_controller->RemoveObserver(panel_layout_manager_);
658 keyboard_controller->RemoveObserver(docked_layout_manager_);
659 keyboard_controller->RemoveObserver(
660 workspace_controller_->layout_manager());
661 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
665 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
666 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
667 return parent ? parent->Contains(window) : false;
670 ////////////////////////////////////////////////////////////////////////////////
671 // RootWindowController, private:
673 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
674 : ash_host_(ash_host),
675 root_window_layout_(NULL),
676 docked_layout_manager_(NULL),
677 panel_layout_manager_(NULL),
678 touch_hud_debug_(NULL),
679 touch_hud_projection_(NULL) {
680 aura::Window* root_window = GetRootWindow();
681 GetRootWindowSettings(root_window)->controller = this;
682 screen_dimmer_.reset(new ScreenDimmer(root_window));
684 stacking_controller_.reset(new StackingController);
685 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
686 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
689 void RootWindowController::Init(RootWindowType root_window_type,
690 bool first_run_after_boot) {
691 aura::Window* root_window = GetRootWindow();
692 Shell* shell = Shell::GetInstance();
693 shell->InitRootWindow(root_window);
695 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
696 CreateContainersInRootWindow(root_window);
698 CreateSystemBackground(first_run_after_boot);
700 InitLayoutManagers();
701 InitTouchHuds();
703 if (Shell::GetPrimaryRootWindowController()->
704 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
705 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
708 shell->AddShellObserver(this);
710 if (root_window_type == PRIMARY) {
711 root_window_layout()->OnWindowResized();
712 shell->InitKeyboard();
713 } else {
714 root_window_layout()->OnWindowResized();
715 ash_host_->AsWindowTreeHost()->Show();
717 // Create a shelf if a user is already logged in.
718 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
719 shelf()->CreateShelf();
721 // Notify shell observers about new root window.
722 shell->OnRootWindowAdded(root_window);
725 #if defined(OS_CHROMEOS)
726 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
727 switches::kAshDisableTouchExplorationMode)) {
728 touch_exploration_manager_.reset(new AshTouchExplorationManager(this));
730 #endif
733 void RootWindowController::InitLayoutManagers() {
734 aura::Window* root_window = GetRootWindow();
735 root_window_layout_ = new RootWindowLayoutManager(root_window);
736 root_window->SetLayoutManager(root_window_layout_);
738 aura::Window* default_container =
739 GetContainer(kShellWindowId_DefaultContainer);
740 // Workspace manager has its own layout managers.
741 workspace_controller_.reset(
742 new WorkspaceController(default_container));
744 aura::Window* always_on_top_container =
745 GetContainer(kShellWindowId_AlwaysOnTopContainer);
746 always_on_top_container->SetLayoutManager(
747 new WorkspaceLayoutManager(always_on_top_container));
748 always_on_top_controller_.reset(new AlwaysOnTopController);
749 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
751 DCHECK(!shelf_.get());
752 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
753 // TODO(harrym): Remove when status area is view.
754 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
755 shelf_.reset(new ShelfWidget(
756 shelf_container, status_container, workspace_controller()));
758 if (!Shell::GetInstance()->session_state_delegate()->
759 IsActiveUserSessionStarted()) {
760 // This window exists only to be a event target on login screen.
761 // It does not have to handle events, nor be visible.
762 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
763 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
765 aura::Window* lock_background_container =
766 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
767 lock_background_container->AddChild(mouse_event_target_.get());
768 mouse_event_target_->Show();
771 // Create Docked windows layout manager
772 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
773 docked_layout_manager_ =
774 new DockedWindowLayoutManager(docked_container, workspace_controller());
775 docked_container->SetLayoutManager(docked_layout_manager_);
777 // Installs SnapLayoutManager to containers who set the
778 // |kSnapsChildrenToPhysicalPixelBoundary| property.
779 wm::InstallSnapLayoutManagerToContainers(root_window);
781 // Create Panel layout manager
782 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
783 panel_layout_manager_ = new PanelLayoutManager(panel_container);
784 panel_container->SetLayoutManager(panel_layout_manager_);
785 panel_container_handler_.reset(new PanelWindowEventHandler);
786 panel_container->AddPreTargetHandler(panel_container_handler_.get());
788 // Install an AttachedPanelWindowTargeter on the panel container to make it
789 // easier to correctly target shelf buttons with touch.
790 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
791 -kResizeOutsideBoundsSize,
792 -kResizeOutsideBoundsSize,
793 -kResizeOutsideBoundsSize);
794 gfx::Insets touch_extend = mouse_extend.Scale(
795 kResizeOutsideBoundsScaleForTouch);
796 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
797 new AttachedPanelWindowTargeter(panel_container,
798 mouse_extend,
799 touch_extend,
800 panel_layout_manager_)));
803 void RootWindowController::InitTouchHuds() {
804 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
805 if (command_line->HasSwitch(switches::kAshTouchHud))
806 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
807 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
808 EnableTouchHudProjection();
811 void RootWindowController::CreateSystemBackground(
812 bool is_first_run_after_boot) {
813 SkColor color = SK_ColorBLACK;
814 #if defined(OS_CHROMEOS)
815 if (is_first_run_after_boot)
816 color = kChromeOsBootColor;
817 #endif
818 system_background_.reset(
819 new SystemBackgroundController(GetRootWindow(), color));
821 #if defined(OS_CHROMEOS)
822 // Make a copy of the system's boot splash screen so we can composite it
823 // onscreen until the desktop background is ready.
824 if (is_first_run_after_boot &&
825 (base::CommandLine::ForCurrentProcess()->HasSwitch(
826 switches::kAshCopyHostBackgroundAtBoot) ||
827 base::CommandLine::ForCurrentProcess()->HasSwitch(
828 switches::kAshAnimateFromBootSplashScreen)))
829 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
830 #endif
833 void RootWindowController::CreateContainersInRootWindow(
834 aura::Window* root_window) {
835 // These containers are just used by PowerButtonController to animate groups
836 // of containers simultaneously without messing up the current transformations
837 // on those containers. These are direct children of the root window; all of
838 // the other containers are their children.
840 // The desktop background container is not part of the lock animation, so it
841 // is not included in those animate groups.
842 // When screen is locked desktop background is moved to lock screen background
843 // container (moved back on unlock). We want to make sure that there's an
844 // opaque layer occluding the non-lock-screen layers.
845 aura::Window* desktop_background_container = CreateContainer(
846 kShellWindowId_DesktopBackgroundContainer,
847 "DesktopBackgroundContainer",
848 root_window);
849 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
851 aura::Window* non_lock_screen_containers = CreateContainer(
852 kShellWindowId_NonLockScreenContainersContainer,
853 "NonLockScreenContainersContainer",
854 root_window);
856 aura::Window* lock_background_containers = CreateContainer(
857 kShellWindowId_LockScreenBackgroundContainer,
858 "LockScreenBackgroundContainer",
859 root_window);
860 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
862 aura::Window* lock_screen_containers = CreateContainer(
863 kShellWindowId_LockScreenContainersContainer,
864 "LockScreenContainersContainer",
865 root_window);
866 aura::Window* lock_screen_related_containers = CreateContainer(
867 kShellWindowId_LockScreenRelatedContainersContainer,
868 "LockScreenRelatedContainersContainer",
869 root_window);
871 CreateContainer(kShellWindowId_UnparentedControlContainer,
872 "UnparentedControlContainer",
873 non_lock_screen_containers);
875 aura::Window* default_container = CreateContainer(
876 kShellWindowId_DefaultContainer,
877 "DefaultContainer",
878 non_lock_screen_containers);
879 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
880 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container);
881 SetUsesScreenCoordinates(default_container);
882 SetUsesEasyResizeTargeter(default_container);
884 aura::Window* always_on_top_container = CreateContainer(
885 kShellWindowId_AlwaysOnTopContainer,
886 "AlwaysOnTopContainer",
887 non_lock_screen_containers);
888 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
889 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container);
890 SetUsesScreenCoordinates(always_on_top_container);
892 aura::Window* docked_container = CreateContainer(
893 kShellWindowId_DockedContainer,
894 "DockedContainer",
895 non_lock_screen_containers);
896 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container);
897 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container);
898 SetUsesScreenCoordinates(docked_container);
899 SetUsesEasyResizeTargeter(docked_container);
901 aura::Window* shelf_container =
902 CreateContainer(kShellWindowId_ShelfContainer,
903 "ShelfContainer",
904 non_lock_screen_containers);
905 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container);
906 SetUsesScreenCoordinates(shelf_container);
907 DescendantShouldStayInSameRootWindow(shelf_container);
909 aura::Window* panel_container = CreateContainer(
910 kShellWindowId_PanelContainer,
911 "PanelContainer",
912 non_lock_screen_containers);
913 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container);
914 SetUsesScreenCoordinates(panel_container);
916 aura::Window* shelf_bubble_container =
917 CreateContainer(kShellWindowId_ShelfBubbleContainer,
918 "ShelfBubbleContainer",
919 non_lock_screen_containers);
920 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container);
921 SetUsesScreenCoordinates(shelf_bubble_container);
922 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
924 aura::Window* app_list_container =
925 CreateContainer(kShellWindowId_AppListContainer,
926 "AppListContainer",
927 non_lock_screen_containers);
928 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container);
929 SetUsesScreenCoordinates(app_list_container);
931 aura::Window* modal_container = CreateContainer(
932 kShellWindowId_SystemModalContainer,
933 "SystemModalContainer",
934 non_lock_screen_containers);
935 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container);
936 modal_container->SetLayoutManager(
937 new SystemModalContainerLayoutManager(modal_container));
938 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
939 SetUsesScreenCoordinates(modal_container);
940 SetUsesEasyResizeTargeter(modal_container);
942 // TODO(beng): Figure out if we can make this use
943 // SystemModalContainerEventFilter instead of stops_event_propagation.
944 aura::Window* lock_container = CreateContainer(
945 kShellWindowId_LockScreenContainer,
946 "LockScreenContainer",
947 lock_screen_containers);
948 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container);
949 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
950 switches::kAshDisableLockLayoutManager)) {
951 lock_container->SetLayoutManager(
952 new WorkspaceLayoutManager(lock_container));
953 } else {
954 lock_container->SetLayoutManager(new LockLayoutManager(lock_container));
956 SetUsesScreenCoordinates(lock_container);
957 // TODO(beng): stopsevents
959 aura::Window* lock_modal_container = CreateContainer(
960 kShellWindowId_LockSystemModalContainer,
961 "LockSystemModalContainer",
962 lock_screen_containers);
963 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container);
964 lock_modal_container->SetLayoutManager(
965 new SystemModalContainerLayoutManager(lock_modal_container));
966 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
967 SetUsesScreenCoordinates(lock_modal_container);
968 SetUsesEasyResizeTargeter(lock_modal_container);
970 aura::Window* status_container =
971 CreateContainer(kShellWindowId_StatusContainer,
972 "StatusContainer",
973 lock_screen_related_containers);
974 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container);
975 SetUsesScreenCoordinates(status_container);
976 DescendantShouldStayInSameRootWindow(status_container);
978 aura::Window* settings_bubble_container = CreateContainer(
979 kShellWindowId_SettingBubbleContainer,
980 "SettingBubbleContainer",
981 lock_screen_related_containers);
982 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
983 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container);
984 SetUsesScreenCoordinates(settings_bubble_container);
985 DescendantShouldStayInSameRootWindow(settings_bubble_container);
987 aura::Window* virtual_keyboard_parent_container =
988 CreateContainer(kShellWindowId_ImeWindowParentContainer,
989 "VirtualKeyboardParentContainer",
990 lock_screen_related_containers);
991 wm::SetSnapsChildrenToPhysicalPixelBoundary(
992 virtual_keyboard_parent_container);
993 virtual_keyboard_parent_container->SetLayoutManager(
994 new VirtualKeyboardContainerLayoutManager(
995 virtual_keyboard_parent_container));
996 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
998 aura::Window* menu_container = CreateContainer(
999 kShellWindowId_MenuContainer,
1000 "MenuContainer",
1001 lock_screen_related_containers);
1002 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
1003 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container);
1004 SetUsesScreenCoordinates(menu_container);
1006 aura::Window* drag_drop_container = CreateContainer(
1007 kShellWindowId_DragImageAndTooltipContainer,
1008 "DragImageAndTooltipContainer",
1009 lock_screen_related_containers);
1010 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
1011 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container);
1012 SetUsesScreenCoordinates(drag_drop_container);
1014 aura::Window* overlay_container = CreateContainer(
1015 kShellWindowId_OverlayContainer,
1016 "OverlayContainer",
1017 lock_screen_related_containers);
1018 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container);
1019 SetUsesScreenCoordinates(overlay_container);
1021 #if defined(OS_CHROMEOS)
1022 aura::Window* mouse_cursor_container = CreateContainer(
1023 kShellWindowId_MouseCursorContainer,
1024 "MouseCursorContainer",
1025 root_window);
1026 SetUsesScreenCoordinates(mouse_cursor_container);
1027 #endif
1029 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1030 "PowerButtonAnimationContainer", root_window);
1033 void RootWindowController::EnableTouchHudProjection() {
1034 if (touch_hud_projection_)
1035 return;
1036 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1039 void RootWindowController::DisableTouchHudProjection() {
1040 if (!touch_hud_projection_)
1041 return;
1042 touch_hud_projection_->Remove();
1045 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1046 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1049 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1050 if (enabled)
1051 EnableTouchHudProjection();
1052 else
1053 DisableTouchHudProjection();
1056 RootWindowController* GetRootWindowController(
1057 const aura::Window* root_window) {
1058 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1061 } // namespace ash