Expand description
Toggle — pressed/unpressed two-state buttons, used either standalone
or grouped. Mirrors the shadcn / Radix Toggle + ToggleGroup primitives
(which themselves reflect the WAI-ARIA role="button" with
aria-pressed and role="group" patterns), so LLM authors trained
on web UI find the same shape here.
Three flavors, three state shapes:
toggle— a single binary on/off button. State is abool.toggle_group— a row of mutually-exclusive options. State is the value of the currently-pressed item. Looks like a panel-lesscrate::widgets::tabsrow; reach for that instead when each option has associated content.toggle_group_multi— a row of independent on/off options. State is a set of pressed values. Use for filter chips, format toggles, anything where multiple options can be on at once.
The app owns the state; the widget is a pure visual + identity
carrier — same controlled pattern used by crate::widgets::radio
and crate::widgets::tabs.
use aetna_core::prelude::*;
use std::collections::HashSet;
struct App {
wrap: bool, // standalone toggle
view: String, // single-select group
filters: HashSet<String>, // multi-select group
}
impl aetna_core::App for App {
fn build(&self, _cx: &BuildCx) -> El {
column([
toggle("wrap", self.wrap, "Wrap lines"),
toggle_group("view", &self.view, [
("list", "List"),
("grid", "Grid"),
("kanban", "Kanban"),
]),
toggle_group_multi("filters", &self.filters, [
("open", "Open"),
("draft", "Draft"),
("merged", "Merged"),
]),
])
}
fn on_event(&mut self, event: UiEvent) {
toggle::apply_event_pressed(&mut self.wrap, &event, "wrap");
toggle::apply_event_single(&mut self.view, &event, "view", |s| {
Some(s.to_string())
});
toggle::apply_event_multi(&mut self.filters, &event, "filters");
}
}§Routed keys
- Standalone toggle:
{key}—Clickflips the bool. - Group items:
{group_key}:toggle:{value}—Clickselects (single) or flips (multi) that value. Usetoggle_option_keyto format and parse.
Chosen to parallel crate::widgets::tabs’s {key}:tab:{value} and
crate::widgets::radio’s {key}:radio:{value} so the controlled-
widget vocabulary stays consistent.
§Dogfood note
Composes only the public widget-kit surface — Kind::Custom,
.focusable() + .paint_overflow() for the focus ring, and
.current() / .ghost() for pressed-vs-unpressed. An app crate can
fork this file and produce an equivalent widget against the same
public API.
Enums§
- Toggle
Action - What a routed
UiEventmeans for a controlled toggle keyedkey.
Functions§
- apply_
event_ multi - Fold a routed
UiEventinto a multi-select toggle group’sHashSet<String>field, flipping the clicked value’s membership. Returnstrueif the event was a toggle event for thiskey. - apply_
event_ pressed - Fold a routed
UiEventinto a standalone toggle’sboolfield. Returnstrueif the event was a press for thiskey. - apply_
event_ single - Fold a routed
UiEventinto a single-select toggle group’s value field. Returnstrueif the event was a toggle event for thiskey. - classify_
event - Classify a routed
UiEventagainst a controlled toggle keyedkey. ReturnsNonefor events that aren’t for this toggle. - toggle
- A standalone two-state button.
pressedpaints the active surface (accent fill + accent foreground + semibold), unpressed renders as ghost. Click on the routed keykeyflips the bool — fold the event back withapply_event_pressed. - toggle_
group - A row of mutually-exclusive toggle items — pick one.
currentis the currently-pressed value, formatted viastd::fmt::Displayand compared against each option’svalue.optionsis an iterable of(value, label)pairs. - toggle_
group_ multi - A row of independent on/off toggle items — flip each
independently.
selectedis the set of currently-pressed values (compared as strings, formatted from each option’svalue).optionsis an iterable of(value, label)pairs. - toggle_
item - A single item inside a toggle group. Apps usually let
toggle_group/toggle_group_multibuild these from(value, label)pairs; reach fortoggle_itemdirectly when composing the row by hand (e.g. mixing in icons or badges per option). - toggle_
option_ key - Format the routed key emitted when a toggle group item is clicked.