Don't leave aborted URLs in the omnibox unless we're on the NTP.
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob6dd3b67491a6ca50cc7a7b5551df67dc252fe879
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_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/touch/touch_hud_debug.h"
32 #include "ash/touch/touch_hud_projection.h"
33 #include "ash/touch/touch_observer_hud.h"
34 #include "ash/wm/always_on_top_controller.h"
35 #include "ash/wm/dock/docked_window_layout_manager.h"
36 #include "ash/wm/panels/attached_panel_window_targeter.h"
37 #include "ash/wm/panels/panel_layout_manager.h"
38 #include "ash/wm/panels/panel_window_event_handler.h"
39 #include "ash/wm/root_window_layout_manager.h"
40 #include "ash/wm/screen_dimmer.h"
41 #include "ash/wm/stacking_controller.h"
42 #include "ash/wm/status_area_layout_manager.h"
43 #include "ash/wm/system_background_controller.h"
44 #include "ash/wm/system_modal_container_layout_manager.h"
45 #include "ash/wm/window_properties.h"
46 #include "ash/wm/window_state.h"
47 #include "ash/wm/window_util.h"
48 #include "ash/wm/workspace/workspace_layout_manager.h"
49 #include "ash/wm/workspace_controller.h"
50 #include "base/command_line.h"
51 #include "base/time/time.h"
52 #include "ui/aura/client/aura_constants.h"
53 #include "ui/aura/client/screen_position_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/view_model.h"
67 #include "ui/views/view_model_utils.h"
68 #include "ui/wm/core/capture_controller.h"
69 #include "ui/wm/core/easy_resize_window_targeter.h"
70 #include "ui/wm/core/visibility_controller.h"
71 #include "ui/wm/core/window_util.h"
72 #include "ui/wm/public/drag_drop_client.h"
73 #include "ui/wm/public/tooltip_client.h"
74 #include "ui/wm/public/window_types.h"
76 #if defined(OS_CHROMEOS)
77 #include "ash/wm/boot_splash_screen_chromeos.h"
78 #endif
80 namespace ash {
81 namespace {
83 #if defined(OS_CHROMEOS)
84 // Duration for the animation that hides the boot splash screen, in
85 // milliseconds. This should be short enough in relation to
86 // wm/window_animation.cc's brightness/grayscale fade animation that the login
87 // background image animation isn't hidden by the splash screen animation.
88 const int kBootSplashScreenHideDurationMs = 500;
89 #endif
91 // Creates a new window for use as a container.
92 aura::Window* CreateContainer(int window_id,
93 const char* name,
94 aura::Window* parent) {
95 aura::Window* container = new aura::Window(NULL);
96 container->set_id(window_id);
97 container->SetName(name);
98 container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
99 parent->AddChild(container);
100 if (window_id != kShellWindowId_UnparentedControlContainer)
101 container->Show();
102 return container;
105 float ToRelativeValue(int value, int src, int dst) {
106 return static_cast<float>(value) / static_cast<float>(src) * dst;
109 void MoveOriginRelativeToSize(const gfx::Size& src_size,
110 const gfx::Size& dst_size,
111 gfx::Rect* bounds_in_out) {
112 gfx::Point origin = bounds_in_out->origin();
113 bounds_in_out->set_origin(gfx::Point(
114 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
115 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
118 // Reparents |window| to |new_parent|.
119 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
120 const gfx::Size src_size = window->parent()->bounds().size();
121 const gfx::Size dst_size = new_parent->bounds().size();
122 // Update the restore bounds to make it relative to the display.
123 wm::WindowState* state = wm::GetWindowState(window);
124 gfx::Rect restore_bounds;
125 bool has_restore_bounds = state->HasRestoreBounds();
127 bool update_bounds = (state->IsNormalOrSnapped() || state->IsMinimized()) &&
128 new_parent->id() != kShellWindowId_DockedContainer;
129 gfx::Rect local_bounds;
130 if (update_bounds) {
131 local_bounds = state->window()->bounds();
132 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
135 if (has_restore_bounds) {
136 restore_bounds = state->GetRestoreBoundsInParent();
137 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
140 new_parent->AddChild(window);
142 // Docked windows have bounds handled by the layout manager in AddChild().
143 if (update_bounds)
144 window->SetBounds(local_bounds);
146 if (has_restore_bounds)
147 state->SetRestoreBoundsInParent(restore_bounds);
150 // Reparents the appropriate set of windows from |src| to |dst|.
151 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
152 // Set of windows to move.
153 const int kContainerIdsToMove[] = {
154 kShellWindowId_DefaultContainer,
155 kShellWindowId_DockedContainer,
156 kShellWindowId_PanelContainer,
157 kShellWindowId_AlwaysOnTopContainer,
158 kShellWindowId_SystemModalContainer,
159 kShellWindowId_LockSystemModalContainer,
160 kShellWindowId_InputMethodContainer,
161 kShellWindowId_UnparentedControlContainer, };
162 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
163 int id = kContainerIdsToMove[i];
164 aura::Window* src_container = Shell::GetContainer(src, id);
165 aura::Window* dst_container = Shell::GetContainer(dst, id);
166 while (!src_container->children().empty()) {
167 // Restart iteration from the source container windows each time as they
168 // may change as a result of moving other windows.
169 aura::Window::Windows::const_iterator iter =
170 src_container->children().begin();
171 while (iter != src_container->children().end() &&
172 SystemModalContainerLayoutManager::IsModalBackground(*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(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(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 {}
257 private:
258 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
261 } // namespace
263 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost* host) {
264 RootWindowController* controller = new RootWindowController(host);
265 controller->Init(RootWindowController::PRIMARY,
266 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
269 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost* host) {
270 RootWindowController* controller = new RootWindowController(host);
271 controller->Init(RootWindowController::SECONDARY, false /* first run */);
274 void RootWindowController::CreateForVirtualKeyboardDisplay(
275 AshWindowTreeHost* host) {
276 RootWindowController* controller = new RootWindowController(host);
277 controller->Init(RootWindowController::VIRTUAL_KEYBOARD,
278 false /* first run */);
281 // static
282 RootWindowController* RootWindowController::ForShelf(aura::Window* window) {
283 return GetRootWindowController(window->GetRootWindow());
286 // static
287 RootWindowController* RootWindowController::ForWindow(
288 const aura::Window* window) {
289 return GetRootWindowController(window->GetRootWindow());
292 // static
293 RootWindowController* RootWindowController::ForTargetRootWindow() {
294 return GetRootWindowController(Shell::GetTargetRootWindow());
297 // static
298 aura::Window* RootWindowController::GetContainerForWindow(
299 aura::Window* window) {
300 aura::Window* container = window->parent();
301 while (container && container->type() != ui::wm::WINDOW_TYPE_UNKNOWN)
302 container = container->parent();
303 return container;
306 RootWindowController::~RootWindowController() {
307 Shutdown();
308 ash_host_.reset();
309 // The CaptureClient needs to be around for as long as the RootWindow is
310 // valid.
311 capture_client_.reset();
314 aura::WindowTreeHost* RootWindowController::GetHost() {
315 return ash_host_->AsWindowTreeHost();
318 const aura::WindowTreeHost* RootWindowController::GetHost() const {
319 return ash_host_->AsWindowTreeHost();
322 aura::Window* RootWindowController::GetRootWindow() {
323 return GetHost()->window();
326 const aura::Window* RootWindowController::GetRootWindow() const {
327 return GetHost()->window();
330 void RootWindowController::SetWallpaperController(
331 DesktopBackgroundWidgetController* controller) {
332 wallpaper_controller_.reset(controller);
335 void RootWindowController::SetAnimatingWallpaperController(
336 AnimatingDesktopController* controller) {
337 if (animating_wallpaper_controller_.get())
338 animating_wallpaper_controller_->StopAnimating();
339 animating_wallpaper_controller_.reset(controller);
342 void RootWindowController::Shutdown() {
343 Shell::GetInstance()->RemoveShellObserver(this);
345 if (animating_wallpaper_controller_.get())
346 animating_wallpaper_controller_->StopAnimating();
347 wallpaper_controller_.reset();
348 animating_wallpaper_controller_.reset();
349 aura::Window* root_window = GetRootWindow();
350 // Change the target root window before closing child windows. If any child
351 // being removed triggers a relayout of the shelf it will try to build a
352 // window list adding windows from the target root window's containers which
353 // may have already gone away.
354 if (Shell::GetTargetRootWindow() == root_window) {
355 Shell::GetInstance()->set_target_root_window(
356 Shell::GetPrimaryRootWindow() == root_window
357 ? NULL
358 : Shell::GetPrimaryRootWindow());
361 CloseChildWindows();
362 GetRootWindowSettings(root_window)->controller = NULL;
363 screen_dimmer_.reset();
364 workspace_controller_.reset();
365 // Forget with the display ID so that display lookup
366 // ends up with invalid display.
367 GetRootWindowSettings(root_window)->display_id =
368 gfx::Display::kInvalidDisplayID;
369 GetRootWindowSettings(root_window)->shutdown = true;
371 system_background_.reset();
372 aura::client::SetScreenPositionClient(root_window, NULL);
375 SystemModalContainerLayoutManager*
376 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
377 aura::Window* modal_container = NULL;
378 if (window) {
379 aura::Window* window_container = GetContainerForWindow(window);
380 if (window_container &&
381 window_container->id() >= kShellWindowId_LockScreenContainer) {
382 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
383 } else {
384 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
386 } else {
387 int modal_window_id = Shell::GetInstance()->session_state_delegate()
388 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
389 kShellWindowId_SystemModalContainer;
390 modal_container = GetContainer(modal_window_id);
392 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
393 modal_container->layout_manager()) : NULL;
396 aura::Window* RootWindowController::GetContainer(int container_id) {
397 return GetRootWindow()->GetChildById(container_id);
400 const aura::Window* RootWindowController::GetContainer(int container_id) const {
401 return ash_host_->AsWindowTreeHost()->window()->GetChildById(container_id);
404 void RootWindowController::ShowShelf() {
405 if (!shelf_->shelf())
406 return;
407 shelf_->shelf()->SetVisible(true);
408 shelf_->status_area_widget()->Show();
411 void RootWindowController::OnShelfCreated() {
412 if (panel_layout_manager_)
413 panel_layout_manager_->SetShelf(shelf_->shelf());
414 if (docked_layout_manager_) {
415 docked_layout_manager_->SetShelf(shelf_->shelf());
416 if (shelf_->shelf_layout_manager())
417 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
420 // Notify shell observers that the shelf has been created.
421 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
424 void RootWindowController::UpdateAfterLoginStatusChange(
425 user::LoginStatus status) {
426 if (status != user::LOGGED_IN_NONE)
427 mouse_event_target_.reset();
428 if (shelf_->status_area_widget())
429 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
432 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
433 #if defined(OS_CHROMEOS)
434 if (CommandLine::ForCurrentProcess()->HasSwitch(
435 switches::kAshAnimateFromBootSplashScreen) &&
436 boot_splash_screen_.get()) {
437 // Make the splash screen fade out so it doesn't obscure the desktop
438 // wallpaper's brightness/grayscale animation.
439 boot_splash_screen_->StartHideAnimation(
440 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
442 #endif
445 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
446 // Make sure the wallpaper is visible.
447 system_background_->SetColor(SK_ColorBLACK);
448 #if defined(OS_CHROMEOS)
449 boot_splash_screen_.reset();
450 #endif
452 Shell::GetInstance()->user_wallpaper_delegate()->
453 OnWallpaperAnimationFinished();
454 // Only removes old component when wallpaper animation finished. If we
455 // remove the old one before the new wallpaper is done fading in there will
456 // be a white flash during the animation.
457 if (animating_wallpaper_controller()) {
458 DesktopBackgroundWidgetController* controller =
459 animating_wallpaper_controller()->GetController(true);
460 // |desktop_widget_| should be the same animating widget we try to move
461 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
462 // before move it to |kDesktopController|.
463 DCHECK_EQ(controller->widget(), widget);
464 // Release the old controller and close its background widget.
465 SetWallpaperController(controller);
469 void RootWindowController::CloseChildWindows() {
470 mouse_event_target_.reset();
472 // Deactivate keyboard container before closing child windows and shutting
473 // down associated layout managers.
474 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
476 // panel_layout_manager_ needs to be shut down before windows are destroyed.
477 if (panel_layout_manager_) {
478 panel_layout_manager_->Shutdown();
479 panel_layout_manager_ = NULL;
481 // docked_layout_manager_ needs to be shut down before windows are destroyed.
482 if (docked_layout_manager_) {
483 if (shelf_ && shelf_->shelf_layout_manager())
484 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
485 docked_layout_manager_->Shutdown();
486 docked_layout_manager_ = NULL;
488 aura::Window* root_window = GetRootWindow();
489 aura::client::SetDragDropClient(root_window, NULL);
491 // TODO(harrym): Remove when Status Area Widget is a child view.
492 if (shelf_) {
493 shelf_->ShutdownStatusAreaWidget();
495 if (shelf_->shelf_layout_manager())
496 shelf_->shelf_layout_manager()->PrepareForShutdown();
499 // Close background widget first as it depends on tooltip.
500 wallpaper_controller_.reset();
501 animating_wallpaper_controller_.reset();
503 workspace_controller_.reset();
504 aura::client::SetTooltipClient(root_window, NULL);
506 // Explicitly destroy top level windows. We do this as during part of
507 // destruction such windows may query the RootWindow for state.
508 std::queue<aura::Window*> non_toplevel_windows;
509 non_toplevel_windows.push(root_window);
510 while (!non_toplevel_windows.empty()) {
511 aura::Window* non_toplevel_window = non_toplevel_windows.front();
512 non_toplevel_windows.pop();
513 aura::WindowTracker toplevel_windows;
514 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
515 aura::Window* child = non_toplevel_window->children()[i];
516 if (!child->owned_by_parent())
517 continue;
518 if (child->delegate())
519 toplevel_windows.Add(child);
520 else
521 non_toplevel_windows.push(child);
523 while (!toplevel_windows.windows().empty())
524 delete *toplevel_windows.windows().begin();
526 // And then remove the containers.
527 while (!root_window->children().empty()) {
528 aura::Window* window = root_window->children()[0];
529 if (window->owned_by_parent()) {
530 delete window;
531 } else {
532 root_window->RemoveChild(window);
536 shelf_.reset();
539 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
540 // Forget the shelf early so that shelf don't update itself using wrong
541 // display info.
542 workspace_controller_->SetShelf(NULL);
543 ReparentAllWindows(GetRootWindow(), dst);
546 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
547 return shelf_->shelf_layout_manager();
550 SystemTray* RootWindowController::GetSystemTray() {
551 // We assume in throughout the code that this will not return NULL. If code
552 // triggers this for valid reasons, it should test status_area_widget first.
553 CHECK(shelf_->status_area_widget());
554 return shelf_->status_area_widget()->system_tray();
557 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
558 ui::MenuSourceType source_type) {
559 DCHECK(Shell::GetInstance()->delegate());
560 scoped_ptr<ui::MenuModel> menu_model(
561 Shell::GetInstance()->delegate()->CreateContextMenu(
562 GetRootWindow(), NULL, NULL));
563 if (!menu_model)
564 return;
566 // Background controller may not be set yet if user clicked on status are
567 // before initial animation completion. See crbug.com/222218
568 if (!wallpaper_controller_.get())
569 return;
571 views::MenuRunner menu_runner(menu_model.get());
572 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
573 NULL, gfx::Rect(location_in_screen, gfx::Size()),
574 views::MenuItemView::TOPLEFT, source_type,
575 views::MenuRunner::CONTEXT_MENU) ==
576 views::MenuRunner::MENU_DELETED) {
577 return;
580 Shell::GetInstance()->UpdateShelfVisibility();
583 void RootWindowController::UpdateShelfVisibility() {
584 shelf_->shelf_layout_manager()->UpdateVisibilityState();
587 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
588 const aura::Window* topmost_window = NULL;
589 const aura::Window* active_window = wm::GetActiveWindow();
590 if (active_window && active_window->GetRootWindow() == GetRootWindow() &&
591 IsSwitchableContainer(active_window->parent())) {
592 // Use the active window when it is on the current root window to determine
593 // the fullscreen state to allow temporarily using a panel or docked window
594 // (which are always above the default container) while a fullscreen
595 // window is open. We only use the active window when in a switchable
596 // container as the launcher should not exit fullscreen mode.
597 topmost_window = active_window;
598 } else {
599 // Otherwise, use the topmost window on the root window's default container
600 // when there is no active window on this root window.
601 const aura::Window::Windows& windows =
602 GetContainer(kShellWindowId_DefaultContainer)->children();
603 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
604 iter != windows.rend(); ++iter) {
605 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
606 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
607 (*iter)->layer()->GetTargetVisibility()) {
608 topmost_window = *iter;
609 break;
613 while (topmost_window) {
614 if (wm::GetWindowState(topmost_window)->IsFullscreen())
615 return topmost_window;
616 topmost_window = ::wm::GetTransientParent(topmost_window);
618 return NULL;
621 void RootWindowController::ActivateKeyboard(
622 keyboard::KeyboardController* keyboard_controller) {
623 if (!keyboard::IsKeyboardEnabled() ||
624 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
625 return;
627 DCHECK(keyboard_controller);
628 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
629 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
630 keyboard_controller->AddObserver(panel_layout_manager_);
631 keyboard_controller->AddObserver(docked_layout_manager_);
632 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
634 aura::Window* parent = GetContainer(
635 kShellWindowId_VirtualKeyboardParentContainer);
636 DCHECK(parent);
637 aura::Window* keyboard_container =
638 keyboard_controller->GetContainerWindow();
639 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
640 parent->AddChild(keyboard_container);
641 // TODO(oshima): Bounds of keyboard container should be handled by
642 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
643 keyboard_container->SetBounds(parent->bounds());
646 void RootWindowController::DeactivateKeyboard(
647 keyboard::KeyboardController* keyboard_controller) {
648 if (!keyboard_controller ||
649 !keyboard_controller->keyboard_container_initialized()) {
650 return;
652 aura::Window* keyboard_container =
653 keyboard_controller->GetContainerWindow();
654 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
655 aura::Window* parent = GetContainer(
656 kShellWindowId_VirtualKeyboardParentContainer);
657 DCHECK(parent);
658 parent->RemoveChild(keyboard_container);
659 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
660 // Virtual keyboard may be deactivated while still showing, notify all
661 // observers that keyboard bounds changed to 0 before remove them.
662 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
663 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
664 keyboard_controller->RemoveObserver(panel_layout_manager_);
665 keyboard_controller->RemoveObserver(docked_layout_manager_);
666 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
671 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
672 aura::Window* parent = GetContainer(
673 kShellWindowId_VirtualKeyboardParentContainer);
674 return parent ? parent->Contains(window) : false;
677 ////////////////////////////////////////////////////////////////////////////////
678 // RootWindowController, private:
680 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
681 : ash_host_(ash_host),
682 root_window_layout_(NULL),
683 docked_layout_manager_(NULL),
684 panel_layout_manager_(NULL),
685 touch_hud_debug_(NULL),
686 touch_hud_projection_(NULL) {
687 aura::Window* root_window = GetRootWindow();
688 GetRootWindowSettings(root_window)->controller = this;
689 screen_dimmer_.reset(new ScreenDimmer(root_window));
691 stacking_controller_.reset(new StackingController);
692 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
693 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
696 void RootWindowController::Init(RootWindowType root_window_type,
697 bool first_run_after_boot) {
698 aura::Window* root_window = GetRootWindow();
699 Shell* shell = Shell::GetInstance();
700 shell->InitRootWindow(root_window);
702 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
703 CreateContainersInRootWindow(root_window);
705 if (root_window_type == VIRTUAL_KEYBOARD) {
706 aura::Window* virtual_keyboard_parent_container = GetContainer(
707 kShellWindowId_VirtualKeyboardParentContainer);
708 virtual_keyboard_parent_container->SetBounds(root_window->bounds());
709 shell->InitKeyboard();
710 return;
713 CreateSystemBackground(first_run_after_boot);
715 InitLayoutManagers();
716 InitTouchHuds();
718 if (Shell::GetPrimaryRootWindowController()->
719 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
720 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
723 shell->AddShellObserver(this);
725 if (root_window_type == PRIMARY) {
726 root_window_layout()->OnWindowResized();
727 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
728 shell->InitKeyboard();
729 } else {
730 root_window_layout()->OnWindowResized();
731 ash_host_->AsWindowTreeHost()->Show();
733 // Create a shelf if a user is already logged in.
734 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
735 shelf()->CreateShelf();
737 // Notify shell observers about new root window.
738 shell->OnRootWindowAdded(root_window);
742 void RootWindowController::InitLayoutManagers() {
743 aura::Window* root_window = GetRootWindow();
744 root_window_layout_ = new RootWindowLayoutManager(root_window);
745 root_window->SetLayoutManager(root_window_layout_);
747 aura::Window* default_container =
748 GetContainer(kShellWindowId_DefaultContainer);
749 // Workspace manager has its own layout managers.
750 workspace_controller_.reset(
751 new WorkspaceController(default_container));
753 aura::Window* always_on_top_container =
754 GetContainer(kShellWindowId_AlwaysOnTopContainer);
755 always_on_top_container->SetLayoutManager(
756 new WorkspaceLayoutManager(always_on_top_container));
757 always_on_top_controller_.reset(new AlwaysOnTopController);
758 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
760 DCHECK(!shelf_.get());
761 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
762 // TODO(harrym): Remove when status area is view.
763 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
764 shelf_.reset(new ShelfWidget(
765 shelf_container, status_container, workspace_controller()));
767 if (!Shell::GetInstance()->session_state_delegate()->
768 IsActiveUserSessionStarted()) {
769 // This window exists only to be a event target on login screen.
770 // It does not have to handle events, nor be visible.
771 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
772 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
774 aura::Window* lock_background_container =
775 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
776 lock_background_container->AddChild(mouse_event_target_.get());
777 mouse_event_target_->Show();
780 // Create Docked windows layout manager
781 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
782 docked_layout_manager_ =
783 new DockedWindowLayoutManager(docked_container, workspace_controller());
784 docked_container->SetLayoutManager(docked_layout_manager_);
786 // Create Panel layout manager
787 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
788 panel_layout_manager_ = new PanelLayoutManager(panel_container);
789 panel_container->SetLayoutManager(panel_layout_manager_);
790 panel_container_handler_.reset(new PanelWindowEventHandler);
791 panel_container->AddPreTargetHandler(panel_container_handler_.get());
793 // Install an AttachedPanelWindowTargeter on the panel container to make it
794 // easier to correctly target shelf buttons with touch.
795 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
796 -kResizeOutsideBoundsSize,
797 -kResizeOutsideBoundsSize,
798 -kResizeOutsideBoundsSize);
799 gfx::Insets touch_extend = mouse_extend.Scale(
800 kResizeOutsideBoundsScaleForTouch);
801 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
802 new AttachedPanelWindowTargeter(panel_container,
803 mouse_extend,
804 touch_extend,
805 panel_layout_manager_)));
808 void RootWindowController::InitTouchHuds() {
809 CommandLine* command_line = CommandLine::ForCurrentProcess();
810 if (command_line->HasSwitch(switches::kAshTouchHud))
811 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
812 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
813 EnableTouchHudProjection();
816 void RootWindowController::CreateSystemBackground(
817 bool is_first_run_after_boot) {
818 SkColor color = SK_ColorBLACK;
819 #if defined(OS_CHROMEOS)
820 if (is_first_run_after_boot)
821 color = kChromeOsBootColor;
822 #endif
823 system_background_.reset(
824 new SystemBackgroundController(GetRootWindow(), color));
826 #if defined(OS_CHROMEOS)
827 // Make a copy of the system's boot splash screen so we can composite it
828 // onscreen until the desktop background is ready.
829 if (is_first_run_after_boot &&
830 (CommandLine::ForCurrentProcess()->HasSwitch(
831 switches::kAshCopyHostBackgroundAtBoot) ||
832 CommandLine::ForCurrentProcess()->HasSwitch(
833 switches::kAshAnimateFromBootSplashScreen)))
834 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
835 #endif
838 void RootWindowController::CreateContainersInRootWindow(
839 aura::Window* root_window) {
840 // These containers are just used by PowerButtonController to animate groups
841 // of containers simultaneously without messing up the current transformations
842 // on those containers. These are direct children of the root window; all of
843 // the other containers are their children.
845 // The desktop background container is not part of the lock animation, so it
846 // is not included in those animate groups.
847 // When screen is locked desktop background is moved to lock screen background
848 // container (moved back on unlock). We want to make sure that there's an
849 // opaque layer occluding the non-lock-screen layers.
850 aura::Window* desktop_background_container = CreateContainer(
851 kShellWindowId_DesktopBackgroundContainer,
852 "DesktopBackgroundContainer",
853 root_window);
854 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
856 aura::Window* non_lock_screen_containers = CreateContainer(
857 kShellWindowId_NonLockScreenContainersContainer,
858 "NonLockScreenContainersContainer",
859 root_window);
861 aura::Window* lock_background_containers = CreateContainer(
862 kShellWindowId_LockScreenBackgroundContainer,
863 "LockScreenBackgroundContainer",
864 root_window);
865 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
867 aura::Window* lock_screen_containers = CreateContainer(
868 kShellWindowId_LockScreenContainersContainer,
869 "LockScreenContainersContainer",
870 root_window);
871 aura::Window* lock_screen_related_containers = CreateContainer(
872 kShellWindowId_LockScreenRelatedContainersContainer,
873 "LockScreenRelatedContainersContainer",
874 root_window);
876 CreateContainer(kShellWindowId_UnparentedControlContainer,
877 "UnparentedControlContainer",
878 non_lock_screen_containers);
880 aura::Window* default_container = CreateContainer(
881 kShellWindowId_DefaultContainer,
882 "DefaultContainer",
883 non_lock_screen_containers);
884 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
885 SetUsesScreenCoordinates(default_container);
886 SetUsesEasyResizeTargeter(default_container);
888 aura::Window* always_on_top_container = CreateContainer(
889 kShellWindowId_AlwaysOnTopContainer,
890 "AlwaysOnTopContainer",
891 non_lock_screen_containers);
892 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
893 SetUsesScreenCoordinates(always_on_top_container);
895 aura::Window* docked_container = CreateContainer(
896 kShellWindowId_DockedContainer,
897 "DockedContainer",
898 non_lock_screen_containers);
899 ::wm::SetChildWindowVisibilityChangesAnimated(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 SetUsesScreenCoordinates(shelf_container);
908 DescendantShouldStayInSameRootWindow(shelf_container);
910 aura::Window* panel_container = CreateContainer(
911 kShellWindowId_PanelContainer,
912 "PanelContainer",
913 non_lock_screen_containers);
914 SetUsesScreenCoordinates(panel_container);
916 aura::Window* shelf_bubble_container =
917 CreateContainer(kShellWindowId_ShelfBubbleContainer,
918 "ShelfBubbleContainer",
919 non_lock_screen_containers);
920 SetUsesScreenCoordinates(shelf_bubble_container);
921 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
923 aura::Window* app_list_container =
924 CreateContainer(kShellWindowId_AppListContainer,
925 "AppListContainer",
926 non_lock_screen_containers);
927 SetUsesScreenCoordinates(app_list_container);
929 aura::Window* modal_container = CreateContainer(
930 kShellWindowId_SystemModalContainer,
931 "SystemModalContainer",
932 non_lock_screen_containers);
933 modal_container->SetLayoutManager(
934 new SystemModalContainerLayoutManager(modal_container));
935 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
936 SetUsesScreenCoordinates(modal_container);
937 SetUsesEasyResizeTargeter(modal_container);
939 aura::Window* input_method_container = CreateContainer(
940 kShellWindowId_InputMethodContainer,
941 "InputMethodContainer",
942 non_lock_screen_containers);
943 ::wm::SetChildWindowVisibilityChangesAnimated(input_method_container);
944 SetUsesScreenCoordinates(input_method_container);
946 // TODO(beng): Figure out if we can make this use
947 // SystemModalContainerEventFilter instead of stops_event_propagation.
948 aura::Window* lock_container = CreateContainer(
949 kShellWindowId_LockScreenContainer,
950 "LockScreenContainer",
951 lock_screen_containers);
952 lock_container->SetLayoutManager(new WorkspaceLayoutManager(lock_container));
953 SetUsesScreenCoordinates(lock_container);
954 // TODO(beng): stopsevents
956 aura::Window* lock_modal_container = CreateContainer(
957 kShellWindowId_LockSystemModalContainer,
958 "LockSystemModalContainer",
959 lock_screen_containers);
960 lock_modal_container->SetLayoutManager(
961 new SystemModalContainerLayoutManager(lock_modal_container));
962 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
963 SetUsesScreenCoordinates(lock_modal_container);
964 SetUsesEasyResizeTargeter(lock_modal_container);
966 aura::Window* status_container =
967 CreateContainer(kShellWindowId_StatusContainer,
968 "StatusContainer",
969 lock_screen_related_containers);
970 SetUsesScreenCoordinates(status_container);
971 DescendantShouldStayInSameRootWindow(status_container);
973 aura::Window* settings_bubble_container = CreateContainer(
974 kShellWindowId_SettingBubbleContainer,
975 "SettingBubbleContainer",
976 lock_screen_related_containers);
977 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
978 SetUsesScreenCoordinates(settings_bubble_container);
979 DescendantShouldStayInSameRootWindow(settings_bubble_container);
981 aura::Window* menu_container = CreateContainer(
982 kShellWindowId_MenuContainer,
983 "MenuContainer",
984 lock_screen_related_containers);
985 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
986 SetUsesScreenCoordinates(menu_container);
988 aura::Window* drag_drop_container = CreateContainer(
989 kShellWindowId_DragImageAndTooltipContainer,
990 "DragImageAndTooltipContainer",
991 lock_screen_related_containers);
992 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
993 SetUsesScreenCoordinates(drag_drop_container);
995 aura::Window* overlay_container = CreateContainer(
996 kShellWindowId_OverlayContainer,
997 "OverlayContainer",
998 lock_screen_related_containers);
999 SetUsesScreenCoordinates(overlay_container);
1001 aura::Window* virtual_keyboard_parent_container = CreateContainer(
1002 kShellWindowId_VirtualKeyboardParentContainer,
1003 "VirtualKeyboardParentContainer",
1004 root_window);
1005 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
1007 #if defined(OS_CHROMEOS)
1008 aura::Window* mouse_cursor_container = CreateContainer(
1009 kShellWindowId_MouseCursorContainer,
1010 "MouseCursorContainer",
1011 root_window);
1012 SetUsesScreenCoordinates(mouse_cursor_container);
1013 #endif
1015 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1016 "PowerButtonAnimationContainer", root_window);
1019 void RootWindowController::EnableTouchHudProjection() {
1020 if (touch_hud_projection_)
1021 return;
1022 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1025 void RootWindowController::DisableTouchHudProjection() {
1026 if (!touch_hud_projection_)
1027 return;
1028 touch_hud_projection_->Remove();
1031 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1032 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1035 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1036 if (enabled)
1037 EnableTouchHudProjection();
1038 else
1039 DisableTouchHudProjection();
1042 RootWindowController* GetRootWindowController(
1043 const aura::Window* root_window) {
1044 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1047 } // namespace ash