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/entities/navigation_drawer_destination.hpp"
24
25#include <memory>
26#include <utility>
27#include <vector>
28
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_drawer_destination.hpp"
36#include "guillaume/theme.hpp"
37
38namespace guillaume::entities::detail
39{
40 namespace
41 {
50 std::shared_ptr<View> makeGroup(ecs::ComponentRegistry &registry,
51 ecs::EntityRegistry &entityRegistry,
52 std::shared_ptr<ecs::Entity> parent,
54 {
55 View::Builder builder(registry, entityRegistry);
56 auto view =
57 builder.makeDefaultView(parent, utility::graphic::PoseF(), {});
58 view->setColor(transparentColor());
59 view->setAxis(axis);
60 view->setMainAxisAlignment(
62 view->setCrossAxisAlignment(
64 view->setPadding(0.0f);
65 view->setSpacing(theme::SpacingScale::get(3));
66 return view;
67 }
68
75 identifierOf(const std::shared_ptr<ecs::Entity> &child)
76 {
77 return child != nullptr ? child->getIdentifier()
79 }
80 } // namespace
81
82 DrawerDestination::DrawerDestination(ecs::ComponentRegistry &registry,
83 const NavigationDrawer::Entry &entry,
84 bool collapsed, float width)
85 : ecs::ParentEntityFiller<components::Layout, components::Transform,
86 components::Bound, components::Color,
87 components::Borders, components::Selectable,
88 components::NavigationDrawerDestination>(
89 registry)
90 {
91 getComponentRegistry()
92 .getComponent<components::NavigationDrawerDestination>(
93 getIdentifier())
94 .setIcon(entry.icon)
95 .setLabel(entry.label)
96 .setRight(entry.right)
97 .setDisabled(entry.disabled)
98 .setCollapsed(collapsed);
99 getComponentRegistry()
100 .getComponent<components::Layout>(getIdentifier())
101 .setFixedWidth(width);
102 systems::NavigationDrawerDestination::install(getComponentRegistry(),
103 getIdentifier());
104 systems::NavigationDrawerDestination::apply(getComponentRegistry(),
105 getIdentifier());
106 }
107
108 void DrawerDestination::build(void)
109 {
110 auto &registry = getComponentRegistry();
111 // Copy: building children may move the component storage.
112 const auto entry =
113 registry.getComponent<components::NavigationDrawerDestination>(
114 getIdentifier());
115
116 if (entry.getLead() == ecs::Entity::InvalidIdentifier) {
117 const auto lead = makeGroup(registry, *this, shared_from_this(),
118 components::Layout::Axis::Horizontal);
119 const auto icon =
120 buildIcon(registry, *lead, lead, entry.getIcon(),
121 components::NavigationDrawerDestination::IconSize,
122 schemeColor(SchemeColorRole::OnSurfaceVariant));
123 std::shared_ptr<Text> label;
124 std::shared_ptr<Text> right;
125 std::vector<std::shared_ptr<ecs::Entity>> content;
126
127 if (icon != nullptr) {
128 content.push_back(icon);
129 }
130
131 if (entry.isCollapsed()) {
132 // The lead group is the indicator around the icon; the
133 // label sits under it.
134 lead->setBorderRadius(getShapeRadius(theme::ShapeRole::Full));
135 lead->setFixedWidth(components::NavigationDrawerDestination::
136 CollapsedIndicatorWidth);
137 lead->setFixedHeight(components::NavigationDrawerDestination::
138 CollapsedIndicatorHeight);
139 label =
140 buildText(registry, *this, shared_from_this(),
141 entry.getLabel(), theme::TypeRole::LabelMedium,
142 schemeColor(SchemeColorRole::OnSurfaceVariant));
143 } else {
144 // The lead group holds the icon and the label; the trailing
145 // text sits at the far edge of the row.
146 label =
147 buildText(registry, *lead, lead, entry.getLabel(),
148 theme::TypeRole::LabelLarge,
149 schemeColor(SchemeColorRole::OnSurfaceVariant));
150 if (label != nullptr) {
151 content.push_back(label);
152 }
153 right =
154 buildText(registry, *this, shared_from_this(),
155 entry.getRight(), theme::TypeRole::LabelLarge,
156 schemeColor(SchemeColorRole::OnSurfaceVariant));
157 }
158 if (!content.empty() || !entry.isCollapsed()) {
159 lead->setEntities(content);
160 }
161
162 registry
163 .getComponent<components::NavigationDrawerDestination>(
164 getIdentifier())
165 .setLead(lead->getIdentifier())
166 .setIconEntity(identifierOf(icon))
167 .setLabelEntity(identifierOf(label))
168 .setRightEntity(identifierOf(right));
169 }
170
171 systems::NavigationDrawerDestination::apply(registry, getIdentifier());
172 }
173
174 void DrawerDestination::setOnPress(std::function<void(void)> onPress)
175 {
179 .setOnPress(std::move(onPress));
180 }
181
187
193
195 const components::Selectable::Handler &handler)
196 {
198 .getComponent<components::Selectable>(getIdentifier())
199 .setOnSelectionChangedHandler(handler);
200 }
201
207
209 {
210 return getComponentRegistry()
213 .getRightEntity();
214 }
215} // namespace guillaume::entities::detail
Axis
Direction along which children are arranged.
Definition layout.hpp:74
State of one destination of a navigation drawer: an icon, a label and an optional text at the trailin...
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
components::NavigationDrawer::Entry Entry
void setDisabled(bool disabled)
Enable or disable the destination. Restyles at once.
bool isSelected(void) const
Whether the destination is the selected one of its drawer.
void setOnPress(std::function< void(void)> onPress)
Set the handler called when the user presses the destination while it is enabled.
ecs::Entity::Identifier getRightIdentifier(void) const
Get the trailing text entity.
void setOnSelectionChanged(const components::Selectable::Handler &handler)
Set the handler of the destination's components::Selectable.
bool isDisabled(void) const
Whether the destination ignores presses.
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the destination ignores input.
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 constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
Definition tokens.hpp:441
Makes the color helpers callable from guillaume::entities without qualification.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
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 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.