Beginner
Disclosure & Grouping
Accordions, toggle groups and the native OS menu bar — for organising what the user does not need to see all at once.
Introduction
Three Controls
Three controls for organising things the user does not need to see all at once.
| Control | Groups |
|---|---|
Accordion | Sections of content that expand and collapse |
ToggleGroup | Toggles that behave as one selection |
NativeMenuBar | Commands, in the operating system's own menu bar |
1Sections
Accordion
An accordion holds labelled sections. addSection returns references to the header and the content area, so you fill the section rather than handing it a widget.
ml::Accordion panel;
auto [header, list] = panel.addSection("Students", 180.f);
list.addItem("Amelia Adamczewski");
list.addItem("Samir Chaudhry");
panel.onSectionChanged([](std::size_t index, bool expanded){
if (expanded) loadSectionData(index);
});
| Method | Effect |
|---|---|
addSection(label, height) | Add a section; returns header and content refs. |
removeSection(index) | Remove by position. |
onSectionChanged(cb) | Index plus whether it is now expanded. |
onSectionChanged is the right place to load a section's data. Populating every section up front costs work the user may never look at — expanding is the signal that they want it.2Selection
ToggleGroup
ToggleGroup makes several independent toggles behave as one control, keyed by string.
ml::ToggleGroup view;
view.add(listToggle, "list");
view.add(gridToggle, "grid");
view.setOn("grid", true);
if (view.isOn("grid")) layoutAsGrid();
| Method | Effect |
|---|---|
add(toggle, key) | Adopt a PillToggle, SegmentToggle or ButtonToggle. |
setOn(key, bool) | Set one member's state. |
isOn(key) | Query by key. |
clearToggles() | Release every member. |
Keys, not indices. A group referenced by position breaks the moment you reorder the toggles or add one in the middle — the string survives both.
3Platform
NativeMenuBar
NativeMenuBar builds a real operating-system menu: the global menu bar on macOS, an HMENU on Windows.
ml::NativeMenuBar menu;
// ... describe menus and items ...
menu.attach(); // macOS: global menu bar. Windows: attached to the SFML window.
| Method | Effect |
|---|---|
attach() | Install the menu. A no-op on unsupported platforms. |
detach() | Remove it. |
attach() being a no-op elsewhere is deliberate — you can call it unconditionally and let platforms without a native menu bar ignore it. Do not put commands only in the native menu, though: on those platforms they would be unreachable. Mirror anything essential in your own UI.