Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Toggle Switch

A toggle switch represents an on/off setting. ToggleConfig supplies the label, initial checked state, and disabled state; the spawned Toggle component holds the current state. BeverlyPlugin installs its interaction and visual systems.

Live Demo

Open the demo in a separate tab.

Basic Usage

#![allow(unused)]
fn main() {
use beverly::components::toggle::{ToggleConfig, ToggleShadowMaterial, spawn_toggle};
use beverly::prelude::ThemeResource;
use bevy::prelude::*;

fn add_toggle(
    parent: &mut ChildSpawnerCommands,
    theme: &ThemeResource,
    materials: &mut Assets<ToggleShadowMaterial>,
) -> Entity {
    spawn_toggle(
        parent,
        ToggleConfig::new().label("Notifications").checked(true),
        theme,
        materials,
    )
}
}

Pass the application’s ResMut<Assets<ToggleShadowMaterial>> into this helper; do not create a temporary asset collection. Use .disabled(true) for a locked setting, or .thumb_icons(...) to configure the two thumb icons.

Read Toggle::checked through a query when synchronizing application settings or status text. The demo counts checked switches, including the disabled one. Model binding is optional. Use ToggleConfig::new().bind(User::enabled) to connect a boolean model field, or handle toggle events explicitly.

Use .error(|errors| text(errors.join("\n"))) on a ToggleConfig or its bound UiElement to render validation messages below the switch. See Error Callbacks.