Histograms for safe-browsing storage version changes.
[chromium-blink-merge.git] / ash / root_window_controller.cc
blobd4878b647d1b9b3ae510e46c37c4e000c09a87e0
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/screen_position_client.h"
52 #include "ui/aura/window.h"
53 #include "ui/aura/window_delegate.h"
54 #include "ui/aura/window_event_dispatcher.h"
55 #include "ui/aura/window_observer.h"
56 #include "ui/aura/window_tracker.h"
57 #include "ui/base/hit_test.h"
58 #include "ui/base/models/menu_model.h"
59 #include "ui/gfx/display.h"
60 #include "ui/gfx/screen.h"
61 #include "ui/keyboard/keyboard_controller.h"
62 #include "ui/keyboard/keyboard_util.h"
63 #include "ui/views/controls/menu/menu_runner.h"
64 #include "ui/views/view_model.h"
65 #include "ui/views/view_model_utils.h"
66 #include "ui/wm/core/capture_controller.h"
67 #include "ui/wm/core/easy_resize_window_targeter.h"
68 #include "ui/wm/core/visibility_controller.h"
69 #include "ui/wm/core/window_util.h"
70 #include "ui/wm/public/drag_drop_client.h"
71 #include "ui/wm/public/tooltip_client.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 != 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() != 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 kShellWindowId_DefaultContainer,
153 kShellWindowId_DockedContainer,
154 kShellWindowId_PanelContainer,
155 kShellWindowId_AlwaysOnTopContainer,
156 kShellWindowId_SystemModalContainer,
157 kShellWindowId_LockSystemModalContainer,
158 kShellWindowId_InputMethodContainer,
159 kShellWindowId_UnparentedControlContainer, };
160 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
161 int id = kContainerIdsToMove[i];
162 aura::Window* src_container = Shell::GetContainer(src, id);
163 aura::Window* dst_container = Shell::GetContainer(dst, id);
164 while (!src_container->children().empty()) {
165 // Restart iteration from the source container windows each time as they
166 // may change as a result of moving other windows.
167 aura::Window::Windows::const_iterator iter =
168 src_container->children().begin();
169 while (iter != src_container->children().end() &&
170 SystemModalContainerLayoutManager::IsModalBackground(*iter)) {
171 ++iter;
173 // If the entire window list is modal background windows then stop.
174 if (iter == src_container->children().end())
175 break;
176 ReparentWindow(*iter, dst_container);
181 // Mark the container window so that a widget added to this container will
182 // use the virtual screeen coordinates instead of parent.
183 void SetUsesScreenCoordinates(aura::Window* container) {
184 container->SetProperty(kUsesScreenCoordinatesKey, true);
187 // Mark the container window so that a widget added to this container will
188 // say in the same root window regardless of the bounds specified.
189 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
190 container->SetProperty(kStayInSameRootWindowKey, true);
193 void SetUsesEasyResizeTargeter(aura::Window* container) {
194 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
195 -kResizeOutsideBoundsSize,
196 -kResizeOutsideBoundsSize,
197 -kResizeOutsideBoundsSize);
198 gfx::Insets touch_extend = mouse_extend.Scale(
199 kResizeOutsideBoundsScaleForTouch);
200 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
201 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
202 touch_extend)));
205 // A window delegate which does nothing. Used to create a window that
206 // is a event target, but do nothing.
207 class EmptyWindowDelegate : public aura::WindowDelegate {
208 public:
209 EmptyWindowDelegate() {}
210 virtual ~EmptyWindowDelegate() {}
212 // aura::WindowDelegate overrides:
213 virtual gfx::Size GetMinimumSize() const OVERRIDE {
214 return gfx::Size();
216 virtual gfx::Size GetMaximumSize() const OVERRIDE {
217 return gfx::Size();
219 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
220 const gfx::Rect& new_bounds) OVERRIDE {
222 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
223 return gfx::kNullCursor;
225 virtual int GetNonClientComponent(
226 const gfx::Point& point) const OVERRIDE {
227 return HTNOWHERE;
229 virtual bool ShouldDescendIntoChildForEventHandling(
230 aura::Window* child,
231 const gfx::Point& location) OVERRIDE {
232 return false;
234 virtual bool CanFocus() OVERRIDE {
235 return false;
237 virtual void OnCaptureLost() OVERRIDE {
239 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
241 virtual void OnDeviceScaleFactorChanged(
242 float device_scale_factor) OVERRIDE {
244 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
245 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
246 delete this;
248 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
250 virtual bool HasHitTestMask() const OVERRIDE {
251 return false;
253 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
255 private:
256 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
259 } // namespace
261 void RootWindowController::CreateForPrimaryDisplay(aura::WindowTreeHost* host) {
262 RootWindowController* controller = new RootWindowController(host);
263 controller->Init(RootWindowController::PRIMARY,
264 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
267 void RootWindowController::CreateForSecondaryDisplay(
268 aura::WindowTreeHost* host) {
269 RootWindowController* controller = new RootWindowController(host);
270 controller->Init(RootWindowController::SECONDARY, false /* first run */);
273 void RootWindowController::CreateForVirtualKeyboardDisplay(
274 aura::WindowTreeHost* host) {
275 RootWindowController* controller = new RootWindowController(host);
276 controller->Init(RootWindowController::VIRTUAL_KEYBOARD,
277 false /* first run */);
280 // static
281 RootWindowController* RootWindowController::ForShelf(aura::Window* window) {
282 return GetRootWindowController(window->GetRootWindow());
285 // static
286 RootWindowController* RootWindowController::ForWindow(
287 const aura::Window* window) {
288 return GetRootWindowController(window->GetRootWindow());
291 // static
292 RootWindowController* RootWindowController::ForTargetRootWindow() {
293 return GetRootWindowController(Shell::GetTargetRootWindow());
296 // static
297 aura::Window* RootWindowController::GetContainerForWindow(
298 aura::Window* window) {
299 aura::Window* container = window->parent();
300 while (container && container->type() != ui::wm::WINDOW_TYPE_UNKNOWN)
301 container = container->parent();
302 return container;
305 RootWindowController::~RootWindowController() {
306 Shutdown();
307 host_.reset();
308 // The CaptureClient needs to be around for as long as the RootWindow is
309 // valid.
310 capture_client_.reset();
313 void RootWindowController::SetWallpaperController(
314 DesktopBackgroundWidgetController* controller) {
315 wallpaper_controller_.reset(controller);
318 void RootWindowController::SetAnimatingWallpaperController(
319 AnimatingDesktopController* controller) {
320 if (animating_wallpaper_controller_.get())
321 animating_wallpaper_controller_->StopAnimating();
322 animating_wallpaper_controller_.reset(controller);
325 void RootWindowController::Shutdown() {
326 Shell::GetInstance()->RemoveShellObserver(this);
328 if (animating_wallpaper_controller_.get())
329 animating_wallpaper_controller_->StopAnimating();
330 wallpaper_controller_.reset();
331 animating_wallpaper_controller_.reset();
333 // Change the target root window before closing child windows. If any child
334 // being removed triggers a relayout of the shelf it will try to build a
335 // window list adding windows from the target root window's containers which
336 // may have already gone away.
337 if (Shell::GetTargetRootWindow() == root_window()) {
338 Shell::GetInstance()->set_target_root_window(
339 Shell::GetPrimaryRootWindow() == root_window() ?
340 NULL : Shell::GetPrimaryRootWindow());
343 CloseChildWindows();
344 GetRootWindowSettings(root_window())->controller = NULL;
345 screen_dimmer_.reset();
346 workspace_controller_.reset();
347 // Forget with the display ID so that display lookup
348 // ends up with invalid display.
349 GetRootWindowSettings(root_window())->display_id =
350 gfx::Display::kInvalidDisplayID;
351 GetRootWindowSettings(root_window())->shutdown = true;
353 system_background_.reset();
354 aura::client::SetScreenPositionClient(root_window(), NULL);
357 SystemModalContainerLayoutManager*
358 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
359 aura::Window* modal_container = NULL;
360 if (window) {
361 aura::Window* window_container = GetContainerForWindow(window);
362 if (window_container &&
363 window_container->id() >= kShellWindowId_LockScreenContainer) {
364 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
365 } else {
366 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
368 } else {
369 int modal_window_id = Shell::GetInstance()->session_state_delegate()
370 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
371 kShellWindowId_SystemModalContainer;
372 modal_container = GetContainer(modal_window_id);
374 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
375 modal_container->layout_manager()) : NULL;
378 aura::Window* RootWindowController::GetContainer(int container_id) {
379 return root_window()->GetChildById(container_id);
382 const aura::Window* RootWindowController::GetContainer(int container_id) const {
383 return host_->window()->GetChildById(container_id);
386 void RootWindowController::ShowShelf() {
387 if (!shelf_->shelf())
388 return;
389 shelf_->shelf()->SetVisible(true);
390 shelf_->status_area_widget()->Show();
393 void RootWindowController::OnShelfCreated() {
394 if (panel_layout_manager_)
395 panel_layout_manager_->SetShelf(shelf_->shelf());
396 if (docked_layout_manager_) {
397 docked_layout_manager_->SetShelf(shelf_->shelf());
398 if (shelf_->shelf_layout_manager())
399 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
403 void RootWindowController::UpdateAfterLoginStatusChange(
404 user::LoginStatus status) {
405 if (status != user::LOGGED_IN_NONE)
406 mouse_event_target_.reset();
407 if (shelf_->status_area_widget())
408 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
411 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
412 #if defined(OS_CHROMEOS)
413 if (CommandLine::ForCurrentProcess()->HasSwitch(
414 switches::kAshAnimateFromBootSplashScreen) &&
415 boot_splash_screen_.get()) {
416 // Make the splash screen fade out so it doesn't obscure the desktop
417 // wallpaper's brightness/grayscale animation.
418 boot_splash_screen_->StartHideAnimation(
419 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
421 #endif
424 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
425 // Make sure the wallpaper is visible.
426 system_background_->SetColor(SK_ColorBLACK);
427 #if defined(OS_CHROMEOS)
428 boot_splash_screen_.reset();
429 #endif
431 Shell::GetInstance()->user_wallpaper_delegate()->
432 OnWallpaperAnimationFinished();
433 // Only removes old component when wallpaper animation finished. If we
434 // remove the old one before the new wallpaper is done fading in there will
435 // be a white flash during the animation.
436 if (animating_wallpaper_controller()) {
437 DesktopBackgroundWidgetController* controller =
438 animating_wallpaper_controller()->GetController(true);
439 // |desktop_widget_| should be the same animating widget we try to move
440 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
441 // before move it to |kDesktopController|.
442 DCHECK_EQ(controller->widget(), widget);
443 // Release the old controller and close its background widget.
444 SetWallpaperController(controller);
448 void RootWindowController::CloseChildWindows() {
449 mouse_event_target_.reset();
451 // Deactivate keyboard container before closing child windows and shutting
452 // down associated layout managers.
453 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
455 // panel_layout_manager_ needs to be shut down before windows are destroyed.
456 if (panel_layout_manager_) {
457 panel_layout_manager_->Shutdown();
458 panel_layout_manager_ = NULL;
460 // docked_layout_manager_ needs to be shut down before windows are destroyed.
461 if (docked_layout_manager_) {
462 if (shelf_ && shelf_->shelf_layout_manager())
463 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
464 docked_layout_manager_->Shutdown();
465 docked_layout_manager_ = NULL;
468 aura::client::SetDragDropClient(root_window(), NULL);
470 // TODO(harrym): Remove when Status Area Widget is a child view.
471 if (shelf_) {
472 shelf_->ShutdownStatusAreaWidget();
474 if (shelf_->shelf_layout_manager())
475 shelf_->shelf_layout_manager()->PrepareForShutdown();
478 // Close background widget first as it depends on tooltip.
479 wallpaper_controller_.reset();
480 animating_wallpaper_controller_.reset();
482 workspace_controller_.reset();
483 aura::client::SetTooltipClient(root_window(), NULL);
485 // Explicitly destroy top level windows. We do this as during part of
486 // destruction such windows may query the RootWindow for state.
487 std::queue<aura::Window*> non_toplevel_windows;
488 non_toplevel_windows.push(root_window());
489 while (!non_toplevel_windows.empty()) {
490 aura::Window* non_toplevel_window = non_toplevel_windows.front();
491 non_toplevel_windows.pop();
492 aura::WindowTracker toplevel_windows;
493 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
494 aura::Window* child = non_toplevel_window->children()[i];
495 if (!child->owned_by_parent())
496 continue;
497 if (child->delegate())
498 toplevel_windows.Add(child);
499 else
500 non_toplevel_windows.push(child);
502 while (!toplevel_windows.windows().empty())
503 delete *toplevel_windows.windows().begin();
505 // And then remove the containers.
506 while (!root_window()->children().empty()) {
507 aura::Window* window = root_window()->children()[0];
508 if (window->owned_by_parent()) {
509 delete window;
510 } else {
511 root_window()->RemoveChild(window);
515 shelf_.reset();
518 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
519 // Forget the shelf early so that shelf don't update itself using wrong
520 // display info.
521 workspace_controller_->SetShelf(NULL);
522 ReparentAllWindows(root_window(), dst);
525 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
526 return shelf_->shelf_layout_manager();
529 SystemTray* RootWindowController::GetSystemTray() {
530 // We assume in throughout the code that this will not return NULL. If code
531 // triggers this for valid reasons, it should test status_area_widget first.
532 CHECK(shelf_->status_area_widget());
533 return shelf_->status_area_widget()->system_tray();
536 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
537 ui::MenuSourceType source_type) {
538 DCHECK(Shell::GetInstance()->delegate());
539 scoped_ptr<ui::MenuModel> menu_model(
540 Shell::GetInstance()->delegate()->CreateContextMenu(root_window(),
541 NULL,
542 NULL));
543 if (!menu_model)
544 return;
546 // Background controller may not be set yet if user clicked on status are
547 // before initial animation completion. See crbug.com/222218
548 if (!wallpaper_controller_.get())
549 return;
551 views::MenuRunner menu_runner(menu_model.get());
552 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
553 NULL, gfx::Rect(location_in_screen, gfx::Size()),
554 views::MenuItemView::TOPLEFT, source_type,
555 views::MenuRunner::CONTEXT_MENU) ==
556 views::MenuRunner::MENU_DELETED) {
557 return;
560 Shell::GetInstance()->UpdateShelfVisibility();
563 void RootWindowController::UpdateShelfVisibility() {
564 shelf_->shelf_layout_manager()->UpdateVisibilityState();
567 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
568 const aura::Window* topmost_window = NULL;
569 const aura::Window* active_window = wm::GetActiveWindow();
570 if (active_window && active_window->GetRootWindow() == root_window() &&
571 IsSwitchableContainer(active_window->parent())) {
572 // Use the active window when it is on the current root window to determine
573 // the fullscreen state to allow temporarily using a panel or docked window
574 // (which are always above the default container) while a fullscreen
575 // window is open. We only use the active window when in a switchable
576 // container as the launcher should not exit fullscreen mode.
577 topmost_window = active_window;
578 } else {
579 // Otherwise, use the topmost window on the root window's default container
580 // when there is no active window on this root window.
581 const aura::Window::Windows& windows =
582 GetContainer(kShellWindowId_DefaultContainer)->children();
583 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
584 iter != windows.rend(); ++iter) {
585 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
586 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
587 (*iter)->layer()->GetTargetVisibility()) {
588 topmost_window = *iter;
589 break;
593 while (topmost_window) {
594 if (wm::GetWindowState(topmost_window)->IsFullscreen())
595 return topmost_window;
596 topmost_window = ::wm::GetTransientParent(topmost_window);
598 return NULL;
601 void RootWindowController::ActivateKeyboard(
602 keyboard::KeyboardController* keyboard_controller) {
603 if (!keyboard::IsKeyboardEnabled() ||
604 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
605 return;
607 DCHECK(keyboard_controller);
608 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
609 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
610 keyboard_controller->AddObserver(panel_layout_manager_);
611 keyboard_controller->AddObserver(docked_layout_manager_);
612 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
614 aura::Window* parent = GetContainer(
615 kShellWindowId_VirtualKeyboardParentContainer);
616 DCHECK(parent);
617 aura::Window* keyboard_container =
618 keyboard_controller->GetContainerWindow();
619 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
620 parent->AddChild(keyboard_container);
621 // TODO(oshima): Bounds of keyboard container should be handled by
622 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
623 keyboard_container->SetBounds(parent->bounds());
626 void RootWindowController::DeactivateKeyboard(
627 keyboard::KeyboardController* keyboard_controller) {
628 if (!keyboard_controller ||
629 !keyboard_controller->keyboard_container_initialized()) {
630 return;
632 aura::Window* keyboard_container =
633 keyboard_controller->GetContainerWindow();
634 if (keyboard_container->GetRootWindow() == root_window()) {
635 aura::Window* parent = GetContainer(
636 kShellWindowId_VirtualKeyboardParentContainer);
637 DCHECK(parent);
638 parent->RemoveChild(keyboard_container);
639 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
640 // Virtual keyboard may be deactivated while still showing, notify all
641 // observers that keyboard bounds changed to 0 before remove them.
642 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
643 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
644 keyboard_controller->RemoveObserver(panel_layout_manager_);
645 keyboard_controller->RemoveObserver(docked_layout_manager_);
646 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
651 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
652 aura::Window* parent = GetContainer(
653 kShellWindowId_VirtualKeyboardParentContainer);
654 return parent ? parent->Contains(window) : false;
657 ////////////////////////////////////////////////////////////////////////////////
658 // RootWindowController, private:
660 RootWindowController::RootWindowController(aura::WindowTreeHost* host)
661 : host_(host),
662 root_window_layout_(NULL),
663 docked_layout_manager_(NULL),
664 panel_layout_manager_(NULL),
665 touch_hud_debug_(NULL),
666 touch_hud_projection_(NULL) {
667 GetRootWindowSettings(root_window())->controller = this;
668 screen_dimmer_.reset(new ScreenDimmer(root_window()));
670 stacking_controller_.reset(new StackingController);
671 aura::client::SetWindowTreeClient(root_window(), stacking_controller_.get());
672 capture_client_.reset(new ::wm::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 WorkspaceLayoutManager(always_on_top_container));
730 always_on_top_controller_.reset(new AlwaysOnTopController);
731 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
733 DCHECK(!shelf_.get());
734 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
735 // TODO(harrym): Remove when status area is view.
736 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
737 shelf_.reset(new ShelfWidget(
738 shelf_container, status_container, workspace_controller()));
740 if (!Shell::GetInstance()->session_state_delegate()->
741 IsActiveUserSessionStarted()) {
742 // This window exists only to be a event target on login screen.
743 // It does not have to handle events, nor be visible.
744 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
745 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
747 aura::Window* lock_background_container =
748 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
749 lock_background_container->AddChild(mouse_event_target_.get());
750 mouse_event_target_->Show();
753 // Create Docked windows layout manager
754 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
755 docked_layout_manager_ =
756 new DockedWindowLayoutManager(docked_container, workspace_controller());
757 docked_container->SetLayoutManager(docked_layout_manager_);
759 // Create Panel layout manager
760 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
761 panel_layout_manager_ = new PanelLayoutManager(panel_container);
762 panel_container->SetLayoutManager(panel_layout_manager_);
763 panel_container_handler_.reset(new PanelWindowEventHandler);
764 panel_container->AddPreTargetHandler(panel_container_handler_.get());
767 void RootWindowController::InitTouchHuds() {
768 CommandLine* command_line = CommandLine::ForCurrentProcess();
769 if (command_line->HasSwitch(switches::kAshTouchHud))
770 set_touch_hud_debug(new TouchHudDebug(root_window()));
771 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
772 EnableTouchHudProjection();
775 void RootWindowController::CreateSystemBackground(
776 bool is_first_run_after_boot) {
777 SkColor color = SK_ColorBLACK;
778 #if defined(OS_CHROMEOS)
779 if (is_first_run_after_boot)
780 color = kChromeOsBootColor;
781 #endif
782 system_background_.reset(
783 new SystemBackgroundController(root_window(), color));
785 #if defined(OS_CHROMEOS)
786 // Make a copy of the system's boot splash screen so we can composite it
787 // onscreen until the desktop background is ready.
788 if (is_first_run_after_boot &&
789 (CommandLine::ForCurrentProcess()->HasSwitch(
790 switches::kAshCopyHostBackgroundAtBoot) ||
791 CommandLine::ForCurrentProcess()->HasSwitch(
792 switches::kAshAnimateFromBootSplashScreen)))
793 boot_splash_screen_.reset(new BootSplashScreen(host()));
794 #endif
797 void RootWindowController::CreateContainersInRootWindow(
798 aura::Window* root_window) {
799 // These containers are just used by PowerButtonController to animate groups
800 // of containers simultaneously without messing up the current transformations
801 // on those containers. These are direct children of the root window; all of
802 // the other containers are their children.
804 // The desktop background container is not part of the lock animation, so it
805 // is not included in those animate groups.
806 // When screen is locked desktop background is moved to lock screen background
807 // container (moved back on unlock). We want to make sure that there's an
808 // opaque layer occluding the non-lock-screen layers.
809 aura::Window* desktop_background_container = CreateContainer(
810 kShellWindowId_DesktopBackgroundContainer,
811 "DesktopBackgroundContainer",
812 root_window);
813 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
815 aura::Window* non_lock_screen_containers = CreateContainer(
816 kShellWindowId_NonLockScreenContainersContainer,
817 "NonLockScreenContainersContainer",
818 root_window);
820 aura::Window* lock_background_containers = CreateContainer(
821 kShellWindowId_LockScreenBackgroundContainer,
822 "LockScreenBackgroundContainer",
823 root_window);
824 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
826 aura::Window* lock_screen_containers = CreateContainer(
827 kShellWindowId_LockScreenContainersContainer,
828 "LockScreenContainersContainer",
829 root_window);
830 aura::Window* lock_screen_related_containers = CreateContainer(
831 kShellWindowId_LockScreenRelatedContainersContainer,
832 "LockScreenRelatedContainersContainer",
833 root_window);
835 CreateContainer(kShellWindowId_UnparentedControlContainer,
836 "UnparentedControlContainer",
837 non_lock_screen_containers);
839 aura::Window* default_container = CreateContainer(
840 kShellWindowId_DefaultContainer,
841 "DefaultContainer",
842 non_lock_screen_containers);
843 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
844 SetUsesScreenCoordinates(default_container);
845 SetUsesEasyResizeTargeter(default_container);
847 aura::Window* always_on_top_container = CreateContainer(
848 kShellWindowId_AlwaysOnTopContainer,
849 "AlwaysOnTopContainer",
850 non_lock_screen_containers);
851 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
852 SetUsesScreenCoordinates(always_on_top_container);
854 aura::Window* docked_container = CreateContainer(
855 kShellWindowId_DockedContainer,
856 "DockedContainer",
857 non_lock_screen_containers);
858 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container);
859 SetUsesScreenCoordinates(docked_container);
860 SetUsesEasyResizeTargeter(docked_container);
862 aura::Window* shelf_container =
863 CreateContainer(kShellWindowId_ShelfContainer,
864 "ShelfContainer",
865 non_lock_screen_containers);
866 SetUsesScreenCoordinates(shelf_container);
867 DescendantShouldStayInSameRootWindow(shelf_container);
869 aura::Window* panel_container = CreateContainer(
870 kShellWindowId_PanelContainer,
871 "PanelContainer",
872 non_lock_screen_containers);
873 SetUsesScreenCoordinates(panel_container);
874 SetUsesEasyResizeTargeter(panel_container);
876 aura::Window* shelf_bubble_container =
877 CreateContainer(kShellWindowId_ShelfBubbleContainer,
878 "ShelfBubbleContainer",
879 non_lock_screen_containers);
880 SetUsesScreenCoordinates(shelf_bubble_container);
881 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
883 aura::Window* app_list_container =
884 CreateContainer(kShellWindowId_AppListContainer,
885 "AppListContainer",
886 non_lock_screen_containers);
887 SetUsesScreenCoordinates(app_list_container);
889 aura::Window* modal_container = CreateContainer(
890 kShellWindowId_SystemModalContainer,
891 "SystemModalContainer",
892 non_lock_screen_containers);
893 modal_container->SetLayoutManager(
894 new SystemModalContainerLayoutManager(modal_container));
895 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
896 SetUsesScreenCoordinates(modal_container);
897 SetUsesEasyResizeTargeter(modal_container);
899 aura::Window* input_method_container = CreateContainer(
900 kShellWindowId_InputMethodContainer,
901 "InputMethodContainer",
902 non_lock_screen_containers);
903 ::wm::SetChildWindowVisibilityChangesAnimated(input_method_container);
904 SetUsesScreenCoordinates(input_method_container);
906 // TODO(beng): Figure out if we can make this use
907 // SystemModalContainerEventFilter instead of stops_event_propagation.
908 aura::Window* lock_container = CreateContainer(
909 kShellWindowId_LockScreenContainer,
910 "LockScreenContainer",
911 lock_screen_containers);
912 lock_container->SetLayoutManager(new WorkspaceLayoutManager(lock_container));
913 SetUsesScreenCoordinates(lock_container);
914 // TODO(beng): stopsevents
916 aura::Window* lock_modal_container = CreateContainer(
917 kShellWindowId_LockSystemModalContainer,
918 "LockSystemModalContainer",
919 lock_screen_containers);
920 lock_modal_container->SetLayoutManager(
921 new SystemModalContainerLayoutManager(lock_modal_container));
922 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
923 SetUsesScreenCoordinates(lock_modal_container);
924 SetUsesEasyResizeTargeter(lock_modal_container);
926 aura::Window* status_container =
927 CreateContainer(kShellWindowId_StatusContainer,
928 "StatusContainer",
929 lock_screen_related_containers);
930 SetUsesScreenCoordinates(status_container);
931 DescendantShouldStayInSameRootWindow(status_container);
933 aura::Window* settings_bubble_container = CreateContainer(
934 kShellWindowId_SettingBubbleContainer,
935 "SettingBubbleContainer",
936 lock_screen_related_containers);
937 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
938 SetUsesScreenCoordinates(settings_bubble_container);
939 DescendantShouldStayInSameRootWindow(settings_bubble_container);
941 aura::Window* menu_container = CreateContainer(
942 kShellWindowId_MenuContainer,
943 "MenuContainer",
944 lock_screen_related_containers);
945 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
946 SetUsesScreenCoordinates(menu_container);
948 aura::Window* drag_drop_container = CreateContainer(
949 kShellWindowId_DragImageAndTooltipContainer,
950 "DragImageAndTooltipContainer",
951 lock_screen_related_containers);
952 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
953 SetUsesScreenCoordinates(drag_drop_container);
955 aura::Window* overlay_container = CreateContainer(
956 kShellWindowId_OverlayContainer,
957 "OverlayContainer",
958 lock_screen_related_containers);
959 SetUsesScreenCoordinates(overlay_container);
961 aura::Window* virtual_keyboard_parent_container = CreateContainer(
962 kShellWindowId_VirtualKeyboardParentContainer,
963 "VirtualKeyboardParentContainer",
964 root_window);
965 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
967 #if defined(OS_CHROMEOS)
968 aura::Window* mouse_cursor_container = CreateContainer(
969 kShellWindowId_MouseCursorContainer,
970 "MouseCursorContainer",
971 root_window);
972 SetUsesScreenCoordinates(mouse_cursor_container);
973 #endif
975 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
976 "PowerButtonAnimationContainer", root_window);
979 void RootWindowController::EnableTouchHudProjection() {
980 if (touch_hud_projection_)
981 return;
982 set_touch_hud_projection(new TouchHudProjection(root_window()));
985 void RootWindowController::DisableTouchHudProjection() {
986 if (!touch_hud_projection_)
987 return;
988 touch_hud_projection_->Remove();
991 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
992 shelf_->shelf_layout_manager()->UpdateVisibilityState();
995 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
996 if (enabled)
997 EnableTouchHudProjection();
998 else
999 DisableTouchHudProjection();
1002 RootWindowController* GetRootWindowController(
1003 const aura::Window* root_window) {
1004 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1007 } // namespace ash