Fix type annotations of gallery/js/ribbon.js.
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob226013f42bdad20ff9f087d9ad5bddaf8bbb9b4a
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 keyboard_controller->AddObserver(
629 always_on_top_controller_->GetLayoutManager());
630 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
631 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
632 DCHECK(parent);
633 aura::Window* keyboard_container =
634 keyboard_controller->GetContainerWindow();
635 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
636 parent->AddChild(keyboard_container);
637 // TODO(oshima): Bounds of keyboard container should be handled by
638 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
639 keyboard_container->SetBounds(parent->bounds());
642 void RootWindowController::DeactivateKeyboard(
643 keyboard::KeyboardController* keyboard_controller) {
644 if (!keyboard_controller ||
645 !keyboard_controller->keyboard_container_initialized()) {
646 return;
648 aura::Window* keyboard_container =
649 keyboard_controller->GetContainerWindow();
650 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
651 aura::Window* parent =
652 GetContainer(kShellWindowId_ImeWindowParentContainer);
653 DCHECK(parent);
654 parent->RemoveChild(keyboard_container);
655 // Virtual keyboard may be deactivated while still showing, notify all
656 // observers that keyboard bounds changed to 0 before remove them.
657 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
658 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
659 keyboard_controller->RemoveObserver(panel_layout_manager_);
660 keyboard_controller->RemoveObserver(docked_layout_manager_);
661 keyboard_controller->RemoveObserver(
662 workspace_controller_->layout_manager());
663 keyboard_controller->RemoveObserver(
664 always_on_top_controller_->GetLayoutManager());
665 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
669 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
670 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
671 return parent ? parent->Contains(window) : false;
674 ////////////////////////////////////////////////////////////////////////////////
675 // RootWindowController, private:
677 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
678 : ash_host_(ash_host),
679 root_window_layout_(NULL),
680 docked_layout_manager_(NULL),
681 panel_layout_manager_(NULL),
682 touch_hud_debug_(NULL),
683 touch_hud_projection_(NULL) {
684 aura::Window* root_window = GetRootWindow();
685 GetRootWindowSettings(root_window)->controller = this;
686 screen_dimmer_.reset(new ScreenDimmer(root_window));
688 stacking_controller_.reset(new StackingController);
689 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
690 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
693 void RootWindowController::Init(RootWindowType root_window_type,
694 bool first_run_after_boot) {
695 aura::Window* root_window = GetRootWindow();
696 Shell* shell = Shell::GetInstance();
697 shell->InitRootWindow(root_window);
699 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
700 CreateContainersInRootWindow(root_window);
702 CreateSystemBackground(first_run_after_boot);
704 InitLayoutManagers();
705 InitTouchHuds();
707 if (Shell::GetPrimaryRootWindowController()->
708 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
709 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
712 shell->AddShellObserver(this);
714 if (root_window_type == PRIMARY) {
715 root_window_layout()->OnWindowResized();
716 shell->InitKeyboard();
717 } else {
718 root_window_layout()->OnWindowResized();
719 ash_host_->AsWindowTreeHost()->Show();
721 // Create a shelf if a user is already logged in.
722 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
723 shelf()->CreateShelf();
725 // Notify shell observers about new root window.
726 shell->OnRootWindowAdded(root_window);
729 #if defined(OS_CHROMEOS)
730 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
731 switches::kAshDisableTouchExplorationMode)) {
732 touch_exploration_manager_.reset(new AshTouchExplorationManager(this));
734 #endif
737 void RootWindowController::InitLayoutManagers() {
738 aura::Window* root_window = GetRootWindow();
739 root_window_layout_ = new RootWindowLayoutManager(root_window);
740 root_window->SetLayoutManager(root_window_layout_);
742 aura::Window* default_container =
743 GetContainer(kShellWindowId_DefaultContainer);
744 // Workspace manager has its own layout managers.
745 workspace_controller_.reset(
746 new WorkspaceController(default_container));
748 aura::Window* always_on_top_container =
749 GetContainer(kShellWindowId_AlwaysOnTopContainer);
750 always_on_top_controller_.reset(
751 new AlwaysOnTopController(always_on_top_container));
753 DCHECK(!shelf_.get());
754 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
755 // TODO(harrym): Remove when status area is view.
756 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
757 shelf_.reset(new ShelfWidget(
758 shelf_container, status_container, workspace_controller()));
760 if (!Shell::GetInstance()->session_state_delegate()->
761 IsActiveUserSessionStarted()) {
762 // This window exists only to be a event target on login screen.
763 // It does not have to handle events, nor be visible.
764 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
765 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
767 aura::Window* lock_background_container =
768 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
769 lock_background_container->AddChild(mouse_event_target_.get());
770 mouse_event_target_->Show();
773 // Create Docked windows layout manager
774 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
775 docked_layout_manager_ =
776 new DockedWindowLayoutManager(docked_container, workspace_controller());
777 docked_container->SetLayoutManager(docked_layout_manager_);
779 // Installs SnapLayoutManager to containers who set the
780 // |kSnapsChildrenToPhysicalPixelBoundary| property.
781 wm::InstallSnapLayoutManagerToContainers(root_window);
783 // Create Panel layout manager
784 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
785 panel_layout_manager_ = new PanelLayoutManager(panel_container);
786 panel_container->SetLayoutManager(panel_layout_manager_);
787 panel_container_handler_.reset(new PanelWindowEventHandler);
788 panel_container->AddPreTargetHandler(panel_container_handler_.get());
790 // Install an AttachedPanelWindowTargeter on the panel container to make it
791 // easier to correctly target shelf buttons with touch.
792 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
793 -kResizeOutsideBoundsSize,
794 -kResizeOutsideBoundsSize,
795 -kResizeOutsideBoundsSize);
796 gfx::Insets touch_extend = mouse_extend.Scale(
797 kResizeOutsideBoundsScaleForTouch);
798 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
799 new AttachedPanelWindowTargeter(panel_container,
800 mouse_extend,
801 touch_extend,
802 panel_layout_manager_)));
805 void RootWindowController::InitTouchHuds() {
806 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
807 if (command_line->HasSwitch(switches::kAshTouchHud))
808 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
809 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
810 EnableTouchHudProjection();
813 void RootWindowController::CreateSystemBackground(
814 bool is_first_run_after_boot) {
815 SkColor color = SK_ColorBLACK;
816 #if defined(OS_CHROMEOS)
817 if (is_first_run_after_boot)
818 color = kChromeOsBootColor;
819 #endif
820 system_background_.reset(
821 new SystemBackgroundController(GetRootWindow(), color));
823 #if defined(OS_CHROMEOS)
824 // Make a copy of the system's boot splash screen so we can composite it
825 // onscreen until the desktop background is ready.
826 if (is_first_run_after_boot &&
827 (base::CommandLine::ForCurrentProcess()->HasSwitch(
828 switches::kAshCopyHostBackgroundAtBoot) ||
829 base::CommandLine::ForCurrentProcess()->HasSwitch(
830 switches::kAshAnimateFromBootSplashScreen)))
831 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
832 #endif
835 void RootWindowController::CreateContainersInRootWindow(
836 aura::Window* root_window) {
837 // These containers are just used by PowerButtonController to animate groups
838 // of containers simultaneously without messing up the current transformations
839 // on those containers. These are direct children of the root window; all of
840 // the other containers are their children.
842 // The desktop background container is not part of the lock animation, so it
843 // is not included in those animate groups.
844 // When screen is locked desktop background is moved to lock screen background
845 // container (moved back on unlock). We want to make sure that there's an
846 // opaque layer occluding the non-lock-screen layers.
847 aura::Window* desktop_background_container = CreateContainer(
848 kShellWindowId_DesktopBackgroundContainer,
849 "DesktopBackgroundContainer",
850 root_window);
851 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
853 aura::Window* non_lock_screen_containers = CreateContainer(
854 kShellWindowId_NonLockScreenContainersContainer,
855 "NonLockScreenContainersContainer",
856 root_window);
858 aura::Window* lock_background_containers = CreateContainer(
859 kShellWindowId_LockScreenBackgroundContainer,
860 "LockScreenBackgroundContainer",
861 root_window);
862 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
864 aura::Window* lock_screen_containers = CreateContainer(
865 kShellWindowId_LockScreenContainersContainer,
866 "LockScreenContainersContainer",
867 root_window);
868 aura::Window* lock_screen_related_containers = CreateContainer(
869 kShellWindowId_LockScreenRelatedContainersContainer,
870 "LockScreenRelatedContainersContainer",
871 root_window);
873 CreateContainer(kShellWindowId_UnparentedControlContainer,
874 "UnparentedControlContainer",
875 non_lock_screen_containers);
877 aura::Window* default_container = CreateContainer(
878 kShellWindowId_DefaultContainer,
879 "DefaultContainer",
880 non_lock_screen_containers);
881 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
882 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container);
883 SetUsesScreenCoordinates(default_container);
884 SetUsesEasyResizeTargeter(default_container);
886 aura::Window* always_on_top_container = CreateContainer(
887 kShellWindowId_AlwaysOnTopContainer,
888 "AlwaysOnTopContainer",
889 non_lock_screen_containers);
890 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
891 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container);
892 SetUsesScreenCoordinates(always_on_top_container);
894 aura::Window* docked_container = CreateContainer(
895 kShellWindowId_DockedContainer,
896 "DockedContainer",
897 non_lock_screen_containers);
898 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container);
899 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container);
900 SetUsesScreenCoordinates(docked_container);
901 SetUsesEasyResizeTargeter(docked_container);
903 aura::Window* shelf_container =
904 CreateContainer(kShellWindowId_ShelfContainer,
905 "ShelfContainer",
906 non_lock_screen_containers);
907 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container);
908 SetUsesScreenCoordinates(shelf_container);
909 DescendantShouldStayInSameRootWindow(shelf_container);
911 aura::Window* panel_container = CreateContainer(
912 kShellWindowId_PanelContainer,
913 "PanelContainer",
914 non_lock_screen_containers);
915 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container);
916 SetUsesScreenCoordinates(panel_container);
918 aura::Window* shelf_bubble_container =
919 CreateContainer(kShellWindowId_ShelfBubbleContainer,
920 "ShelfBubbleContainer",
921 non_lock_screen_containers);
922 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container);
923 SetUsesScreenCoordinates(shelf_bubble_container);
924 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
926 aura::Window* app_list_container =
927 CreateContainer(kShellWindowId_AppListContainer,
928 "AppListContainer",
929 non_lock_screen_containers);
930 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container);
931 SetUsesScreenCoordinates(app_list_container);
933 aura::Window* modal_container = CreateContainer(
934 kShellWindowId_SystemModalContainer,
935 "SystemModalContainer",
936 non_lock_screen_containers);
937 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container);
938 modal_container->SetLayoutManager(
939 new SystemModalContainerLayoutManager(modal_container));
940 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
941 SetUsesScreenCoordinates(modal_container);
942 SetUsesEasyResizeTargeter(modal_container);
944 // TODO(beng): Figure out if we can make this use
945 // SystemModalContainerEventFilter instead of stops_event_propagation.
946 aura::Window* lock_container = CreateContainer(
947 kShellWindowId_LockScreenContainer,
948 "LockScreenContainer",
949 lock_screen_containers);
950 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container);
951 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
952 switches::kAshDisableLockLayoutManager)) {
953 lock_container->SetLayoutManager(
954 new WorkspaceLayoutManager(lock_container));
955 } else {
956 lock_container->SetLayoutManager(new LockLayoutManager(lock_container));
958 SetUsesScreenCoordinates(lock_container);
959 // TODO(beng): stopsevents
961 aura::Window* lock_modal_container = CreateContainer(
962 kShellWindowId_LockSystemModalContainer,
963 "LockSystemModalContainer",
964 lock_screen_containers);
965 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container);
966 lock_modal_container->SetLayoutManager(
967 new SystemModalContainerLayoutManager(lock_modal_container));
968 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
969 SetUsesScreenCoordinates(lock_modal_container);
970 SetUsesEasyResizeTargeter(lock_modal_container);
972 aura::Window* status_container =
973 CreateContainer(kShellWindowId_StatusContainer,
974 "StatusContainer",
975 lock_screen_related_containers);
976 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container);
977 SetUsesScreenCoordinates(status_container);
978 DescendantShouldStayInSameRootWindow(status_container);
980 aura::Window* settings_bubble_container = CreateContainer(
981 kShellWindowId_SettingBubbleContainer,
982 "SettingBubbleContainer",
983 lock_screen_related_containers);
984 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
985 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container);
986 SetUsesScreenCoordinates(settings_bubble_container);
987 DescendantShouldStayInSameRootWindow(settings_bubble_container);
989 aura::Window* virtual_keyboard_parent_container =
990 CreateContainer(kShellWindowId_ImeWindowParentContainer,
991 "VirtualKeyboardParentContainer",
992 lock_screen_related_containers);
993 wm::SetSnapsChildrenToPhysicalPixelBoundary(
994 virtual_keyboard_parent_container);
995 virtual_keyboard_parent_container->SetLayoutManager(
996 new VirtualKeyboardContainerLayoutManager(
997 virtual_keyboard_parent_container));
998 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
1000 aura::Window* menu_container = CreateContainer(
1001 kShellWindowId_MenuContainer,
1002 "MenuContainer",
1003 lock_screen_related_containers);
1004 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
1005 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container);
1006 SetUsesScreenCoordinates(menu_container);
1008 aura::Window* drag_drop_container = CreateContainer(
1009 kShellWindowId_DragImageAndTooltipContainer,
1010 "DragImageAndTooltipContainer",
1011 lock_screen_related_containers);
1012 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
1013 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container);
1014 SetUsesScreenCoordinates(drag_drop_container);
1016 aura::Window* overlay_container = CreateContainer(
1017 kShellWindowId_OverlayContainer,
1018 "OverlayContainer",
1019 lock_screen_related_containers);
1020 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container);
1021 SetUsesScreenCoordinates(overlay_container);
1023 #if defined(OS_CHROMEOS)
1024 aura::Window* mouse_cursor_container = CreateContainer(
1025 kShellWindowId_MouseCursorContainer,
1026 "MouseCursorContainer",
1027 root_window);
1028 SetUsesScreenCoordinates(mouse_cursor_container);
1029 #endif
1031 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1032 "PowerButtonAnimationContainer", root_window);
1035 void RootWindowController::EnableTouchHudProjection() {
1036 if (touch_hud_projection_)
1037 return;
1038 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1041 void RootWindowController::DisableTouchHudProjection() {
1042 if (!touch_hud_projection_)
1043 return;
1044 touch_hud_projection_->Remove();
1047 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1048 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1051 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1052 if (enabled)
1053 EnableTouchHudProjection();
1054 else
1055 DisableTouchHudProjection();
1058 RootWindowController* GetRootWindowController(
1059 const aura::Window* root_window) {
1060 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1063 } // namespace ash