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/entities/navigation_bar_destination.hpp"
24
25#include <memory>
26#include <utility>
27
28#include "guillaume/entities/badge.hpp"
29#include "guillaume/entities/builder_base.hpp"
30#include "guillaume/entities/content_helpers.hpp"
31#include "guillaume/entities/icon.hpp"
33#include "guillaume/entities/text.hpp"
34#include "guillaume/entities/view.hpp"
35#include "guillaume/systems/navigation_bar_destination.hpp"
36#include "guillaume/theme.hpp"
37
38namespace guillaume::entities::detail
39{
40 BarDestination::BarDestination(ecs::ComponentRegistry &registry,
42 : ecs::ParentEntityFiller<components::Layout, components::Transform,
43 components::Bound, components::Color,
44 components::Borders, components::Selectable,
45 components::NavigationBarDestination>(
46 registry)
47 {
48 getComponentRegistry()
49 .getComponent<components::NavigationBarDestination>(getIdentifier())
50 .setIcon(spec.icon)
51 .setFocusedIcon(spec.focusedIcon)
52 .setLabel(spec.label)
53 .setBadge(spec.badge);
54 systems::NavigationBarDestination::install(getComponentRegistry(),
55 getIdentifier());
56 systems::NavigationBarDestination::apply(getComponentRegistry(),
57 getIdentifier());
58 }
59
60 void BarDestination::build(void)
61 {
62 auto &registry = getComponentRegistry();
63 // Copy: building children may move the component storage.
64 const auto spec =
65 registry.getComponent<components::NavigationBarDestination>(
66 getIdentifier());
67
68 if (spec.getIndicator() == ecs::Entity::InvalidIdentifier) {
69 View::Builder pillBuilder(registry, *this);
70 const auto pill = pillBuilder.makeDefaultView(
71 shared_from_this(), utility::graphic::PoseF(), {});
72 pill->setColor(transparentColor());
73 pill->setBorderRadius(getShapeRadius(theme::ShapeRole::Full));
74 pill->setAxis(components::Layout::Axis::Horizontal);
75 pill->setMainAxisAlignment(
76 components::Layout::MainAxisAlignment::Center);
77 pill->setCrossAxisAlignment(
78 components::Layout::CrossAxisAlignment::Center);
79 pill->setPadding(0.0f);
80 pill->setFixedWidth(
81 components::NavigationBarDestination::IndicatorWidth);
82 pill->setFixedHeight(
83 components::NavigationBarDestination::IndicatorHeight);
84
85 const auto icon =
86 buildIcon(registry, *pill, pill, spec.getIcon(),
87 components::NavigationBarDestination::IconSize,
88 schemeColor(SchemeColorRole::OnSurfaceVariant));
89 if (icon != nullptr) {
90 pill->setEntities({ icon });
91 }
92
93 const auto label =
94 buildText(registry, *this, shared_from_this(), spec.getLabel(),
95 theme::TypeRole::LabelMedium,
96 schemeColor(SchemeColorRole::OnSurfaceVariant));
97
98 registry
99 .getComponent<components::NavigationBarDestination>(
100 getIdentifier())
101 .setIndicator(pill->getIdentifier())
102 .setIconEntity(icon != nullptr ? icon->getIdentifier()
103 : ecs::Entity::InvalidIdentifier)
104 .setLabelEntity(label != nullptr
105 ? label->getIdentifier()
106 : ecs::Entity::InvalidIdentifier);
107 }
108
109 buildBadge();
110 systems::NavigationBarDestination::apply(registry, getIdentifier());
111 }
112
113 void BarDestination::buildBadge(void)
114 {
115 // Children need the owning handle as their parent.
116 if (weak_from_this().expired()) {
117 return;
118 }
119
120 auto &registry = getComponentRegistry();
121 // Copy: building children may move the component storage.
122 const auto spec =
123 registry.getComponent<components::NavigationBarDestination>(
124 getIdentifier());
125
126 if (spec.getBadgeEntity() != ecs::Entity::InvalidIdentifier
127 || spec.getBadge().empty()) {
128 return;
129 }
130
131 const auto pill = getEntity<View>(spec.getIndicator());
132 if (pill == nullptr) {
133 return;
134 }
135
136 Badge::Builder builder(registry, *pill);
137 const auto badge = builder.withVariant(Badge::Variant::Large)
138 .withLabel(spec.getBadge())
139 .registerEntity(pill);
140 if (badge != nullptr) {
141 registry
142 .getComponent<components::NavigationBarDestination>(
143 getIdentifier())
144 .setBadgeEntity(badge->getIdentifier());
145 }
146 }
147
148 void BarDestination::setOnPress(std::function<void(void)> onPress)
149 {
152 .setOnPress(std::move(onPress));
153 }
154
155 void BarDestination::setBadge(const std::string &badge)
156 {
159 .setBadge(badge);
160 // A first badge label needs the badge child.
161 buildBadge();
163 getIdentifier());
164 }
165
174
176 const components::Selectable::Handler &handler)
177 {
179 .getComponent<components::Selectable>(getIdentifier())
180 .setOnSelectionChangedHandler(handler);
181 }
182
188
195
197 {
198 return getComponentRegistry()
200 .getIconEntity();
201 }
202
204 {
205 return getComponentRegistry()
207 .getLabelEntity();
208 }
209
211 {
212 return getComponentRegistry()
214 .getBadgeEntity();
215 }
216} // namespace guillaume::entities::detail
State of one destination of a navigation bar: an icon in a pill, a label under it and an optional bad...
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
std::function< void(bool)> Handler
Callback receiving the new state: true when just selected.
Definition selection.hpp:60
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
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
components::NavigationBar::Destination Destination
void setOnPress(std::function< void(void)> onPress)
Set the handler called when the user presses the destination.
void setOnSelectionChanged(const components::Selectable::Handler &handler)
Set the handler of the destination's components::Selectable.
ecs::Entity::Identifier getLabelIdentifier(void) const
Get the label entity.
bool isSelected(void) const
Whether the destination is the selected one of its bar.
ecs::Entity::Identifier getIconIdentifier(void) const
Get the icon entity.
ecs::Entity::Identifier getIndicatorIdentifier(void) const
Get the active-indicator pill: the rounded shape behind the icon.
void setShowLabel(bool show)
Show or hide the label. Restyles at once.
ecs::Entity::Identifier getBadgeIdentifier(void) const
Get the badge entity.
void setBadge(const std::string &badge)
Change the badge label.
static bool isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination is the selected one.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one destination from its selection and interaction state, then arrange it.
Makes the color helpers callable from guillaume::entities without qualification.
std::shared_ptr< Text > buildText(ecs::ComponentRegistry &registry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &content, float fontSize, const utility::graphic::Color32Bit &color)
Create a Text of a given font size as a child of a widget, or nothing when there is no content.
std::shared_ptr< Icon > buildIcon(ecs::ComponentRegistry &registry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &glyphName, float fontSize, const utility::graphic::Color32Bit &color, components::Glyph::Style style=components::Glyph::Style::Outlined)
Create an Icon as a child of a widget, or nothing when there is no glyph to show.
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.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.