Skip to main content

Module toggle

Module toggle 

Source
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 a bool.
  • toggle_group — a row of mutually-exclusive options. State is the value of the currently-pressed item. Looks like a panel-less crate::widgets::tabs row; 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}Click flips the bool.
  • Group items: {group_key}:toggle:{value}Click selects (single) or flips (multi) that value. Use toggle_option_key to 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§

ToggleAction
What a routed UiEvent means for a controlled toggle keyed key.

Functions§

apply_event_multi
Fold a routed UiEvent into a multi-select toggle group’s HashSet<String> field, flipping the clicked value’s membership. Returns true if the event was a toggle event for this key.
apply_event_pressed
Fold a routed UiEvent into a standalone toggle’s bool field. Returns true if the event was a press for this key.
apply_event_single
Fold a routed UiEvent into a single-select toggle group’s value field. Returns true if the event was a toggle event for this key.
classify_event
Classify a routed UiEvent against a controlled toggle keyed key. Returns None for events that aren’t for this toggle.
toggle
A standalone two-state button. pressed paints the active surface (accent fill + accent foreground + semibold), unpressed renders as ghost. Click on the routed key key flips the bool — fold the event back with apply_event_pressed.
toggle_group
A row of mutually-exclusive toggle items — pick one. current is the currently-pressed value, formatted via std::fmt::Display and compared against each option’s value. options is an iterable of (value, label) pairs.
toggle_group_multi
A row of independent on/off toggle items — flip each independently. selected is the set of currently-pressed values (compared as strings, formatted from each option’s value). options is an iterable of (value, label) pairs.
toggle_item
A single item inside a toggle group. Apps usually let toggle_group / toggle_group_multi build these from (value, label) pairs; reach for toggle_item directly 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.