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
23#include "guillaume/components/button.hpp"
24
25#include <utility>
26
28{
29 const std::string &Button::getIconGlyphName(void) const noexcept
30 {
31 return _iconGlyphName;
32 }
33
34 Button &Button::setIconGlyphName(const std::string &name)
35 {
36 if (_iconGlyphName != name) {
37 _iconGlyphName = name;
38 setHasChanged(true);
39 }
40 return *this;
41 }
42
44 {
45 return _iconStyle;
46 }
47
49 {
50 if (_iconStyle != style) {
51 _iconStyle = style;
52 setHasChanged(true);
53 }
54 return *this;
55 }
56
57 const std::string &Button::getSelectedIconGlyphName(void) const noexcept
58 {
59 return _selectedIconGlyphName;
60 }
61
62 Button &Button::setSelectedIconGlyphName(const std::string &name)
63 {
64 if (_selectedIconGlyphName != name) {
65 _selectedIconGlyphName = name;
66 setHasChanged(true);
67 }
68 return *this;
69 }
70
71 const std::string &Button::getLabelContent(void) const noexcept
72 {
73 return _labelContent;
74 }
75
76 Button &Button::setLabelContent(const std::string &content)
77 {
78 if (_labelContent != content) {
79 _labelContent = content;
80 setHasChanged(true);
81 }
82 return *this;
83 }
84
86 {
87 return _icon;
88 }
89
91 {
92 if (_icon != identifier) {
93 _icon = identifier;
94 setHasChanged(true);
95 }
96 return *this;
97 }
98
100 {
101 return _label;
102 }
103
105 {
106 if (_label != identifier) {
107 _label = identifier;
108 setHasChanged(true);
109 }
110 return *this;
111 }
112
113 bool Button::isToggle(void) const noexcept
114 {
115 return _toggle;
116 }
117
119 {
120 if (_toggle != toggle) {
121 _toggle = toggle;
122 setHasChanged(true);
123 }
124 return *this;
125 }
126
127 bool Button::isSelected(void) const noexcept
128 {
129 return _selected;
130 }
131
133 {
134 if (_selected != selected) {
135 _selected = selected;
136 setHasChanged(true);
137 }
138 return *this;
139 }
140
141 bool Button::isMorph(void) const noexcept
142 {
143 return _morph;
144 }
145
147 {
148 if (_morph != morph) {
149 _morph = morph;
150 setHasChanged(true);
151 }
152 return *this;
153 }
154
155 bool Button::isTrailingIcon(void) const noexcept
156 {
157 return _trailingIcon;
158 }
159
161 {
162 if (_trailingIcon != trailing) {
163 _trailingIcon = trailing;
164 setHasChanged(true);
165 }
166 return *this;
167 }
168
169 bool Button::isLoading(void) const noexcept
170 {
171 return _loading;
172 }
173
175 {
176 if (_loading != loading) {
177 _loading = loading;
178 setHasChanged(true);
179 }
180 return *this;
181 }
182
183 bool Button::isVisible(void) const noexcept
184 {
185 return _visible;
186 }
187
189 {
190 if (_visible != visible) {
191 _visible = visible;
192 setHasChanged(true);
193 }
194 return *this;
195 }
196
197 const Button::Handler &Button::getOnClick(void) const noexcept
198 {
199 return _onClick;
200 }
201
203 {
204 _onClick = std::move(handler);
205 return *this;
206 }
207
208 const Button::Handler &Button::getOnLongPress(void) const noexcept
209 {
210 return _onLongPress;
211 }
212
214 {
215 _onLongPress = std::move(handler);
216 return *this;
217 }
218
220 {
221 return _pressStart;
222 }
223
225 {
226 if (_pressStart != time) {
227 _pressStart = time;
228 setHasChanged(true);
229 }
230 return *this;
231 }
232
235 {
236 return _containerColorOverride;
237 }
238
240 {
241 if (_containerColorOverride != color) {
242 _containerColorOverride = color;
243 setHasChanged(true);
244 }
245 return *this;
246 }
247
250 {
251 return _contentColorOverride;
252 }
253
255 {
256 if (_contentColorOverride != color) {
257 _contentColorOverride = color;
258 setHasChanged(true);
259 }
260 return *this;
261 }
262
264 {
265 return _restyle;
266 }
267
269 {
270 if (_restyle != function) {
271 _restyle = function;
272 setHasChanged(true);
273 }
274 return *this;
275 }
276} // 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
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
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
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
State. Every class here is plain data attached to an entity.