guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
navigation_bar_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_bar_destination.hpp"
24
25#include <vector>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/badge.hpp"
29#include "guillaume/components/borders.hpp"
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/glyph.hpp"
33#include "guillaume/components/layout.hpp"
34#include "guillaume/components/selection.hpp"
35#include "guillaume/components/transform.hpp"
39#include "guillaume/systems/badge.hpp"
40#include "guillaume/systems/layout.hpp"
41#include "guillaume/theme.hpp"
42
43namespace guillaume::systems
44{
45 namespace
46 {
49
56 void colorChild(ecs::ComponentRegistry &registry,
58 const utility::graphic::Color32Bit &color)
59 {
60 if (registry.hasComponent<components::Color>(child)) {
61 registry.getComponent<components::Color>(child).setColor(color);
62 }
63 }
64
71 void arrangeGroup(ecs::ComponentRegistry &registry,
73 {
74 if (!registry.hasComponent<components::Layout>(group)
75 || !registry.hasComponent<components::Transform>(group)
76 || !registry.hasComponent<components::Bound>(group)) {
77 return;
78 }
79
81 registry, group,
82 registry.getComponent<components::Layout>(group).getChildren(),
83 Layout::layerOf(registry, group));
84 }
85
93 void styleBadge(ecs::ComponentRegistry &registry,
94 ecs::Entity::Identifier badge, const std::string &label)
95 {
96 if (!registry.hasComponent<components::Badge>(badge)) {
97 return;
98 }
99
100 auto &config = registry.getComponent<components::Badge>(badge);
101 const auto variant = label.empty()
104
105 if (config.getVariant() == variant && config.getLabel() == label) {
106 return;
107 }
108
109 // The badge's own system sizes it; apply it now so the result is
110 // visible at once.
111 config.setVariant(variant).setLabel(label);
112 Badge::apply(registry, badge);
113 }
114 } // namespace
115
121
123 ecs::ComponentRegistry &componentRegistry,
124 const ecs::Entity::Identifier &entityIdentifier)
125 {
126 ecs::ComponentRegistry *registry = &componentRegistry;
127 const ecs::Entity::Identifier identifier = entityIdentifier;
128
130 componentRegistry, identifier, components::AccessibilityRole::Tab,
131 [registry, identifier]() {
132 press(*registry, identifier);
133 },
134 [registry, identifier]() {
135 apply(*registry, identifier);
136 });
137 }
138
140 const ecs::ComponentRegistry &componentRegistry,
141 const ecs::Entity::Identifier &entityIdentifier)
142 {
143 return componentRegistry.hasComponent<components::Selectable>(
144 entityIdentifier)
145 && componentRegistry
146 .getComponent<components::Selectable>(entityIdentifier)
147 .isSelected();
148 }
149
151 ecs::ComponentRegistry &componentRegistry,
152 const ecs::Entity::Identifier &entityIdentifier)
153 {
154 // Copy: the callback may replace itself.
155 const auto onPress =
156 componentRegistry
158 entityIdentifier)
159 .getOnPress();
160
161 if (onPress) {
162 onPress();
163 }
164 }
165
167 ecs::ComponentRegistry &componentRegistry,
168 const ecs::Entity::Identifier &entityIdentifier,
169 const std::string &badge)
170 {
171 componentRegistry
173 entityIdentifier)
174 .setBadge(badge);
175 apply(componentRegistry, entityIdentifier);
176 }
177
179 ecs::ComponentRegistry &componentRegistry,
180 const ecs::Entity::Identifier &entityIdentifier)
181 {
182 const auto &destination =
183 componentRegistry
185 entityIdentifier);
186 const bool selected = isSelected(componentRegistry, entityIdentifier);
187
188 componentRegistry.getComponent<components::Layout>(entityIdentifier)
194 .setPadding(0.0f);
195 componentRegistry.getComponent<components::Color>(entityIdentifier)
196 .setColor(transparentColor());
197 componentRegistry.getComponent<components::Borders>(entityIdentifier)
198 .setBorderRadius(getShapeRadius(theme::ShapeRole::None))
200
201 const auto pillColor = selected
202 ? schemeColor(SchemeColorRole::SecondaryContainer)
204 const auto iconColor = selected
205 ? schemeColor(SchemeColorRole::OnSecondaryContainer)
206 : schemeColor(SchemeColorRole::OnSurfaceVariant);
207 const auto labelColor = selected
208 ? schemeColor(SchemeColorRole::OnSurface)
209 : schemeColor(SchemeColorRole::OnSurfaceVariant);
210
211 // The state layer of the destination is drawn on its indicator.
212 colorChild(componentRegistry, destination.getIndicator(),
213 interaction::stateLayerColor(componentRegistry,
214 entityIdentifier, pillColor,
215 iconColor));
216
217 const auto icon = destination.getIconEntity();
218 if (componentRegistry.hasComponent<components::Glyph>(icon)) {
219 componentRegistry.getComponent<components::Glyph>(icon).setName(
220 selected && !destination.getFocusedIcon().empty()
221 ? destination.getFocusedIcon()
222 : destination.getIcon());
223 }
224 colorChild(componentRegistry, icon, iconColor);
225
226 // A hidden label keeps its place in the column.
227 colorChild(componentRegistry, destination.getLabelEntity(),
228 destination.isLabelShown() ? labelColor
229 : transparentColor());
230
231 styleBadge(componentRegistry, destination.getBadgeEntity(),
232 destination.getBadge());
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 indicator = destination.getIndicator();
254 const auto badge = destination.getBadgeEntity();
255 std::vector<ecs::Entity::Identifier> children;
256
257 for (const auto child: { indicator, destination.getLabelEntity() }) {
258 if (child != ecs::Entity::InvalidIdentifier) {
259 children.push_back(child);
260 }
261 }
262
263 // Size the indicator, place it and the label in the column, then
264 // place the icon inside the moved indicator.
265 auto &layout = componentRegistry.getComponent<components::Layout>(
266 entityIdentifier);
267 layout.setChildren(children);
268 arrangeGroup(componentRegistry, indicator);
269 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
270 Layout::layerOf(componentRegistry, entityIdentifier));
271 arrangeGroup(componentRegistry, indicator);
272
274 && componentRegistry.hasComponent<components::Transform>(
275 indicator)) {
277 componentRegistry, indicator, badge,
278 utility::graphic::PositionF(
281 Layout::layerOf(componentRegistry, indicator));
282 // The label of the badge follows it.
283 arrangeGroup(componentRegistry, badge);
284 }
285 }
286
288 const ecs::Entity::Identifier &entityIdentifier)
289 {
291 entityIdentifier)) {
292 return;
293 }
294
295 apply(getComponentRegistry(), entityIdentifier);
296 }
297} // 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.
@ Large
20 dp high badge with a label (the default).
Definition badge.hpp:73
@ Dot
6 dp dot without label.
Definition badge.hpp:71
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
An icon: one glyph of the Material Symbols font, chosen by name, at a given size.
Definition glyph.hpp:61
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
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
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
@ 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 bar: an icon in a pill, a label under it and an optional bad...
ecs::Entity::Identifier getIndicator(void) const noexcept
Get the pill drawn behind the icon of the selected destination.
static constexpr float BadgeOffsetY
Vertical offset of the badge from the indicator origin, in dp.
ecs::Entity::Identifier getLabelEntity(void) const noexcept
Get the child that draws the label.
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
Where an entity is in space: its pose (position and orientation) and its scale.
Definition transform.hpp:64
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...
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Size and color one badge and its label.
Definition badge.cpp:166
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 isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination is the selected one.
static void arrange(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Place the children of one destination without restyling it.
static void setBadge(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const std::string &badge)
Set the badge label and restyle the destination.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the destination as a press on it would.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one destination from its selection and interaction state, then arrange it.
NavigationBarDestination(void)
Create the system in the Layout phase.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one destination; called by the frame loop.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make a destination pressable.
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
void placeChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, const utility::graphic::PositionF &offset, std::uint32_t parentLayer)
Place a child at an offset from the position of its parent.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
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 schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ 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.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
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.
Placement helpers for the children systems::Layout does not arrange.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.