Skip to content

Instantly share code, notes, and snippets.

@ItsDoot
Last active March 30, 2026 03:44
Show Gist options
  • Select an option

  • Save ItsDoot/84dd443b00d65871127c056948914934 to your computer and use it in GitHub Desktop.

Select an option

Save ItsDoot/84dd443b00d65871127c056948914934 to your computer and use it in GitHub Desktop.
Systems as `bevy_immediate` ui components
/////////////////////////////////////////////
// Trait + Implementation
/////////////////////////////////////////////
pub trait SystemWidgets<Caps: CapSet>: Sized {
/// Calls the given system, providing this ui entity as its input.
fn run_system(self, id: SystemId<In<Entity>>) -> Self;
}
impl<Caps: CapSet> SystemWidgets<Caps> for ImmEntity<'_, '_, '_, Caps> {
fn run_system(self, id: SystemId<In<Entity>>) -> Self {
self.at_this_moment_apply_commands(|commands| {
let entity = commands.id();
commands.commands().run_system_with(id, entity);
})
}
}
/////////////////////////////////////////////
// Example Usage
/////////////////////////////////////////////
#[derive(Component)
pub struct MyUi(SystemId<In<Entity>>);
fn my_ui_system(In(root): In<Entity>, ctx: ImmCtx<CapsUiFeathers>) {
let mut ui = ctx.build_immediate_from("my_ui_system", root);
// Now use `ui` as you would normally to build ui nodes
}
fn my_startup_system(mut commands: Commands) {
// Register it and spawn it
let system_id = commands.register_system(my_ui_system);
commands.insert(MyUi(system_id));
}
#[derive(Component)]
pub struct UiRoot;
impl ImmediateAttach<CapsUiFeathers> for UiRoot {
type Params = Query<'static, 'static, &'static MyUi>;
fn construct(ui: &mut Imm<CapsUiFeathers>, query: &mut SystemParamItem<Self::Params>) {
// query for your SystemId however you want
let my_ui = query.single();
ui.ch_id("my_ui")
// This is the root entity for the ui system, so anything you spawn on it will
// affect the ui system as its parent entity.
.on_spawn_insert(|| Node {
..Default::default()
})
.run_system(my_ui.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment