#[cfg(feature = "std")]
use crate::app::{App, RendererFactory};
use crate::components::{ProgressBar, Slider, Switch};
use crate::ecs::{Entity, World};
use crate::layout::{FlexDirection, LayoutStyle};
#[cfg(feature = "std")]
use crate::plugins::{FpsSummaryPlugin, StdInstantClockPlugin};
#[cfg(feature = "std")]
use crate::surface::Surface;
use crate::types::{Dimension, Fixed};
use crate::widget::Style;
use crate::widget::spawn_children;
use crate::widget::theme::ColorToken;
fn column_style() -> Style {
Style {
layout: LayoutStyle {
direction: FlexDirection::Column,
grow: Fixed::from_int(1),
padding: crate::layout::Padding {
top: Dimension::px(12),
left: Dimension::px(12),
right: Dimension::px(12),
bottom: Dimension::px(12),
},
..Default::default()
},
..Default::default()
}
}
fn row_style(height: i32) -> Style {
Style {
layout: LayoutStyle {
height: Dimension::px(height),
..Default::default()
},
..Default::default()
}
}
pub fn build_widgets(world: &mut World, parent: Entity) {
let column = spawn_children(world, column_style(), |c| {
c.spawn(
Slider::build(Fixed::ZERO, Fixed::from_int(100))
.style(row_style(20))
.fill_color(ColorToken::Primary),
);
c.spawn(Switch::build().style(row_style(26)));
c.spawn(ProgressBar::build().value(0.6).style(row_style(12)));
});
world.insert(column, crate::widget::Parent(parent));
if let Some(children) = world.get_mut::<crate::widget::Children>(parent) {
children.0.push(column);
}
}
#[cfg(feature = "std")]
pub fn setup_app<B, F>(app: &mut App<B, F>, parent: Entity)
where
B: Surface,
F: RendererFactory<B>,
{
app.add_plugin(StdInstantClockPlugin)
.add_plugin(FpsSummaryPlugin::default());
build_widgets(&mut app.world, parent);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::widget::Children;
use crate::widget::IdMap;
use crate::widget::builder::WidgetBuilder;
#[test]
fn build_widgets_smoke() {
let mut world = World::new();
world.insert_resource(IdMap::new());
let parent = WidgetBuilder::new(&mut world).id();
build_widgets(&mut world, parent);
let column = world
.get::<Children>(parent)
.and_then(|c| c.0.first().copied());
let column = column.expect("column parented");
assert_eq!(world.get::<Children>(column).map(|c| c.0.len()), Some(3));
assert!(world.has::<Slider>(world.get::<Children>(column).unwrap().0[0]));
}
}