fullscreen_window_finder.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2016 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.
  4. #include "ash/wm/fullscreen_window_finder.h"
  5. #include "ash/public/cpp/shell_window_ids.h"
  6. #include "ash/wm/desks/desks_util.h"
  7. #include "ash/wm/switchable_windows.h"
  8. #include "ash/wm/window_state.h"
  9. #include "ash/wm/window_util.h"
  10. #include "base/containers/adapters.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/compositor/layer.h"
  13. #include "ui/wm/core/window_util.h"
  14. namespace ash {
  15. namespace {
  16. // Gets the parent switchable container of |context|.
  17. aura::Window* GetSwitchableContainerForContext(aura::Window* context) {
  18. while (context && !IsSwitchableContainer(context))
  19. context = context->parent();
  20. return context;
  21. }
  22. // Returns the active window if it is a child of a switchable container, or
  23. // nullptr otherwise.
  24. aura::Window* GetActiveWindowInSwitchableContainer() {
  25. aura::Window* active_window = window_util::GetActiveWindow();
  26. if (!active_window || !IsSwitchableContainer(active_window->parent()))
  27. return nullptr;
  28. return active_window;
  29. }
  30. // Given |container|, returns its topmost visible and positionable child.
  31. aura::Window* GetTopMostWindowInContainer(aura::Window* container) {
  32. DCHECK(container);
  33. DCHECK(IsSwitchableContainer(container));
  34. for (auto* child : base::Reversed(container->children())) {
  35. // `child` may be type `aura::client::WINDOW_TYPE_CONTROL` which has no
  36. // WindowState.
  37. if (WindowState::Get(child) &&
  38. WindowState::Get(child)->IsUserPositionable() &&
  39. child->layer()->GetTargetVisibility()) {
  40. return child;
  41. }
  42. }
  43. return nullptr;
  44. }
  45. // Given a |topmost_window|, returns it or one of its transient parents if the
  46. // returned window is fullscreen or pinned. Otherwise, return nullptr.
  47. aura::Window* FindFullscreenOrPinnedWindow(aura::Window* topmost_window) {
  48. while (topmost_window) {
  49. const WindowState* window_state = WindowState::Get(topmost_window);
  50. if (window_state->IsFullscreen() || window_state->IsPinned())
  51. return topmost_window;
  52. topmost_window = ::wm::GetTransientParent(topmost_window);
  53. if (topmost_window)
  54. topmost_window = topmost_window->GetToplevelWindow();
  55. }
  56. return nullptr;
  57. }
  58. } // namespace
  59. aura::Window* GetWindowForFullscreenModeForContext(aura::Window* context) {
  60. DCHECK(!context->IsRootWindow());
  61. // Get the active window on the same switchable container as that of
  62. // |context| if any.
  63. aura::Window* switchable_container_for_context =
  64. GetSwitchableContainerForContext(context);
  65. aura::Window* topmost_window = GetActiveWindowInSwitchableContainer();
  66. if (!topmost_window || (switchable_container_for_context !=
  67. GetSwitchableContainerForContext(topmost_window))) {
  68. if (!switchable_container_for_context)
  69. return nullptr;
  70. // If there's no active window, then get the topmost child on the switchable
  71. // container of context.
  72. topmost_window =
  73. GetTopMostWindowInContainer(switchable_container_for_context);
  74. }
  75. return FindFullscreenOrPinnedWindow(topmost_window);
  76. }
  77. aura::Window* GetWindowForFullscreenModeInRoot(aura::Window* root) {
  78. DCHECK(root->IsRootWindow());
  79. // Get the active window on the same |root| if any.
  80. aura::Window* topmost_window = GetActiveWindowInSwitchableContainer();
  81. if (!topmost_window || topmost_window->GetRootWindow() != root) {
  82. // If there's no active window, then get the top most child of the active
  83. // desk container.
  84. topmost_window = GetTopMostWindowInContainer(
  85. desks_util::GetActiveDeskContainerForRoot(root));
  86. }
  87. return FindFullscreenOrPinnedWindow(topmost_window);
  88. }
  89. } // namespace ash