GPUI Box GitHub

Components / controls

Switch builder

A control that takes effect the moment it is flipped.

A builder is RenderOnce. Construct and mount it in one expression.

use gpui_kit::controls::toggle::Switch;

Reviewed in choice.

Also drawn by motion-state, which arranges components rather than reviewing them.

Construct
new(ident: impl Into<Ident>) -> Self
Options

Chain onto the value.

label(label: impl Into<SharedString>) -> Self
description(description: impl Into<SharedString>) -> Self
named(name: impl Into<SharedString>) -> Self
on(on: bool) -> Self
invalid(invalid: bool) -> Self
on_change(handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self
bind(binding: &Binding<bool>, cx: &App) -> Self
Example · choice
fn choice(_window: &mut Window, cx: &mut App) -> AnyElement {
    let theme = cx.theme().clone();
    div()
        .flex()
        .flex_col()
        .gap(px(theme.space(Space::Md)))
        .p(px(theme.space(Space::Lg)))
        .w(px(360.0))
        .child(
            Checkbox::new("scene.choice.telemetry")
                .label("Send anonymous usage data")
                .description("Counts only, never file contents")
                .checked(true)
                .on_change(|_, _, _| {}),
        )
        .child(
            Checkbox::new("scene.choice.partial")
                .label("Some providers enabled")
                .mixed()
                .on_change(|_, _, _| {}),
        )
        .child(
            Checkbox::new("scene.choice.locked")
                .label("Managed by policy")
                .checked(true)
                .disabled(true),
        )
        .child(
            Radio::new("scene.choice.ask")
                .label("Ask before every action")
                .selected(true)
                .on_select(|_, _| {}),
        )
        .child(
            Radio::new("scene.choice.auto")
                .label("Run without asking")
                .description("Consequential actions still require approval")
                .on_select(|_, _| {}),
        )
        .child(
            Switch::new("scene.choice.preview")
                .label("Preview releases")
                .on(true)
                .on_change(|_, _, _| {}),
        )
        .child(
            Switch::new("scene.choice.background-updates")
                .label("Background updates")
                .on(false)
                .on_change(|_, _, _| {}),
        )
        .child(
            Switch::new("scene.choice.managed-updates")
                .label("Managed updates")
                .on(true)
                .disabled(true),
        )
        .child(
            Slider::new("scene.choice.temperature")
                .label("Temperature")
                .range(0.0, 2.0)
                .step(0.1)
                .value(0.7)
                .display("0.7")
                .on_change(|_, _, _| {}),
        )
        .child(
            Slider::new("scene.choice.window")
                .label("Window")
                .range(0.0, 1.0)
                .values(0.2, 0.8)
                .marks([0.0, 0.25, 0.5, 0.75, 1.0])
                .display("0.2 – 0.8")
                .on_range_change(|_, _, _, _| {}),
        )
        .child(caption(
            &theme,
            "Vertical range uses the same value, marks, and keyboard contract",
        ))
        .child(
            div().h(px(220.0)).child(
                Slider::new("scene.choice.vertical")
                    .label("Vertical")
                    .orientation(SliderOrientation::Vertical)
                    .range(0.0, 100.0)
                    .step(10.0)
                    .value(60.0)
                    .marks([0.0, 25.0, 50.0, 75.0, 100.0])
                    .display("60")
                    .on_change(|_, _, _| {}),
            ),
        )
        .into_any_element()
}

Source: crates/gpui-kit/src/controls/toggle.rs