guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
button.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 <chrono>
26#include <functional>
27#include <optional>
28#include <string>
29
30#include <utility/graphic/color.hpp>
31
32#include "guillaume/components/glyph.hpp"
33#include "guillaume/ecs/component_registry.hpp"
34
35#include "guillaume/ecs/component.hpp"
36#include "guillaume/ecs/entity.hpp"
37
39{
40
76 class Button: public ecs::Component
77 {
78 public:
80 using Handler = std::function<void(void)>;
81
83 using TimePoint = std::chrono::steady_clock::time_point;
84
86 using OptionalColor = std::optional<utility::graphic::Color32Bit>;
87
93
94 private:
95 std::string _iconGlyphName {};
96 Glyph::Style _iconStyle { Glyph::Style::Outlined };
97 std::string
98 _selectedIconGlyphName {};
99 std::string _labelContent {};
102 };
103
106 };
107
108 bool _toggle { false };
109 bool _selected { false };
110 bool _morph { false };
111 bool _trailingIcon { false };
112 bool _loading {
113 false
114 };
115
116 bool _visible { true };
117 Handler _onClick {};
118 Handler
119 _onLongPress {};
120 TimePoint _pressStart {};
121 OptionalColor _containerColorOverride {};
122 OptionalColor _contentColorOverride {};
123 RestyleFunction _restyle {
124 nullptr
125 };
126
127 public:
132 Button(void) = default;
133
137 ~Button(void) = default;
138
144 const std::string &getIconGlyphName(void) const noexcept;
145
154 Button &setIconGlyphName(const std::string &name);
155
160 Glyph::Style getIconStyle(void) const noexcept;
161
169
175 const std::string &getSelectedIconGlyphName(void) const noexcept;
176
183 Button &setSelectedIconGlyphName(const std::string &name);
184
190 const std::string &getLabelContent(void) const noexcept;
191
199 Button &setLabelContent(const std::string &content);
200
206 ecs::Entity::Identifier getIcon(void) const noexcept;
207
218
224 ecs::Entity::Identifier getLabel(void) const noexcept;
225
233
238 bool isToggle(void) const noexcept;
239
246 Button &setToggle(bool toggle);
247
252 bool isSelected(void) const noexcept;
253
263 Button &setSelected(bool selected);
264
269 bool isMorph(void) const noexcept;
270
280 Button &setMorph(bool morph);
281
286 bool isTrailingIcon(void) const noexcept;
287
296 Button &setTrailingIcon(bool trailing);
297
302 bool isLoading(void) const noexcept;
303
313 Button &setLoading(bool loading);
314
319 bool isVisible(void) const noexcept;
320
330 Button &setVisible(bool visible);
331
336 const Handler &getOnClick(void) const noexcept;
337
346 Button &setOnClick(Handler handler);
347
352 const Handler &getOnLongPress(void) const noexcept;
353
365 Button &setOnLongPress(Handler handler);
366
372 TimePoint getPressStart(void) const noexcept;
373
383
388 const OptionalColor &getContainerColorOverride(void) const noexcept;
389
401
406 const OptionalColor &getContentColorOverride(void) const noexcept;
407
415
421 RestyleFunction getRestyle(void) const noexcept;
422
433 };
434
435} // namespace guillaume::components
Button & setLabelContent(const std::string &content)
Set the text of the label.
Definition button.cpp:76
Button & setIconGlyphName(const std::string &name)
Set the name of the icon glyph shown at rest.
Definition button.cpp:34
Button & setToggle(bool toggle)
Make the button a toggle or a plain button.
Definition button.cpp:118
Button(void)=default
Create the state of a plain button: no icon, no label, not a toggle, visible, no callback.
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
Button & setContentColorOverride(const OptionalColor &color)
Replace the themed content (icon and label) color.
Definition button.cpp:254
RestyleFunction getRestyle(void) const noexcept
Get the function that restyles this button at once.
Definition button.cpp:263
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
Glyph::Style getIconStyle(void) const noexcept
Get the style the icon glyph is drawn in.
Definition button.cpp:43
Button & setVisible(bool visible)
Show or hide the button.
Definition button.cpp:188
bool isVisible(void) const noexcept
Check whether the button is shown.
Definition button.cpp:183
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 & setOnLongPress(Handler handler)
Set the callback run when the button is released after a long press.
Definition button.cpp:213
ecs::Entity::Identifier getIcon(void) const noexcept
Get the child entity that draws the icon.
Definition button.cpp:85
Button & setContainerColorOverride(const OptionalColor &color)
Replace the themed container (background) color.
Definition button.cpp:239
Button & setMorph(bool morph)
Choose whether the corner shape changes when selected.
Definition button.cpp:146
const std::string & getLabelContent(void) const noexcept
Get the text of the label.
Definition button.cpp:71
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
bool isToggle(void) const noexcept
Check whether the button is a toggle.
Definition button.cpp:113
TimePoint getPressStart(void) const noexcept
Get when the current press started.
Definition button.cpp:219
Button & setSelectedIconGlyphName(const std::string &name)
Set the name of the icon glyph shown while selected.
Definition button.cpp:62
bool isTrailingIcon(void) const noexcept
Check on which side of the label the icon sits.
Definition button.cpp:155
const OptionalColor & getContentColorOverride(void) const noexcept
Get the color that replaces the themed content color.
Definition button.cpp:249
Button & setLabel(ecs::Entity::Identifier identifier)
Record which child entity draws the label.
Definition button.cpp:104
Button & setOnClick(Handler handler)
Set the callback run when the button is released.
Definition button.cpp:202
Button & setRestyle(RestyleFunction function)
Register the function that restyles this button at once.
Definition button.cpp:268
Button & setLoading(bool loading)
Show a progress glyph in place of the icon, or the icon again.
Definition button.cpp:174
Button & setIconStyle(Glyph::Style style)
Set the style the icon glyph is drawn in.
Definition button.cpp:48
std::function< void(void)> Handler
Callback run when the button is clicked or long-pressed.
Definition button.hpp:80
Button & setIcon(ecs::Entity::Identifier identifier)
Record which child entity draws the icon.
Definition button.cpp:90
const Handler & getOnClick(void) const noexcept
Get the click callback.
Definition button.cpp:197
ecs::Entity::Identifier getLabel(void) const noexcept
Get the child entity that draws the label.
Definition button.cpp:99
Button & setTrailingIcon(bool trailing)
Choose on which side of the label the icon sits.
Definition button.cpp:160
bool isMorph(void) const noexcept
Check whether the corner shape changes when selected.
Definition button.cpp:141
std::optional< utility::graphic::Color32Bit > OptionalColor
A color that replaces a themed one, or nothing to keep the theme.
Definition button.hpp:86
~Button(void)=default
Default destructor.
void(*)(ecs::ComponentRegistry &, const ecs::Entity::Identifier &) RestyleFunction
Definition button.hpp:91
const Handler & getOnLongPress(void) const noexcept
Get the long-press callback.
Definition button.cpp:208
Style
Family of the Material Symbols font the icon belongs to.
Definition glyph.hpp:66
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Base class of every component: a piece of state attached to an entity, plus a "changed" flag.
Definition component.hpp:72
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
State. Every class here is plain data attached to an entity.