Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ToggleGroup

Layer: molecule · Path: src/molecules/toggle_group.rs · Exports: toggle_group::ToggleGroup

A segmented single-select control bound to a &mut usize. Options render as a connected row of Buttons inside a Surface container: the selected segment is a raised Secondary button (looks like a real button), the rest Ghost. Models the shadcn Toggle Group / Button Group.

Design

  • Purpose / when to use — Compact mutually-exclusive choice with short labels (gizmo space: Local/World, alignment, view mode). For longer labels or descriptions use RadioGroup/RadioCard.
  • AnatomySurface::pad(SPACE_1).radius(RADIUS_MD) → horizontal row of small Buttons; active = ButtonVariant::Secondary, others ButtonVariant::Ghost.
  • States — exactly one segment is the raised secondary button (*selected == i).
  • Tokens / layout consumedcore::SPACE_1 (surface pad), RADIUS_MD. See tokens.

API

MethodEffect
ToggleGroup::new(selected: &'a mut usize) -> SelfBind the active index.
.options<S: Into<String>>(options: impl IntoIterator<Item = S>) -> SelfSet the segment labels.
.show(self, ui: &mut Ui) -> ResponseRender; writes *selected = i on click. Returns the surface Response.

Usage

#![allow(unused)]
fn main() {
use ouroboros_ui::molecules::ToggleGroup;

// minimal
let mut sel = 0usize;
ToggleGroup::new(&mut sel)
    .options(["Local", "World"])
    .show(ui);
}

Composition

Composes Surface + Button. It never paints — see the guards.

Notes

  • Two-way binding via &mut usize.
  • Per-segment ids use ("toggle_group", i), so multiple groups per frame are safe.
  • Visually near-identical to Tabs Container, but semantically a value selector rather than a view switcher.