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 <algorithm>
24#include <cctype>
25#include <memory>
26#include <utility>
27
28#include "guillaume/entities/button.hpp"
30#include "guillaume/components/elevation.hpp"
31#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/theme.hpp"
33#include "guillaume/systems/common_button.hpp"
34
35namespace guillaume::entities
36{
38 ecs::EntityRegistry &entityRegistry)
39 : EntityBuilderBase<Button>(componentRegistry, entityRegistry)
40 {
41 reset();
42 }
43
44 std::shared_ptr<Button> Button::Builder::buildEntity(void)
45 {
46 auto entity = Button::create(
47 this->getComponentRegistry(), _iconGlyphName, _iconStyle,
48 _labelContent, _isToggle, _colorStyle, _shape, _size, _isMorph,
49 _isDisabled, _isTrailingIcon, _accessibilityLabel, _onClick);
50 return entity;
51 }
52
54 {
55 _iconGlyphName.clear();
56 _iconStyle = components::Glyph::Style::Outlined;
57 _labelContent.clear();
58 _isToggle = false;
59 _colorStyle = Color::Text;
60 _shape = Shape::Round;
61 _size = Size::Small;
62 _isMorph = false;
63 _isDisabled = false;
64 _isTrailingIcon = false;
65 _accessibilityLabel.clear();
66 _onClick = {};
67 }
68
69 Button::Builder &Button::Builder::withIcon(const std::string &iconGlyphName)
70 {
71 _iconGlyphName = iconGlyphName;
72 return *this;
73 }
74
76 const components::Glyph::Style &iconStyle)
77 {
78 _iconStyle = iconStyle;
79 return *this;
80 }
81
82 Button::Builder &Button::Builder::withLabel(const std::string &labelContent)
83 {
84 _labelContent = labelContent;
85 return *this;
86 }
87
89 Button::Builder::withOnClick(std::function<void(void)> onClick)
90 {
91 _onClick = std::move(onClick);
92 return *this;
93 }
94
96 {
97 _isToggle = isToggle;
98 return *this;
99 }
100
102 {
103 _colorStyle = colorStyle;
104 return *this;
105 }
106
108 {
109 _shape = shape;
110 return *this;
111 }
112
114 {
115 _size = size;
116 return *this;
117 }
118
120 {
121 _isMorph = isMorph;
122 return *this;
123 }
124
126 {
127 _isDisabled = isDisabled;
128 return *this;
129 }
130
132 Button::Builder::withTrailingIcon(const bool &isTrailingIcon)
133 {
134 _isTrailingIcon = isTrailingIcon;
135 return *this;
136 }
137
139 const std::string &accessibilityLabel)
140 {
141 _accessibilityLabel = accessibilityLabel;
142 return *this;
143 }
144
145 std::shared_ptr<Button> Button::Builder::makeButton(
146 std::shared_ptr<Entity> parent, const std::string &labelContent,
147 std::function<void(void)> onClick, Color colorStyle, Shape shape,
148 Size size, bool isMorph)
149 {
150 return withLabel(labelContent)
151 .withOnClick(onClick)
152 .withColorStyle(colorStyle)
153 .withShape(shape)
154 .withSize(size)
155 .withMorph(isMorph)
156 .registerEntity(parent);
157 }
158
159 std::shared_ptr<Button> Button::Builder::makeIconButton(
160 std::shared_ptr<Entity> parent, const std::string &labelContent,
161 const std::string &iconGlyphName,
162 const components::Glyph::Style &iconStyle,
163 std::function<void(void)> onClick, Color colorStyle, Shape shape,
164 Size size, bool isMorph)
165 {
166 return withIcon(iconGlyphName)
167 .withIconStyle(iconStyle)
168 .withLabel(labelContent)
169 .withOnClick(onClick)
170 .withColorStyle(colorStyle)
171 .withShape(shape)
172 .withSize(size)
173 .withMorph(isMorph)
174 .registerEntity(parent);
175 }
176
177 Button::Button(ecs::ComponentRegistry &registry,
178 const std::string &iconGlyphName,
179 const components::Glyph::Style &iconStyle,
180 const std::string &labelContent, bool isToggle,
181 Color colorStyle, Shape shape, Size size, bool isMorph,
182 bool isDisabled, bool isTrailingIcon,
183 const std::string &accessibilityLabel,
184 std::function<void(void)> onClick)
185 : ButtonBase(registry, iconGlyphName, iconStyle, labelContent, isToggle,
186 isMorph, isDisabled, isTrailingIcon, accessibilityLabel,
187 std::move(onClick))
188 {
191 .setColorStyle(colorStyle)
192 .setShape(shape)
193 .setSize(size);
197 }
198
204
205 utility::graphic::Color32Bit Button::getInitialContentColor(void) const
206 {
208 getIdentifier());
209 }
210
211 Button &Button::setMinWidth(float minWidth)
212 {
215 .setMinWidth(minWidth);
216 restyle();
217 return *this;
218 }
219
220 Button &Button::setCompact(bool compact)
221 {
224 .setCompact(compact);
225 restyle();
226 return *this;
227 }
228
229 Button &Button::setUppercase(bool uppercase)
230 {
233 .setUppercase(uppercase);
234 restyle();
235 return *this;
236 }
237
238 Button &Button::setColorStyle(const Color &colorStyle)
239 {
242 .setColorStyle(colorStyle);
243 restyle();
244 return *this;
245 }
246
247 Button &Button::setShape(const Shape &shape)
248 {
251 .setShape(shape);
252 restyle();
253 return *this;
254 }
255
256 Button &Button::setSize(const Size &size)
257 {
260 .setSize(size);
261 restyle();
262 return *this;
263 }
264
265 bool Button::isCompact(void) const
266 {
267 return getComponentRegistry()
269 .isCompact();
270 }
271
278
285
292
299
300} // namespace guillaume::entities
Look of a Material 3 common button: color style, corner shape, size and padding options.
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...
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.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
Entity::Signature getSignature(void) const noexcept
Get the set of component types the entity declares.
Definition entity.cpp:99
static Signature getSignatureFromTypes(void)
Build the signature that has exactly the bits of the given component types.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
void setSignature(void)
Replace the signature by the one made of the given component types.
bool isDisabled(void) const
Tell whether the button is disabled.
ButtonBase(ecs::ComponentRegistry &registry, const std::string &iconGlyphName, const components::Glyph::Style &iconStyle, const std::string &labelContent, bool isToggle, bool isMorph, bool isDisabled, bool isTrailingIcon, const std::string &accessibilityLabel, std::function< void(void)> onClick)
Create the entity with its components and store the initial state. The children are created later,...
void restyle(void)
Recompute colors and geometry now.
Configures, creates and registers Button widgets.
Definition button.hpp:97
Builder & withToggle(const bool &isToggle)
Make the button a toggle: each click flips its selected state.
Definition button.cpp:95
Builder & withColorStyle(const Color &colorStyle)
Set the color style.
Definition button.cpp:101
Builder & withDisabled(const bool &isDisabled)
Choose whether the button starts disabled.
Definition button.cpp:125
std::shared_ptr< Button > buildEntity(void) override
Build a Button, with its icon and label children, from the current configuration, without registering...
Definition button.cpp:44
Builder & withLabel(const std::string &labelContent)
Set the label text.
Definition button.cpp:82
Builder & withTrailingIcon(const bool &isTrailingIcon)
Choose the side of the icon.
Definition button.cpp:132
Builder & withIcon(const std::string &iconGlyphName)
Set the icon shown next to the label.
Definition button.cpp:69
Builder & withOnClick(std::function< void(void)> onClick)
Set the click handler.
Definition button.cpp:89
std::shared_ptr< Button > makeIconButton(std::shared_ptr< Entity > parent, const std::string &labelContent, const std::string &iconGlyphName, const components::Glyph::Style &iconStyle, std::function< void(void)> onClick, Color colorStyle, Shape shape, Size size, bool isMorph)
Build a button with an icon and a label, register it under parent and reset the builder.
Definition button.cpp:159
Builder & withSize(const Size &size)
Set the size.
Definition button.cpp:113
void reset(void) override
Restore the default configuration.
Definition button.cpp:53
Builder & withShape(const Shape &shape)
Set the corner shape.
Definition button.cpp:107
Builder & withIconStyle(const components::Glyph::Style &iconStyle)
Set the glyph style of the icon.
Definition button.cpp:75
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Definition button.cpp:37
Builder & withAccessibilityLabel(const std::string &accessibilityLabel)
Set the label read by assistive technology.
Definition button.cpp:138
std::shared_ptr< Button > makeButton(std::shared_ptr< Entity > parent, const std::string &labelContent, std::function< void(void)> onClick, Color colorStyle, Shape shape, Size size, bool isMorph)
Build a button with a label and no icon, register it under parent and reset the builder.
Definition button.cpp:145
Builder & withMorph(const bool &isMorph)
Choose whether a selected toggle button swaps its shape (Round becomes Square and Square becomes Roun...
Definition button.cpp:119
components::CommonButton::Color Color
Definition button.hpp:80
Button & setShape(const Shape &shape)
Set the corner shape. Restyles at once.
Definition button.cpp:247
Button & setMinWidth(float minWidth)
Raise the minimum container width above MinWidth. Restyles at once.
Definition button.cpp:211
Shape getShape(void) const
Get the corner shape.
Definition button.cpp:286
Button & setUppercase(bool uppercase)
Render the label in upper case (Paper uppercase). Restyles at once.
Definition button.cpp:229
bool isCompact(void) const
Check whether the button uses the compact horizontal padding.
Definition button.cpp:265
Button & setCompact(bool compact)
Use the compact horizontal padding (Paper compact). Restyles at once.
Definition button.cpp:220
Button & setSize(const Size &size)
Set the size, which decides the paddings, the icon size and the corner radius. Restyles at once.
Definition button.cpp:256
static std::shared_ptr< Button > create(Arguments &&...arguments)
Create a button and its icon and label children, without registering it in an entity registry.
Definition button.hpp:334
bool isUppercase(void) const
Check whether the label is displayed in upper case.
Definition button.cpp:272
Size getSize(void) const
Get the size.
Definition button.cpp:293
Button & setColorStyle(const Color &colorStyle)
Set the color style. Restyles at once.
Definition button.cpp:238
components::CommonButton::Size Size
Definition button.hpp:88
components::CommonButton::Shape Shape
Definition button.hpp:84
float getIconFontSize(void) const override
Get the font size of the icon child for the current size.
Definition button.cpp:199
Color getColorStyle(void) const
Get the color style.
Definition button.cpp:279
utility::graphic::Color32Bit getInitialContentColor(void) const override
Get the color the icon and label children are created with.
Definition button.cpp:205
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
static utility::graphic::Color32Bit contentColor(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the color of the icon and label of the button when it is enabled.
static float iconFontSize(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the font size of the icon for the configured size.
static void install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity behave as a common button.
Makes the Button style tables reachable as guillaume::entities::button_style.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46