guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
snackbar.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/snackbar.hpp"
24
25#include <memory>
26#include <utility>
27#include <vector>
28
29#include "guillaume/entities/content_helpers.hpp"
30#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/systems/snackbar.hpp"
33#include "guillaume/theme.hpp"
34
35namespace guillaume::entities
36{
38 ecs::EntityRegistry &entityRegistry)
39 : EntityBuilderBase<Snackbar>(componentRegistry, entityRegistry)
40 {
41 reset();
42 }
43
44 std::shared_ptr<Snackbar> Snackbar::Builder::buildEntity(void)
45 {
46 auto entity = Snackbar::create(this->getComponentRegistry(), _config,
47 _variant, _message, _action);
48 entity->setDuration(_duration);
49 entity->setOnDismiss(_onDismiss);
50 entity->setOnAction(_onAction);
51 entity->setVisible(_visible);
52 return entity;
53 }
54
56 {
57 _config = SurfaceConfig();
58 _variant = Variant::SingleLine;
59 _message.clear();
60 _action.clear();
61 _visible = true;
62 _duration = DurationMedium;
63 _onDismiss = {};
64 _onAction = {};
65
67 _config.mainAxisAlignment =
69 _config.crossAxisAlignment =
71 _config.padding = 16.0f;
72 _config.spacing = 8.0f;
73 _config.setFixedWidth(344.0f);
74 }
75
77 Snackbar::Builder::withPose(const utility::graphic::PoseF &pose)
78 {
79 _config.pose = pose;
80 return *this;
81 }
82
84 {
85 _variant = variant;
86 return *this;
87 }
88
90 Snackbar::Builder::withMessage(const std::string &message)
91 {
92 _message = message;
93 return *this;
94 }
95
97 {
98 _action = action;
99 return *this;
100 }
101
103 {
104 _visible = visible;
105 return *this;
106 }
107
109 {
110 _duration = seconds;
111 return *this;
112 }
113
115 Snackbar::Builder::withOnDismiss(std::function<void(void)> onDismiss)
116 {
117 _onDismiss = std::move(onDismiss);
118 return *this;
119 }
120
122 Snackbar::Builder::withOnAction(std::function<void(void)> onAction)
123 {
124 _onAction = std::move(onAction);
125 return *this;
126 }
127
128 std::shared_ptr<Snackbar> Snackbar::Builder::makeSnackbar(
129 std::shared_ptr<ecs::Entity> parent, Variant variant,
130 const std::string &message, const std::string &action)
131 {
132 return withVariant(variant)
133 .withMessage(message)
134 .withAction(action)
135 .registerEntity(parent);
136 }
137
138 Snackbar::Snackbar(ecs::ComponentRegistry &registry,
139 const SurfaceConfig &config, Variant variant,
140 const std::string &message, const std::string &action)
141 : SurfaceBase<components::Overlay, components::Elevation,
142 components::Accessibility, components::Snackbar>(registry,
143 config)
144 {
147 .setVariant(variant)
148 .setMessage(message)
149 .setAction(action);
152 }
153
154 Snackbar &Snackbar::setVisible(bool visible)
155 {
156 if (visible) {
158 } else {
160 }
161 return *this;
162 }
163
164 Snackbar &Snackbar::show(void)
165 {
166 return setVisible(true);
167 }
168
169 Snackbar &Snackbar::hide(void)
170 {
171 return setVisible(false);
172 }
173
174 bool Snackbar::isVisible(void) const
175 {
176 return getComponentRegistry()
178 .isVisible();
179 }
180
181 Snackbar &Snackbar::setDuration(float seconds)
182 {
185 .setDuration(seconds);
187 return *this;
188 }
189
196
197 Snackbar &Snackbar::setOnDismiss(std::function<void(void)> onDismiss)
198 {
201 .setOnDismiss(std::move(onDismiss));
202 return *this;
203 }
204
205 Snackbar &Snackbar::setOnAction(std::function<void(void)> onAction)
206 {
209 .setOnAction(std::move(onAction));
210 return *this;
211 }
212
213 Snackbar &Snackbar::pressAction(void)
214 {
216 return *this;
217 }
218
219 std::shared_ptr<Text> Snackbar::getActionEntity(void) const
220 {
221 // The lookup is const because this getter is; the child itself is
222 // shared and mutable, so callers get a non-const handle.
223 return std::const_pointer_cast<Text>(getEntity<Text>(
225 .getComponent<components::Snackbar>(getIdentifier())
226 .getActionEntity()));
227 }
228
229 Snackbar &Snackbar::setVariant(Variant variant)
230 {
233 .setVariant(variant);
234 buildContent();
236 return *this;
237 }
238
245
246 Snackbar &Snackbar::setMessage(const std::string &message)
247 {
250 .setMessage(message);
251 buildContent();
253 return *this;
254 }
255
256 void Snackbar::build(void)
257 {
258 buildContent();
260 }
261
262 void Snackbar::buildContent(void)
263 {
264 // Children need the owning handle as their parent.
265 if (weak_from_this().expired()) {
266 return;
267 }
268
269 auto &registry = getComponentRegistry();
270 auto &snackbar =
271 registry.getComponent<components::Snackbar>(getIdentifier());
272
273 if (snackbar.getMessageEntity() == ecs::Entity::InvalidIdentifier) {
274 const auto message =
275 buildText(registry, *this, shared_from_this(),
276 snackbar.getMessage(), theme::TypeRole::BodyMedium,
277 schemeColor(SchemeColorRole::InverseOnSurface));
278 if (message != nullptr) {
279 snackbar.setMessageEntity(message->getIdentifier());
280 }
281 }
282
283 if (snackbar.getVariant() == Variant::WithAction
284 && snackbar.getActionEntity() == ecs::Entity::InvalidIdentifier) {
285 const auto action =
286 buildText(registry, *this, shared_from_this(),
287 snackbar.getAction(), theme::TypeRole::LabelLarge,
288 schemeColor(SchemeColorRole::InversePrimary));
289 if (action != nullptr) {
290 snackbar.setActionEntity(action->getIdentifier());
292 }
293 }
294
295 // Hand the layout children to the surface.
296 std::vector<std::shared_ptr<ecs::Entity>> children;
297 if (auto message = getEntity<Text>(snackbar.getMessageEntity())) {
298 children.push_back(std::move(message));
299 }
300 if (auto action = getEntity<Text>(snackbar.getActionEntity())) {
301 children.push_back(std::move(action));
302 }
303 setChildren(children);
304 }
305
306} // namespace guillaume::entities
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
State of a snackbar: a short message at the bottom of the screen, with an optional action,...
Definition snackbar.hpp:54
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.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
std::shared_ptr< EntityType > getEntity(Entity::Identifier identifier)
Find an entity owned directly by this registry, by identifier.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a Snackbar, then builds it and registers it in the entity registry given at construction.
Definition snackbar.hpp:132
Builder & withOnAction(std::function< void(void)> onAction)
Set the handler called when the action is pressed (Paper action.onPress).
Definition snackbar.cpp:122
Builder & withAction(const std::string &action)
Set the label of the action.
Definition snackbar.cpp:96
Builder & withVariant(Variant variant)
Set the snackbar variant.
Definition snackbar.cpp:83
Builder & withMessage(const std::string &message)
Set the message.
Definition snackbar.cpp:90
Builder & withDuration(float seconds)
Set how long the snackbar stays shown (Paper duration).
Definition snackbar.cpp:108
Builder & withPose(const utility::graphic::PoseF &pose)
Set the pose the snackbar is created with.
Definition snackbar.cpp:77
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Definition snackbar.cpp:37
Builder & withOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the snackbar dismisses itself (see Snackbar::setOnDismiss()).
Definition snackbar.cpp:115
void reset(void) override
Restore the defaults: SingleLine variant, no message, no action label, shown, DurationMedium (7 secon...
Definition snackbar.cpp:55
std::shared_ptr< Snackbar > buildEntity(void) override
Build a snackbar and its children from the current configuration, without registering it or setting i...
Definition snackbar.cpp:44
std::shared_ptr< Snackbar > makeSnackbar(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &message, const std::string &action="")
Build a snackbar, register it under parent and reset the builder.
Definition snackbar.cpp:128
Builder & withVisible(bool visible)
Choose whether the snackbar is shown once built.
Definition snackbar.cpp:102
bool isVisible(void) const
Check whether the snackbar is shown.
Definition snackbar.cpp:174
float getDuration(void) const
Get how long the snackbar stays shown.
Definition snackbar.cpp:190
Snackbar & setVisible(bool visible)
Show or hide the snackbar (Paper visible).
Definition snackbar.cpp:154
Snackbar & show(void)
Show the snackbar; same as setVisible(true).
Definition snackbar.cpp:164
Snackbar & pressAction(void)
Activate the action as a press on its label would: call the action handler, then hide the snackbar an...
Definition snackbar.cpp:213
Snackbar & setDuration(float seconds)
Set how long the snackbar stays shown (Paper duration).
Definition snackbar.cpp:181
Snackbar & setOnAction(std::function< void(void)> onAction)
Set the handler called when the action is pressed (Paper action.onPress).
Definition snackbar.cpp:205
static std::shared_ptr< Snackbar > create(Arguments &&...arguments)
Create a snackbar and its children, without registering it in an entity registry.
Definition snackbar.hpp:270
components::Snackbar::Variant Variant
Look of the snackbar: SingleLine, MultiLine or WithAction.
Definition snackbar.hpp:104
Snackbar & hide(void)
Hide the snackbar without calling the dismiss handler; same as setVisible(false).
Definition snackbar.cpp:169
Snackbar & setMessage(const std::string &message)
Change the message text.
Definition snackbar.cpp:246
std::shared_ptr< Text > getActionEntity(void) const
Get the text entity that shows the action label.
Definition snackbar.cpp:219
Snackbar & setVariant(Variant variant)
Change the snackbar variant.
Definition snackbar.cpp:229
Variant getVariant(void) const
Get the snackbar variant.
Definition snackbar.cpp:239
Snackbar & setOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the snackbar dismisses itself (Paper onDismiss).
Definition snackbar.cpp:197
static constexpr float DurationMedium
Default display time: 7 seconds (Paper Snackbar.DURATION_MEDIUM).
Definition snackbar.hpp:117
static void pressAction(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the action as a press on it would.
Definition snackbar.cpp:155
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a snackbar its accessibility semantics and connect its dismissal.
Definition snackbar.cpp:76
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one snackbar and arrange its children.
Definition snackbar.cpp:174
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the snackbar and restart its auto-dismiss countdown.
Definition snackbar.cpp:118
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the snackbar without telling anyone.
Definition snackbar.cpp:126
static void installAction(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the action child a button that calls pressAction().
Definition snackbar.cpp:96
Makes the color helpers callable from guillaume::entities without qualification.
State. Every class here is plain data attached to an entity.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
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.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ LabelLarge
14 / 20, medium weight.
Definition typescale.hpp:57
@ BodyMedium
14 / 20, regular.
Definition typescale.hpp:61
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...
Definition surface.hpp:71