Add new PPB_OPENGLES2_DRAWBUFFERS_DEV_INTERFACE_1_0 pepper interface.
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob92fe388d62799156c91235c544e11b9d7814e5f5
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/root_window_settings.h"
19 #include "ash/session_state_delegate.h"
20 #include "ash/shelf/shelf_layout_manager.h"
21 #include "ash/shelf/shelf_types.h"
22 #include "ash/shelf/shelf_widget.h"
23 #include "ash/shell.h"
24 #include "ash/shell_delegate.h"
25 #include "ash/shell_factory.h"
26 #include "ash/shell_window_ids.h"
27 #include "ash/switchable_windows.h"
28 #include "ash/system/status_area_widget.h"
29 #include "ash/system/tray/system_tray_delegate.h"
30 #include "ash/touch/touch_hud_debug.h"
31 #include "ash/touch/touch_hud_projection.h"
32 #include "ash/touch/touch_observer_hud.h"
33 #include "ash/wm/always_on_top_controller.h"
34 #include "ash/wm/dock/docked_window_layout_manager.h"
35 #include "ash/wm/panels/panel_layout_manager.h"
36 #include "ash/wm/panels/panel_window_event_handler.h"
37 #include "ash/wm/root_window_layout_manager.h"
38 #include "ash/wm/screen_dimmer.h"
39 #include "ash/wm/stacking_controller.h"
40 #include "ash/wm/status_area_layout_manager.h"
41 #include "ash/wm/system_background_controller.h"
42 #include "ash/wm/system_modal_container_layout_manager.h"
43 #include "ash/wm/window_properties.h"
44 #include "ash/wm/window_state.h"
45 #include "ash/wm/window_util.h"
46 #include "ash/wm/workspace/workspace_layout_manager.h"
47 #include "ash/wm/workspace_controller.h"
48 #include "base/command_line.h"
49 #include "base/time/time.h"
50 #include "ui/aura/client/aura_constants.h"
51 #include "ui/aura/client/drag_drop_client.h"
52 #include "ui/aura/client/screen_position_client.h"
53 #include "ui/aura/client/tooltip_client.h"
54 #include "ui/aura/window.h"
55 #include "ui/aura/window_delegate.h"
56 #include "ui/aura/window_event_dispatcher.h"
57 #include "ui/aura/window_observer.h"
58 #include "ui/aura/window_tracker.h"
59 #include "ui/base/hit_test.h"
60 #include "ui/base/models/menu_model.h"
61 #include "ui/gfx/display.h"
62 #include "ui/gfx/screen.h"
63 #include "ui/keyboard/keyboard_controller.h"
64 #include "ui/keyboard/keyboard_util.h"
65 #include "ui/views/controls/menu/menu_runner.h"
66 #include "ui/views/corewm/capture_controller.h"
67 #include "ui/views/corewm/visibility_controller.h"
68 #include "ui/views/corewm/window_util.h"
69 #include "ui/views/view_model.h"
70 #include "ui/views/view_model_utils.h"
71 #include "ui/wm/public/easy_resize_window_targeter.h"
72 #include "ui/wm/public/window_types.h"
74 #if defined(OS_CHROMEOS)
75 #include "ash/wm/boot_splash_screen_chromeos.h"
76 #endif
78 namespace ash {
79 namespace {
81 #if defined(OS_CHROMEOS)
82 // Duration for the animation that hides the boot splash screen, in
83 // milliseconds. This should be short enough in relation to
84 // wm/window_animation.cc's brightness/grayscale fade animation that the login
85 // background image animation isn't hidden by the splash screen animation.
86 const int kBootSplashScreenHideDurationMs = 500;
87 #endif
89 // Creates a new window for use as a container.
90 aura::Window* CreateContainer(int window_id,
91 const char* name,
92 aura::Window* parent) {
93 aura::Window* container = new aura::Window(NULL);
94 container->set_id(window_id);
95 container->SetName(name);
96 container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
97 parent->AddChild(container);
98 if (window_id != internal::kShellWindowId_UnparentedControlContainer)
99 container->Show();
100 return container;
103 float ToRelativeValue(int value, int src, int dst) {
104 return static_cast<float>(value) / static_cast<float>(src) * dst;
107 void MoveOriginRelativeToSize(const gfx::Size& src_size,
108 const gfx::Size& dst_size,
109 gfx::Rect* bounds_in_out) {
110 gfx::Point origin = bounds_in_out->origin();
111 bounds_in_out->set_origin(gfx::Point(
112 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
113 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
116 // Reparents |window| to |new_parent|.
117 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
118 const gfx::Size src_size = window->parent()->bounds().size();
119 const gfx::Size dst_size = new_parent->bounds().size();
120 // Update the restore bounds to make it relative to the display.
121 wm::WindowState* state = wm::GetWindowState(window);
122 gfx::Rect restore_bounds;
123 bool has_restore_bounds = state->HasRestoreBounds();
125 bool update_bounds = (state->IsNormalOrSnapped() || state->IsMinimized()) &&
126 new_parent->id() != internal::kShellWindowId_DockedContainer;
127 gfx::Rect local_bounds;
128 if (update_bounds) {
129 local_bounds = state->window()->bounds();
130 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
133 if (has_restore_bounds) {
134 restore_bounds = state->GetRestoreBoundsInParent();
135 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
138 new_parent->AddChild(window);
140 // Docked windows have bounds handled by the layout manager in AddChild().
141 if (update_bounds)
142 window->SetBounds(local_bounds);
144 if (has_restore_bounds)
145 state->SetRestoreBoundsInParent(restore_bounds);
148 // Reparents the appropriate set of windows from |src| to |dst|.
149 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
150 // Set of windows to move.
151 const int kContainerIdsToMove[] = {
152 internal::kShellWindowId_DefaultContainer,
153 internal::kShellWindowId_DockedContainer,
154 internal::kShellWindowId_PanelContainer,
155 internal::kShellWindowId_AlwaysOnTopContainer,
156 internal::kShellWindowId_SystemModalContainer,
157 internal::kShellWindowId_LockSystemModalContainer,
158 internal::kShellWindowId_InputMethodContainer,
159 internal::kShellWindowId_UnparentedControlContainer,
161 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
162 int id = kContainerIdsToMove[i];
163 aura::Window* src_container = Shell::GetContainer(src, id);
164 aura::Window* dst_container = Shell::GetContainer(dst, id);
165 while (!src_container->children().empty()) {
166 // Restart iteration from the source container windows each time as they
167 // may change as a result of moving other windows.
168 aura::Window::Windows::const_iterator iter =
169 src_container->children().begin();
170 while (iter != src_container->children().end() &&
171 internal::SystemModalContainerLayoutManager::IsModalBackground(
172 *iter)) {
173 ++iter;
175 // If the entire window list is modal background windows then stop.
176 if (iter == src_container->children().end())
177 break;
178 ReparentWindow(*iter, dst_container);
183 // Mark the container window so that a widget added to this container will
184 // use the virtual screeen coordinates instead of parent.
185 void SetUsesScreenCoordinates(aura::Window* container) {
186 container->SetProperty(internal::kUsesScreenCoordinatesKey, true);
189 // Mark the container window so that a widget added to this container will
190 // say in the same root window regardless of the bounds specified.
191 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
192 container->SetProperty(internal::kStayInSameRootWindowKey, true);
195 void SetUsesEasyResizeTargeter(aura::Window* container) {
196 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
197 -kResizeOutsideBoundsSize,
198 -kResizeOutsideBoundsSize,
199 -kResizeOutsideBoundsSize);
200 gfx::Insets touch_extend = mouse_extend.Scale(
201 kResizeOutsideBoundsScaleForTouch);
202 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
203 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
204 touch_extend)));
207 // A window delegate which does nothing. Used to create a window that
208 // is a event target, but do nothing.
209 class EmptyWindowDelegate : public aura::WindowDelegate {
210 public:
211 EmptyWindowDelegate() {}
212 virtual ~EmptyWindowDelegate() {}
214 // aura::WindowDelegate overrides:
215 virtual gfx::Size GetMinimumSize() const OVERRIDE {
216 return gfx::Size();
218 virtual gfx::Size GetMaximumSize() const OVERRIDE {
219 return gfx::Size();
221 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
222 const gfx::Rect& new_bounds) OVERRIDE {
224 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
225 return gfx::kNullCursor;
227 virtual int GetNonClientComponent(
228 const gfx::Point& point) const OVERRIDE {
229 return HTNOWHERE;
231 virtual bool ShouldDescendIntoChildForEventHandling(
232 aura::Window* child,
233 const gfx::Point& location) OVERRIDE {
234 return false;
236 virtual bool CanFocus() OVERRIDE {
237 return false;
239 virtual void OnCaptureLost() OVERRIDE {
241 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
243 virtual void OnDeviceScaleFactorChanged(
244 float device_scale_factor) OVERRIDE {
246 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
247 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
248 delete this;
250 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
252 virtual bool HasHitTestMask() const OVERRIDE {
253 return false;
255 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
256 virtual void DidRecreateLayer(ui::Layer* old_layer,
257 ui::Layer* new_layer) OVERRIDE {}
259 private:
260 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
263 } // namespace
265 namespace internal {
267 void RootWindowController::CreateForPrimaryDisplay(aura::WindowTreeHost* host) {
268 RootWindowController* controller = new RootWindowController(host);
269 controller->Init(RootWindowController::PRIMARY,
270 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
273 void RootWindowController::CreateForSecondaryDisplay(
274 aura::WindowTreeHost* host) {
275 RootWindowController* controller = new RootWindowController(host);
276 controller->Init(RootWindowController::SECONDARY, false /* first run */);
279 void RootWindowController::CreateForVirtualKeyboardDisplay(
280 aura::WindowTreeHost* host) {
281 RootWindowController* controller = new RootWindowController(host);
282 controller->Init(RootWindowController::VIRTUAL_KEYBOARD,
283 false /* first run */);
286 // static
287 RootWindowController* RootWindowController::ForShelf(aura::Window* window) {
288 return GetRootWindowController(window->GetRootWindow());
291 // static
292 RootWindowController* RootWindowController::ForWindow(
293 const aura::Window* window) {
294 return GetRootWindowController(window->GetRootWindow());
297 // static
298 RootWindowController* RootWindowController::ForTargetRootWindow() {
299 return internal::GetRootWindowController(Shell::GetTargetRootWindow());
302 // static
303 aura::Window* RootWindowController::GetContainerForWindow(
304 aura::Window* window) {
305 aura::Window* container = window->parent();
306 while (container && container->type() != ui::wm::WINDOW_TYPE_UNKNOWN)
307 container = container->parent();
308 return container;
311 RootWindowController::~RootWindowController() {
312 Shutdown();
313 host_.reset();
314 // The CaptureClient needs to be around for as long as the RootWindow is
315 // valid.
316 capture_client_.reset();
319 void RootWindowController::SetWallpaperController(
320 DesktopBackgroundWidgetController* controller) {
321 wallpaper_controller_.reset(controller);
324 void RootWindowController::SetAnimatingWallpaperController(
325 AnimatingDesktopController* controller) {
326 if (animating_wallpaper_controller_.get())
327 animating_wallpaper_controller_->StopAnimating();
328 animating_wallpaper_controller_.reset(controller);
331 void RootWindowController::Shutdown() {
332 Shell::GetInstance()->RemoveShellObserver(this);
334 if (animating_wallpaper_controller_.get())
335 animating_wallpaper_controller_->StopAnimating();
336 wallpaper_controller_.reset();
337 animating_wallpaper_controller_.reset();
339 // Change the target root window before closing child windows. If any child
340 // being removed triggers a relayout of the shelf it will try to build a
341 // window list adding windows from the target root window's containers which
342 // may have already gone away.
343 if (Shell::GetTargetRootWindow() == root_window()) {
344 Shell::GetInstance()->set_target_root_window(
345 Shell::GetPrimaryRootWindow() == root_window() ?
346 NULL : Shell::GetPrimaryRootWindow());
349 CloseChildWindows();
350 GetRootWindowSettings(root_window())->controller = NULL;
351 screen_dimmer_.reset();
352 workspace_controller_.reset();
353 // Forget with the display ID so that display lookup
354 // ends up with invalid display.
355 internal::GetRootWindowSettings(root_window())->display_id =
356 gfx::Display::kInvalidDisplayID;
357 // And this root window should no longer process events.
358 host()->dispatcher()->PrepareForShutdown();
360 system_background_.reset();
361 aura::client::SetScreenPositionClient(root_window(), NULL);
364 SystemModalContainerLayoutManager*
365 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
366 aura::Window* modal_container = NULL;
367 if (window) {
368 aura::Window* window_container = GetContainerForWindow(window);
369 if (window_container &&
370 window_container->id() >= kShellWindowId_LockScreenContainer) {
371 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
372 } else {
373 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
375 } else {
376 int modal_window_id = Shell::GetInstance()->session_state_delegate()
377 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
378 kShellWindowId_SystemModalContainer;
379 modal_container = GetContainer(modal_window_id);
381 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
382 modal_container->layout_manager()) : NULL;
385 aura::Window* RootWindowController::GetContainer(int container_id) {
386 return root_window()->GetChildById(container_id);
389 const aura::Window* RootWindowController::GetContainer(int container_id) const {
390 return host_->window()->GetChildById(container_id);
393 void RootWindowController::ShowShelf() {
394 if (!shelf_->shelf())
395 return;
396 shelf_->shelf()->SetVisible(true);
397 shelf_->status_area_widget()->Show();
400 void RootWindowController::OnShelfCreated() {
401 if (panel_layout_manager_)
402 panel_layout_manager_->SetShelf(shelf_->shelf());
403 if (docked_layout_manager_) {
404 docked_layout_manager_->SetShelf(shelf_->shelf());
405 if (shelf_->shelf_layout_manager())
406 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
410 void RootWindowController::UpdateAfterLoginStatusChange(
411 user::LoginStatus status) {
412 if (status != user::LOGGED_IN_NONE)
413 mouse_event_target_.reset();
414 if (shelf_->status_area_widget())
415 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
418 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
419 #if defined(OS_CHROMEOS)
420 if (CommandLine::ForCurrentProcess()->HasSwitch(
421 switches::kAshAnimateFromBootSplashScreen) &&
422 boot_splash_screen_.get()) {
423 // Make the splash screen fade out so it doesn't obscure the desktop
424 // wallpaper's brightness/grayscale animation.
425 boot_splash_screen_->StartHideAnimation(
426 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
428 #endif
431 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
432 // Make sure the wallpaper is visible.
433 system_background_->SetColor(SK_ColorBLACK);
434 #if defined(OS_CHROMEOS)
435 boot_splash_screen_.reset();
436 #endif
438 Shell::GetInstance()->user_wallpaper_delegate()->
439 OnWallpaperAnimationFinished();
440 // Only removes old component when wallpaper animation finished. If we
441 // remove the old one before the new wallpaper is done fading in there will
442 // be a white flash during the animation.
443 if (animating_wallpaper_controller()) {
444 DesktopBackgroundWidgetController* controller =
445 animating_wallpaper_controller()->GetController(true);
446 // |desktop_widget_| should be the same animating widget we try to move
447 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
448 // before move it to |kDesktopController|.
449 DCHECK_EQ(controller->widget(), widget);
450 // Release the old controller and close its background widget.
451 SetWallpaperController(controller);
455 void RootWindowController::CloseChildWindows() {
456 mouse_event_target_.reset();
458 // Deactivate keyboard container before closing child windows and shutting
459 // down associated layout managers.
460 DeactivateKeyboard(Shell::GetInstance()->keyboard_controller());
462 // panel_layout_manager_ needs to be shut down before windows are destroyed.
463 if (panel_layout_manager_) {
464 panel_layout_manager_->Shutdown();
465 panel_layout_manager_ = NULL;
467 // docked_layout_manager_ needs to be shut down before windows are destroyed.
468 if (docked_layout_manager_) {
469 if (shelf_ && shelf_->shelf_layout_manager())
470 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
471 docked_layout_manager_->Shutdown();
472 docked_layout_manager_ = NULL;
475 aura::client::SetDragDropClient(root_window(), NULL);
477 // TODO(harrym): Remove when Status Area Widget is a child view.
478 if (shelf_) {
479 shelf_->ShutdownStatusAreaWidget();
481 if (shelf_->shelf_layout_manager())
482 shelf_->shelf_layout_manager()->PrepareForShutdown();
485 // Close background widget first as it depends on tooltip.
486 wallpaper_controller_.reset();
487 animating_wallpaper_controller_.reset();
489 workspace_controller_.reset();
490 aura::client::SetTooltipClient(root_window(), NULL);
492 // Explicitly destroy top level windows. We do this as during part of
493 // destruction such windows may query the RootWindow for state.
494 std::queue<aura::Window*> non_toplevel_windows;
495 non_toplevel_windows.push(root_window());
496 while (!non_toplevel_windows.empty()) {
497 aura::Window* non_toplevel_window = non_toplevel_windows.front();
498 non_toplevel_windows.pop();
499 aura::WindowTracker toplevel_windows;
500 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
501 aura::Window* child = non_toplevel_window->children()[i];
502 if (!child->owned_by_parent())
503 continue;
504 if (child->delegate())
505 toplevel_windows.Add(child);
506 else
507 non_toplevel_windows.push(child);
509 while (!toplevel_windows.windows().empty())
510 delete *toplevel_windows.windows().begin();
512 // And then remove the containers.
513 while (!root_window()->children().empty()) {
514 aura::Window* window = root_window()->children()[0];
515 if (window->owned_by_parent()) {
516 delete window;
517 } else {
518 root_window()->RemoveChild(window);
522 shelf_.reset();
525 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
526 // Forget the shelf early so that shelf don't update itself using wrong
527 // display info.
528 workspace_controller_->SetShelf(NULL);
529 ReparentAllWindows(root_window(), dst);
532 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
533 return shelf_->shelf_layout_manager();
536 SystemTray* RootWindowController::GetSystemTray() {
537 // We assume in throughout the code that this will not return NULL. If code
538 // triggers this for valid reasons, it should test status_area_widget first.
539 CHECK(shelf_->status_area_widget());
540 return shelf_->status_area_widget()->system_tray();
543 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
544 ui::MenuSourceType source_type) {
545 DCHECK(Shell::GetInstance()->delegate());
546 scoped_ptr<ui::MenuModel> menu_model(
547 Shell::GetInstance()->delegate()->CreateContextMenu(root_window(),
548 NULL,
549 NULL));
550 if (!menu_model)
551 return;
553 // Background controller may not be set yet if user clicked on status are
554 // before initial animation completion. See crbug.com/222218
555 if (!wallpaper_controller_.get())
556 return;
558 views::MenuRunner menu_runner(menu_model.get());
559 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
560 NULL, gfx::Rect(location_in_screen, gfx::Size()),
561 views::MenuItemView::TOPLEFT, source_type,
562 views::MenuRunner::CONTEXT_MENU) ==
563 views::MenuRunner::MENU_DELETED) {
564 return;
567 Shell::GetInstance()->UpdateShelfVisibility();
570 void RootWindowController::UpdateShelfVisibility() {
571 shelf_->shelf_layout_manager()->UpdateVisibilityState();
574 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
575 const aura::Window* topmost_window = NULL;
576 const aura::Window* active_window = wm::GetActiveWindow();
577 if (active_window && active_window->GetRootWindow() == root_window() &&
578 IsSwitchableContainer(active_window->parent())) {
579 // Use the active window when it is on the current root window to determine
580 // the fullscreen state to allow temporarily using a panel or docked window
581 // (which are always above the default container) while a fullscreen
582 // window is open. We only use the active window when in a switchable
583 // container as the launcher should not exit fullscreen mode.
584 topmost_window = active_window;
585 } else {
586 // Otherwise, use the topmost window on the root window's default container
587 // when there is no active window on this root window.
588 const aura::Window::Windows& windows =
589 GetContainer(kShellWindowId_DefaultContainer)->children();
590 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
591 iter != windows.rend(); ++iter) {
592 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
593 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
594 (*iter)->layer()->GetTargetVisibility()) {
595 topmost_window = *iter;
596 break;
600 while (topmost_window) {
601 if (wm::GetWindowState(topmost_window)->IsFullscreen())
602 return topmost_window;
603 topmost_window = views::corewm::GetTransientParent(topmost_window);
605 return NULL;
608 void RootWindowController::ActivateKeyboard(
609 keyboard::KeyboardController* keyboard_controller) {
610 if (!keyboard::IsKeyboardEnabled() ||
611 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
612 return;
614 DCHECK(keyboard_controller);
615 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
616 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
617 keyboard_controller->AddObserver(panel_layout_manager_);
618 keyboard_controller->AddObserver(docked_layout_manager_);
620 aura::Window* parent = GetContainer(
621 kShellWindowId_VirtualKeyboardParentContainer);
622 DCHECK(parent);
623 aura::Window* keyboard_container =
624 keyboard_controller->GetContainerWindow();
625 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
626 parent->AddChild(keyboard_container);
627 // TODO(oshima): Bounds of keyboard container should be handled by
628 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
629 keyboard_container->SetBounds(parent->bounds());
632 void RootWindowController::DeactivateKeyboard(
633 keyboard::KeyboardController* keyboard_controller) {
634 if (!keyboard_controller ||
635 !keyboard_controller->keyboard_container_initialized()) {
636 return;
638 aura::Window* keyboard_container =
639 keyboard_controller->GetContainerWindow();
640 if (keyboard_container->GetRootWindow() == root_window()) {
641 aura::Window* parent = GetContainer(
642 kShellWindowId_VirtualKeyboardParentContainer);
643 DCHECK(parent);
644 parent->RemoveChild(keyboard_container);
645 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
646 // Virtual keyboard may be deactivated while still showing, notify all
647 // observers that keyboard bounds changed to 0 before remove them.
648 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
649 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
650 keyboard_controller->RemoveObserver(panel_layout_manager_);
651 keyboard_controller->RemoveObserver(docked_layout_manager_);
656 ////////////////////////////////////////////////////////////////////////////////
657 // RootWindowController, private:
659 RootWindowController::RootWindowController(aura::WindowTreeHost* host)
660 : host_(host),
661 root_window_layout_(NULL),
662 docked_layout_manager_(NULL),
663 panel_layout_manager_(NULL),
664 touch_hud_debug_(NULL),
665 touch_hud_projection_(NULL) {
666 GetRootWindowSettings(root_window())->controller = this;
667 screen_dimmer_.reset(new ScreenDimmer(root_window()));
669 stacking_controller_.reset(new StackingController);
670 aura::client::SetWindowTreeClient(root_window(), stacking_controller_.get());
671 capture_client_.reset(
672 new views::corewm::ScopedCaptureClient(root_window()));
675 void RootWindowController::Init(RootWindowType root_window_type,
676 bool first_run_after_boot) {
677 Shell* shell = Shell::GetInstance();
678 shell->InitRootWindow(root_window());
680 host_->SetCursor(ui::kCursorPointer);
681 CreateContainersInRootWindow(root_window());
683 if (root_window_type == VIRTUAL_KEYBOARD) {
684 shell->InitKeyboard();
685 return;
688 CreateSystemBackground(first_run_after_boot);
690 InitLayoutManagers();
691 InitTouchHuds();
693 if (Shell::GetPrimaryRootWindowController()->
694 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
695 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
698 shell->AddShellObserver(this);
700 if (root_window_type == PRIMARY) {
701 root_window_layout()->OnWindowResized();
702 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
703 shell->InitKeyboard();
704 } else {
705 root_window_layout()->OnWindowResized();
706 shell->desktop_background_controller()->OnRootWindowAdded(root_window());
707 shell->high_contrast_controller()->OnRootWindowAdded(root_window());
708 host_->Show();
710 // Create a shelf if a user is already logged in.
711 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
712 shelf()->CreateShelf();
716 void RootWindowController::InitLayoutManagers() {
717 root_window_layout_ = new RootWindowLayoutManager(root_window());
718 root_window()->SetLayoutManager(root_window_layout_);
720 aura::Window* default_container =
721 GetContainer(kShellWindowId_DefaultContainer);
722 // Workspace manager has its own layout managers.
723 workspace_controller_.reset(
724 new WorkspaceController(default_container));
726 aura::Window* always_on_top_container =
727 GetContainer(kShellWindowId_AlwaysOnTopContainer);
728 always_on_top_container->SetLayoutManager(
729 new internal::WorkspaceLayoutManager(
730 always_on_top_container));
731 always_on_top_controller_.reset(new internal::AlwaysOnTopController);
732 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
734 DCHECK(!shelf_.get());
735 aura::Window* shelf_container =
736 GetContainer(internal::kShellWindowId_ShelfContainer);
737 // TODO(harrym): Remove when status area is view.
738 aura::Window* status_container =
739 GetContainer(internal::kShellWindowId_StatusContainer);
740 shelf_.reset(new ShelfWidget(
741 shelf_container, status_container, workspace_controller()));
743 if (!Shell::GetInstance()->session_state_delegate()->
744 IsActiveUserSessionStarted()) {
745 // This window exists only to be a event target on login screen.
746 // It does not have to handle events, nor be visible.
747 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
748 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
750 aura::Window* lock_background_container =
751 GetContainer(internal::kShellWindowId_LockScreenBackgroundContainer);
752 lock_background_container->AddChild(mouse_event_target_.get());
753 mouse_event_target_->Show();
756 // Create Docked windows layout manager
757 aura::Window* docked_container = GetContainer(
758 internal::kShellWindowId_DockedContainer);
759 docked_layout_manager_ =
760 new internal::DockedWindowLayoutManager(docked_container,
761 workspace_controller());
762 docked_container->SetLayoutManager(docked_layout_manager_);
764 // Create Panel layout manager
765 aura::Window* panel_container = GetContainer(
766 internal::kShellWindowId_PanelContainer);
767 panel_layout_manager_ =
768 new internal::PanelLayoutManager(panel_container);
769 panel_container->SetLayoutManager(panel_layout_manager_);
770 panel_container_handler_.reset(new PanelWindowEventHandler);
771 panel_container->AddPreTargetHandler(panel_container_handler_.get());
774 void RootWindowController::InitTouchHuds() {
775 CommandLine* command_line = CommandLine::ForCurrentProcess();
776 if (command_line->HasSwitch(switches::kAshTouchHud))
777 set_touch_hud_debug(new TouchHudDebug(root_window()));
778 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
779 EnableTouchHudProjection();
782 void RootWindowController::CreateSystemBackground(
783 bool is_first_run_after_boot) {
784 SkColor color = SK_ColorBLACK;
785 #if defined(OS_CHROMEOS)
786 if (is_first_run_after_boot)
787 color = kChromeOsBootColor;
788 #endif
789 system_background_.reset(
790 new SystemBackgroundController(root_window(), color));
792 #if defined(OS_CHROMEOS)
793 // Make a copy of the system's boot splash screen so we can composite it
794 // onscreen until the desktop background is ready.
795 if (is_first_run_after_boot &&
796 (CommandLine::ForCurrentProcess()->HasSwitch(
797 switches::kAshCopyHostBackgroundAtBoot) ||
798 CommandLine::ForCurrentProcess()->HasSwitch(
799 switches::kAshAnimateFromBootSplashScreen)))
800 boot_splash_screen_.reset(new BootSplashScreen(host()));
801 #endif
804 void RootWindowController::CreateContainersInRootWindow(
805 aura::Window* root_window) {
806 // These containers are just used by PowerButtonController to animate groups
807 // of containers simultaneously without messing up the current transformations
808 // on those containers. These are direct children of the root window; all of
809 // the other containers are their children.
811 // The desktop background container is not part of the lock animation, so it
812 // is not included in those animate groups.
813 // When screen is locked desktop background is moved to lock screen background
814 // container (moved back on unlock). We want to make sure that there's an
815 // opaque layer occluding the non-lock-screen layers.
816 aura::Window* desktop_background_container = CreateContainer(
817 kShellWindowId_DesktopBackgroundContainer,
818 "DesktopBackgroundContainer",
819 root_window);
820 views::corewm::SetChildWindowVisibilityChangesAnimated(
821 desktop_background_container);
823 aura::Window* non_lock_screen_containers = CreateContainer(
824 kShellWindowId_NonLockScreenContainersContainer,
825 "NonLockScreenContainersContainer",
826 root_window);
828 aura::Window* lock_background_containers = CreateContainer(
829 kShellWindowId_LockScreenBackgroundContainer,
830 "LockScreenBackgroundContainer",
831 root_window);
832 views::corewm::SetChildWindowVisibilityChangesAnimated(
833 lock_background_containers);
835 aura::Window* lock_screen_containers = CreateContainer(
836 kShellWindowId_LockScreenContainersContainer,
837 "LockScreenContainersContainer",
838 root_window);
839 aura::Window* lock_screen_related_containers = CreateContainer(
840 kShellWindowId_LockScreenRelatedContainersContainer,
841 "LockScreenRelatedContainersContainer",
842 root_window);
844 CreateContainer(kShellWindowId_UnparentedControlContainer,
845 "UnparentedControlContainer",
846 non_lock_screen_containers);
848 aura::Window* default_container = CreateContainer(
849 kShellWindowId_DefaultContainer,
850 "DefaultContainer",
851 non_lock_screen_containers);
852 views::corewm::SetChildWindowVisibilityChangesAnimated(default_container);
853 SetUsesScreenCoordinates(default_container);
854 SetUsesEasyResizeTargeter(default_container);
856 aura::Window* always_on_top_container = CreateContainer(
857 kShellWindowId_AlwaysOnTopContainer,
858 "AlwaysOnTopContainer",
859 non_lock_screen_containers);
860 views::corewm::SetChildWindowVisibilityChangesAnimated(
861 always_on_top_container);
862 SetUsesScreenCoordinates(always_on_top_container);
864 aura::Window* docked_container = CreateContainer(
865 kShellWindowId_DockedContainer,
866 "DockedContainer",
867 non_lock_screen_containers);
868 views::corewm::SetChildWindowVisibilityChangesAnimated(docked_container);
869 SetUsesScreenCoordinates(docked_container);
870 SetUsesEasyResizeTargeter(docked_container);
872 aura::Window* shelf_container =
873 CreateContainer(kShellWindowId_ShelfContainer,
874 "ShelfContainer",
875 non_lock_screen_containers);
876 SetUsesScreenCoordinates(shelf_container);
877 DescendantShouldStayInSameRootWindow(shelf_container);
879 aura::Window* panel_container = CreateContainer(
880 kShellWindowId_PanelContainer,
881 "PanelContainer",
882 non_lock_screen_containers);
883 SetUsesScreenCoordinates(panel_container);
884 SetUsesEasyResizeTargeter(panel_container);
886 aura::Window* shelf_bubble_container =
887 CreateContainer(kShellWindowId_ShelfBubbleContainer,
888 "ShelfBubbleContainer",
889 non_lock_screen_containers);
890 SetUsesScreenCoordinates(shelf_bubble_container);
891 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
893 aura::Window* app_list_container =
894 CreateContainer(kShellWindowId_AppListContainer,
895 "AppListContainer",
896 non_lock_screen_containers);
897 SetUsesScreenCoordinates(app_list_container);
899 aura::Window* modal_container = CreateContainer(
900 kShellWindowId_SystemModalContainer,
901 "SystemModalContainer",
902 non_lock_screen_containers);
903 modal_container->SetLayoutManager(
904 new SystemModalContainerLayoutManager(modal_container));
905 views::corewm::SetChildWindowVisibilityChangesAnimated(modal_container);
906 SetUsesScreenCoordinates(modal_container);
907 SetUsesEasyResizeTargeter(modal_container);
909 aura::Window* input_method_container = CreateContainer(
910 kShellWindowId_InputMethodContainer,
911 "InputMethodContainer",
912 non_lock_screen_containers);
913 views::corewm::SetChildWindowVisibilityChangesAnimated(
914 input_method_container);
915 SetUsesScreenCoordinates(input_method_container);
917 // TODO(beng): Figure out if we can make this use
918 // SystemModalContainerEventFilter instead of stops_event_propagation.
919 aura::Window* lock_container = CreateContainer(
920 kShellWindowId_LockScreenContainer,
921 "LockScreenContainer",
922 lock_screen_containers);
923 lock_container->SetLayoutManager(
924 new internal::WorkspaceLayoutManager(lock_container));
925 SetUsesScreenCoordinates(lock_container);
926 // TODO(beng): stopsevents
928 aura::Window* lock_modal_container = CreateContainer(
929 kShellWindowId_LockSystemModalContainer,
930 "LockSystemModalContainer",
931 lock_screen_containers);
932 lock_modal_container->SetLayoutManager(
933 new SystemModalContainerLayoutManager(lock_modal_container));
934 views::corewm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
935 SetUsesScreenCoordinates(lock_modal_container);
936 SetUsesEasyResizeTargeter(lock_modal_container);
938 aura::Window* status_container =
939 CreateContainer(kShellWindowId_StatusContainer,
940 "StatusContainer",
941 lock_screen_related_containers);
942 SetUsesScreenCoordinates(status_container);
943 DescendantShouldStayInSameRootWindow(status_container);
945 aura::Window* settings_bubble_container = CreateContainer(
946 kShellWindowId_SettingBubbleContainer,
947 "SettingBubbleContainer",
948 lock_screen_related_containers);
949 views::corewm::SetChildWindowVisibilityChangesAnimated(
950 settings_bubble_container);
951 SetUsesScreenCoordinates(settings_bubble_container);
952 DescendantShouldStayInSameRootWindow(settings_bubble_container);
954 aura::Window* menu_container = CreateContainer(
955 kShellWindowId_MenuContainer,
956 "MenuContainer",
957 lock_screen_related_containers);
958 views::corewm::SetChildWindowVisibilityChangesAnimated(menu_container);
959 SetUsesScreenCoordinates(menu_container);
961 aura::Window* drag_drop_container = CreateContainer(
962 kShellWindowId_DragImageAndTooltipContainer,
963 "DragImageAndTooltipContainer",
964 lock_screen_related_containers);
965 views::corewm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
966 SetUsesScreenCoordinates(drag_drop_container);
968 aura::Window* overlay_container = CreateContainer(
969 kShellWindowId_OverlayContainer,
970 "OverlayContainer",
971 lock_screen_related_containers);
972 SetUsesScreenCoordinates(overlay_container);
974 aura::Window* virtual_keyboard_parent_container = CreateContainer(
975 kShellWindowId_VirtualKeyboardParentContainer,
976 "VirtualKeyboardParentContainer",
977 root_window);
978 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
980 #if defined(OS_CHROMEOS)
981 aura::Window* mouse_cursor_container = CreateContainer(
982 kShellWindowId_MouseCursorContainer,
983 "MouseCursorContainer",
984 root_window);
985 SetUsesScreenCoordinates(mouse_cursor_container);
986 #endif
988 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
989 "PowerButtonAnimationContainer", root_window);
992 void RootWindowController::EnableTouchHudProjection() {
993 if (touch_hud_projection_)
994 return;
995 set_touch_hud_projection(new TouchHudProjection(root_window()));
998 void RootWindowController::DisableTouchHudProjection() {
999 if (!touch_hud_projection_)
1000 return;
1001 touch_hud_projection_->Remove();
1004 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1005 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1008 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1009 if (enabled)
1010 EnableTouchHudProjection();
1011 else
1012 DisableTouchHudProjection();
1015 RootWindowController* GetRootWindowController(
1016 const aura::Window* root_window) {
1017 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1020 } // namespace internal
1021 } // namespace ash