use bevy_ecs::change_detection::DetectChanges;
use bevy_ecs::hierarchy::ChildOf;
use bevy_ecs::prelude::{Commands, Component, Entity, Query, Res, World};
use bevy_ecs::system::SystemParam;
use bevy_ui::{Display, FlexDirection, Node};
use super::{ActivePage, Page};
use crate::commands::tui::layout::{filling, set_display};
use crate::commands::tui::session::Session;
#[derive(SystemParam)]
pub struct ShownSurface<'w> {
active: Res<'w, ActivePage>,
session: Res<'w, Session>,
}
pub fn shown_in(world: &World) -> Option<Page> {
showing(
world.resource::<Session>().is_empty(),
*world.resource::<ActivePage>(),
)
}
const fn showing(is_empty: bool, active: ActivePage) -> Option<Page> {
if is_empty { None } else { Some(active.0) }
}
impl ShownSurface<'_> {
#[must_use]
pub fn surface(&self) -> Option<Page> {
showing(self.session.is_empty(), *self.active)
}
#[must_use]
pub fn is_changed(&self) -> bool {
self.active.is_changed() || self.session.is_changed()
}
}
#[derive(Component, Clone, Copy, Debug)]
pub struct SurfaceRoot(pub Option<Page>);
#[derive(Component, Clone, Copy, Debug)]
pub struct FocusStop;
pub fn spawn_surface(commands: &mut Commands, body: Entity, surface: Option<Page>) -> Entity {
commands
.spawn((
SurfaceRoot(surface),
Node {
flex_direction: FlexDirection::Column,
display: Display::None,
..filling()
},
ChildOf(body),
))
.id()
}
pub(super) fn show_the_surface(shown: ShownSurface, mut roots: Query<(&SurfaceRoot, &mut Node)>) {
if !shown.is_changed() {
return;
}
let shown = shown.surface();
for (root, mut node) in &mut roots {
set_display(&mut node, root.0 == shown);
}
}