guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
navigation_drawer.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/systems/navigation_drawer.hpp"
24
25#include "guillaume/components/accessibility.hpp"
26#include "guillaume/components/borders.hpp"
27#include "guillaume/components/bound.hpp"
28#include "guillaume/components/color.hpp"
29#include "guillaume/components/layout.hpp"
30#include "guillaume/components/navigation_drawer_destination.hpp"
31#include "guillaume/components/overlay.hpp"
32#include "guillaume/components/selection.hpp"
33#include "guillaume/components/transform.hpp"
35#include "guillaume/systems/backdrop.hpp"
36#include "guillaume/systems/layout.hpp"
37#include "guillaume/systems/navigation_drawer_destination.hpp"
38#include "guillaume/systems/selection.hpp"
39
40namespace guillaume::systems
41{
42 namespace
43 {
47
55 bool isDestination(const ecs::ComponentRegistry &registry,
56 ecs::Entity::Identifier destination)
57 {
58 return registry
59 .hasComponent<components::NavigationDrawerDestination>(
60 destination);
61 }
62 } // namespace
63
68
69 float
76
78 ecs::ComponentRegistry &componentRegistry,
79 const ecs::Entity::Identifier &entityIdentifier)
80 {
81 ecs::ComponentRegistry *registry = &componentRegistry;
82 const ecs::Entity::Identifier identifier = entityIdentifier;
83
84 componentRegistry.getComponent<components::SelectionGroup>(identifier)
85 .setAllowEmpty(false);
86
87 const auto &drawer =
89 identifier);
90 const auto &destinations = drawer.getDestinationEntities();
91 const auto &sections = drawer.getSectionEntities();
92
93 for (std::size_t index = 0; index < destinations.size(); ++index) {
94 const auto destination = destinations[index];
95 if (!isDestination(componentRegistry, destination)) {
96 continue;
97 }
98
99 componentRegistry
101 destination)
102 .setOnPress([registry, identifier, index]() {
103 press(*registry, identifier, index);
104 });
105
106 if (componentRegistry.hasComponent<components::Selectable>(
107 destination)) {
108 componentRegistry
110 .setOnSelectionChangedHandler([registry, identifier,
111 index](bool selected) {
112 if (selected) {
113 destinationSelected(*registry, identifier, index);
114 }
115 });
116 }
117 }
118
119 // The section headers are the titled section entries, in order.
120 std::size_t section = 0;
121 for (const auto &entry: drawer.getEntries()) {
122 if (section >= sections.size()) {
123 break;
124 }
125 if (entry.kind != EntryKind::Section || entry.label.empty()) {
126 continue;
127 }
128
129 const auto header = sections[section++];
130 if (!componentRegistry.hasComponent<components::Accessibility>(
131 header)) {
132 componentRegistry.addComponent<components::Accessibility>(
133 header);
134 }
135 componentRegistry.getComponent<components::Accessibility>(header)
137 .setLabel(entry.label)
138 .setFocusable(false);
139 }
140 }
141
143 const ecs::ComponentRegistry &componentRegistry,
144 const ecs::Entity::Identifier &entityIdentifier)
145 {
146 const auto &destinations =
147 componentRegistry
149 .getDestinationEntities();
150
151 for (std::size_t index = 0; index < destinations.size(); ++index) {
152 if (NavigationDrawerDestination::isSelected(componentRegistry,
153 destinations[index])) {
154 return index;
155 }
156 }
157 return 0;
158 }
159
161 ecs::ComponentRegistry &componentRegistry,
162 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
163 {
164 auto &drawer =
166 entityIdentifier);
167 const auto &destinations = drawer.getDestinationEntities();
168
169 if (index >= destinations.size()) {
170 return;
171 }
172
173 // The destinations report their selection change back to this
174 // system: the guard keeps that from selecting again.
175 drawer.setUpdatingSelection(true);
176 Selection::select(componentRegistry, entityIdentifier, destinations,
177 destinations[index]);
178 drawer.setUpdatingSelection(false);
179
180 apply(componentRegistry, entityIdentifier);
181 }
182
184 ecs::ComponentRegistry &componentRegistry,
185 const ecs::Entity::Identifier &entityIdentifier)
186 {
187 const auto &destinations =
188 componentRegistry
190 .getDestinationEntities();
191
192 for (std::size_t index = 0; index < destinations.size(); ++index) {
193 if (isDestination(componentRegistry, destinations[index])
195 componentRegistry, destinations[index])) {
196 select(componentRegistry, entityIdentifier, index);
197 return;
198 }
199 }
200
201 apply(componentRegistry, entityIdentifier);
202 }
203
205 ecs::ComponentRegistry &componentRegistry,
206 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
207 {
208 if (componentRegistry
210 .isUpdatingSelection()) {
211 return;
212 }
213
214 select(componentRegistry, entityIdentifier, index);
215 }
216
217 void
219 const ecs::Entity::Identifier &entityIdentifier,
220 std::size_t index)
221 {
222 const auto &destinations =
223 componentRegistry
225 .getDestinationEntities();
226
227 if (index >= destinations.size()
228 || !isDestination(componentRegistry, destinations[index])
229 || NavigationDrawerDestination::isDisabled(componentRegistry,
230 destinations[index])) {
231 return;
232 }
233
234 const bool changed =
235 selectedIndex(componentRegistry, entityIdentifier) != index;
236 select(componentRegistry, entityIdentifier, index);
237
238 // Copy: the callback may replace itself.
239 const auto onIndexChange =
240 componentRegistry
242 .getOnIndexChange();
243 if (changed && onIndexChange) {
244 onIndexChange(index);
245 }
246 }
247
249 ecs::ComponentRegistry &componentRegistry,
250 const ecs::Entity::Identifier &entityIdentifier, std::size_t index,
251 bool disabled)
252 {
253 auto &drawer =
255 entityIdentifier);
256 const auto &destinations = drawer.getDestinationEntities();
257
258 drawer.setDestinationDisabled(index, disabled);
259 if (index < destinations.size()
260 && isDestination(componentRegistry, destinations[index])) {
262 componentRegistry, destinations[index], disabled);
263 }
264 }
265
267 const ecs::Entity::Identifier &entityIdentifier)
268 {
269 componentRegistry.getComponent<components::Overlay>(entityIdentifier)
270 .setVisible(true);
271 apply(componentRegistry, entityIdentifier);
272 }
273
275 const ecs::Entity::Identifier &entityIdentifier)
276 {
277 componentRegistry.getComponent<components::Overlay>(entityIdentifier)
278 .setVisible(false);
279 apply(componentRegistry, entityIdentifier);
280 }
281
282 void
284 const ecs::Entity::Identifier &entityIdentifier)
285 {
286 const auto &drawer =
288 entityIdentifier);
289 const Variant variant = drawer.getVariant();
290 const bool collapsed = variant == Variant::Collapsed;
291 const bool modal = variant == Variant::Modal;
292 const float width = widthOf(variant);
293 const auto &destinations = drawer.getDestinationEntities();
294
295 auto &layout = componentRegistry.getComponent<components::Layout>(
296 entityIdentifier);
304 .setFixedWidth(width)
306
307 // The drawer is attached to the leading edge: only its trailing
308 // corners are rounded.
309 const float radius = getShapeRadius(
311 componentRegistry.getComponent<components::Borders>(entityIdentifier)
312 .setTopLeftRadius(0.0f)
314 .setTopRightRadius(radius)
315 .setBottomRightRadius(radius);
316
317 componentRegistry.getComponent<components::Color>(entityIdentifier)
318 .setColor(schemeColor(modal ? SchemeColorRole::SurfaceContainerLow
319 : SchemeColorRole::Surface));
320
321 for (const auto destination: destinations) {
322 if (!isDestination(componentRegistry, destination)) {
323 continue;
324 }
325
326 componentRegistry.getComponent<components::Layout>(destination)
327 .setFixedWidth(width - 2.0f * Padding);
328 NavigationDrawerDestination::apply(componentRegistry, destination);
329 }
330
331 // The entries are the children the Layout system places in a
332 // column; the content of the destinations follows them.
333 layout.setChildren(drawer.getEntryEntities());
334 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
335 Layout::layerOf(componentRegistry, entityIdentifier));
336
337 for (const auto destination: destinations) {
338 if (isDestination(componentRegistry, destination)) {
339 NavigationDrawerDestination::arrange(componentRegistry,
340 destination);
341 }
342 }
343
344 auto &overlay = componentRegistry.getComponent<components::Overlay>(
345 entityIdentifier);
346 overlay.setModal(modal)
348 .setDismissOnEscape(true);
349
350 const auto backdrop = drawer.getBackdrop();
351 if (backdrop != ecs::Entity::InvalidIdentifier) {
352 // Only a modal drawer dims what is behind it.
353 Backdrop::setVisible(componentRegistry, backdrop,
354 modal && overlay.isVisible());
355 const auto &bound =
356 componentRegistry.getComponent<components::Bound>(
357 entityIdentifier);
359 componentRegistry, backdrop,
360 componentRegistry
361 .getComponent<components::Transform>(entityIdentifier)
362 .getPose(),
363 bound.getWidth(), bound.getHeight());
364 }
365 }
366
368 const ecs::Entity::Identifier &entityIdentifier)
369 {
370 if (!requireComponent<components::NavigationDrawer>(entityIdentifier)) {
371 return;
372 }
373
374 apply(getComponentRegistry(), entityIdentifier);
375 }
376} // namespace guillaume::systems
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setFocusable(bool focusable)
Include the entity in the Tab traversal or leave it out.
Accessibility & setLabel(const std::string &label)
Set the name announced for the entity, such as "Close".
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Borders & setBottomLeftRadius(BorderRadius radius)
Set the radius of the bottom-left corner.
Definition borders.cpp:89
Borders & setTopRightRadius(BorderRadius radius)
Set the radius of the top-right corner.
Definition borders.cpp:59
Borders & setBottomRightRadius(BorderRadius radius)
Set the radius of the bottom-right corner.
Definition borders.cpp:74
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
Layout & setFixedWidth(float width)
Force the container width instead of wrapping the children.
Definition layout.cpp:112
Layout & setAxis(Axis axis)
Choose between a row and a column.
Definition layout.cpp:32
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Definition layout.cpp:92
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
Definition layout.cpp:47
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
Layout & setFixedHeight(float height)
Force the container height instead of wrapping the children.
Definition layout.cpp:144
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Definition layout.cpp:77
State of one destination of a navigation drawer: an icon, a label and an optional text at the trailin...
State of a navigation drawer: the side sheet that lists the destinations of an app,...
static constexpr float Width
Width of a standard or modal drawer, in dp.
static constexpr float CollapsedWidth
Width of a collapsed drawer (rail), in dp.
Variant getVariant(void) const noexcept
Get how the drawer is presented.
const std::vector< ecs::Entity::Identifier > & getDestinationEntities(void) const noexcept
Get the destination children.
static constexpr float Height
Height of the drawer, in dp.
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
Definition overlay.hpp:55
Overlay & setModal(bool modal)
Declare the overlay modal (it must be answered before the rest of the interface is used) or not.
Definition overlay.cpp:50
Overlay & setDismissOnOutsideClick(bool dismiss)
Choose whether a click outside the overlay dismisses it.
Definition overlay.cpp:65
Overlay & setDismissOnEscape(bool dismiss)
Choose whether the Escape key dismisses the overlay.
Definition overlay.cpp:80
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
Marks an entity whose Selectable children are mutually exclusive, and remembers which one is selected...
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
static void follow(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::PoseF &ownerPose, float ownerWidth, float ownerHeight)
Center a backdrop on its owner and place it half a layer behind it.
Definition backdrop.cpp:76
static void setVisible(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool visible)
Show or hide a backdrop.
Definition backdrop.cpp:55
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, std::uint32_t layer=0)
Arrange children inside a container and size the container.
Definition layout.cpp:126
static std::uint32_t layerOf(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Definition layout.cpp:90
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination ignores input.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one destination from its selection, enabled and interaction state, then arrange it.
static void arrange(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Place the children of one destination without restyling it.
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the destination and restyle it.
static bool isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination is the selected one.
static float widthOf(components::NavigationDrawer::Variant variant)
Get the drawer width of a variant, in dp.
static void destinationSelected(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
React to a destination that became selected without going through the drawer.
static constexpr float Spacing
Gap between two stacked entries: 4 dp.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Shape and color the drawer, style its destinations, stack its entries and place its backdrop.
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select a destination from code.
NavigationDrawer(void)
Create the system in the Layout phase.
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the drawer and its backdrop.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Activate a destination as a press on it would.
static void setDestinationDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, bool disabled)
Enable or disable one destination and restyle it.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one navigation drawer; called by the frame loop.
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the drawer, and its backdrop when it is modal.
static void selectFirstEnabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Select the first destination that is not disabled, then restyle.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Connect a navigation drawer to its destinations and section headers.
static constexpr float Padding
Space between the edge of the drawer and its entries: 12 dp.
static std::size_t selectedIndex(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the index of the selected destination.
static ecs::Entity::Identifier select(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &groupIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, ecs::Entity::Identifier selectedIdentifier)
Select one child of a group, at once.
State. Every class here is plain data attached to an entity.
@ Header
Heading (app bar title, list subheader).
The entity-component-system core, independent of any widget.
Definition component.hpp:28
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Rules. Every class here runs each frame on the entities that have the components it declares.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Large
16 dp (FABs, drawers).
Definition tokens.hpp:47
@ None
0 dp: square corners.
Definition tokens.hpp:43
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.