guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
button.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#include "guillaume/systems/button.hpp"
23
24#include <cmath>
25
26#include "guillaume/components/accessibility.hpp"
27#include "guillaume/components/color.hpp"
28#include "guillaume/components/glyph.hpp"
29#include "guillaume/components/hand_button_interaction.hpp"
30#include "guillaume/components/hand_hover_interaction.hpp"
31#include "guillaume/components/mouse_button_interaction.hpp"
32#include "guillaume/components/mouse_hover_interaction.hpp"
33#include "guillaume/components/style_state.hpp"
34#include "guillaume/components/text.hpp"
35#include "guillaume/components/transform.hpp"
36#include "guillaume/components/typography.hpp"
37#include "guillaume/systems/typography.hpp"
38#include "guillaume/theme.hpp"
39
40namespace guillaume::systems
41{
43 ecs::Entity::Identifier identifier)
44 : _registry(registry)
45 , _identifier(identifier)
46 {
47 }
48
49 Button::Child::operator bool(void) const
50 {
51 return _identifier != ecs::Entity::InvalidIdentifier
52 && _registry.hasComponent<components::Transform>(_identifier);
53 }
54
56 {
57 return _identifier;
58 }
59
60 const Button::Child &
61 Button::Child::setColor(const utility::graphic::Color32Bit &color) const
62 {
63 if (_registry.hasComponent<components::Color>(_identifier)) {
64 _registry.getComponent<components::Color>(_identifier)
65 .setColor(color);
66 }
67 return *this;
68 }
69
71 {
72 if (_registry.hasComponent<components::Glyph>(_identifier)) {
73 _registry.getComponent<components::Glyph>(_identifier)
74 .setFontSize(size);
75 }
76 return *this;
77 }
78
80 {
81 if (_registry.hasComponent<components::Typography>(_identifier)
82 && _registry.hasComponent<components::Text>(_identifier)) {
83 _registry.getComponent<components::Typography>(_identifier)
84 .setRole(role)
85 .setFontSize(std::nullopt);
86 Typography::apply(_registry, _identifier);
87 }
88 return *this;
89 }
90
91 const Button::Child &
92 Button::Child::setContent(const std::string &content) const
93 {
94 if (_registry.hasComponent<components::Text>(_identifier)) {
95 _registry.getComponent<components::Text>(_identifier)
96 .setContent(content);
97 }
98 return *this;
99 }
100
102 : ecs::SystemFiller<components::Button>(ecs::Phase::Event)
103 {
104 }
105
107 const ecs::Entity::Identifier &identifier)
108 {
109 ecs::ComponentRegistry *storage = &registry;
110 const ecs::Entity::Identifier id = identifier;
111 const auto pressed = [storage, id]() {
112 press(*storage, id);
113 };
114 const auto released = [storage, id]() {
115 release(*storage, id);
116 };
117 const auto hovered = [storage, id]() {
118 hoverChanged(*storage, id);
119 };
120
122 .setOnButtonPressHandler(utility::event::HandButtonEvent::Button::A,
123 pressed)
124 .setOnButtonReleaseHandler(
125 utility::event::HandButtonEvent::Button::A, released);
127 .setOnButtonPressHandler(
128 utility::event::MouseButtonEvent::Button::Left, pressed)
129 .setOnButtonReleaseHandler(
130 utility::event::MouseButtonEvent::Button::Left, released);
132 .setOnHoverHandler(hovered)
133 .setOnUnhoverHandler(hovered);
135 .setOnHoverHandler(hovered)
136 .setOnUnhoverHandler(hovered);
137 }
138
140 const ecs::Entity::Identifier &identifier)
141 {
142 return !registry.getComponent<components::StyleState>(identifier)
143 .isEnabled();
144 }
145
147 const ecs::Entity::Identifier &identifier)
148 {
149 return registry
151 .isHovered()
152 || registry
154 .isHovered();
155 }
156
158 const ecs::Entity::Identifier &identifier)
159 {
160 return registry
162 .isButtonPressed(utility::event::HandButtonEvent::Button::A)
163 || registry
165 .isButtonPressed(
166 utility::event::MouseButtonEvent::Button::Left);
167 }
168
169 const std::string &
171 {
172 static const std::string loadingGlyph(LoadingGlyph);
173
174 if (button.isLoading()) {
175 return loadingGlyph;
176 }
177 if (button.isSelected() && !button.getSelectedIconGlyphName().empty()) {
178 return button.getSelectedIconGlyphName();
179 }
180 return button.getIconGlyphName();
181 }
182
183 utility::graphic::Color32Bit
185 const utility::graphic::Color32Bit &color)
186 {
187 return button.getContainerColorOverride().value_or(color);
188 }
189
190 utility::graphic::Color32Bit
192 const utility::graphic::Color32Bit &color)
193 {
194 return button.getContentColorOverride().value_or(color);
195 }
196
198 const ecs::Entity::Identifier &identifier,
199 float width, float height)
200 {
201 const utility::graphic::SizeUI margin(
202 static_cast<unsigned int>(
203 std::ceil(theme::SpacingScale::touchMargin(width))),
204 static_cast<unsigned int>(
205 std::ceil(theme::SpacingScale::touchMargin(height))));
206
208 .setAccessibilityMargin(margin);
210 .setAccessibilityMargin(margin);
211 }
212
214 const ecs::Entity::Identifier &identifier)
215 {
216 const auto &button =
217 registry.getComponent<components::Button>(identifier);
218
219 if (registry.hasComponent<components::Glyph>(button.getIcon())) {
220 registry.getComponent<components::Glyph>(button.getIcon())
222 .setStyle(button.getIconStyle());
223 }
224 registry.getComponent<components::Accessibility>(identifier)
225 .setSelected(button.isToggle() && button.isSelected());
226 }
227
229 const ecs::Entity::Identifier &identifier)
230 {
231 const auto function =
232 registry.getComponent<components::Button>(identifier).getRestyle();
233
234 if (function != nullptr) {
235 function(registry, identifier);
236 }
237 }
238
240 const ecs::Entity::Identifier &identifier,
241 bool selected)
242 {
243 auto &button = registry.getComponent<components::Button>(identifier);
244
245 if (!button.isToggle()) {
246 return;
247 }
248 button.setSelected(selected);
249 syncContent(registry, identifier);
250 restyle(registry, identifier);
251 }
252
254 const ecs::Entity::Identifier &identifier)
255 {
256 if (isDisabled(registry, identifier)) {
257 return;
258 }
259 restyle(registry, identifier);
260 }
261
263 const ecs::Entity::Identifier &identifier)
264 {
265 auto &button = registry.getComponent<components::Button>(identifier);
266
267 if (isDisabled(registry, identifier) || !button.isVisible()) {
268 return;
269 }
270 button.setPressStart(std::chrono::steady_clock::now());
271 restyle(registry, identifier);
272 }
273
275 const ecs::Entity::Identifier &identifier)
276 {
277 auto &button = registry.getComponent<components::Button>(identifier);
278
279 if (isDisabled(registry, identifier) || !button.isVisible()) {
280 return;
281 }
282 if (button.isToggle()) {
283 button.setSelected(!button.isSelected());
284 }
285 syncContent(registry, identifier);
286 restyle(registry, identifier);
287
288 // Copies: a callback may replace itself or the other one.
289 const auto onLongPress = button.getOnLongPress();
290 const auto onClick = button.getOnClick();
291 const bool longPress = onLongPress
292 && button.getPressStart() != components::Button::TimePoint {}
293 && std::chrono::steady_clock::now() - button.getPressStart()
295 button.setPressStart({});
296 if (longPress) {
297 onLongPress();
298 } else if (onClick) {
299 onClick();
300 }
301 }
302
303 void Button::update(const ecs::Entity::Identifier &entityIdentifier)
304 {
305 if (!requireComponent<components::Button>(entityIdentifier)) {
306 return;
307 }
308
309 syncContent(getComponentRegistry(), entityIdentifier);
310 }
311} // 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...
State shared by every pressable button, whatever its look.
Definition button.hpp:77
const OptionalColor & getContainerColorOverride(void) const noexcept
Get the color that replaces the themed container color.
Definition button.cpp:234
bool isLoading(void) const noexcept
Check whether the button shows a progress glyph.
Definition button.cpp:169
const std::string & getIconGlyphName(void) const noexcept
Get the name of the icon glyph shown at rest.
Definition button.cpp:29
Button & setPressStart(TimePoint time)
Record when the current press started.
Definition button.cpp:224
const std::string & getSelectedIconGlyphName(void) const noexcept
Get the name of the icon glyph shown while selected.
Definition button.cpp:57
std::chrono::steady_clock::time_point TimePoint
Point in time on the clock used to measure how long a press lasts.
Definition button.hpp:83
Button & setSelected(bool selected)
Store the selection.
Definition button.cpp:132
bool isSelected(void) const noexcept
Check whether the button is selected.
Definition button.cpp:127
const OptionalColor & getContentColorOverride(void) const noexcept
Get the color that replaces the themed content color.
Definition button.cpp:249
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
An icon: one glyph of the Material Symbols font, chosen by name, at a given size.
Definition glyph.hpp:61
Glyph & setName(const std::string &name)
Choose the icon by name.
Definition glyph.cpp:47
Glyph & setStyle(const Style &style)
Set the font family of the icon.
Definition glyph.cpp:77
Whether an XR hand (controller) aims at an entity, what to call when the aim enters or leaves,...
HandHoverInteraction & setOnUnhoverHandler(const UnhoverHandler &handler)
Store the callback run when the hand ray leaves the entity.
Whether the mouse pointer is over an entity, what to call when it enters or leaves,...
MouseHoverInteraction & setOnUnhoverHandler(const UnhoverHandler &handler)
Store the callback run when the pointer leaves the entity.
The interaction state of an entity in one place: enabled or disabled, and whether it is hovered,...
A run of text: its content, its font metrics and the position of the editing cursor.
Definition text.hpp:66
Where an entity is in space: its pose (position and orientation) and its scale.
Definition transform.hpp:64
Which typescale role a text uses (BodyMedium, TitleLarge, ...) and an optional explicit size.
Typography & setFontSize(std::optional< float > fontSize)
Override the font size of the role, or clear the override.
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...
Short-lived handle on the icon or label child of a button, used by the variant systems to style it.
Definition button.hpp:96
const Child & setContent(const std::string &content) const
Write the string of the child's components::Text.
Definition button.cpp:92
ecs::Entity::Identifier getIdentifier(void) const noexcept
Get the entity this handle points to.
Definition button.cpp:55
Child(ecs::ComponentRegistry &registry, ecs::Entity::Identifier identifier)
Create a handle on a child.
Definition button.cpp:42
const Child & setFontSize(float size) const
Write the font size of the child's components::Glyph.
Definition button.cpp:70
const Child & setColor(const utility::graphic::Color32Bit &color) const
Write the color of the child's components::Color.
Definition button.cpp:61
const Child & setTypeRole(theme::TypeRole role) const
Give the label a role of the type scale and resolve it at once.
Definition button.cpp:79
static void press(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
React to the button being pressed: record when the press started and restyle.
Definition button.cpp:262
static void hoverChanged(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
React to a pointer or a hand entering or leaving the button: restyle it so the hover tint appears or ...
Definition button.cpp:253
static bool isDisabled(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Whether the button ignores input and is drawn disabled.
Definition button.cpp:139
static void applyTouchTarget(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, float width, float height)
Make a small button as easy to hit as a large one, without drawing it larger.
Definition button.cpp:197
static bool isHovered(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Whether a pointer is over the button.
Definition button.cpp:146
static constexpr std::chrono::milliseconds LongPressDuration
Definition button.hpp:176
static bool isPressed(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Whether the button is being held down.
Definition button.cpp:157
static void release(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
React to the button being released: this is the click.
Definition button.cpp:274
static constexpr const char * LoadingGlyph
Definition button.hpp:172
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call syncContent on one button.
Definition button.cpp:303
static utility::graphic::Color32Bit resolveContentColor(const components::Button &button, const utility::graphic::Color32Bit &color)
Let a custom content color replace the themed one.
Definition button.cpp:191
static void setSelected(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, bool selected)
Select or unselect a toggle button from code and show it at once.
Definition button.cpp:239
static void restyle(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Recompute the colors and geometry of the button at once.
Definition button.cpp:228
static const std::string & activeIconGlyphName(const components::Button &button)
Choose the icon glyph a button shows in its current state.
Definition button.cpp:170
static void syncContent(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Copy what components::Button says onto the icon child and the accessibility state.
Definition button.cpp:213
Button(void)
Create the system for the Event phase, matching entities that have a components::Button.
Definition button.cpp:101
static void install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity react to input as a button.
Definition button.cpp:106
static utility::graphic::Color32Bit resolveContainerColor(const components::Button &button, const utility::graphic::Color32Bit &color)
Let a custom container color replace the themed one.
Definition button.cpp:184
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Resolve the metrics of one text and write them into its components::Text.
static constexpr float touchMargin(float extent)
Get the invisible margin that makes a small control as easy to hit as MinTouchTarget.
Definition tokens.hpp:453
State. Every class here is plain data attached to an entity.
ButtonInteraction< utility::event::MouseButtonEvent, utility::event::MouseButtonEvent::Button, MouseButtons.size(), MouseButtons > MouseButtonInteraction
Which mouse buttons are held on an entity, with a press and a release handler per button.
ButtonInteraction< utility::event::HandButtonEvent, utility::event::HandButtonEvent::Button, HandButtons.size(), HandButtons > HandButtonInteraction
Which XR controller buttons are held on an entity, with a press and a release handler per button.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.
TypeRole
The named text styles of Material Design 3: what a piece of text is, not how big it is.
Definition typescale.hpp:47