Collapsible
Layer: molecule · Path:
src/molecules/collapsible.rs· Exports:collapsible::Collapsible
A caret-headed section that hides or reveals its content. Open state persists in egui temp memory keyed by id (or by the title), so it survives across frames. The content closure only runs when open. Analogous to the shadcn Collapsible / Unity Foldout.
Design
- Purpose / when to use — Inspector sections, settings groups, anything foldable to manage vertical density.
- Anatomy — A clickable header row: a muted caret
Icon(CARET_DOWNwhen open,CARET_RIGHTwhen closed) +SPACE_1+ abody_strongTexttitle. When open,SPACE_2then thecontentclosure. - States — open / closed, toggled by clicking the header. Persisted via
ui.datatemp storage under the resolved id. - Tokens / layout consumed —
core::SPACE_1(caret→title),SPACE_2(header→content). See tokens.
API
| Method | Effect |
|---|---|
Collapsible::new(title: impl Into<String>) -> Self | Construct; defaults to closed. |
.default_open(open: bool) -> Self | Initial open state when no persisted value exists. |
.id_source(id: impl std::hash::Hash) -> Self | Explicit id for persistence + interaction (defaults to format!("collapsible::{title}")). |
.show(self, ui: &mut Ui, content: impl FnOnce(&mut Ui)) -> Response | Render; content runs only when open. Returns the header interaction Response. |
Usage
#![allow(unused)]
fn main() {
use ouroboros_ui::molecules::Collapsible;
use ouroboros_ui::atoms::Text;
// minimal
Collapsible::new("Rendering").show(ui, |ui| {
Text::new("Material, shadows…").muted().show(ui);
});
}
#![allow(unused)]
fn main() {
use ouroboros_ui::molecules::Collapsible;
use ouroboros_ui::atoms::Text;
// realistic — open by default, explicit id
Collapsible::new("Transform")
.default_open(true)
.id_source("inspector::transform")
.show(ui, |ui| {
Text::new("Position / Rotation / Scale").muted().show(ui);
});
}
Composition
Composes Icon + Text, with ui.interact over the header row’s rect for the click target. It never paints — see the guards.
Notes
- Open state lives in
ui.datatemp memory under the resolved id; the interaction id isid.with("header"). - The returned
Responseis the header (use.clicked()to react to toggles beyond the visual). - If two collapsibles share a title, pass distinct
id_sources to avoid shared open state.