Switch
Layer: atom · Path:
src/atoms/switch.rs· Exports:switch::Switch
A boolean toggle bound to a &mut bool, with an animated sliding thumb. A pill track (primary when on, border_strong when off) holds a background-filled thumb that slides via animate_bool_with_time. All dimensions derive from tokens; includes a focus ring and disabled dim.
Design
-
Purpose / when to use — an on/off setting that applies immediately (the “iOS toggle”). For a checkbox-style boolean (especially in forms/lists with labels) use
Checkbox; for a pressable two-state button useToggle. -
Anatomy — pill track → hover veil → circular thumb sliding left↔right with the animated
t→ focus ring. Track width =track_h + SPACE_4. -
Variants / sizes / states
Size Track height Thumb diameter SmICON_MD16ICON_SM14Md(default)ICON_LG20ICON_MD16LgICON_XL24ICON_LG20States: on (
primarytrack) / off (border_strongtrack — chosen overmutedso the thumb stays legible in dark mode); hover (hover_tveil); focus (ring); disabled (disabled_color, sense → hover). -
Tokens consumed —
theme.primary(on track),theme.border_strong(off track),theme.background(thumb),theme.hover_overlay,theme.ring,core::ICON_*(dims),core::SPACE_4(track extra width),core::DURATION_FAST(thumb slide),core::hover_t,core::disabled_color,core::Size. -
Accessibility — emits
WidgetInfo::selected(WidgetType::Checkbox, enabled, on, "")(no label). Focus ring viafocus::focus_ring_rect.
API
| Signature | Effect |
|---|---|
Switch::new(on: &mut bool) -> Self | Bind to a boolean. |
.enabled(enabled: bool) -> Self / .disabled() | Enable/disable. |
.size(s: Size) -> Self / .sm() / .lg() | Size (core::Size). |
.id_source(id: impl Hash) -> Self | Stable id for animation/interaction (else response.id). |
.show(self, ui: &mut Ui) -> Response | Toggle on click, mark_changed, return Response. |
Usage
#![allow(unused)]
fn main() {
use ouroboros_ui::atoms::Switch;
let mut dark = true;
if Switch::new(&mut dark).show(ui).changed() {
// apply theme
}
}
#![allow(unused)]
fn main() {
use ouroboros_ui::atoms::Switch;
let mut enabled = false;
Switch::new(&mut enabled).lg().id_source("notifications").show(ui);
}
Composition
Atom: paints track + thumb directly. Composes no other atoms. The Switch carries no label — pair it with a Text atom in a molecule/row for a labeled setting.
Notes
- Binding is
&mut bool. On click it flips*onand callsmark_changed(); check.changed(). - The thumb position animates over
DURATION_FASTkeyed byid_source(orresponse.id) — setid_sourcefor switches in loops to avoid animation cross-talk. - Off-state track is
border_strong, intentionally notmuted, for dark-mode legibility.