guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
snackbar.hpp
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#pragma once
24
25#include <utility>
26#include <functional>
27#include <memory>
28#include <string>
29
30#include <utility/graphic/color.hpp>
31#include <utility/graphic/pose.hpp>
32
33#include "guillaume/ecs/component_registry.hpp"
34#include "guillaume/ecs/entity_builder.hpp"
35
36#include "guillaume/components/accessibility.hpp"
37#include "guillaume/components/elevation.hpp"
38#include "guillaume/components/overlay.hpp"
39#include "guillaume/components/snackbar.hpp"
40
41#include "guillaume/entities/builder_base.hpp"
42#include "guillaume/entities/surface.hpp"
43#include "guillaume/entities/text.hpp"
44
45namespace guillaume::entities
46{
47
98 class Snackbar:
99 public SurfaceBase<components::Overlay, components::Elevation,
100 components::Accessibility, components::Snackbar>
101 {
102 public:
105
110 static constexpr float DurationShort =
112
117 static constexpr float DurationMedium =
119
124 static constexpr float DurationLong =
126
131 class Builder: public EntityBuilderBase<Snackbar>
132 {
133 private:
134 SurfaceConfig _config;
135 Variant _variant { Variant::SingleLine };
136 std::string _message {};
137 std::string _action {};
138 bool _visible { true };
139 float _duration { DurationMedium };
140 std::function<void(void)> _onDismiss {};
141 std::function<void(void)> _onAction {};
142
143 public:
151 Builder(ecs::ComponentRegistry &componentRegistry,
152 ecs::EntityRegistry &entityRegistry);
153
161 std::shared_ptr<Snackbar> buildEntity(void) override;
162
168 void reset(void) override;
169
176 Builder &withPose(const utility::graphic::PoseF &pose);
177
183 Builder &withVariant(Variant variant);
184
191 Builder &withMessage(const std::string &message);
192
199 Builder &withAction(const std::string &action);
200
207 Builder &withVisible(bool visible);
208
217 Builder &withDuration(float seconds);
218
225 Builder &withOnDismiss(std::function<void(void)> onDismiss);
226
233 Builder &withOnAction(std::function<void(void)> onAction);
234
250 std::shared_ptr<Snackbar>
251 makeSnackbar(std::shared_ptr<ecs::Entity> parent,
252 Variant variant, const std::string &message,
253 const std::string &action = "");
254 };
255
256 public:
269 template<typename... Arguments>
270 static std::shared_ptr<Snackbar> create(Arguments &&...arguments)
271 {
272 std::shared_ptr<Snackbar> entity(
273 new Snackbar(std::forward<Arguments>(arguments)...));
274 entity->build();
275 return entity;
276 }
277
281 ~Snackbar(void) override = default;
282
292 Snackbar &setVariant(Variant variant);
293
298 Variant getVariant(void) const;
299
308 Snackbar &setMessage(const std::string &message);
309
320 Snackbar &setVisible(bool visible);
321
326 Snackbar &show(void);
327
333 Snackbar &hide(void);
334
339 bool isVisible(void) const;
340
350 Snackbar &setDuration(float seconds);
351
357 float getDuration(void) const;
358
369 Snackbar &setOnDismiss(std::function<void(void)> onDismiss);
370
377 Snackbar &setOnAction(std::function<void(void)> onAction);
378
387 Snackbar &pressAction(void);
388
394 std::shared_ptr<Text> getActionEntity(void) const;
395
396 private:
403 void buildContent(void);
404
405 private:
417 Snackbar(ecs::ComponentRegistry &registry, const SurfaceConfig &config,
418 Variant variant, const std::string &message,
419 const std::string &action);
420
427 void build(void);
428 };
429
430} // namespace guillaume::entities
static constexpr float DurationLong
Long display time, in seconds.
Definition snackbar.hpp:75
static constexpr float DurationMedium
Medium display time, in seconds. This is the default.
Definition snackbar.hpp:72
static constexpr float DurationShort
Short display time, in seconds.
Definition snackbar.hpp:69
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Owns entities and gives the systems the list of entities to visit.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
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(void) override=default
Default destructor.
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
static constexpr float DurationLong
Long display time: 10 seconds (Paper Snackbar.DURATION_LONG).
Definition snackbar.hpp:124
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 constexpr float DurationShort
Short display time: 4 seconds (Paper Snackbar.DURATION_SHORT).
Definition snackbar.hpp:110
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
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