Switch

An on/off setting.

Try it

Toggle the switch to update the notification setting. This preview runs the exact code sample below.

Open example separately ↗

Basic use

Radix · integration snippet
use gpui::Context;
use xgpui_luma_look_radix as radix;

struct Example {
    control: xgpui_luma::controls::switch::Switch,
    notifications_enabled: bool,
}

impl Example {
    fn new(cx: &mut Context<Self>) -> Self {
        let control = radix::Switch::new("switch")
            .with_data(true)
            .on_value_changed(cx, |app: &mut Self, on, cx| {
                app.notifications_enabled = on;
                cx.notify();
            })
            .spawn(cx);

        Self { control, notifications_enabled: true }
    }
}

Integration snippet, checked against the current SDK and Radix crates. cx is the initialized owner context. The control retains its callbacks; callback owners are held weakly. Render self.control.clone() in the owner’s view.

SDK behavior & customization

Change carries on. Track and thumb presentation are supplied by the template.

Use the module’s template contract for structural changes. Builder modifiers and item templates vary by control.

Events

Callbacks update application state. Repeated registrations append; the control retains them and holds the owner weakly. cx.notify() requests an owner redraw. set_data synchronizes state silently; on_value_changed follows the existing user-change event.

Event / payloadCallbackApplication use
Change { on: bool }.on_value_changed(cx, |app, on, cx| { … })Update the application’s on/off setting.
FocusChanged { focused: bool }.on_focus_changed(cx, |app, focused, cx| { … })Focus state changed.
EnabledChanged { enabled: bool }.on_enabled_changed(cx, |app, enabled, cx| { … })Enabled state changed.
HoverChanged { hovered: bool }.on_hover_changed(cx, |app, hovered, cx| { … })Pointer hover state changed.

SwitchEvent · Event definitions. Use .on_event(cx, |app, event, cx| { … }) for all events. Factory-created controls support the same callbacks through SwitchEventExt. Existing cx.subscribe observers remain supported.

Source

SDK module · Radix adapter