#include "ash/wm/lock_action_handler_layout_manager.h"
#include <utility>
#include <vector>
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/mojom/tray_action.mojom.h"
#include "ash/shell.h"
#include "ash/wm/lock_window_state.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "base/functional/bind.h"
#include "ui/wm/core/window_animations.h"
namespace ash {
namespace {
bool ShowChildWindows(mojom::TrayActionState action_state,
LockScreenActionBackgroundState background_state) {
return (action_state == mojom::TrayActionState::kActive ||
action_state == mojom::TrayActionState::kLaunching) &&
(background_state == LockScreenActionBackgroundState::kShown ||
background_state == LockScreenActionBackgroundState::kHidden);
}
}
LockActionHandlerLayoutManager::LockActionHandlerLayoutManager(
aura::Window* window,
LockScreenActionBackgroundController* action_background_controller)
: LockLayoutManager(window),
action_background_controller_(action_background_controller) {
TrayAction* tray_action = Shell::Get()->tray_action();
tray_action_observation_.Observe(tray_action);
action_background_observation_.Observe(action_background_controller_.get());
}
LockActionHandlerLayoutManager::~LockActionHandlerLayoutManager() = default;
void LockActionHandlerLayoutManager::OnWindowAddedToLayout(
aura::Window* child) {
::wm::SetWindowVisibilityAnimationTransition(child, ::wm::ANIMATE_NONE);
WindowState* window_state =
action_background_controller_->IsBackgroundWindow(child)
? LockWindowState::SetLockWindowState(child)
: LockWindowState::SetLockWindowStateWithShelfExcluded(child);
WMEvent event(WM_EVENT_ADDED_TO_WORKSPACE);
window_state->OnWMEvent(&event);
}
void LockActionHandlerLayoutManager::OnChildWindowVisibilityChanged(
aura::Window* child,
bool visible) {
if (action_background_controller_->IsBackgroundWindow(child)) {
window()->StackChildAtBottom(child);
return;
}
if (visible &&
!ShowChildWindows(Shell::Get()->tray_action()->GetLockScreenNoteState(),
action_background_controller_->state())) {
child->Hide();
}
}
void LockActionHandlerLayoutManager::OnLockScreenNoteStateChanged(
mojom::TrayActionState state) {
bool background_changed = false;
switch (state) {
case mojom::TrayActionState::kNotAvailable:
background_changed =
action_background_controller_->HideBackgroundImmediately();
break;
case mojom::TrayActionState::kAvailable:
background_changed = action_background_controller_->HideBackground();
break;
case mojom::TrayActionState::kLaunching:
case mojom::TrayActionState::kActive:
background_changed = action_background_controller_->ShowBackground();
break;
}
if (background_changed)
return;
UpdateChildren(state, action_background_controller_->state());
}
void LockActionHandlerLayoutManager::OnLockScreenActionBackgroundStateChanged(
LockScreenActionBackgroundState state) {
UpdateChildren(Shell::Get()->tray_action()->GetLockScreenNoteState(), state);
}
void LockActionHandlerLayoutManager::UpdateChildren(
mojom::TrayActionState action_state,
LockScreenActionBackgroundState background_state) {
bool show_children = ShowChildWindows(action_state, background_state);
aura::Window* child_to_activate = nullptr;
for (aura::Window* child : window()->children()) {
if (action_background_controller_->IsBackgroundWindow(child))
continue;
if (show_children) {
child->Show();
child_to_activate = child;
} else {
child->Hide();
}
}
if (child_to_activate)
wm::ActivateWindow(child_to_activate);
}
}