RealisticSwitch

A latching push switch, a raised box that sinks into its socket when pressed, stays down and glows while on, and springs back up on the next press.

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 evilbuttons

Usage

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.

MethodTypeDefaultDescription
new(id)impl Into<ElementId>-Unique id for the switch state.
checked(bool)bool-Controlled on state.
default_checked(bool)boolfalseInitial 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>#e1261cCap color. Its walls, shading, and glow are mixed from it.
mark_color(c)impl Into<Hsla>whiteColor of the power mark on the cap.
tone(t)RealisticSwitchToneAutoSocket finish: follow the window's appearance (Auto), or force ceramic white (Light) or graphite (Dark).
illuminated(bool)booltrueLight the cap from inside while it is latched on.
size(s)RealisticSwitchSizeDefaultSm, Default or Lg; scales the whole switch.
disabled(bool)boolfalseDisables the switch.

Good to know

  • size scales the whole switch.
  • RealisticSwitchTone::Auto follows the window's light/dark appearance; pass Light or Dark to match your app's own theme instead.
  • There is no click sound: GPUI has no audio API, so the React version's sound prop has no equivalent here.

Search docs

Jump to a button documentation page