guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
icon_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 <array>
24#include <cstddef>
25#include <memory>
26#include <optional>
27#include <stdexcept>
28#include <utility>
29
30#include "guillaume/entities/icon_button.hpp"
31#include "guillaume/entities/builder_base.hpp"
33#include "guillaume/theme.hpp"
34#include "guillaume/systems/icon_button.hpp"
35
36namespace guillaume::entities
37{
39 ecs::EntityRegistry &entityRegistry)
40 : EntityBuilderBase<IconButton>(componentRegistry, entityRegistry)
41 {
42 reset();
43 }
44
45 std::shared_ptr<IconButton> IconButton::Builder::buildEntity(void)
46 {
47 auto entity = IconButton::create(
48 this->getComponentRegistry(), _iconGlyphName,
49 _selectedIconGlyphName, _iconStyle, _isToggle, _variant, _size,
50 _isDisabled, _accessibilityLabel, _onClick);
51 return entity;
52 }
53
55 {
56 _iconGlyphName.clear();
57 _selectedIconGlyphName.clear();
58 _iconStyle = components::Glyph::Style::Outlined;
59 _isToggle = false;
60 _variant = Variant::Standard;
61 _size = Size::Medium;
62 _isDisabled = false;
63 _accessibilityLabel.clear();
64 _onClick = {};
65 }
66
68 IconButton::Builder::withIcon(const std::string &iconGlyphName)
69 {
70 _iconGlyphName = iconGlyphName;
71 return *this;
72 }
73
75 const std::string &selectedIconGlyphName)
76 {
77 _selectedIconGlyphName = selectedIconGlyphName;
78 return *this;
79 }
80
82 const components::Glyph::Style &iconStyle)
83 {
84 _iconStyle = iconStyle;
85 return *this;
86 }
87
89 IconButton::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
103 {
104 _variant = variant;
105 return *this;
106 }
107
109 {
110 _size = size;
111 return *this;
112 }
113
116 {
117 _isDisabled = isDisabled;
118 return *this;
119 }
120
122 const std::string &accessibilityLabel)
123 {
124 _accessibilityLabel = accessibilityLabel;
125 return *this;
126 }
127
128 std::shared_ptr<IconButton> IconButton::Builder::makeIconButton(
129 std::shared_ptr<Entity> parent, const std::string &iconGlyphName,
130 std::function<void(void)> onClick, Variant variant, Size size)
131 {
132 return withIcon(iconGlyphName)
133 .withOnClick(onClick)
134 .withVariant(variant)
135 .withSize(size)
136 .registerEntity(parent);
137 }
138
139 IconButton::IconButton(ecs::ComponentRegistry &registry,
140 const std::string &iconGlyphName,
141 const std::string &selectedIconGlyphName,
142 const components::Glyph::Style &iconStyle,
143 bool isToggle, Variant variant, Size size,
144 bool isDisabled,
145 const std::string &accessibilityLabel,
146 std::function<void(void)> onClick)
147 : ButtonBase(registry, iconGlyphName, iconStyle, "", isToggle, false,
148 isDisabled, false, accessibilityLabel, std::move(onClick))
149 {
152 .setVariant(variant)
153 .setSize(size);
157 setSelectedIconGlyphName(selectedIconGlyphName);
158 }
159
165
166 utility::graphic::Color32Bit IconButton::getInitialContentColor(void) const
167 {
169 getIdentifier());
170 }
171
172 IconButton &IconButton::setVariant(const Variant &variant)
173 {
176 .setVariant(variant);
177 restyle();
178 return *this;
179 }
180
181 IconButton &IconButton::setSize(const Size &size)
182 {
185 .setSize(size);
186 restyle();
187 return *this;
188 }
189
196
203
204} // namespace guillaume::entities
Style
Family of the Material Symbols font the icon belongs to.
Definition glyph.hpp:66
Look of a Material 3 icon button: color variant and size.
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.
ButtonBase & setSelectedIconGlyphName(const std::string &selectedIconGlyphName)
Set the icon shown instead of the regular one while the button is selected.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures, creates and registers IconButton widgets.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Builder & withSelectedIcon(const std::string &selectedIconGlyphName)
Set the icon shown while a toggle button is selected.
std::shared_ptr< IconButton > makeIconButton(std::shared_ptr< Entity > parent, const std::string &iconGlyphName, std::function< void(void)> onClick, Variant variant, Size size)
Build an icon button, register it under parent and reset the builder.
Builder & withOnClick(std::function< void(void)> onClick)
Set the click handler.
Builder & withIconStyle(const components::Glyph::Style &iconStyle)
Set the glyph style of the icon.
std::shared_ptr< IconButton > buildEntity(void) override
Build an IconButton, with its icon child, from the current configuration, without registering it.
Builder & withToggle(const bool &isToggle)
Make the button a toggle: each click flips its selected state.
Builder & withDisabled(const bool &isDisabled)
Choose whether the button starts disabled.
Builder & withIcon(const std::string &iconGlyphName)
Set the icon.
void reset(void) override
Restore the default configuration.
Builder & withAccessibilityLabel(const std::string &accessibilityLabel)
Set the label read by assistive technology.
Builder & withVariant(const Variant &variant)
Set the color variant.
Builder & withSize(const Size &size)
Set the size.
IconButton & setSize(const Size &size)
Set the size, which decides the container and icon sizes. Restyles at once.
Variant getVariant(void) const
Get the color variant.
components::IconButton::Size Size
Size: Small, Medium or Large (see components::IconButton::Size).
utility::graphic::Color32Bit getInitialContentColor(void) const override
Get the color the icon child is created with.
IconButton & setVariant(const Variant &variant)
Set the color variant. Restyles at once.
float getIconFontSize(void) const override
Get the font size of the icon child for the current size.
Size getSize(void) const
Get the size.
components::IconButton::Variant Variant
static std::shared_ptr< IconButton > create(Arguments &&...arguments)
Create an icon button and its icon child, without registering it in an entity registry.
static void install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity behave as an icon button.
static float iconFontSize(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the font size of the icon for the configured size.
static utility::graphic::Color32Bit contentColor(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the color of the icon of the button when it is enabled.
Makes the color helpers callable from guillaume::entities without qualification.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46