Reland of Disable smoothness.top_25_smooth on win/mac/linux. (patchset #1 id:1 of...
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob1953fedce4cd949b12bbce1fdb70d60953dec388
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/window_properties.h"
48 #include "ash/wm/window_state.h"
49 #include "ash/wm/window_util.h"
50 #include "ash/wm/workspace/workspace_layout_manager.h"
51 #include "ash/wm/workspace_controller.h"
52 #include "base/command_line.h"
53 #include "base/time/time.h"
54 #include "ui/aura/client/aura_constants.h"
55 #include "ui/aura/client/screen_position_client.h"
56 #include "ui/aura/window.h"
57 #include "ui/aura/window_delegate.h"
58 #include "ui/aura/window_event_dispatcher.h"
59 #include "ui/aura/window_observer.h"
60 #include "ui/aura/window_tracker.h"
61 #include "ui/base/hit_test.h"
62 #include "ui/base/models/menu_model.h"
63 #include "ui/gfx/display.h"
64 #include "ui/gfx/screen.h"
65 #include "ui/keyboard/keyboard_controller.h"
66 #include "ui/keyboard/keyboard_util.h"
67 #include "ui/views/controls/menu/menu_runner.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/core/capture_controller.h"
71 #include "ui/wm/core/easy_resize_window_targeter.h"
72 #include "ui/wm/core/visibility_controller.h"
73 #include "ui/wm/core/window_util.h"
74 #include "ui/wm/public/drag_drop_client.h"
75 #include "ui/wm/public/tooltip_client.h"
76 #include "ui/wm/public/window_types.h"
78 #if defined(OS_CHROMEOS)
79 #include "ash/ash_touch_exploration_manager_chromeos.h"
80 #include "ash/wm/boot_splash_screen_chromeos.h"
81 #include "ui/chromeos/touch_exploration_controller.h"
82 #endif
84 namespace ash {
85 namespace {
87 #if defined(OS_CHROMEOS)
88 // Duration for the animation that hides the boot splash screen, in
89 // milliseconds. This should be short enough in relation to
90 // wm/window_animation.cc's brightness/grayscale fade animation that the login
91 // background image animation isn't hidden by the splash screen animation.
92 const int kBootSplashScreenHideDurationMs = 500;
93 #endif
95 // Creates a new window for use as a container.
96 aura::Window* CreateContainer(int window_id,
97 const char* name,
98 aura::Window* parent) {
99 aura::Window* container = new aura::Window(NULL);
100 container->set_id(window_id);
101 container->SetName(name);
102 container->Init(ui::LAYER_NOT_DRAWN);
103 parent->AddChild(container);
104 if (window_id != kShellWindowId_UnparentedControlContainer)
105 container->Show();
106 return container;
109 float ToRelativeValue(int value, int src, int dst) {
110 return static_cast<float>(value) / static_cast<float>(src) * dst;
113 void MoveOriginRelativeToSize(const gfx::Size& src_size,
114 const gfx::Size& dst_size,
115 gfx::Rect* bounds_in_out) {
116 gfx::Point origin = bounds_in_out->origin();
117 bounds_in_out->set_origin(gfx::Point(
118 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
119 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
122 // Reparents |window| to |new_parent|.
123 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
124 const gfx::Size src_size = window->parent()->bounds().size();
125 const gfx::Size dst_size = new_parent->bounds().size();
126 // Update the restore bounds to make it relative to the display.
127 wm::WindowState* state = wm::GetWindowState(window);
128 gfx::Rect restore_bounds;
129 bool has_restore_bounds = state->HasRestoreBounds();
131 bool update_bounds = (state->IsNormalOrSnapped() || state->IsMinimized()) &&
132 new_parent->id() != kShellWindowId_DockedContainer;
133 gfx::Rect local_bounds;
134 if (update_bounds) {
135 local_bounds = state->window()->bounds();
136 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
139 if (has_restore_bounds) {
140 restore_bounds = state->GetRestoreBoundsInParent();
141 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
144 new_parent->AddChild(window);
146 // Docked windows have bounds handled by the layout manager in AddChild().
147 if (update_bounds)
148 window->SetBounds(local_bounds);
150 if (has_restore_bounds)
151 state->SetRestoreBoundsInParent(restore_bounds);
154 // Reparents the appropriate set of windows from |src| to |dst|.
155 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
156 // Set of windows to move.
157 const int kContainerIdsToMove[] = {
158 kShellWindowId_DefaultContainer,
159 kShellWindowId_DockedContainer,
160 kShellWindowId_PanelContainer,
161 kShellWindowId_AlwaysOnTopContainer,
162 kShellWindowId_SystemModalContainer,
163 kShellWindowId_LockSystemModalContainer,
164 kShellWindowId_UnparentedControlContainer,
165 kShellWindowId_OverlayContainer,
167 const int kExtraContainerIdsToMoveInUnifiedMode[] = {
168 kShellWindowId_LockScreenContainer,
169 kShellWindowId_LockScreenBackgroundContainer,
171 std::vector<int> container_ids(
172 kContainerIdsToMove,
173 kContainerIdsToMove + arraysize(kContainerIdsToMove));
174 // Check the default_multi_display_mode because this is also necessary
175 // in trasition between mirror <-> unified mode.
176 if (Shell::GetInstance()->display_manager()->default_multi_display_mode() ==
177 DisplayManager::UNIFIED) {
178 for (int id : kExtraContainerIdsToMoveInUnifiedMode)
179 container_ids.push_back(id);
182 for (int id : container_ids) {
183 aura::Window* src_container = Shell::GetContainer(src, id);
184 aura::Window* dst_container = Shell::GetContainer(dst, id);
185 while (!src_container->children().empty()) {
186 // Restart iteration from the source container windows each time as they
187 // may change as a result of moving other windows.
188 aura::Window::Windows::const_iterator iter =
189 src_container->children().begin();
190 while (iter != src_container->children().end() &&
191 SystemModalContainerLayoutManager::IsModalBackground(*iter)) {
192 ++iter;
194 // If the entire window list is modal background windows then stop.
195 if (iter == src_container->children().end())
196 break;
197 ReparentWindow(*iter, dst_container);
202 // Mark the container window so that a widget added to this container will
203 // use the virtual screeen coordinates instead of parent.
204 void SetUsesScreenCoordinates(aura::Window* container) {
205 container->SetProperty(kUsesScreenCoordinatesKey, true);
208 // Mark the container window so that a widget added to this container will
209 // say in the same root window regardless of the bounds specified.
210 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
211 container->SetProperty(kStayInSameRootWindowKey, true);
214 void SetUsesEasyResizeTargeter(aura::Window* container) {
215 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
216 -kResizeOutsideBoundsSize,
217 -kResizeOutsideBoundsSize,
218 -kResizeOutsideBoundsSize);
219 gfx::Insets touch_extend = mouse_extend.Scale(
220 kResizeOutsideBoundsScaleForTouch);
221 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
222 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
223 touch_extend)));
226 // A window delegate which does nothing. Used to create a window that
227 // is a event target, but do nothing.
228 class EmptyWindowDelegate : public aura::WindowDelegate {
229 public:
230 EmptyWindowDelegate() {}
231 ~EmptyWindowDelegate() override {}
233 // aura::WindowDelegate overrides:
234 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
235 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
236 void OnBoundsChanged(const gfx::Rect& old_bounds,
237 const gfx::Rect& new_bounds) override {}
238 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
239 return gfx::kNullCursor;
241 int GetNonClientComponent(const gfx::Point& point) const override {
242 return HTNOWHERE;
244 bool ShouldDescendIntoChildForEventHandling(
245 aura::Window* child,
246 const gfx::Point& location) override {
247 return false;
249 bool CanFocus() override { return false; }
250 void OnCaptureLost() override {}
251 void OnPaint(const ui::PaintContext& context) override {}
252 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
253 void OnWindowDestroying(aura::Window* window) override {}
254 void OnWindowDestroyed(aura::Window* window) override { delete this; }
255 void OnWindowTargetVisibilityChanged(bool visible) override {}
256 bool HasHitTestMask() const override { return false; }
257 void GetHitTestMask(gfx::Path* mask) const override {}
259 private:
260 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
263 } // namespace
265 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost* host) {
266 RootWindowController* controller = new RootWindowController(host);
267 controller->Init(RootWindowController::PRIMARY,
268 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
271 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost* host) {
272 RootWindowController* controller = new RootWindowController(host);
273 controller->Init(RootWindowController::SECONDARY, false /* first run */);
276 // static
277 RootWindowController* RootWindowController::ForShelf(
278 const aura::Window* window) {
279 CHECK(Shell::HasInstance());
280 return GetRootWindowController(window->GetRootWindow());
283 // static
284 RootWindowController* RootWindowController::ForWindow(
285 const aura::Window* window) {
286 CHECK(Shell::HasInstance());
287 return GetRootWindowController(window->GetRootWindow());
290 // static
291 RootWindowController* RootWindowController::ForTargetRootWindow() {
292 CHECK(Shell::HasInstance());
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 ash_host_.reset();
308 // The CaptureClient needs to be around for as long as the RootWindow is
309 // valid.
310 capture_client_.reset();
313 aura::WindowTreeHost* RootWindowController::GetHost() {
314 return ash_host_->AsWindowTreeHost();
317 const aura::WindowTreeHost* RootWindowController::GetHost() const {
318 return ash_host_->AsWindowTreeHost();
321 aura::Window* RootWindowController::GetRootWindow() {
322 return GetHost()->window();
325 const aura::Window* RootWindowController::GetRootWindow() const {
326 return GetHost()->window();
329 void RootWindowController::SetWallpaperController(
330 DesktopBackgroundWidgetController* controller) {
331 wallpaper_controller_.reset(controller);
334 void RootWindowController::SetAnimatingWallpaperController(
335 AnimatingDesktopController* controller) {
336 if (animating_wallpaper_controller_.get())
337 animating_wallpaper_controller_->StopAnimating();
338 animating_wallpaper_controller_.reset(controller);
341 void RootWindowController::Shutdown() {
342 Shell* shell = Shell::GetInstance();
343 shell->RemoveShellObserver(this);
345 #if defined(OS_CHROMEOS)
346 if (touch_exploration_manager_) {
347 touch_exploration_manager_.reset();
349 #endif
351 if (animating_wallpaper_controller_.get())
352 animating_wallpaper_controller_->StopAnimating();
353 wallpaper_controller_.reset();
354 animating_wallpaper_controller_.reset();
355 aura::Window* root_window = GetRootWindow();
356 // Change the target root window before closing child windows. If any child
357 // being removed triggers a relayout of the shelf it will try to build a
358 // window list adding windows from the target root window's containers which
359 // may have already gone away.
360 if (Shell::GetTargetRootWindow() == root_window) {
361 shell->set_target_root_window(
362 Shell::GetPrimaryRootWindow() == root_window
363 ? NULL
364 : Shell::GetPrimaryRootWindow());
367 CloseChildWindows();
368 GetRootWindowSettings(root_window)->controller = NULL;
369 screen_dimmer_.reset();
370 workspace_controller_.reset();
371 // Forget with the display ID so that display lookup
372 // ends up with invalid display.
373 GetRootWindowSettings(root_window)->display_id =
374 gfx::Display::kInvalidDisplayID;
375 ash_host_->PrepareForShutdown();
377 system_background_.reset();
378 aura::client::SetScreenPositionClient(root_window, NULL);
381 SystemModalContainerLayoutManager*
382 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
383 aura::Window* modal_container = NULL;
384 if (window) {
385 aura::Window* window_container = GetContainerForWindow(window);
386 if (window_container &&
387 window_container->id() >= kShellWindowId_LockScreenContainer) {
388 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
389 } else {
390 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
392 } else {
393 int modal_window_id = Shell::GetInstance()->session_state_delegate()
394 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
395 kShellWindowId_SystemModalContainer;
396 modal_container = GetContainer(modal_window_id);
398 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
399 modal_container->layout_manager()) : NULL;
402 aura::Window* RootWindowController::GetContainer(int container_id) {
403 return GetRootWindow()->GetChildById(container_id);
406 const aura::Window* RootWindowController::GetContainer(int container_id) const {
407 return ash_host_->AsWindowTreeHost()->window()->GetChildById(container_id);
410 void RootWindowController::ShowShelf() {
411 if (!shelf_->shelf())
412 return;
413 shelf_->shelf()->SetVisible(true);
414 shelf_->status_area_widget()->Show();
417 void RootWindowController::OnShelfCreated() {
418 if (panel_layout_manager_)
419 panel_layout_manager_->SetShelf(shelf_->shelf());
420 if (docked_layout_manager_) {
421 docked_layout_manager_->SetShelf(shelf_->shelf());
422 if (shelf_->shelf_layout_manager())
423 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
426 // Notify shell observers that the shelf has been created.
427 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
430 void RootWindowController::UpdateAfterLoginStatusChange(
431 user::LoginStatus status) {
432 if (status != user::LOGGED_IN_NONE)
433 mouse_event_target_.reset();
434 if (shelf_->status_area_widget())
435 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
438 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
439 #if defined(OS_CHROMEOS)
440 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kAshAnimateFromBootSplashScreen) &&
442 boot_splash_screen_.get()) {
443 // Make the splash screen fade out so it doesn't obscure the desktop
444 // wallpaper's brightness/grayscale animation.
445 boot_splash_screen_->StartHideAnimation(
446 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
448 #endif
451 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
452 // Make sure the wallpaper is visible.
453 system_background_->SetColor(SK_ColorBLACK);
454 #if defined(OS_CHROMEOS)
455 boot_splash_screen_.reset();
456 #endif
458 Shell::GetInstance()->user_wallpaper_delegate()->
459 OnWallpaperAnimationFinished();
460 // Only removes old component when wallpaper animation finished. If we
461 // remove the old one before the new wallpaper is done fading in there will
462 // be a white flash during the animation.
463 if (animating_wallpaper_controller()) {
464 DesktopBackgroundWidgetController* controller =
465 animating_wallpaper_controller()->GetController(true);
466 // |desktop_widget_| should be the same animating widget we try to move
467 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
468 // before move it to |kDesktopController|.
469 DCHECK_EQ(controller->widget(), widget);
470 // Release the old controller and close its background widget.
471 SetWallpaperController(controller);
475 void RootWindowController::CloseChildWindows() {
476 mouse_event_target_.reset();
478 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
479 // to fire notifications.
480 if (docked_layout_manager_ && shelf_ && shelf_->shelf_layout_manager())
481 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
483 // Deactivate keyboard container before closing child windows and shutting
484 // down associated layout managers.
485 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
487 // panel_layout_manager_ needs to be shut down before windows are destroyed.
488 if (panel_layout_manager_) {
489 panel_layout_manager_->Shutdown();
490 panel_layout_manager_ = NULL;
492 // docked_layout_manager_ needs to be shut down before windows are destroyed.
493 if (docked_layout_manager_) {
494 docked_layout_manager_->Shutdown();
495 docked_layout_manager_ = NULL;
497 aura::Window* root_window = GetRootWindow();
498 aura::client::SetDragDropClient(root_window, NULL);
500 // TODO(harrym): Remove when Status Area Widget is a child view.
501 if (shelf_) {
502 shelf_->ShutdownStatusAreaWidget();
504 if (shelf_->shelf_layout_manager())
505 shelf_->shelf_layout_manager()->PrepareForShutdown();
508 // Close background widget first as it depends on tooltip.
509 wallpaper_controller_.reset();
510 animating_wallpaper_controller_.reset();
512 workspace_controller_.reset();
513 aura::client::SetTooltipClient(root_window, NULL);
515 // Explicitly destroy top level windows. We do this as during part of
516 // destruction such windows may query the RootWindow for state.
517 std::queue<aura::Window*> non_toplevel_windows;
518 non_toplevel_windows.push(root_window);
519 while (!non_toplevel_windows.empty()) {
520 aura::Window* non_toplevel_window = non_toplevel_windows.front();
521 non_toplevel_windows.pop();
522 aura::WindowTracker toplevel_windows;
523 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
524 aura::Window* child = non_toplevel_window->children()[i];
525 if (!child->owned_by_parent())
526 continue;
527 if (child->delegate())
528 toplevel_windows.Add(child);
529 else
530 non_toplevel_windows.push(child);
532 while (!toplevel_windows.windows().empty())
533 delete *toplevel_windows.windows().begin();
535 // And then remove the containers.
536 while (!root_window->children().empty()) {
537 aura::Window* window = root_window->children()[0];
538 if (window->owned_by_parent()) {
539 delete window;
540 } else {
541 root_window->RemoveChild(window);
545 shelf_.reset();
548 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
549 // Forget the shelf early so that shelf don't update itself using wrong
550 // display info.
551 workspace_controller_->SetShelf(NULL);
552 ReparentAllWindows(GetRootWindow(), dst);
555 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
556 return shelf_->shelf_layout_manager();
559 SystemTray* RootWindowController::GetSystemTray() {
560 // We assume in throughout the code that this will not return NULL. If code
561 // triggers this for valid reasons, it should test status_area_widget first.
562 CHECK(shelf_->status_area_widget());
563 return shelf_->status_area_widget()->system_tray();
566 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
567 ui::MenuSourceType source_type) {
568 DCHECK(Shell::GetInstance()->delegate());
569 scoped_ptr<ui::MenuModel> menu_model(
570 Shell::GetInstance()->delegate()->CreateContextMenu(
571 GetRootWindow(), NULL, NULL));
572 if (!menu_model)
573 return;
575 // Background controller may not be set yet if user clicked on status are
576 // before initial animation completion. See crbug.com/222218
577 if (!wallpaper_controller_.get())
578 return;
580 views::MenuRunner menu_runner(menu_model.get(),
581 views::MenuRunner::CONTEXT_MENU);
582 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
583 NULL,
584 gfx::Rect(location_in_screen, gfx::Size()),
585 views::MENU_ANCHOR_TOPLEFT,
586 source_type) == views::MenuRunner::MENU_DELETED) {
587 return;
590 Shell::GetInstance()->UpdateShelfVisibility();
593 void RootWindowController::UpdateShelfVisibility() {
594 shelf_->shelf_layout_manager()->UpdateVisibilityState();
597 aura::Window* RootWindowController::GetWindowForFullscreenMode() {
598 aura::Window* topmost_window = NULL;
599 aura::Window* active_window = wm::GetActiveWindow();
600 if (active_window && active_window->GetRootWindow() == GetRootWindow() &&
601 IsSwitchableContainer(active_window->parent())) {
602 // Use the active window when it is on the current root window to determine
603 // the fullscreen state to allow temporarily using a panel or docked window
604 // (which are always above the default container) while a fullscreen
605 // window is open. We only use the active window when in a switchable
606 // container as the launcher should not exit fullscreen mode.
607 topmost_window = active_window;
608 } else {
609 // Otherwise, use the topmost window on the root window's default container
610 // when there is no active window on this root window.
611 const aura::Window::Windows& windows =
612 GetContainer(kShellWindowId_DefaultContainer)->children();
613 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
614 iter != windows.rend(); ++iter) {
615 if (wm::IsWindowUserPositionable(*iter) &&
616 (*iter)->layer()->GetTargetVisibility()) {
617 topmost_window = *iter;
618 break;
622 while (topmost_window) {
623 if (wm::GetWindowState(topmost_window)->IsFullscreen())
624 return topmost_window;
625 topmost_window = ::wm::GetTransientParent(topmost_window);
627 return NULL;
630 void RootWindowController::ActivateKeyboard(
631 keyboard::KeyboardController* keyboard_controller) {
632 if (!keyboard::IsKeyboardEnabled() ||
633 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
634 return;
636 DCHECK(keyboard_controller);
637 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
638 keyboard_controller->AddObserver(panel_layout_manager_);
639 keyboard_controller->AddObserver(docked_layout_manager_);
640 keyboard_controller->AddObserver(workspace_controller_->layout_manager());
641 keyboard_controller->AddObserver(
642 always_on_top_controller_->GetLayoutManager());
643 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
644 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
645 DCHECK(parent);
646 aura::Window* keyboard_container =
647 keyboard_controller->GetContainerWindow();
648 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
649 parent->AddChild(keyboard_container);
652 void RootWindowController::DeactivateKeyboard(
653 keyboard::KeyboardController* keyboard_controller) {
654 if (!keyboard_controller ||
655 !keyboard_controller->keyboard_container_initialized()) {
656 return;
658 aura::Window* keyboard_container =
659 keyboard_controller->GetContainerWindow();
660 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
661 aura::Window* parent =
662 GetContainer(kShellWindowId_ImeWindowParentContainer);
663 DCHECK(parent);
664 parent->RemoveChild(keyboard_container);
665 // Virtual keyboard may be deactivated while still showing, notify all
666 // observers that keyboard bounds changed to 0 before remove them.
667 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
668 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
669 keyboard_controller->RemoveObserver(panel_layout_manager_);
670 keyboard_controller->RemoveObserver(docked_layout_manager_);
671 keyboard_controller->RemoveObserver(
672 workspace_controller_->layout_manager());
673 keyboard_controller->RemoveObserver(
674 always_on_top_controller_->GetLayoutManager());
675 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
679 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
680 aura::Window* parent = GetContainer(kShellWindowId_ImeWindowParentContainer);
681 return parent ? parent->Contains(window) : false;
684 ////////////////////////////////////////////////////////////////////////////////
685 // RootWindowController, private:
687 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
688 : ash_host_(ash_host),
689 root_window_layout_(NULL),
690 docked_layout_manager_(NULL),
691 panel_layout_manager_(NULL),
692 touch_hud_debug_(NULL),
693 touch_hud_projection_(NULL) {
694 aura::Window* root_window = GetRootWindow();
695 GetRootWindowSettings(root_window)->controller = this;
696 screen_dimmer_.reset(new ScreenDimmer(root_window));
698 stacking_controller_.reset(new StackingController);
699 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
700 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
703 void RootWindowController::Init(RootWindowType root_window_type,
704 bool first_run_after_boot) {
705 aura::Window* root_window = GetRootWindow();
706 Shell* shell = Shell::GetInstance();
707 shell->InitRootWindow(root_window);
709 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
710 CreateContainersInRootWindow(root_window);
712 CreateSystemBackground(first_run_after_boot);
714 InitLayoutManagers();
715 InitTouchHuds();
717 if (Shell::GetPrimaryRootWindowController()->
718 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
719 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
722 shell->AddShellObserver(this);
724 if (root_window_type == PRIMARY) {
725 root_window_layout()->OnWindowResized();
726 shell->InitKeyboard();
727 } else {
728 root_window_layout()->OnWindowResized();
729 ash_host_->AsWindowTreeHost()->Show();
731 // Create a shelf if a user is already logged in.
732 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
733 shelf()->CreateShelf();
735 // Notify shell observers about new root window.
736 shell->OnRootWindowAdded(root_window);
739 #if defined(OS_CHROMEOS)
740 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
741 switches::kAshDisableTouchExplorationMode)) {
742 touch_exploration_manager_.reset(new AshTouchExplorationManager(this));
744 #endif
747 void RootWindowController::InitLayoutManagers() {
748 aura::Window* root_window = GetRootWindow();
749 root_window_layout_ = new RootWindowLayoutManager(root_window);
750 root_window->SetLayoutManager(root_window_layout_);
752 aura::Window* default_container =
753 GetContainer(kShellWindowId_DefaultContainer);
754 // Workspace manager has its own layout managers.
755 workspace_controller_.reset(
756 new WorkspaceController(default_container));
758 aura::Window* always_on_top_container =
759 GetContainer(kShellWindowId_AlwaysOnTopContainer);
760 always_on_top_controller_.reset(
761 new AlwaysOnTopController(always_on_top_container));
763 DCHECK(!shelf_.get());
764 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
765 // TODO(harrym): Remove when status area is view.
766 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
767 shelf_.reset(new ShelfWidget(
768 shelf_container, status_container, workspace_controller()));
770 if (!Shell::GetInstance()->session_state_delegate()->
771 IsActiveUserSessionStarted()) {
772 // This window exists only to be a event target on login screen.
773 // It does not have to handle events, nor be visible.
774 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
775 mouse_event_target_->Init(ui::LAYER_NOT_DRAWN);
777 aura::Window* lock_background_container =
778 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
779 lock_background_container->AddChild(mouse_event_target_.get());
780 mouse_event_target_->Show();
783 // Create Docked windows layout manager
784 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
785 docked_layout_manager_ =
786 new DockedWindowLayoutManager(docked_container, workspace_controller());
787 docked_container->SetLayoutManager(docked_layout_manager_);
789 // Installs SnapLayoutManager to containers who set the
790 // |kSnapsChildrenToPhysicalPixelBoundary| property.
791 wm::InstallSnapLayoutManagerToContainers(root_window);
793 // Create Panel layout manager
794 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
795 panel_layout_manager_ = new PanelLayoutManager(panel_container);
796 panel_container->SetLayoutManager(panel_layout_manager_);
797 panel_container_handler_.reset(new PanelWindowEventHandler);
798 panel_container->AddPreTargetHandler(panel_container_handler_.get());
800 // Install an AttachedPanelWindowTargeter on the panel container to make it
801 // easier to correctly target shelf buttons with touch.
802 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
803 -kResizeOutsideBoundsSize,
804 -kResizeOutsideBoundsSize,
805 -kResizeOutsideBoundsSize);
806 gfx::Insets touch_extend = mouse_extend.Scale(
807 kResizeOutsideBoundsScaleForTouch);
808 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
809 new AttachedPanelWindowTargeter(panel_container,
810 mouse_extend,
811 touch_extend,
812 panel_layout_manager_)));
815 void RootWindowController::InitTouchHuds() {
816 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
817 if (command_line->HasSwitch(switches::kAshTouchHud))
818 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
819 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
820 EnableTouchHudProjection();
823 void RootWindowController::CreateSystemBackground(
824 bool is_first_run_after_boot) {
825 SkColor color = SK_ColorBLACK;
826 #if defined(OS_CHROMEOS)
827 if (is_first_run_after_boot)
828 color = kChromeOsBootColor;
829 #endif
830 system_background_.reset(
831 new SystemBackgroundController(GetRootWindow(), color));
833 #if defined(OS_CHROMEOS)
834 // Make a copy of the system's boot splash screen so we can composite it
835 // onscreen until the desktop background is ready.
836 if (is_first_run_after_boot &&
837 (base::CommandLine::ForCurrentProcess()->HasSwitch(
838 switches::kAshCopyHostBackgroundAtBoot) ||
839 base::CommandLine::ForCurrentProcess()->HasSwitch(
840 switches::kAshAnimateFromBootSplashScreen)))
841 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
842 #endif
845 void RootWindowController::CreateContainersInRootWindow(
846 aura::Window* root_window) {
847 // These containers are just used by PowerButtonController to animate groups
848 // of containers simultaneously without messing up the current transformations
849 // on those containers. These are direct children of the root window; all of
850 // the other containers are their children.
852 // The desktop background container is not part of the lock animation, so it
853 // is not included in those animate groups.
854 // When screen is locked desktop background is moved to lock screen background
855 // container (moved back on unlock). We want to make sure that there's an
856 // opaque layer occluding the non-lock-screen layers.
857 aura::Window* desktop_background_container = CreateContainer(
858 kShellWindowId_DesktopBackgroundContainer,
859 "DesktopBackgroundContainer",
860 root_window);
861 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
863 aura::Window* non_lock_screen_containers = CreateContainer(
864 kShellWindowId_NonLockScreenContainersContainer,
865 "NonLockScreenContainersContainer",
866 root_window);
867 // Clip all windows inside this container, as half pixel of the window's
868 // texture may become visible when the screen is scaled. crbug.com/368591.
869 non_lock_screen_containers->layer()->SetMasksToBounds(true);
871 aura::Window* lock_background_containers = CreateContainer(
872 kShellWindowId_LockScreenBackgroundContainer,
873 "LockScreenBackgroundContainer",
874 root_window);
875 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
877 aura::Window* lock_screen_containers = CreateContainer(
878 kShellWindowId_LockScreenContainersContainer,
879 "LockScreenContainersContainer",
880 root_window);
881 aura::Window* lock_screen_related_containers = CreateContainer(
882 kShellWindowId_LockScreenRelatedContainersContainer,
883 "LockScreenRelatedContainersContainer",
884 root_window);
886 CreateContainer(kShellWindowId_UnparentedControlContainer,
887 "UnparentedControlContainer",
888 non_lock_screen_containers);
890 aura::Window* default_container = CreateContainer(
891 kShellWindowId_DefaultContainer,
892 "DefaultContainer",
893 non_lock_screen_containers);
894 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
895 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container);
896 SetUsesScreenCoordinates(default_container);
897 SetUsesEasyResizeTargeter(default_container);
899 aura::Window* always_on_top_container = CreateContainer(
900 kShellWindowId_AlwaysOnTopContainer,
901 "AlwaysOnTopContainer",
902 non_lock_screen_containers);
903 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
904 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container);
905 SetUsesScreenCoordinates(always_on_top_container);
907 aura::Window* docked_container = CreateContainer(
908 kShellWindowId_DockedContainer,
909 "DockedContainer",
910 non_lock_screen_containers);
911 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container);
912 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container);
913 SetUsesScreenCoordinates(docked_container);
914 SetUsesEasyResizeTargeter(docked_container);
916 aura::Window* shelf_container =
917 CreateContainer(kShellWindowId_ShelfContainer,
918 "ShelfContainer",
919 non_lock_screen_containers);
920 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container);
921 SetUsesScreenCoordinates(shelf_container);
922 DescendantShouldStayInSameRootWindow(shelf_container);
924 aura::Window* panel_container = CreateContainer(
925 kShellWindowId_PanelContainer,
926 "PanelContainer",
927 non_lock_screen_containers);
928 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container);
929 SetUsesScreenCoordinates(panel_container);
931 aura::Window* shelf_bubble_container =
932 CreateContainer(kShellWindowId_ShelfBubbleContainer,
933 "ShelfBubbleContainer",
934 non_lock_screen_containers);
935 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container);
936 SetUsesScreenCoordinates(shelf_bubble_container);
937 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
939 aura::Window* app_list_container =
940 CreateContainer(kShellWindowId_AppListContainer,
941 "AppListContainer",
942 non_lock_screen_containers);
943 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container);
944 SetUsesScreenCoordinates(app_list_container);
946 aura::Window* modal_container = CreateContainer(
947 kShellWindowId_SystemModalContainer,
948 "SystemModalContainer",
949 non_lock_screen_containers);
950 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container);
951 modal_container->SetLayoutManager(
952 new SystemModalContainerLayoutManager(modal_container));
953 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
954 SetUsesScreenCoordinates(modal_container);
955 SetUsesEasyResizeTargeter(modal_container);
957 // TODO(beng): Figure out if we can make this use
958 // SystemModalContainerEventFilter instead of stops_event_propagation.
959 aura::Window* lock_container = CreateContainer(
960 kShellWindowId_LockScreenContainer,
961 "LockScreenContainer",
962 lock_screen_containers);
963 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container);
964 lock_container->SetLayoutManager(new LockLayoutManager(lock_container));
965 SetUsesScreenCoordinates(lock_container);
966 // TODO(beng): stopsevents
968 aura::Window* lock_modal_container = CreateContainer(
969 kShellWindowId_LockSystemModalContainer,
970 "LockSystemModalContainer",
971 lock_screen_containers);
972 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container);
973 lock_modal_container->SetLayoutManager(
974 new SystemModalContainerLayoutManager(lock_modal_container));
975 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
976 SetUsesScreenCoordinates(lock_modal_container);
977 SetUsesEasyResizeTargeter(lock_modal_container);
979 aura::Window* status_container =
980 CreateContainer(kShellWindowId_StatusContainer,
981 "StatusContainer",
982 lock_screen_related_containers);
983 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container);
984 SetUsesScreenCoordinates(status_container);
985 DescendantShouldStayInSameRootWindow(status_container);
987 aura::Window* settings_bubble_container = CreateContainer(
988 kShellWindowId_SettingBubbleContainer,
989 "SettingBubbleContainer",
990 lock_screen_related_containers);
991 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
992 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container);
993 SetUsesScreenCoordinates(settings_bubble_container);
994 DescendantShouldStayInSameRootWindow(settings_bubble_container);
996 aura::Window* virtual_keyboard_parent_container =
997 CreateContainer(kShellWindowId_ImeWindowParentContainer,
998 "VirtualKeyboardParentContainer",
999 lock_screen_related_containers);
1000 wm::SetSnapsChildrenToPhysicalPixelBoundary(
1001 virtual_keyboard_parent_container);
1002 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
1004 aura::Window* menu_container = CreateContainer(
1005 kShellWindowId_MenuContainer,
1006 "MenuContainer",
1007 lock_screen_related_containers);
1008 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
1009 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container);
1010 SetUsesScreenCoordinates(menu_container);
1012 aura::Window* drag_drop_container = CreateContainer(
1013 kShellWindowId_DragImageAndTooltipContainer,
1014 "DragImageAndTooltipContainer",
1015 lock_screen_related_containers);
1016 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
1017 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container);
1018 SetUsesScreenCoordinates(drag_drop_container);
1020 aura::Window* overlay_container = CreateContainer(
1021 kShellWindowId_OverlayContainer,
1022 "OverlayContainer",
1023 lock_screen_related_containers);
1024 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container);
1025 SetUsesScreenCoordinates(overlay_container);
1027 #if defined(OS_CHROMEOS)
1028 aura::Window* mouse_cursor_container = CreateContainer(
1029 kShellWindowId_MouseCursorContainer,
1030 "MouseCursorContainer",
1031 root_window);
1032 SetUsesScreenCoordinates(mouse_cursor_container);
1033 #endif
1035 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1036 "PowerButtonAnimationContainer", root_window);
1039 void RootWindowController::EnableTouchHudProjection() {
1040 if (touch_hud_projection_)
1041 return;
1042 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1045 void RootWindowController::DisableTouchHudProjection() {
1046 if (!touch_hud_projection_)
1047 return;
1048 touch_hud_projection_->Remove();
1051 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1052 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1055 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1056 if (enabled)
1057 EnableTouchHudProjection();
1058 else
1059 DisableTouchHudProjection();
1062 RootWindowController* GetRootWindowController(
1063 const aura::Window* root_window) {
1064 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1067 } // namespace ash