guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
navigation_bar.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.hpp"
24
25#include <algorithm>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/bound.hpp"
29#include "guillaume/components/color.hpp"
30#include "guillaume/components/layout.hpp"
31#include "guillaume/components/navigation_bar_destination.hpp"
32#include "guillaume/components/selection.hpp"
34#include "guillaume/systems/layout.hpp"
35#include "guillaume/systems/navigation_bar_destination.hpp"
36#include "guillaume/systems/selection.hpp"
37#include "guillaume/theme.hpp"
38
39namespace guillaume::systems
40{
41 namespace
42 {
44
51 bool isDestination(const ecs::ComponentRegistry &registry,
52 ecs::Entity::Identifier destination)
53 {
54 return registry.hasComponent<components::NavigationBarDestination>(
55 destination);
56 }
57 } // namespace
58
63
65 const ecs::Entity::Identifier &entityIdentifier)
66 {
67 ecs::ComponentRegistry *registry = &componentRegistry;
68 const ecs::Entity::Identifier identifier = entityIdentifier;
69
70 componentRegistry.getComponent<components::SelectionGroup>(identifier)
71 .setAllowEmpty(false);
72
73 if (!componentRegistry.hasComponent<components::Accessibility>(
74 identifier)) {
75 componentRegistry.addComponent<components::Accessibility>(
76 identifier);
77 }
78 componentRegistry.getComponent<components::Accessibility>(identifier)
80 .setFocusable(false);
81
82 const auto &destinations =
83 componentRegistry
85 .getDestinationEntities();
86
87 for (std::size_t index = 0; index < destinations.size(); ++index) {
88 const auto destination = destinations[index];
89 if (!isDestination(componentRegistry, destination)) {
90 continue;
91 }
92
93 componentRegistry
95 .setOnPress([registry, identifier, index]() {
96 press(*registry, identifier, index);
97 });
98
99 if (componentRegistry.hasComponent<components::Selectable>(
100 destination)) {
101 componentRegistry
103 .setOnSelectionChangedHandler([registry, identifier,
104 index](bool selected) {
105 if (selected) {
106 destinationSelected(*registry, identifier, index);
107 }
108 });
109 }
110 }
111 }
112
114 const ecs::ComponentRegistry &componentRegistry,
115 const ecs::Entity::Identifier &entityIdentifier)
116 {
117 const auto &destinations =
118 componentRegistry
119 .getComponent<components::NavigationBar>(entityIdentifier)
120 .getDestinationEntities();
121
122 for (std::size_t index = 0; index < destinations.size(); ++index) {
123 if (NavigationBarDestination::isSelected(componentRegistry,
124 destinations[index])) {
125 return index;
126 }
127 }
128 return 0;
129 }
130
132 const ecs::Entity::Identifier &entityIdentifier,
133 std::size_t index)
134 {
135 auto &bar = componentRegistry.getComponent<components::NavigationBar>(
136 entityIdentifier);
137 const auto &destinations = bar.getDestinationEntities();
138
139 if (index >= destinations.size()) {
140 return;
141 }
142
143 // The destinations report their selection change back to this
144 // system: the guard keeps that from selecting again.
145 bar.setUpdatingSelection(true);
146 Selection::select(componentRegistry, entityIdentifier, destinations,
147 destinations[index]);
148 bar.setUpdatingSelection(false);
149
150 apply(componentRegistry, entityIdentifier);
151 }
152
154 ecs::ComponentRegistry &componentRegistry,
155 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
156 {
157 if (componentRegistry
159 .isUpdatingSelection()) {
160 return;
161 }
162
163 select(componentRegistry, entityIdentifier, index);
164 }
165
167 const ecs::Entity::Identifier &entityIdentifier,
168 std::size_t index)
169 {
170 if (index
171 >= componentRegistry
173 .getDestinationEntities()
174 .size()) {
175 return;
176 }
177
178 // Copy: a callback may replace itself.
179 const auto onTabPress =
180 componentRegistry
181 .getComponent<components::NavigationBar>(entityIdentifier)
182 .getOnTabPress();
183 if (onTabPress) {
184 onTabPress(index);
185 }
186
187 const bool changed =
188 selectedIndex(componentRegistry, entityIdentifier) != index;
189 select(componentRegistry, entityIdentifier, index);
190
191 const auto onIndexChange =
192 componentRegistry
193 .getComponent<components::NavigationBar>(entityIdentifier)
194 .getOnIndexChange();
195 if (changed && onIndexChange) {
196 onIndexChange(index);
197 }
198 }
199
200 void
202 const ecs::Entity::Identifier &entityIdentifier,
203 std::size_t index, const std::string &badge)
204 {
205 auto &bar = componentRegistry.getComponent<components::NavigationBar>(
206 entityIdentifier);
207 const auto &destinations = bar.getDestinationEntities();
208
209 bar.setDestinationBadge(index, badge);
210 if (index < bar.getDestinations().size() && index < destinations.size()
211 && isDestination(componentRegistry, destinations[index])) {
212 NavigationBarDestination::setBadge(componentRegistry,
213 destinations[index], badge);
214 }
215 }
216
218 const ecs::Entity::Identifier &entityIdentifier)
219 {
220 const auto &bar =
221 componentRegistry.getComponent<components::NavigationBar>(
222 entityIdentifier);
223 const auto &destinations = bar.getDestinationEntities();
224 const bool showAllLabels = bar.getVariant() == Variant::WithLabels;
225
226 componentRegistry.getComponent<components::Color>(entityIdentifier)
227 .setColor(style::schemeColor(SchemeColorRole::SurfaceContainer));
228
229 // The destinations share the width of the bar.
230 auto &layout = componentRegistry.getComponent<components::Layout>(
231 entityIdentifier);
232 const float padding = layout.getPadding();
233 const float width = layout.hasFixedWidth()
234 ? layout.getFixedWidth()
235 : componentRegistry
236 .getComponent<components::Bound>(entityIdentifier)
237 .getWidth();
238 const float destinationWidth = destinations.empty()
239 ? 0.0f
240 : std::max(0.0f,
241 (width - 2.0f * padding)
242 / static_cast<float>(destinations.size()));
243 const float destinationHeight =
244 components::NavigationBar::Height - 2.0f * padding;
245
246 for (const auto destination: destinations) {
247 if (!isDestination(componentRegistry, destination)) {
248 continue;
249 }
250
251 componentRegistry.getComponent<components::Layout>(destination)
252 .setFixedWidth(destinationWidth)
253 .setFixedHeight(destinationHeight);
254 // Without labels, only the selected destination shows its own.
255 componentRegistry
257 .setShowLabel(showAllLabels
259 componentRegistry, destination));
260 NavigationBarDestination::apply(componentRegistry, destination);
261 }
262
263 // The destinations are the children the Layout system places in a
264 // row; their content follows them.
265 layout.setChildren(destinations);
266 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
267 Layout::layerOf(componentRegistry, entityIdentifier));
268
269 for (const auto destination: destinations) {
270 if (isDestination(componentRegistry, destination)) {
271 NavigationBarDestination::arrange(componentRegistry,
272 destination);
273 }
274 }
275 }
276
277 void NavigationBar::update(const ecs::Entity::Identifier &entityIdentifier)
278 {
279 if (!requireComponent<components::NavigationBar>(entityIdentifier)) {
280 return;
281 }
282
283 apply(getComponentRegistry(), entityIdentifier);
284 }
285} // 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.
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
float getPadding(void) const
Get the inset between the container's edges and its children.
Definition layout.cpp:87
Layout & setFixedHeight(float height)
Force the container height instead of wrapping the children.
Definition layout.cpp:144
State of one destination of a navigation bar: an icon in a pill, a label under it and an optional bad...
State of a navigation bar: the bottom strip of three to five destinations that switches between the t...
static constexpr float Height
Height of the bar, in dp.
Variant
Whether every destination shows its label (WithLabels) or only the selected one does (WithoutLabels).
const std::vector< ecs::Entity::Identifier > & getDestinationEntities(void) const noexcept
Get the destination children.
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
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 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 apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one destination from its selection and interaction state, then arrange it.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one navigation bar; called by the frame loop.
static void setBadge(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, const std::string &badge)
Set the badge of a destination and restyle that destination.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Connect a navigation bar to its destinations.
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 std::size_t selectedIndex(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the index of the selected destination.
NavigationBar(void)
Create the system in the Layout phase.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Color the bar, size and style its destinations and place them in a row.
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select a destination from code.
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 bar.
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.
@ TabList
Group of navigation destinations.
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.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.