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/systems/snackbar.hpp"
24
25#include <vector>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/borders.hpp"
29#include "guillaume/components/bound.hpp"
30#include "guillaume/components/color.hpp"
31#include "guillaume/components/elevation.hpp"
32#include "guillaume/components/layout.hpp"
33#include "guillaume/components/overlay.hpp"
34#include "guillaume/components/text.hpp"
35#include "guillaume/components/transform.hpp"
38#include "guillaume/systems/layout.hpp"
39#include "guillaume/theme.hpp"
40
41namespace guillaume::systems
42{
43 namespace
44 {
45 using Variant = components::Snackbar::Variant;
48
50 constexpr float SingleLineHeight = 48.0f;
51
53 constexpr float MultiLineHeight = 68.0f;
54
61 void colorChild(ecs::ComponentRegistry &registry,
63 const utility::graphic::Color32Bit &color)
64 {
65 if (registry.hasComponent<components::Color>(child)) {
66 registry.getComponent<components::Color>(child).setColor(color);
67 }
68 }
69 } // namespace
70
75
77 const ecs::Entity::Identifier &entityIdentifier)
78 {
79 ecs::ComponentRegistry *registry = &componentRegistry;
80 const ecs::Entity::Identifier identifier = entityIdentifier;
81
82 componentRegistry.getComponent<components::Accessibility>(identifier)
85
86 // systems::Overlay reports the timeout and Escape through this.
87 componentRegistry.getComponent<components::Overlay>(identifier)
88 .setOnDismissHandler([registry, identifier]() {
89 dismiss(*registry, identifier);
90 });
91
92 installAction(componentRegistry, identifier);
93 }
94
95 void
97 const ecs::Entity::Identifier &entityIdentifier)
98 {
99 ecs::ComponentRegistry *registry = &componentRegistry;
100 const ecs::Entity::Identifier identifier = entityIdentifier;
101 const auto &snackbar =
102 componentRegistry.getComponent<components::Snackbar>(identifier);
103 const auto action = snackbar.getActionEntity();
104
105 if (action == ecs::Entity::InvalidIdentifier) {
106 return;
107 }
108
109 interaction::makeInteractive(componentRegistry, action,
111 [registry, identifier]() {
112 pressAction(*registry, identifier);
113 });
114 componentRegistry.getComponent<components::Accessibility>(action)
115 .setLabel(snackbar.getAction());
116 }
117
118 void Snackbar::show(ecs::ComponentRegistry &componentRegistry,
119 const ecs::Entity::Identifier &entityIdentifier)
120 {
121 componentRegistry.getComponent<components::Snackbar>(entityIdentifier)
122 .setVisible(true);
123 apply(componentRegistry, entityIdentifier);
124 }
125
126 void Snackbar::hide(ecs::ComponentRegistry &componentRegistry,
127 const ecs::Entity::Identifier &entityIdentifier)
128 {
129 componentRegistry.getComponent<components::Snackbar>(entityIdentifier)
130 .setVisible(false);
131 apply(componentRegistry, entityIdentifier);
132 }
133
135 const ecs::Entity::Identifier &entityIdentifier)
136 {
137 auto &snackbar = componentRegistry.getComponent<components::Snackbar>(
138 entityIdentifier);
139
140 // A dismissal the snackbar requested itself (hide) is not reported:
141 // like Paper, onDismiss only fires on timeout, Escape or action.
142 if (!snackbar.isVisible()) {
143 return;
144 }
145 snackbar.setVisible(false);
146 apply(componentRegistry, entityIdentifier);
147
148 // Copy: the callback may replace itself.
149 const auto onDismiss = snackbar.getOnDismiss();
150 if (onDismiss) {
151 onDismiss();
152 }
153 }
154
156 const ecs::Entity::Identifier &entityIdentifier)
157 {
158 const auto &snackbar =
159 componentRegistry.getComponent<components::Snackbar>(
160 entityIdentifier);
161
162 if (!snackbar.isVisible()) {
163 return;
164 }
165
166 // Copy: the callback may replace itself.
167 const auto onAction = snackbar.getOnAction();
168 if (onAction) {
169 onAction();
170 }
171 dismiss(componentRegistry, entityIdentifier);
172 }
173
175 const ecs::Entity::Identifier &entityIdentifier)
176 {
177 const auto &snackbar =
178 componentRegistry.getComponent<components::Snackbar>(
179 entityIdentifier);
180 const bool visible = snackbar.isVisible();
181 const auto message = snackbar.getMessageEntity();
182 const auto action = snackbar.getActionEntity();
183
184 std::vector<ecs::Entity::Identifier> children;
185 if (message != ecs::Entity::InvalidIdentifier) {
186 children.push_back(message);
187 }
188 if (action != ecs::Entity::InvalidIdentifier) {
189 children.push_back(action);
190 }
191
192 auto &layout = componentRegistry.getComponent<components::Layout>(
193 entityIdentifier);
199 .setPadding(16.0f)
200 .setSpacing(8.0f)
201 .setFixedHeight(snackbar.getVariant() == Variant::MultiLine
202 ? MultiLineHeight
203 : SingleLineHeight)
204 .setChildren(children);
205
206 componentRegistry.getComponent<components::Borders>(entityIdentifier)
208 componentRegistry.getComponent<components::Color>(entityIdentifier)
209 .setColor(visible ? schemeColor(SchemeColorRole::InverseSurface)
210 : transparentColor());
211 componentRegistry.getComponent<components::Elevation>(entityIdentifier)
212 .setElevationLevel(visible ? theme::ElevationLevel::Level3
214
215 auto &overlay = componentRegistry.getComponent<components::Overlay>(
216 entityIdentifier);
218 snackbar.getDuration());
219 if (overlay.isVisible() != visible) {
220 // Showing restarts the auto-dismiss countdown.
221 overlay.setElapsed(0.0f);
222 overlay.setVisible(visible);
223 }
224
225 componentRegistry
226 .getComponent<components::Accessibility>(entityIdentifier)
227 .setLabel(snackbar.getMessage());
228
229 if (componentRegistry.hasComponent<components::Text>(message)) {
230 componentRegistry.getComponent<components::Text>(message)
231 .setContent(snackbar.getMessage());
232 }
233 colorChild(componentRegistry, message,
234 visible ? schemeColor(SchemeColorRole::InverseOnSurface)
235 : transparentColor());
236 colorChild(componentRegistry, action,
237 visible ? schemeColor(SchemeColorRole::InversePrimary)
238 : transparentColor());
239
240 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
241 Layout::layerOf(componentRegistry, entityIdentifier));
242 }
243
244 void Snackbar::update(const ecs::Entity::Identifier &entityIdentifier)
245 {
246 if (!requireComponent<components::Snackbar>(entityIdentifier)) {
247 return;
248 }
249
250 apply(getComponentRegistry(), entityIdentifier);
251 }
252} // 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 & setLiveRegion(LiveRegion liveRegion)
Set how urgently a change of the entity is announced.
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
Layout & setChildren(const std::vector< ecs::Entity::Identifier > &children)
Replace the list of children to arrange.
Definition layout.cpp:171
Layout & setAxis(Axis axis)
Choose between a row and a column.
Definition layout.cpp:32
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Definition layout.cpp:92
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
Definition layout.cpp:47
Layout & setFixedHeight(float height)
Force the container height instead of wrapping the children.
Definition layout.cpp:144
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Definition layout.cpp:77
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
Definition overlay.hpp:55
Overlay & setModal(bool modal)
Declare the overlay modal (it must be answered before the rest of the interface is used) or not.
Definition overlay.cpp:50
Overlay & setDismissOnOutsideClick(bool dismiss)
Choose whether a click outside the overlay dismisses it.
Definition overlay.cpp:65
Overlay & setAutoDismiss(float delay)
Set how long the overlay stays shown before it is dismissed by itself.
Definition overlay.cpp:95
State of a snackbar: a short message at the bottom of the screen, with an optional action,...
Definition snackbar.hpp:54
const Handler & getOnAction(void) const noexcept
Get the callback run when the action is pressed.
Definition snackbar.cpp:142
ecs::Entity::Identifier getActionEntity(void) const noexcept
Get the pressable child that draws the action label.
Definition snackbar.cpp:86
bool isVisible(void) const noexcept
Tell whether the snackbar is shown.
Definition snackbar.cpp:100
Snackbar & setVisible(bool visible)
Record whether the snackbar is shown.
Definition snackbar.cpp:105
A run of text: its content, its font metrics and the position of the editing cursor.
Definition text.hpp:66
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.
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
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 void dismiss(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Close the snackbar as the timeout, Escape or its action would.
Definition snackbar.cpp:134
static void pressAction(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the action as a press on it would.
Definition snackbar.cpp:155
Snackbar(void)
Create the system in the Layout phase.
Definition snackbar.cpp:71
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
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one snackbar; called by the frame loop.
Definition snackbar.cpp:244
State. Every class here is plain data attached to an entity.
@ Alert
Live announcement (snackbar).
@ Polite
Announced once the current speech is over.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
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.
Rules. Every class here runs each frame on the entities that have the components it declares.
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.
@ Level3
Third raised level.
Definition tokens.hpp:134
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
@ ExtraSmall
4 dp (text fields, menus, snackbars).
Definition tokens.hpp:44
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
void makeInteractive(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.