Preview
Loading GPUI
Props
Variants
Colors
Pass any color.
Loading GPUI
div()
.flex()
.gap_6()
.child(RealisticSwitch::new("red").size(RealisticSwitchSize::Lg).default_checked(true))
.child(
RealisticSwitch::new("blue")
.size(RealisticSwitchSize::Lg)
.color(rgb(0x1f6feb))
.default_checked(true),
)
.child(
RealisticSwitch::new("white")
.size(RealisticSwitchSize::Lg)
.color(rgb(0xf5f5f5))
.mark_color(rgb(0x2a2a2a))
.illuminated(false),
)Sizes
Loading GPUI
div()
.flex()
.items_center()
.gap_6()
.child(RealisticSwitch::new("sm").size(RealisticSwitchSize::Sm))
.child(RealisticSwitch::new("default"))
.child(RealisticSwitch::new("lg").size(RealisticSwitchSize::Lg))Install
cargo add evilbuttonsUsage
use evilbuttons::RealisticSwitch;
use gpui::{div, prelude::*, Context, IntoElement, Render, Window};
struct PowerSwitch;
impl Render for PowerSwitch {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().child(
RealisticSwitch::new("main-power").on_change(|on, _, _| println!("power: {on}")),
)
}
}let view = cx.entity();
RealisticSwitch::new("live")
.checked(self.is_live)
.on_change(move |on, _, cx| {
view.update(cx, |this, cx| {
this.is_live = on;
cx.notify();
})
})
.color(rgb(0x1f6feb))Props
RealisticSwitch is a GPUI RenderOnce builder; its on state (when uncontrolled) and cap motion are keyed by id.
| Method | Type | Default | Description |
|---|---|---|---|
new(id) | impl Into<ElementId> | - | Unique id for the switch state. |
checked(bool) | bool | - | Controlled on state. |
default_checked(bool) | bool | false | Initial state when uncontrolled. |
on_change(f) | Fn(bool, &mut Window, &mut App) | - | Called with the new state when the switch latches or releases. |
color(c) | impl Into<Hsla> | #e1261c | Cap color. Its walls, shading, and glow are mixed from it. |
mark_color(c) | impl Into<Hsla> | white | Color of the power mark on the cap. |
tone(t) | RealisticSwitchTone | Auto | Socket finish: follow the window's appearance (Auto), or force ceramic white (Light) or graphite (Dark). |
illuminated(bool) | bool | true | Light the cap from inside while it is latched on. |
size(s) | RealisticSwitchSize | Default | Sm, Default or Lg; scales the whole switch. |
disabled(bool) | bool | false | Disables the switch. |
Good to know
sizescales the whole switch.RealisticSwitchTone::Autofollows the window's light/dark appearance; passLightorDarkto match your app's own theme instead.- There is no click sound: GPUI has no audio API, so the React version's
soundprop has no equivalent here.