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 "guillaume/systems/dialog.hpp"
24
25#include <string>
26#include <vector>
27
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/borders.hpp"
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/elevation.hpp"
33#include "guillaume/components/layout.hpp"
34#include "guillaume/components/overlay.hpp"
35#include "guillaume/components/text.hpp"
36#include "guillaume/components/transform.hpp"
38#include "guillaume/systems/backdrop.hpp"
39#include "guillaume/systems/layout.hpp"
40
41namespace guillaume::systems
42{
43 namespace
44 {
45 using Variant = components::Dialog::Variant;
47
54 void colorChild(ecs::ComponentRegistry &registry,
56 const utility::graphic::Color32Bit &color)
57 {
58 if (registry.hasComponent<components::Color>(child)) {
59 registry.getComponent<components::Color>(child).setColor(color);
60 }
61 }
62
69 void writeChild(ecs::ComponentRegistry &registry,
71 const std::string &content)
72 {
73 if (registry.hasComponent<components::Text>(child)) {
74 registry.getComponent<components::Text>(child).setContent(
75 content);
76 }
77 }
78 } // namespace
79
82 {
83 }
84
85 void Dialog::install(ecs::ComponentRegistry &componentRegistry,
86 const ecs::Entity::Identifier &entityIdentifier)
87 {
88 componentRegistry
89 .getComponent<components::Accessibility>(entityIdentifier)
91 .setFocusable(false);
92
93 installTitle(componentRegistry, entityIdentifier);
94 }
95
97 const ecs::Entity::Identifier &entityIdentifier)
98 {
99 const auto &dialog = componentRegistry.getComponent<components::Dialog>(
100 entityIdentifier);
101 const auto title = dialog.getTitleEntity();
102
103 if (title == ecs::Entity::InvalidIdentifier) {
104 return;
105 }
106 if (!componentRegistry.hasComponent<components::Accessibility>(title)) {
107 componentRegistry.addComponent<components::Accessibility>(title);
108 }
109 componentRegistry.getComponent<components::Accessibility>(title)
111 .setLabel(dialog.getTitle())
112 .setFocusable(false);
113 }
114
115 void Dialog::show(ecs::ComponentRegistry &componentRegistry,
116 const ecs::Entity::Identifier &entityIdentifier)
117 {
118 componentRegistry.getComponent<components::Overlay>(entityIdentifier)
119 .setVisible(true);
120 apply(componentRegistry, entityIdentifier);
121 }
122
123 void Dialog::hide(ecs::ComponentRegistry &componentRegistry,
124 const ecs::Entity::Identifier &entityIdentifier)
125 {
126 componentRegistry.getComponent<components::Overlay>(entityIdentifier)
127 .setVisible(false);
128 apply(componentRegistry, entityIdentifier);
129 }
130
132 const ecs::Entity::Identifier &entityIdentifier)
133 {
134 if (!componentRegistry
135 .getComponent<components::Overlay>(entityIdentifier)
136 .isVisible()) {
137 return;
138 }
139 hide(componentRegistry, entityIdentifier);
140
141 // Copy: the handler may replace itself.
142 const auto onDismiss =
143 componentRegistry
144 .getComponent<components::Overlay>(entityIdentifier)
145 .getOnDismissHandler();
146 if (onDismiss) {
147 onDismiss();
148 }
149 }
150
152 const ecs::Entity::Identifier &entityIdentifier,
153 std::size_t index)
154 {
155 const auto &actions =
156 componentRegistry.getComponent<components::Dialog>(entityIdentifier)
157 .getActions();
158
159 if (index >= actions.size()) {
160 return;
161 }
162
163 // Copy: the handler may replace the actions.
164 const auto onPress = actions[index].onPress;
165 if (onPress) {
166 onPress();
167 }
168 }
169
170 void Dialog::apply(ecs::ComponentRegistry &componentRegistry,
171 const ecs::Entity::Identifier &entityIdentifier)
172 {
173 const auto &dialog = componentRegistry.getComponent<components::Dialog>(
174 entityIdentifier);
175 const bool fullScreen = dialog.getVariant() == Variant::FullScreen;
176 const auto icon = dialog.getIconEntity();
177 const auto title = dialog.getTitleEntity();
178 const auto message = dialog.getMessageEntity();
179 const bool hasIcon = icon != ecs::Entity::InvalidIdentifier;
180
181 std::vector<ecs::Entity::Identifier> children;
182 for (const auto child:
183 { icon, title, message, dialog.getActionRow() }) {
184 if (child != ecs::Entity::InvalidIdentifier) {
185 children.push_back(child);
186 }
187 }
188
189 // M3: with a hero icon, the icon and the headline are centred.
190 auto &layout = componentRegistry.getComponent<components::Layout>(
191 entityIdentifier);
200 .setChildren(children);
201 if (fullScreen) {
202 layout.setFixedHeight(components::Dialog::FullScreenHeight);
203 } else {
204 layout.clearFixedHeight();
205 }
206
207 componentRegistry.getComponent<components::Borders>(entityIdentifier)
208 .setBorderRadius(
211 componentRegistry.getComponent<components::Color>(entityIdentifier)
213 componentRegistry.getComponent<components::Elevation>(entityIdentifier)
214 .setElevationLevel(theme::ElevationLevel::Level3);
215 componentRegistry
216 .getComponent<components::Accessibility>(entityIdentifier)
217 .setLabel(dialog.getTitle());
218
219 auto &overlay = componentRegistry.getComponent<components::Overlay>(
220 entityIdentifier);
221 overlay.setModal(true)
222 .setDismissOnOutsideClick(!fullScreen)
223 .setDismissOnEscape(true);
224
225 colorChild(componentRegistry, icon,
226 schemeColor(SchemeColorRole::Secondary));
227 writeChild(componentRegistry, title, dialog.getTitle());
228 colorChild(componentRegistry, title,
229 schemeColor(SchemeColorRole::OnSurface));
230 if (componentRegistry.hasComponent<components::Accessibility>(title)) {
231 componentRegistry.getComponent<components::Accessibility>(title)
232 .setLabel(dialog.getTitle());
233 }
234 writeChild(componentRegistry, message, dialog.getMessage());
235 colorChild(componentRegistry, message,
236 schemeColor(SchemeColorRole::OnSurfaceVariant));
237
238 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
239 Layout::layerOf(componentRegistry, entityIdentifier));
240
241 const auto backdrop = dialog.getBackdrop();
242 if (backdrop != ecs::Entity::InvalidIdentifier) {
243 // A full-screen dialog covers the viewport and needs no backdrop.
244 Backdrop::setVisible(componentRegistry, backdrop,
245 overlay.isVisible() && !fullScreen);
246 const auto &bound =
247 componentRegistry.getComponent<components::Bound>(
248 entityIdentifier);
250 componentRegistry, backdrop,
251 componentRegistry
252 .getComponent<components::Transform>(entityIdentifier)
253 .getPose(),
254 bound.getWidth(), bound.getHeight());
255 }
256 }
257
258 void Dialog::update(const ecs::Entity::Identifier &entityIdentifier)
259 {
260 if (!requireComponent<components::Dialog>(entityIdentifier)) {
261 return;
262 }
263
264 apply(getComponentRegistry(), entityIdentifier);
265 }
266} // 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 & setFocusable(bool focusable)
Include the entity in the Tab traversal or leave it out.
Accessibility & setLabel(const std::string &label)
Set the name announced for the entity, such as "Close".
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
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 FullScreenHeight
Height of a full-screen dialog, in dp.
Definition dialog.hpp:81
Variant getVariant(void) const noexcept
Get the kind of dialog.
Definition dialog.cpp:30
static constexpr float Width
Width of the dialog panel, in dp.
Definition dialog.hpp:78
ecs::Entity::Identifier getTitleEntity(void) const noexcept
Get the child that draws the title.
Definition dialog.cpp:129
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 & setFixedWidth(float width)
Force the container width instead of wrapping the children.
Definition layout.cpp:112
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
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
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 & setDismissOnEscape(bool dismiss)
Choose whether the Escape key dismisses the overlay.
Definition overlay.cpp:80
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.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
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
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
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...
static void follow(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::PoseF &ownerPose, float ownerWidth, float ownerHeight)
Center a backdrop on its owner and place it half a layer behind it.
Definition backdrop.cpp:76
static void setVisible(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool visible)
Show or hide a backdrop.
Definition backdrop.cpp:55
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
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one dialog; called by the frame loop.
Definition dialog.cpp:258
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the dialog and its backdrop.
Definition dialog.cpp:115
Dialog(void)
Create the system in the Layout phase.
Definition dialog.cpp:80
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
State. Every class here is plain data attached to an entity.
@ Header
Heading (app bar title, list subheader).
The entity-component-system core, independent of any widget.
Definition component.hpp:28
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 schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Level3
Third raised level.
Definition tokens.hpp:134
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
Definition tokens.hpp:48
@ None
0 dp: square corners.
Definition tokens.hpp:43
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.