#include "ash/wm/fullscreen_window_finder.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/switchable_windows.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/containers/adapters.h"
#include "ui/aura/window.h"
#include "ui/compositor/layer.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
aura::Window* GetSwitchableContainerForContext(aura::Window* context) {
while (context && !IsSwitchableContainer(context))
context = context->parent();
return context;
}
aura::Window* GetActiveWindowInSwitchableContainer() {
aura::Window* active_window = window_util::GetActiveWindow();
if (!active_window || !IsSwitchableContainer(active_window->parent()))
return nullptr;
return active_window;
}
aura::Window* GetTopMostWindowInContainer(aura::Window* container) {
DCHECK(container);
DCHECK(IsSwitchableContainer(container));
for (auto* child : base::Reversed(container->children())) {
if (WindowState::Get(child) &&
WindowState::Get(child)->IsUserPositionable() &&
child->layer()->GetTargetVisibility()) {
return child;
}
}
return nullptr;
}
aura::Window* FindFullscreenOrPinnedWindow(aura::Window* topmost_window) {
while (topmost_window) {
const WindowState* window_state = WindowState::Get(topmost_window);
if (window_state->IsFullscreen() || window_state->IsPinned())
return topmost_window;
topmost_window = ::wm::GetTransientParent(topmost_window);
if (topmost_window)
topmost_window = topmost_window->GetToplevelWindow();
}
return nullptr;
}
}
aura::Window* GetWindowForFullscreenModeForContext(aura::Window* context) {
DCHECK(!context->IsRootWindow());
aura::Window* switchable_container_for_context =
GetSwitchableContainerForContext(context);
aura::Window* topmost_window = GetActiveWindowInSwitchableContainer();
if (!topmost_window || (switchable_container_for_context !=
GetSwitchableContainerForContext(topmost_window))) {
if (!switchable_container_for_context)
return nullptr;
topmost_window =
GetTopMostWindowInContainer(switchable_container_for_context);
}
return FindFullscreenOrPinnedWindow(topmost_window);
}
aura::Window* GetWindowForFullscreenModeInRoot(aura::Window* root) {
DCHECK(root->IsRootWindow());
aura::Window* topmost_window = GetActiveWindowInSwitchableContainer();
if (!topmost_window || topmost_window->GetRootWindow() != root) {
topmost_window = GetTopMostWindowInContainer(
desks_util::GetActiveDeskContainerForRoot(root));
}
return FindFullscreenOrPinnedWindow(topmost_window);
}
}