Roll src/third_party/WebKit 27dc83a:9434cdf (svn 191486:191535)
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob1e1c5bdaa20937b8e9146734d587308473a3bf39
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/root_window_controller.h"
7 #include <queue>
8 #include <vector>
10 #include "ash/ash_constants.h"
11 #include "ash/ash_switches.h"
12 #include "ash/desktop_background/desktop_background_controller.h"
13 #include "ash/desktop_background/desktop_background_widget_controller.h"
14 #include "ash/desktop_background/user_wallpaper_delegate.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/focus_cycler.h"
17 #include "ash/high_contrast/high_contrast_controller.h"
18 #include "ash/host/ash_window_tree_host.h"
19 #include "ash/root_window_settings.h"
20 #include "ash/session/session_state_delegate.h"
21 #include "ash/shelf/shelf_layout_manager.h"
22 #include "ash/shelf/shelf_types.h"
23 #include "ash/shelf/shelf_widget.h"
24 #include "ash/shell.h"
25 #include "ash/shell_delegate.h"
26 #include "ash/shell_factory.h"
27 #include "ash/shell_window_ids.h"
28 #include "ash/switchable_windows.h"
29 #include "ash/system/status_area_widget.h"
30 #include "ash/system/tray/system_tray_delegate.h"
31 #include "ash/system/tray/system_tray_notifier.h"
32 #include "ash/touch/touch_hud_debug.h"
33 #include "ash/touch/touch_hud_projection.h"
34 #include "ash/touch/touch_observer_hud.h"
35 #include "ash/wm/always_on_top_controller.h"
36 #include "ash/wm/dock/docked_window_layout_manager.h"
37 #include "ash/wm/lock_layout_manager.h"
38 #include "ash/wm/panels/attached_panel_window_targeter.h"
39 #include "ash/wm/panels/panel_layout_manager.h"
40 #include "ash/wm/panels/panel_window_event_handler.h"
41 #include "ash/wm/root_window_layout_manager.h"
42 #include "ash/wm/screen_dimmer.h"
43 #include "ash/wm/stacking_controller.h"
44 #include "ash/wm/status_area_layout_manager.h"
45 #include "ash/wm/system_background_controller.h"
46 #include "ash/wm/system_modal_container_layout_manager.h"
47 #include "ash/wm/virtual_keyboard_container_layout_manager.h"
48 #include "ash/wm/window_properties.h"
49 #include "ash/wm/window_state.h"
50 #include "ash/wm/window_util.h"
51 #include "ash/wm/workspace/workspace_layout_manager.h"
52 #include "ash/wm/workspace_controller.h"
53 #include "base/command_line.h"
54 #include "base/time/time.h"
55 #include "ui/aura/client/aura_constants.h"
56 #include "ui/aura/client/screen_position_client.h"
57 #include "ui/aura/window.h"
58 #include "ui/aura/window_delegate.h"
59 #include "ui/aura/window_event_dispatcher.h"
60 #include "ui/aura/window_observer.h"
61 #include "ui/aura/window_tracker.h"
62 #include "ui/base/hit_test.h"
63 #include "ui/base/models/menu_model.h"
64 #include "ui/gfx/display.h"
65 #include "ui/gfx/screen.h"
66 #include "ui/keyboard/keyboard_controller.h"
67 #include "ui/keyboard/keyboard_util.h"
68 #include "ui/views/controls/menu/menu_runner.h"
69 #include "ui/views/view_model.h"
70 #include "ui/views/view_model_utils.h"
71 #include "ui/wm/core/capture_controller.h"
72 #include "ui/wm/core/easy_resize_window_targeter.h"
73 #include "ui/wm/core/visibility_controller.h"
74 #include "ui/wm/core/window_util.h"
75 #include "ui/wm/public/drag_drop_client.h"
76 #include "ui/wm/public/tooltip_client.h"
77 #include "ui/wm/public/window_types.h"
79 #if defined(OS_CHROMEOS)
80 #include "ash/ash_touch_exploration_manager_chromeos.h"
81 #include "ash/wm/boot_splash_screen_chromeos.h"
82 #include "ui/chromeos/touch_exploration_controller.h"
83 #endif
85 namespace ash {
86 namespace {
88 #if defined(OS_CHROMEOS)
89 // Duration for the animation that hides the boot splash screen, in
90 // milliseconds. This should be short enough in relation to
91 // wm/window_animation.cc's brightness/grayscale fade animation that the login
92 // background image animation isn't hidden by the splash screen animation.
93 const int kBootSplashScreenHideDurationMs = 500;
94 #endif
96 // Creates a new window for use as a container.
97 aura::Window* CreateContainer(int window_id,
98 const char* name,
99 aura::Window* parent) {
100 aura::Window* container = new aura::Window(NULL);
101 container->set_id(window_id);
102 container->SetName(name);
103 container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
104 parent->AddChild(container);
105 if (window_id != kShellWindowId_UnparentedControlContainer)
106 container->Show();
107 return container;
110 float ToRelativeValue(int value, int src, int dst) {
111 return static_cast<float>(value) / static_cast<float>(src) * dst;
114 void MoveOriginRelativeToSize(const gfx::Size& src_size,
115 const gfx::Size& dst_size,
116 gfx::Rect* bounds_in_out) {
117 gfx::Point origin = bounds_in_out->origin();
118 bounds_in_out->set_origin(gfx::Point(
119 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
120 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
123 // Reparents |window| to |new_parent|.
124 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
125 const gfx::Size src_size = window->parent()->bounds().size();
126 const gfx::Size dst_size = new_parent->bounds().size();
127 // Update the restore bounds to make it relative to the display.
128 wm::WindowState* state = wm::GetWindowState(window);
129 gfx::Rect restore_bounds;
130 bool has_restore_bounds = state->HasRestoreBounds();
132 bool update_bounds = (state->IsNormalOrSnapped() || state->IsMinimized()) &&
133 new_parent->id() != kShellWindowId_DockedContainer;
134 gfx::Rect local_bounds;
135 if (update_bounds) {
136 local_bounds = state->window()->bounds();
137 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
140 if (has_restore_bounds) {
141 restore_bounds = state->GetRestoreBoundsInParent();
142 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
145 new_parent->AddChild(window);
147 // Docked windows have bounds handled by the layout manager in AddChild().
148 if (update_bounds)
149 window->SetBounds(local_bounds);
151 if (has_restore_bounds)
152 state->SetRestoreBoundsInParent(restore_bounds);
155 // Reparents the appropriate set of windows from |src| to |dst|.
156 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
157 // Set of windows to move.
158 const int kContainerIdsToMove[] = {
159 kShellWindowId_DefaultContainer,
160 kShellWindowId_DockedContainer,
161 kShellWindowId_PanelContainer,
162 kShellWindowId_AlwaysOnTopContainer,
163 kShellWindowId_SystemModalContainer,
164 kShellWindowId_LockSystemModalContainer,
165 kShellWindowId_UnparentedControlContainer,
166 kShellWindowId_OverlayContainer, };
167 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
168 int id = kContainerIdsToMove[i];
169 aura::Window* src_container = Shell::GetContainer(src, id);
170 aura::Window* dst_container = Shell::GetContainer(dst, id);
171 while (!src_container->children().empty()) {
172 // Restart iteration from the source container windows each time as they
173 // may change as a result of moving other windows.
174 aura::Window::Windows::const_iterator iter =
175 src_container->children().begin();
176 while (iter != src_container->children().end() &&
177 SystemModalContainerLayoutManager::IsModalBackground(*iter)) {
178 ++iter;
180 // If the entire window list is modal background windows then stop.
181 if (iter == src_container->children().end())
182 break;
183 ReparentWindow(*iter, dst_container);
188 // Mark the container window so that a widget added to this container will
189 // use the virtual screeen coordinates instead of parent.
190 void SetUsesScreenCoordinates(aura::Window* container) {
191 container->SetProperty(kUsesScreenCoordinatesKey, true);
194 // Mark the container window so that a widget added to this container will
195 // say in the same root window regardless of the bounds specified.
196 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
197 container->SetProperty(kStayInSameRootWindowKey, true);
200 void SetUsesEasyResizeTargeter(aura::Window* container) {
201 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
202 -kResizeOutsideBoundsSize,
203 -kResizeOutsideBoundsSize,
204 -kResizeOutsideBoundsSize);
205 gfx::Insets touch_extend = mouse_extend.Scale(
206 kResizeOutsideBoundsScaleForTouch);
207 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
208 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
209 touch_extend)));
212 // A window delegate which does nothing. Used to create a window that
213 // is a event target, but do nothing.
214 class EmptyWindowDelegate : public aura::WindowDelegate {
215 public:
216 EmptyWindowDelegate() {}
217 ~EmptyWindowDelegate() override {}
219 // aura::WindowDelegate overrides:
220 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
221 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
222 void OnBoundsChanged(const gfx::Rect& old_bounds,
223 const gfx::Rect& new_bounds) override {}
224 ui::TextInputClient* GetFocusedTextInputClient() override { return nullptr; }
225 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
226 return gfx::kNullCursor;
228 int GetNonClientComponent(const gfx::Point& point) const override {
229 return HTNOWHERE;
231 bool ShouldDescendIntoChildForEventHandling(
232 aura::Window* child,
233 const gfx::Point& location) override {
234 return false;
236 bool CanFocus() override { return false; }
237 void OnCaptureLost() override {}
238 void OnPaint(gfx::Canvas* canvas) override {}
239 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
240 void OnWindowDestroying(aura::Window* window) override {}
241 void OnWindowDestroyed(aura::Window* window) override { delete this; }
242 void OnWindowTargetVisibilityChanged(bool visible) override {}
243 bool HasHitTestMask() const override { return false; }
244 void GetHitTestMask(gfx::Path* mask) const override {}
246 private:
247 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
250 } // namespace
252 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost* host) {
253 RootWindowController* controller = new RootWindowController(host);
254 controller->Init(RootWindowController::PRIMARY,
255 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
258 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost* host) {
259 RootWindowController* controller = new RootWindowController(host);
260 controller->Init(RootWindowController::SECONDARY, false /* first run */);
263 // static
264 RootWindowController* RootWindowController::ForShelf(
265 const aura::Window* window) {
266 CHECK(Shell::HasInstance());
267 return GetRootWindowController(window->GetRootWindow());
270 // static
271 RootWindowController* RootWindowController::ForWindow(
272 const aura::Window* window) {
273 CHECK(Shell::HasInstance());
274 return GetRootWindowController(window->GetRootWindow());
277 // static
278 RootWindowController* RootWindowController::ForTargetRootWindow() {
279 CHECK(Shell::HasInstance());
280 return GetRootWindowController(Shell::GetTargetRootWindow());
283 // static
284 aura::Window* RootWindowController::GetContainerForWindow(
285 aura::Window* window) {
286 aura::Window* container = window->parent();
287 while (container && container->type() != ui::wm::WINDOW_TYPE_UNKNOWN)
288 container = container->parent();
289 return container;
292 RootWindowController::~RootWindowController() {
293 Shutdown();
294 ash_host_.reset();
295 // The CaptureClient needs to be around for as long as the RootWindow is
296 // valid.
297 capture_client_.reset();
300 aura::WindowTreeHost* RootWindowController::GetHost() {
301 return ash_host_->AsWindowTreeHost();
304 const aura::WindowTreeHost* RootWindowController::GetHost() const {
305 return ash_host_->AsWindowTreeHost();
308 aura::Window* RootWindowController::GetRootWindow() {
309 return GetHost()->window();
312 const aura::Window* RootWindowController::GetRootWindow() const {
313 return GetHost()->window();
316 void RootWindowController::SetWallpaperController(
317 DesktopBackgroundWidgetController* controller) {
318 wallpaper_controller_.reset(controller);
321 void RootWindowController::SetAnimatingWallpaperController(
322 AnimatingDesktopController* controller) {
323 if (animating_wallpaper_controller_.get())
324 animating_wallpaper_controller_->StopAnimating();
325 animating_wallpaper_controller_.reset(controller);
328 void RootWindowController::Shutdown() {
329 Shell* shell = Shell::GetInstance();
330 shell->RemoveShellObserver(this);
332 #if defined(OS_CHROMEOS)
333 if (touch_exploration_manager_) {
334 touch_exploration_manager_.reset();
336 #endif
338 if (animating_wallpaper_controller_.get())
339 animating_wallpaper_controller_->StopAnimating();
340 wallpaper_controller_.reset();
341 animating_wallpaper_controller_.reset();
342 aura::Window* root_window = GetRootWindow();
343 // Change the target root window before closing child windows. If any child
344 // being removed triggers a relayout of the shelf it will try to build a
345 // window list adding windows from the target root window's containers which
346 // may have already gone away.
347 if (Shell::GetTargetRootWindow() == root_window) {
348 shell->set_target_root_window(
349 Shell::GetPrimaryRootWindow() == root_window
350 ? NULL
351 : Shell::GetPrimaryRootWindow());
354 CloseChildWindows();
355 GetRootWindowSettings(root_window)->controller = NULL;
356 screen_dimmer_.reset();
357 workspace_controller_.reset();
358 // Forget with the display ID so that display lookup
359 // ends up with invalid display.
360 GetRootWindowSettings(root_window)->display_id =
361 gfx::Display::kInvalidDisplayID;
362 ash_host_->PrepareForShutdown();
364 system_background_.reset();
365 aura::client::SetScreenPositionClient(root_window, NULL);
368 SystemModalContainerLayoutManager*
369 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
370 aura::Window* modal_container = NULL;
371 if (window) {
372 aura::Window* window_container = GetContainerForWindow(window);
373 if (window_container &&
374 window_container->id() >= kShellWindowId_LockScreenContainer) {
375 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
376 } else {
377 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
379 } else {
380 int modal_window_id = Shell::GetInstance()->session_state_delegate()
381 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
382 kShellWindowId_SystemModalContainer;
383 modal_container = GetContainer(modal_window_id);
385 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
386 modal_container->layout_manager()) : NULL;
389 aura::Window* RootWindowController::GetContainer(int container_id) {
390 return GetRootWindow()->GetChildById(container_id);
393 const aura::Window* RootWindowController::GetContainer(int container_id) const {
394 return ash_host_->AsWindowTreeHost()->window()->GetChildById(container_id);
397 void RootWindowController::ShowShelf() {
398 if (!shelf_->shelf())
399 return;
400 shelf_->shelf()->SetVisible(true);
401 shelf_->status_area_widget()->Show();
404 void RootWindowController::OnShelfCreated() {
405 if (panel_layout_manager_)
406 panel_layout_manager_->SetShelf(shelf_->shelf());
407 if (docked_layout_manager_) {
408 docked_layout_manager_->SetShelf(shelf_->shelf());
409 if (shelf_->shelf_layout_manager())
410 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
413 // Notify shell observers that the shelf has been created.
414 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
417 void RootWindowController::UpdateAfterLoginStatusChange(
418 user::LoginStatus status) {
419 if (status != user::LOGGED_IN_NONE)
420 mouse_event_target_.reset();
421 if (shelf_->status_area_widget())
422 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
425 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
426 #if defined(OS_CHROMEOS)
427 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
428 switches::kAshAnimateFromBootSplashScreen) &&
429 boot_splash_screen_.get()) {
430 // Make the splash screen fade out so it doesn't obscure the desktop
431 // wallpaper's brightness/grayscale animation.
432 boot_splash_screen_->StartHideAnimation(
433 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
435 #endif
438 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
439 // Make sure the wallpaper is visible.
440 system_background_->SetColor(SK_ColorBLACK);
441 #if defined(OS_CHROMEOS)
442 boot_splash_screen_.reset();
443 #endif
445 Shell::GetInstance()->user_wallpaper_delegate()->
446 OnWallpaperAnimationFinished();
447 // Only removes old component when wallpaper animation finished. If we
448 // remove the old one before the new wallpaper is done fading in there will
449 // be a white flash during the animation.
450 if (animating_wallpaper_controller()) {
451 DesktopBackgroundWidgetController* controller =
452 animating_wallpaper_controller()->GetController(true);
453 // |desktop_widget_| should be the same animating widget we try to move
454 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
455 // before move it to |kDesktopController|.
456 DCHECK_EQ(controller->widget(), widget);
457 // Release the old controller and close its background widget.
458 SetWallpaperController(controller);
462 void RootWindowController::CloseChildWindows() {
463 mouse_event_target_.reset();
465 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
466 // to fire notifications.
467 if (docked_layout_manager_ && shelf_ && shelf_->shelf_layout_manager())
468 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
470 // Deactivate keyboard container before closing child windows and shutting
471 // down associated layout managers.
472 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
474 // panel_layout_manager_ needs to be shut down before windows are destroyed.
475 if (panel_layout_manager_) {
476 panel_layout_manager_->Shutdown();
477 panel_layout_manager_ = NULL;
479 // docked_layout_manager_ needs to be shut down before windows are destroyed.
480 if (docked_layout_manager_) {
481 docked_layout_manager_->Shutdown();
482 docked_layout_manager_ = NULL;
484 aura::Window* root_window = GetRootWindow();
485 aura::client::SetDragDropClient(root_window, NULL);
487 // TODO(harrym): Remove when Status Area Widget is a child view.
488 if (shelf_) {
489 shelf_->ShutdownStatusAreaWidget();
491 if (shelf_->shelf_layout_manager())
492 shelf_->shelf_layout_manager()->PrepareForShutdown();
495 // Close background widget first as it depends on tooltip.
496 wallpaper_controller_.reset();
497 animating_wallpaper_controller_.reset();
499 workspace_controller_.reset();
500 aura::client::SetTooltipClient(root_window, NULL);
502 // Explicitly destroy top level windows. We do this as during part of
503 // destruction such windows may query the RootWindow for state.
504 std::queue<aura::Window*> non_toplevel_windows;
505 non_toplevel_windows.push(root_window);
506 while (!non_toplevel_windows.empty()) {
507 aura::Window* non_toplevel_window = non_toplevel_windows.front();
508 non_toplevel_windows.pop();
509 aura::WindowTracker toplevel_windows;
510 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
511 aura::Window* child = non_toplevel_window->children()[i];
512 if (!child->owned_by_parent())
513 continue;
514 if (child->delegate())
515 toplevel_windows.Add(child);
516 else
517 non_toplevel_windows.push(child);
519 while (!toplevel_windows.windows().empty())
520 delete *toplevel_windows.windows().begin();
522 // And then remove the containers.
523 while (!root_window->children().empty()) {
524 aura::Window* window = root_window->children()[0];
525 if (window->owned_by_parent()) {
526 delete window;
527 } else {
528 root_window->RemoveChild(window);
532 shelf_.reset();
535 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
536 // Forget the shelf early so that shelf don't update itself using wrong
537 // display info.
538 workspace_controller_->SetShelf(NULL);
539 ReparentAllWindows(GetRootWindow(), dst);
542 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
543 return shelf_->shelf_layout_manager();
546 SystemTray* RootWindowController::GetSystemTray() {
547 // We assume in throughout the code that this will not return NULL. If code
548 // triggers this for valid reasons, it should test status_area_widget first.
549 CHECK(shelf_->status_area_widget());
550 return shelf_->status_area_widget()->system_tray();
553 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
554 ui::MenuSourceType source_type) {
555 DCHECK(Shell::GetInstance()->delegate());
556 scoped_ptr<ui::MenuModel> menu_model(
557 Shell::GetInstance()->delegate()->CreateContextMenu(
558 GetRootWindow(), NULL, NULL));
559 if (!menu_model)
560 return;
562 // Background controller may not be set yet if user clicked on status are
563 // before initial animation completion. See crbug.com/222218
564 if (!wallpaper_controller_.get())
565 return;
567 views::MenuRunner menu_runner(menu_model.get(),
568 views::MenuRunner::CONTEXT_MENU);
569 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
570 NULL,
571 gfx::Rect(location_in_screen, gfx::Size()),
572 views::MENU_ANCHOR_TOPLEFT,
573 source_type) == views::MenuRunner::MENU_DELETED) {
574 return;
577 Shell::GetInstance()->UpdateShelfVisibility();
580 void RootWindowController::UpdateShelfVisibility() {
581 shelf_->shelf_layout_manager()->UpdateVisibilityState();
584 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
585 const aura::Window* topmost_window = NULL;
586 const aura::Window* active_window = wm::GetActiveWindow();
587 if (active_window && active_window->GetRootWindow() == GetRootWindow() &&
588 IsSwitchableContainer(active_window->parent())) {
589 // Use the active window when it is on the current root window to determine
590 // the fullscreen state to allow temporarily using a panel or docked window
591 // (which are always above the default container) while a fullscreen
592 // window is open. We only use the active window when in a switchable
593 // container as the launcher should not exit fullscreen mode.
594 topmost_window = active_window;
595 } else {
596 // Otherwise, use the topmost window on the root window's default container
597 // when there is no active window on this root window.
598 const aura::Window::Windows& windows =
599 GetContainer(kShellWindowId_DefaultContainer)->children();
600 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
601 iter != windows.rend(); ++iter) {
602 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
603 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
604 (*iter)->layer()->GetTargetVisibility()) {
605 topmost_window = *iter;
606 break;
610 while (topmost_window) {
611 if (wm::GetWindowState(topmost_window)->IsFullscreen())
612 return topmost_window;
613 topmost_window = ::wm::GetTransientParent(topmost_window);
615 return NULL;
618 void RootWindowController::ActivateKeyboard(
619 keyboard::KeyboardController* keyboard_controller) {
620 if (!keyboard::IsKeyboardEnabled() ||
621 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
622 return;
624 DCHECK(keyboard_controller);
625 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
626 keyboard_controller->AddObserver(panel_layout_manager_);
627 keyboard_controller->AddObserver(docked_layout_manager_);
628 keyboard_controller->AddObserver(workspace_controller_->layout_manager());
629 keyboard_controller->AddObserver(
630 always_on_top_controller_->GetLayoutManager());
631 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
632 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
633 DCHECK(parent);
634 aura::Window* keyboard_container =
635 keyboard_controller->GetContainerWindow();
636 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
637 parent->AddChild(keyboard_container);
638 // TODO(oshima): Bounds of keyboard container should be handled by
639 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
640 keyboard_container->SetBounds(parent->bounds());
643 void RootWindowController::DeactivateKeyboard(
644 keyboard::KeyboardController* keyboard_controller) {
645 if (!keyboard_controller ||
646 !keyboard_controller->keyboard_container_initialized()) {
647 return;
649 aura::Window* keyboard_container =
650 keyboard_controller->GetContainerWindow();
651 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
652 aura::Window* parent =
653 GetContainer(kShellWindowId_ImeWindowParentContainer);
654 DCHECK(parent);
655 parent->RemoveChild(keyboard_container);
656 // Virtual keyboard may be deactivated while still showing, notify all
657 // observers that keyboard bounds changed to 0 before remove them.
658 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
659 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
660 keyboard_controller->RemoveObserver(panel_layout_manager_);
661 keyboard_controller->RemoveObserver(docked_layout_manager_);
662 keyboard_controller->RemoveObserver(
663 workspace_controller_->layout_manager());
664 keyboard_controller->RemoveObserver(
665 always_on_top_controller_->GetLayoutManager());
666 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
670 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
671 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
672 return parent ? parent->Contains(window) : false;
675 ////////////////////////////////////////////////////////////////////////////////
676 // RootWindowController, private:
678 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
679 : ash_host_(ash_host),
680 root_window_layout_(NULL),
681 docked_layout_manager_(NULL),
682 panel_layout_manager_(NULL),
683 touch_hud_debug_(NULL),
684 touch_hud_projection_(NULL) {
685 aura::Window* root_window = GetRootWindow();
686 GetRootWindowSettings(root_window)->controller = this;
687 screen_dimmer_.reset(new ScreenDimmer(root_window));
689 stacking_controller_.reset(new StackingController);
690 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
691 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
694 void RootWindowController::Init(RootWindowType root_window_type,
695 bool first_run_after_boot) {
696 aura::Window* root_window = GetRootWindow();
697 Shell* shell = Shell::GetInstance();
698 shell->InitRootWindow(root_window);
700 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
701 CreateContainersInRootWindow(root_window);
703 CreateSystemBackground(first_run_after_boot);
705 InitLayoutManagers();
706 InitTouchHuds();
708 if (Shell::GetPrimaryRootWindowController()->
709 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
710 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
713 shell->AddShellObserver(this);
715 if (root_window_type == PRIMARY) {
716 root_window_layout()->OnWindowResized();
717 shell->InitKeyboard();
718 } else {
719 root_window_layout()->OnWindowResized();
720 ash_host_->AsWindowTreeHost()->Show();
722 // Create a shelf if a user is already logged in.
723 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
724 shelf()->CreateShelf();
726 // Notify shell observers about new root window.
727 shell->OnRootWindowAdded(root_window);
730 #if defined(OS_CHROMEOS)
731 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
732 switches::kAshDisableTouchExplorationMode)) {
733 touch_exploration_manager_.reset(new AshTouchExplorationManager(this));
735 #endif
738 void RootWindowController::InitLayoutManagers() {
739 aura::Window* root_window = GetRootWindow();
740 root_window_layout_ = new RootWindowLayoutManager(root_window);
741 root_window->SetLayoutManager(root_window_layout_);
743 aura::Window* default_container =
744 GetContainer(kShellWindowId_DefaultContainer);
745 // Workspace manager has its own layout managers.
746 workspace_controller_.reset(
747 new WorkspaceController(default_container));
749 aura::Window* always_on_top_container =
750 GetContainer(kShellWindowId_AlwaysOnTopContainer);
751 always_on_top_controller_.reset(
752 new AlwaysOnTopController(always_on_top_container));
754 DCHECK(!shelf_.get());
755 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
756 // TODO(harrym): Remove when status area is view.
757 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
758 shelf_.reset(new ShelfWidget(
759 shelf_container, status_container, workspace_controller()));
761 if (!Shell::GetInstance()->session_state_delegate()->
762 IsActiveUserSessionStarted()) {
763 // This window exists only to be a event target on login screen.
764 // It does not have to handle events, nor be visible.
765 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
766 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
768 aura::Window* lock_background_container =
769 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
770 lock_background_container->AddChild(mouse_event_target_.get());
771 mouse_event_target_->Show();
774 // Create Docked windows layout manager
775 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
776 docked_layout_manager_ =
777 new DockedWindowLayoutManager(docked_container, workspace_controller());
778 docked_container->SetLayoutManager(docked_layout_manager_);
780 // Installs SnapLayoutManager to containers who set the
781 // |kSnapsChildrenToPhysicalPixelBoundary| property.
782 wm::InstallSnapLayoutManagerToContainers(root_window);
784 // Create Panel layout manager
785 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
786 panel_layout_manager_ = new PanelLayoutManager(panel_container);
787 panel_container->SetLayoutManager(panel_layout_manager_);
788 panel_container_handler_.reset(new PanelWindowEventHandler);
789 panel_container->AddPreTargetHandler(panel_container_handler_.get());
791 // Install an AttachedPanelWindowTargeter on the panel container to make it
792 // easier to correctly target shelf buttons with touch.
793 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
794 -kResizeOutsideBoundsSize,
795 -kResizeOutsideBoundsSize,
796 -kResizeOutsideBoundsSize);
797 gfx::Insets touch_extend = mouse_extend.Scale(
798 kResizeOutsideBoundsScaleForTouch);
799 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
800 new AttachedPanelWindowTargeter(panel_container,
801 mouse_extend,
802 touch_extend,
803 panel_layout_manager_)));
806 void RootWindowController::InitTouchHuds() {
807 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
808 if (command_line->HasSwitch(switches::kAshTouchHud))
809 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
810 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
811 EnableTouchHudProjection();
814 void RootWindowController::CreateSystemBackground(
815 bool is_first_run_after_boot) {
816 SkColor color = SK_ColorBLACK;
817 #if defined(OS_CHROMEOS)
818 if (is_first_run_after_boot)
819 color = kChromeOsBootColor;
820 #endif
821 system_background_.reset(
822 new SystemBackgroundController(GetRootWindow(), color));
824 #if defined(OS_CHROMEOS)
825 // Make a copy of the system's boot splash screen so we can composite it
826 // onscreen until the desktop background is ready.
827 if (is_first_run_after_boot &&
828 (base::CommandLine::ForCurrentProcess()->HasSwitch(
829 switches::kAshCopyHostBackgroundAtBoot) ||
830 base::CommandLine::ForCurrentProcess()->HasSwitch(
831 switches::kAshAnimateFromBootSplashScreen)))
832 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
833 #endif
836 void RootWindowController::CreateContainersInRootWindow(
837 aura::Window* root_window) {
838 // These containers are just used by PowerButtonController to animate groups
839 // of containers simultaneously without messing up the current transformations
840 // on those containers. These are direct children of the root window; all of
841 // the other containers are their children.
843 // The desktop background container is not part of the lock animation, so it
844 // is not included in those animate groups.
845 // When screen is locked desktop background is moved to lock screen background
846 // container (moved back on unlock). We want to make sure that there's an
847 // opaque layer occluding the non-lock-screen layers.
848 aura::Window* desktop_background_container = CreateContainer(
849 kShellWindowId_DesktopBackgroundContainer,
850 "DesktopBackgroundContainer",
851 root_window);
852 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
854 aura::Window* non_lock_screen_containers = CreateContainer(
855 kShellWindowId_NonLockScreenContainersContainer,
856 "NonLockScreenContainersContainer",
857 root_window);
859 aura::Window* lock_background_containers = CreateContainer(
860 kShellWindowId_LockScreenBackgroundContainer,
861 "LockScreenBackgroundContainer",
862 root_window);
863 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
865 aura::Window* lock_screen_containers = CreateContainer(
866 kShellWindowId_LockScreenContainersContainer,
867 "LockScreenContainersContainer",
868 root_window);
869 aura::Window* lock_screen_related_containers = CreateContainer(
870 kShellWindowId_LockScreenRelatedContainersContainer,
871 "LockScreenRelatedContainersContainer",
872 root_window);
874 CreateContainer(kShellWindowId_UnparentedControlContainer,
875 "UnparentedControlContainer",
876 non_lock_screen_containers);
878 aura::Window* default_container = CreateContainer(
879 kShellWindowId_DefaultContainer,
880 "DefaultContainer",
881 non_lock_screen_containers);
882 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
883 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container);
884 SetUsesScreenCoordinates(default_container);
885 SetUsesEasyResizeTargeter(default_container);
887 aura::Window* always_on_top_container = CreateContainer(
888 kShellWindowId_AlwaysOnTopContainer,
889 "AlwaysOnTopContainer",
890 non_lock_screen_containers);
891 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
892 wm::SetSnapsChildrenToPhysicalPixelBoundary(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 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container);
901 SetUsesScreenCoordinates(docked_container);
902 SetUsesEasyResizeTargeter(docked_container);
904 aura::Window* shelf_container =
905 CreateContainer(kShellWindowId_ShelfContainer,
906 "ShelfContainer",
907 non_lock_screen_containers);
908 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container);
909 SetUsesScreenCoordinates(shelf_container);
910 DescendantShouldStayInSameRootWindow(shelf_container);
912 aura::Window* panel_container = CreateContainer(
913 kShellWindowId_PanelContainer,
914 "PanelContainer",
915 non_lock_screen_containers);
916 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container);
917 SetUsesScreenCoordinates(panel_container);
919 aura::Window* shelf_bubble_container =
920 CreateContainer(kShellWindowId_ShelfBubbleContainer,
921 "ShelfBubbleContainer",
922 non_lock_screen_containers);
923 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container);
924 SetUsesScreenCoordinates(shelf_bubble_container);
925 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
927 aura::Window* app_list_container =
928 CreateContainer(kShellWindowId_AppListContainer,
929 "AppListContainer",
930 non_lock_screen_containers);
931 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container);
932 SetUsesScreenCoordinates(app_list_container);
934 aura::Window* modal_container = CreateContainer(
935 kShellWindowId_SystemModalContainer,
936 "SystemModalContainer",
937 non_lock_screen_containers);
938 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container);
939 modal_container->SetLayoutManager(
940 new SystemModalContainerLayoutManager(modal_container));
941 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
942 SetUsesScreenCoordinates(modal_container);
943 SetUsesEasyResizeTargeter(modal_container);
945 // TODO(beng): Figure out if we can make this use
946 // SystemModalContainerEventFilter instead of stops_event_propagation.
947 aura::Window* lock_container = CreateContainer(
948 kShellWindowId_LockScreenContainer,
949 "LockScreenContainer",
950 lock_screen_containers);
951 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container);
952 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
953 switches::kAshDisableLockLayoutManager)) {
954 lock_container->SetLayoutManager(
955 new WorkspaceLayoutManager(lock_container));
956 } else {
957 lock_container->SetLayoutManager(new LockLayoutManager(lock_container));
959 SetUsesScreenCoordinates(lock_container);
960 // TODO(beng): stopsevents
962 aura::Window* lock_modal_container = CreateContainer(
963 kShellWindowId_LockSystemModalContainer,
964 "LockSystemModalContainer",
965 lock_screen_containers);
966 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container);
967 lock_modal_container->SetLayoutManager(
968 new SystemModalContainerLayoutManager(lock_modal_container));
969 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
970 SetUsesScreenCoordinates(lock_modal_container);
971 SetUsesEasyResizeTargeter(lock_modal_container);
973 aura::Window* status_container =
974 CreateContainer(kShellWindowId_StatusContainer,
975 "StatusContainer",
976 lock_screen_related_containers);
977 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container);
978 SetUsesScreenCoordinates(status_container);
979 DescendantShouldStayInSameRootWindow(status_container);
981 aura::Window* settings_bubble_container = CreateContainer(
982 kShellWindowId_SettingBubbleContainer,
983 "SettingBubbleContainer",
984 lock_screen_related_containers);
985 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
986 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container);
987 SetUsesScreenCoordinates(settings_bubble_container);
988 DescendantShouldStayInSameRootWindow(settings_bubble_container);
990 aura::Window* virtual_keyboard_parent_container =
991 CreateContainer(kShellWindowId_ImeWindowParentContainer,
992 "VirtualKeyboardParentContainer",
993 lock_screen_related_containers);
994 wm::SetSnapsChildrenToPhysicalPixelBoundary(
995 virtual_keyboard_parent_container);
996 virtual_keyboard_parent_container->SetLayoutManager(
997 new VirtualKeyboardContainerLayoutManager(
998 virtual_keyboard_parent_container));
999 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
1001 aura::Window* menu_container = CreateContainer(
1002 kShellWindowId_MenuContainer,
1003 "MenuContainer",
1004 lock_screen_related_containers);
1005 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
1006 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container);
1007 SetUsesScreenCoordinates(menu_container);
1009 aura::Window* drag_drop_container = CreateContainer(
1010 kShellWindowId_DragImageAndTooltipContainer,
1011 "DragImageAndTooltipContainer",
1012 lock_screen_related_containers);
1013 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
1014 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container);
1015 SetUsesScreenCoordinates(drag_drop_container);
1017 aura::Window* overlay_container = CreateContainer(
1018 kShellWindowId_OverlayContainer,
1019 "OverlayContainer",
1020 lock_screen_related_containers);
1021 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container);
1022 SetUsesScreenCoordinates(overlay_container);
1024 #if defined(OS_CHROMEOS)
1025 aura::Window* mouse_cursor_container = CreateContainer(
1026 kShellWindowId_MouseCursorContainer,
1027 "MouseCursorContainer",
1028 root_window);
1029 SetUsesScreenCoordinates(mouse_cursor_container);
1030 #endif
1032 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1033 "PowerButtonAnimationContainer", root_window);
1036 void RootWindowController::EnableTouchHudProjection() {
1037 if (touch_hud_projection_)
1038 return;
1039 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1042 void RootWindowController::DisableTouchHudProjection() {
1043 if (!touch_hud_projection_)
1044 return;
1045 touch_hud_projection_->Remove();
1048 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1049 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1052 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1053 if (enabled)
1054 EnableTouchHudProjection();
1055 else
1056 DisableTouchHudProjection();
1059 RootWindowController* GetRootWindowController(
1060 const aura::Window* root_window) {
1061 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1064 } // namespace ash