guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
navigation_drawer_destination.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_destination.hpp"
24
25#include <vector>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/borders.hpp"
29#include "guillaume/components/bound.hpp"
30#include "guillaume/components/color.hpp"
31#include "guillaume/components/layout.hpp"
32#include "guillaume/components/selection.hpp"
33#include "guillaume/components/style_state.hpp"
34#include "guillaume/components/transform.hpp"
37#include "guillaume/systems/layout.hpp"
38#include "guillaume/theme.hpp"
39
40namespace guillaume::systems
41{
42 namespace
43 {
47
54 void colorChild(ecs::ComponentRegistry &registry,
56 const utility::graphic::Color32Bit &color)
57 {
58 if (registry.hasComponent<components::Color>(child)) {
59 registry.getComponent<components::Color>(child).setColor(color);
60 }
61 }
62
69 void arrangeGroup(ecs::ComponentRegistry &registry,
71 {
72 if (!registry.hasComponent<components::Layout>(group)
73 || !registry.hasComponent<components::Transform>(group)
74 || !registry.hasComponent<components::Bound>(group)) {
75 return;
76 }
77
79 registry, group,
80 registry.getComponent<components::Layout>(group).getChildren(),
81 Layout::layerOf(registry, group));
82 }
83 } // namespace
84
90
92 ecs::ComponentRegistry &componentRegistry,
93 const ecs::Entity::Identifier &entityIdentifier)
94 {
95 ecs::ComponentRegistry *registry = &componentRegistry;
96 const ecs::Entity::Identifier identifier = entityIdentifier;
97
99 componentRegistry, identifier,
101 [registry, identifier]() {
102 press(*registry, identifier);
103 },
104 [registry, identifier]() {
105 apply(*registry, identifier);
106 });
107 }
108
110 const ecs::ComponentRegistry &componentRegistry,
111 const ecs::Entity::Identifier &entityIdentifier)
112 {
113 return componentRegistry.hasComponent<components::Selectable>(
114 entityIdentifier)
115 && componentRegistry
116 .getComponent<components::Selectable>(entityIdentifier)
117 .isSelected();
118 }
119
121 const ecs::ComponentRegistry &componentRegistry,
122 const ecs::Entity::Identifier &entityIdentifier)
123 {
124 return componentRegistry
126 entityIdentifier)
127 .isDisabled()
128 || !interaction::isEnabled(componentRegistry, entityIdentifier);
129 }
130
132 ecs::ComponentRegistry &componentRegistry,
133 const ecs::Entity::Identifier &entityIdentifier, bool disabled)
134 {
135 componentRegistry
137 entityIdentifier)
138 .setDisabled(disabled);
139 apply(componentRegistry, entityIdentifier);
140 }
141
143 ecs::ComponentRegistry &componentRegistry,
144 const ecs::Entity::Identifier &entityIdentifier)
145 {
146 if (isDisabled(componentRegistry, entityIdentifier)) {
147 return;
148 }
149
150 // Copy: the callback may replace itself.
151 const auto onPress =
152 componentRegistry
154 entityIdentifier)
155 .getOnPress();
156
157 if (onPress) {
158 onPress();
159 }
160 }
161
163 ecs::ComponentRegistry &componentRegistry,
164 const ecs::Entity::Identifier &entityIdentifier)
165 {
166 const auto &destination =
167 componentRegistry
169 entityIdentifier);
170 const bool collapsed = destination.isCollapsed();
171
172 // A collapsed item stacks its indicator above its label; a regular
173 // item is a row with the trailing text pushed to the far edge.
174 auto &layout = componentRegistry.getComponent<components::Layout>(
175 entityIdentifier);
176 if (collapsed) {
178 .setMainAxisAlignment(
180 .setCrossAxisAlignment(
182 .setSpacing(theme::SpacingScale::get(1))
183 .setPadding(0.0f)
186 } else {
188 .setMainAxisAlignment(
189 destination.getRight().empty()
192 .setCrossAxisAlignment(
194 .setSpacing(theme::SpacingScale::get(3))
195 .setPadding(theme::SpacingScale::get(4))
196 .setFixedHeight(
198 }
199
200 componentRegistry.getComponent<components::Borders>(entityIdentifier)
201 .setBorderRadius(getShapeRadius(theme::ShapeRole::Full))
203
204 if (componentRegistry.hasComponent<components::StyleState>(
205 entityIdentifier)) {
206 componentRegistry
207 .getComponent<components::StyleState>(entityIdentifier)
208 .setEnabled(!destination.isDisabled());
209 }
210
211 const bool selected = isSelected(componentRegistry, entityIdentifier);
212 const bool disabled = isDisabled(componentRegistry, entityIdentifier);
213 const auto pill = selected && !disabled
214 ? schemeColor(SchemeColorRole::SecondaryContainer)
216 const auto content = disabled
218 : (selected ? schemeColor(SchemeColorRole::OnSecondaryContainer)
219 : schemeColor(SchemeColorRole::OnSurfaceVariant));
220 const auto pillColor = interaction::stateLayerColor(
221 componentRegistry, entityIdentifier, pill, content);
222
223 // The active pill is the indicator of a collapsed item, and the
224 // whole row of a regular one.
225 componentRegistry.getComponent<components::Color>(entityIdentifier)
226 .setColor(collapsed ? transparentColor() : pillColor);
227 colorChild(componentRegistry, destination.getLead(),
228 collapsed ? pillColor : transparentColor());
229
230 colorChild(componentRegistry, destination.getIconEntity(), content);
231 colorChild(componentRegistry, destination.getLabelEntity(), content);
232 colorChild(componentRegistry, destination.getRightEntity(), content);
233
234 if (componentRegistry.hasComponent<components::Accessibility>(
235 entityIdentifier)) {
236 componentRegistry
237 .getComponent<components::Accessibility>(entityIdentifier)
238 .setLabel(destination.getLabel())
239 .setSelected(selected);
240 }
241
242 arrange(componentRegistry, entityIdentifier);
243 }
244
246 ecs::ComponentRegistry &componentRegistry,
247 const ecs::Entity::Identifier &entityIdentifier)
248 {
249 const auto &destination =
250 componentRegistry
252 entityIdentifier);
253 const auto lead = destination.getLead();
254 std::vector<ecs::Entity::Identifier> children;
255
256 // The label of a regular item is inside the lead group.
257 if (lead != ecs::Entity::InvalidIdentifier) {
258 children.push_back(lead);
259 }
260 if (destination.isCollapsed()
261 && destination.getLabelEntity() != ecs::Entity::InvalidIdentifier) {
262 children.push_back(destination.getLabelEntity());
263 }
264 if (destination.getRightEntity() != ecs::Entity::InvalidIdentifier) {
265 children.push_back(destination.getRightEntity());
266 }
267
268 // Size the lead group, place the children, then place the content
269 // of the moved lead group.
270 auto &layout = componentRegistry.getComponent<components::Layout>(
271 entityIdentifier);
272 layout.setChildren(children);
273 arrangeGroup(componentRegistry, lead);
274 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
275 Layout::layerOf(componentRegistry, entityIdentifier));
276 arrangeGroup(componentRegistry, lead);
277 }
278
280 const ecs::Entity::Identifier &entityIdentifier)
281 {
283 entityIdentifier)) {
284 return;
285 }
286
287 apply(getComponentRegistry(), entityIdentifier);
288 }
289} // 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 & setSelected(bool selected)
Set whether the entity is the selected one of its group.
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Borders & setColor(const utility::graphic::Color32Bit &color)
Set the color of the stroke.
Definition borders.cpp:107
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 & setChildren(const std::vector< ecs::Entity::Identifier > &children)
Replace the list of children to arrange.
Definition layout.cpp:171
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
State of one destination of a navigation drawer: an icon, a label and an optional text at the trailin...
ecs::Entity::Identifier getLead(void) const noexcept
Get the leading child of the destination.
static constexpr float Height
Height of a regular destination, in dp.
bool isCollapsed(void) const noexcept
Tell which rendering the destination uses.
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
The interaction state of an entity in one place: enabled or disabled, and whether it is hovered,...
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.
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...
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 void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make a destination pressable.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the destination as a press on it would.
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.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one destination; called by the frame loop.
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the destination and restyle it.
NavigationDrawerDestination(void)
Create the system in the Layout phase.
static bool isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination is the selected one.
static constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
Definition tokens.hpp:441
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
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 transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isEnabled(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
void makeInteractive(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &content)
Container color of a widget in its current input state.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.