use bevy::prelude::*;
use crate::tokens;
const TRACK_HEIGHT_PX: f32 = 6.0;
#[derive(Component)]
pub struct ProgressBar;
#[derive(Component)]
pub struct ProgressBarFill;
pub fn progress_bar(fraction: f32) -> impl Bundle {
let clamped = fraction.clamp(0.0, 1.0);
(
ProgressBar,
Node {
width: Val::Percent(100.0),
height: Val::Px(TRACK_HEIGHT_PX),
border: UiRect::all(Val::Px(1.0)),
overflow: Overflow::clip(),
..default()
},
BorderColor::all(tokens::BORDER_SUBTLE),
BackgroundColor(tokens::PANEL_BG),
children![(
ProgressBarFill,
Node {
width: Val::Percent(clamped * 100.0),
height: Val::Percent(100.0),
..default()
},
BackgroundColor(tokens::TEXT_ACCENT),
)],
)
}
pub fn set_progress_fill(
bar_entity: Entity,
fraction: f32,
children_q: &Query<&Children>,
fill_q: &mut Query<&mut Node, With<ProgressBarFill>>,
) {
let Ok(children) = children_q.get(bar_entity) else {
return;
};
let clamped = fraction.clamp(0.0, 1.0);
for child in children.iter() {
if let Ok(mut node) = fill_q.get_mut(child) {
node.width = Val::Percent(clamped * 100.0);
}
}
}