guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
dialog.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 <memory>
24#include <utility>
25
26#include "guillaume/entities/dialog.hpp"
27
28#include "guillaume/entities/builder_base.hpp"
29#include "guillaume/entities/content_helpers.hpp"
31#include "guillaume/systems/dialog.hpp"
32#include "guillaume/theme.hpp"
33
34namespace guillaume::entities
35{
36 namespace
37 {
39 constexpr float DialogWidth = components::Dialog::Width;
40
42 constexpr float DialogPadding = systems::Dialog::Padding;
43
45 constexpr float DialogSpacing = systems::Dialog::Spacing;
46
48 constexpr float HeroIconSize = 24.0f;
49
55 std::vector<Dialog::Action>
56 toActions(const std::vector<std::string> &labels)
57 {
58 std::vector<Dialog::Action> actions;
59 actions.reserve(labels.size());
60 for (const auto &label: labels) {
61 actions.push_back(Dialog::Action { label, {} });
62 }
63 return actions;
64 }
65 } // namespace
66
68 ecs::EntityRegistry &entityRegistry)
69 : EntityBuilderBase<Dialog>(componentRegistry, entityRegistry)
70 {
71 reset();
72 }
73
74 std::shared_ptr<Dialog> Dialog::Builder::buildEntity(void)
75 {
76 auto entity =
77 Dialog::create(this->getComponentRegistry(), _config, _variant,
78 _title, _message, _actions, _icon, _onDismiss);
79 return entity;
80 }
81
83 {
84 _config = SurfaceConfig();
85 _variant = Variant::Alert;
86 _title.clear();
87 _message.clear();
88 _actions.clear();
89 _icon.clear();
90 _onDismiss = {};
91
93 _config.mainAxisAlignment =
95 _config.crossAxisAlignment =
97 _config.padding = DialogPadding;
98 _config.spacing = DialogSpacing;
99 _config.borderRadius = getShapeRadius(theme::ShapeRole::ExtraLarge);
100 _config.setFixedWidth(DialogWidth);
101 }
102
104 Dialog::Builder::withPose(const utility::graphic::PoseF &pose)
105 {
106 _config.pose = pose;
107 return *this;
108 }
109
111 {
112 _variant = variant;
113 return *this;
114 }
115
117 {
118 _title = title;
119 return *this;
120 }
121
123 {
124 _message = message;
125 return *this;
126 }
127
129 Dialog::Builder::addAction(const std::string &label,
130 std::function<void(void)> onPress)
131 {
132 _actions.push_back(Action { label, std::move(onPress) });
133 return *this;
134 }
135
136 Dialog::Builder &Dialog::Builder::withIcon(const std::string &iconGlyph)
137 {
138 _icon = iconGlyph;
139 return *this;
140 }
141
143 Dialog::Builder::withOnDismiss(std::function<void(void)> onDismiss)
144 {
145 _onDismiss = std::move(onDismiss);
146 return *this;
147 }
148
149 std::shared_ptr<Dialog>
150 Dialog::Builder::makeDialog(std::shared_ptr<ecs::Entity> parent,
151 Variant variant, const std::string &title,
152 const std::string &message,
153 const std::vector<std::string> &actions)
154 {
155 withVariant(variant).withTitle(title).withMessage(message);
156
157 for (const auto &action: actions) {
158 addAction(action);
159 }
160
161 return registerEntity(parent);
162 }
163
165 const SurfaceConfig &config, Variant variant,
166 const std::string &title, const std::string &message,
167 const std::vector<std::string> &actions)
168 : Dialog(registry, config, variant, title, message, toActions(actions),
169 "", {})
170 {
171 }
172
174 const SurfaceConfig &config, Variant variant,
175 const std::string &title, const std::string &message,
176 const std::vector<Action> &actions, const std::string &icon,
177 std::function<void(void)> onDismiss)
178 : SurfaceBase<components::Overlay, components::Elevation,
179 components::Accessibility, components::Dialog>(registry,
180 config)
181 {
184 .setVariant(variant)
185 .setTitle(title)
186 .setMessage(message)
187 .setActions(actions)
188 .setIcon(icon);
191 .setOnDismissHandler(std::move(onDismiss));
194 }
195
197 {
200 .setVariant(variant);
201 // A Confirmation dialog may need its default hero icon.
202 buildContent();
204 return *this;
205 }
206
213
214 Dialog &Dialog::setTitle(const std::string &title)
215 {
218 .setTitle(title);
219 buildContent();
221 return *this;
222 }
223
224 Dialog &Dialog::setMessage(const std::string &message)
225 {
228 .setMessage(message);
229 buildContent();
231 return *this;
232 }
233
235 {
237 return *this;
238 }
239
241 {
243 return *this;
244 }
245
246 bool Dialog::isVisible(void) const
247 {
248 return getComponentRegistry()
250 .isVisible();
251 }
252
254 {
255 return show();
256 }
257
259 {
260 return hide();
261 }
262
263 bool Dialog::isOpen(void) const
264 {
265 return isVisible();
266 }
267
273
274 Dialog &Dialog::setOnDismiss(std::function<void(void)> onDismiss)
275 {
278 .setOnDismissHandler(std::move(onDismiss));
279 return *this;
280 }
281
282 std::size_t Dialog::getActionCount(void) const
283 {
284 return getComponentRegistry()
286 .getActionEntities()
287 .size();
288 }
289
291 {
292 const auto &actions =
295 .getActionEntities();
296
297 return index < actions.size() ? actions[index]
299 }
300
307
314
321
322 void Dialog::build(void)
323 {
324 buildContent();
326 }
327
328 void Dialog::buildContent(void)
329 {
330 // Children need the owning handle as their parent.
331 if (weak_from_this().expired()) {
332 return;
333 }
334
335 auto &registry = getComponentRegistry();
336 auto &dialog =
338
339 if (dialog.getBackdrop() == ecs::Entity::InvalidIdentifier) {
340 const auto backdrop = Backdrop::create(registry);
341 backdrop->setParent(shared_from_this());
342 addEntity(backdrop);
343 dialog.setBackdrop(backdrop->getIdentifier());
344 }
345
346 const std::string glyph = dialog.getEffectiveIcon();
347 if (dialog.getIconEntity() == ecs::Entity::InvalidIdentifier
348 && !glyph.empty()) {
349 const auto icon = buildIcon(
350 registry, *this, shared_from_this(), glyph, HeroIconSize,
351 schemeColor(SchemeColorRole::Secondary));
352 if (icon != nullptr) {
353 dialog.setIconEntity(icon->getIdentifier());
354 }
355 }
356
357 if (dialog.getTitleEntity() == ecs::Entity::InvalidIdentifier
358 && !dialog.getTitle().empty()) {
359 const auto title =
360 buildText(registry, *this, shared_from_this(),
361 dialog.getTitle(), theme::TypeRole::HeadlineSmall,
362 schemeColor(SchemeColorRole::OnSurface));
363 if (title != nullptr) {
364 dialog.setTitleEntity(title->getIdentifier());
366 }
367 }
368
369 if (dialog.getMessageEntity() == ecs::Entity::InvalidIdentifier
370 && !dialog.getMessage().empty()) {
371 const auto message =
372 buildText(registry, *this, shared_from_this(),
373 dialog.getMessage(), theme::TypeRole::BodyMedium,
374 schemeColor(SchemeColorRole::OnSurfaceVariant));
375 if (message != nullptr) {
376 dialog.setMessageEntity(message->getIdentifier());
377 }
378 }
379
380 if (dialog.getActionRow() == ecs::Entity::InvalidIdentifier
381 && !dialog.getActions().empty()) {
382 buildActionRow();
383 }
384
385 // Hand the layout children to the surface.
386 Children children;
387 if (auto icon = getEntity<Icon>(dialog.getIconEntity())) {
388 children.push_back(std::move(icon));
389 }
390 if (auto title = getEntity<Text>(dialog.getTitleEntity())) {
391 children.push_back(std::move(title));
392 }
393 if (auto message = getEntity<Text>(dialog.getMessageEntity())) {
394 children.push_back(std::move(message));
395 }
396 if (auto row = getEntity<View>(dialog.getActionRow())) {
397 children.push_back(std::move(row));
398 }
399 setChildren(children);
400 }
401
402 void Dialog::buildActionRow(void)
403 {
404 auto &registry = getComponentRegistry();
405 auto &dialog =
407
408 View::Builder rowBuilder(registry, *this);
409 const auto row = rowBuilder.makeDefaultView(
410 shared_from_this(), utility::graphic::PoseF(), {});
411 if (row == nullptr) {
412 return;
413 }
414 row->setColor(transparentColor());
416 row->setMainAxisAlignment(components::Layout::MainAxisAlignment::End);
417 row->setCrossAxisAlignment(
419 row->setSpacing(theme::SpacingScale::get(2));
420 row->setPadding(0.0f);
421 row->setFixedWidth(DialogWidth - 2.0f * DialogPadding);
422
423 // The press handlers outlive this call: capture the registry and
424 // the identifier, never the entity object.
425 ecs::ComponentRegistry *registryPointer = &registry;
426 const ecs::Entity::Identifier identifier = getIdentifier();
427 const auto &actions = dialog.getActions();
428 std::vector<std::shared_ptr<ecs::Entity>> buttons;
429 std::vector<ecs::Entity::Identifier> identifiers;
430
431 for (std::size_t index = 0; index < actions.size(); ++index) {
432 if (actions[index].label.empty()) {
433 continue;
434 }
435 Button::Builder buttonBuilder(registry, *row);
436 const auto button =
437 buttonBuilder.withLabel(actions[index].label)
438 .withColorStyle(Button::Color::Text)
439 .withShape(Button::Shape::Round)
440 .withSize(Button::Size::Small)
441 .withOnClick([registryPointer, identifier, index]() {
442 systems::Dialog::pressAction(*registryPointer,
443 identifier, index);
444 })
445 .registerEntity(row);
446 if (button != nullptr) {
447 buttons.push_back(button);
448 identifiers.push_back(button->getIdentifier());
449 }
450 }
451 row->setEntities(buttons);
452
453 dialog.setActionEntities(identifiers)
454 .setActionRow(row->getIdentifier());
455 }
456
457} // namespace guillaume::entities
State of a dialog: a modal panel with an optional icon, a title, a message and a row of text buttons.
Definition dialog.hpp:54
static constexpr float Width
Width of the dialog panel, in dp.
Definition dialog.hpp:78
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
@ End
Packed at the right (row) or bottom (column).
Definition layout.hpp:89
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
Definition overlay.hpp:55
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.
Owns entities and gives the systems the list of entities to visit.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
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
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
static std::shared_ptr< Backdrop > create(Arguments &&...arguments)
Build a hidden backdrop without registering it in an entity registry.
Definition backdrop.hpp:100
Configures a Dialog, then builds it and registers it in the entity registry given at construction.
Definition dialog.hpp:126
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Definition dialog.cpp:67
Builder & withTitle(const std::string &title)
Set the title, shown above the message.
Definition dialog.cpp:116
Builder & withPose(const utility::graphic::PoseF &pose)
Set the pose the dialog is created with.
Definition dialog.cpp:104
Builder & addAction(const std::string &label, std::function< void(void)> onPress={})
Append an action, shown as a text button at the end of the action row.
Definition dialog.cpp:129
void reset(void) override
Restore the defaults: Alert variant, no title, no message, no action, no icon, no dismiss handler,...
Definition dialog.cpp:82
std::shared_ptr< Dialog > makeDialog(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &title, const std::string &message, const std::vector< std::string > &actions={})
Build a dialog, register it under parent and reset the builder.
Definition dialog.cpp:150
Builder & withVariant(Variant variant)
Set the dialog variant.
Definition dialog.cpp:110
Builder & withMessage(const std::string &message)
Set the message, shown under the title.
Definition dialog.cpp:122
std::shared_ptr< Dialog > buildEntity(void) override
Build a dialog and its children from the current configuration, without registering it or setting its...
Definition dialog.cpp:74
Builder & withIcon(const std::string &iconGlyph)
Set the icon shown above the title.
Definition dialog.cpp:136
Builder & withOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the dialog is dismissed (see Dialog::setOnDismiss()).
Definition dialog.cpp:143
Dialog & close(void)
Hide the dialog; same as hide().
Definition dialog.cpp:258
Dialog & show(void)
Show the dialog and, except for the FullScreen variant, its backdrop.
Definition dialog.cpp:234
bool isOpen(void) const
Check whether the dialog is shown; same as isVisible().
Definition dialog.cpp:263
ecs::Entity::Identifier getIconIdentifier(void) const
Get the identifier of the icon shown above the title.
Definition dialog.cpp:315
Dialog(ecs::ComponentRegistry &registry, const SurfaceConfig &config, Variant variant, const std::string &title, const std::string &message, const std::vector< Action > &actions, const std::string &icon, std::function< void(void)> onDismiss={})
Construct a dialog with pressable actions and an icon.
Definition dialog.cpp:173
Dialog & open(void)
Show the dialog; same as show().
Definition dialog.cpp:253
bool isVisible(void) const
Check whether the dialog is shown.
Definition dialog.cpp:246
static std::shared_ptr< Dialog > create(Arguments &&...arguments)
Create a dialog and its children, without registering it in an entity registry.
Definition dialog.hpp:283
Dialog & setVariant(Variant variant)
Change the dialog variant.
Definition dialog.cpp:196
Variant getVariant(void) const
Get the dialog variant.
Definition dialog.cpp:207
Dialog & dismiss(void)
Dismiss the dialog as the user would: hide it, then call the dismiss handler.
Definition dialog.cpp:268
ecs::Entity::Identifier getBackdropIdentifier(void) const
Get the identifier of the backdrop drawn behind the panel.
Definition dialog.cpp:308
components::Dialog::Action Action
Definition dialog.hpp:119
ecs::Entity::Identifier getActionRowIdentifier(void) const
Get the identifier of the row that holds the action buttons.
Definition dialog.cpp:301
Dialog & setMessage(const std::string &message)
Change the message text.
Definition dialog.cpp:224
ecs::Entity::Identifier getActionIdentifier(std::size_t index) const
Get the identifier of an action button.
Definition dialog.cpp:290
Dialog & setOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the dialog is dismissed (Paper onDismiss).
Definition dialog.cpp:274
Dialog & setTitle(const std::string &title)
Change the title text.
Definition dialog.cpp:214
Dialog & hide(void)
Hide the dialog and its backdrop.
Definition dialog.cpp:240
std::size_t getActionCount(void) const
Get the number of action buttons.
Definition dialog.cpp:282
components::Dialog::Variant Variant
Look of the dialog: Alert, Simple, Confirmation or FullScreen.
Definition dialog.hpp:115
std::shared_ptr< Dialog > registerEntity(std::shared_ptr< ecs::Entity > parent)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one dialog, arrange its children and place its backdrop.
Definition dialog.cpp:170
static void dismiss(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Close the dialog as the user would with Escape or a click outside.
Definition dialog.cpp:131
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the dialog and its backdrop without telling anyone.
Definition dialog.cpp:123
static constexpr float Padding
Space between the edge of the panel and its content: 24 dp.
Definition dialog.hpp:71
static void pressAction(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Activate an action as a press on its button would.
Definition dialog.cpp:151
static void installTitle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Expose the title child to assistive technology as a header.
Definition dialog.cpp:96
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a dialog panel its accessibility semantics.
Definition dialog.cpp:85
static constexpr float Spacing
Gap between two stacked sections of the dialog: 16 dp.
Definition dialog.hpp:74
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the dialog and its backdrop.
Definition dialog.cpp:115
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.
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
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.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
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.
@ HeadlineSmall
24 / 32, regular.
Definition typescale.hpp:53
@ BodyMedium
14 / 20, regular.
Definition typescale.hpp:61
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
Definition tokens.hpp:48
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
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