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 "guillaume/systems/icon_button.hpp"
24
25#include <algorithm>
26#include <array>
27#include <cctype>
28#include <cstddef>
29#include <optional>
30#include <stdexcept>
31#include <string>
32
33#include "guillaume/components/borders.hpp"
34#include "guillaume/components/bound.hpp"
35#include "guillaume/components/color.hpp"
36#include "guillaume/components/elevation.hpp"
37#include "guillaume/components/transform.hpp"
41#include "guillaume/systems/button.hpp"
42#include "guillaume/systems/layout.hpp"
43#include "guillaume/theme.hpp"
44
45namespace guillaume::systems
46{
47 namespace
48 {
49 using Variant = components::IconButton::Variant;
50 using Role = SchemeColorRole;
51
53 struct SizeMetrics {
54 float containerSize;
55 float iconFontSize;
56 };
57
59 constexpr std::array<SizeMetrics, 3> sizeTable { {
60 { 32.0f, 18.0f }, // Small
61 { 40.0f, 24.0f }, // Medium
62 { 48.0f, 30.0f }, // Large
63 } };
64
67 struct Palette {
68 std::optional<Role> activeContainer;
69 std::optional<Role> idleContainer;
70 Role activeContent;
71 Role idleContent;
74 bool plainIsActive;
75 };
76
78 constexpr std::array<Palette, 4> paletteTable { {
79 // Standard
80 { std::nullopt, std::nullopt, Role::Primary, Role::OnSurfaceVariant,
81 false },
82 // Filled
83 { Role::Primary, Role::SurfaceVariant, Role::OnPrimary,
84 Role::Primary, true },
85 // Tonal
86 { Role::SecondaryContainer, Role::SurfaceVariant,
87 Role::OnSecondaryContainer, Role::OnSurfaceVariant, true },
88 // Outlined
89 { Role::InverseSurface, std::nullopt, Role::InverseOnSurface,
90 Role::OnSurfaceVariant, false },
91 } };
92
93 const SizeMetrics &metrics(components::IconButton::Size size)
94 {
95 const auto index = static_cast<std::size_t>(size);
96 if (index >= sizeTable.size()) {
97 throw std::runtime_error("Invalid icon button size");
98 }
99 return sizeTable[index];
100 }
101
102 const Palette &palette(components::IconButton::Variant variant)
103 {
104 const auto index = static_cast<std::size_t>(variant);
105 if (index >= paletteTable.size()) {
106 throw std::runtime_error("Invalid icon button variant");
107 }
108 return paletteTable[index];
109 }
110
111 bool isActive(const Palette &entry, bool isToggle, bool isSelected)
112 {
113 return isSelected || (entry.plainIsActive && !isToggle);
114 }
115
117 {
118 return metrics(size).containerSize;
119 }
120
122 {
123 return metrics(size).iconFontSize;
124 }
125
127 {
128 // Paper/M3 icon buttons are circular at every size.
129 static_cast<void>(metrics(size));
131 }
132
133 static utility::graphic::Color32Bit
134 getContainerColorFor(components::IconButton::Variant variant,
135 bool isToggle, bool isSelected)
136 {
137 const Palette &entry = palette(variant);
138 const auto role = isActive(entry, isToggle, isSelected)
139 ? entry.activeContainer
140 : entry.idleContainer;
141
142 return role ? getActiveColor(*role)
143 : utility::graphic::Color32Bit(0, 0, 0, 0);
144 }
145
146 static utility::graphic::Color32Bit
148 bool isToggle, bool isSelected)
149 {
150 const Palette &entry = palette(variant);
151
152 return getActiveColor(isActive(entry, isToggle, isSelected)
153 ? entry.activeContent
154 : entry.idleContent);
155 }
156
161 static utility::graphic::Color32Bit
162 stateLayerOver(const utility::graphic::Color32Bit &base,
163 const utility::graphic::Color32Bit &content,
164 theme::StateLayer layer)
165 {
166 if (base.getAlpha() == 0) {
167 return style::withAlpha(content, getStateLayerAlpha(layer));
168 }
169 return style::stateLayer(base, content, layer);
170 }
171
172 static utility::graphic::Color32Bit
174 bool isSelected)
175 {
176 const auto &scheme = guillaume::getActiveScheme();
177
178 if (variant != components::IconButton::Variant::Outlined) {
179 return utility::graphic::Color32Bit(0, 0, 0, 0);
180 }
181
182 if (isSelected) {
183 return utility::graphic::Color32Bit(0, 0, 0, 0);
184 }
185
186 return scheme.getColor(SchemeColorRole::Outline).getColor();
187 }
188
189 } // namespace
190
196
198 const ecs::Entity::Identifier &identifier)
199 {
200 if (!registry.hasComponent<components::Elevation>(identifier)) {
201 registry.addComponent<components::Elevation>(identifier)
202 .setElevationLevel(theme::ElevationLevel::Level0);
203 }
204 Button::install(registry, identifier);
205 registry.getComponent<components::Button>(identifier)
206 .setRestyle(&IconButton::apply);
207 }
208
210 const ecs::Entity::Identifier &identifier)
211 {
212 const auto &config =
213 registry.getComponent<components::IconButton>(identifier);
214 return getIconFontSize(config.getSize());
215 }
216
217 utility::graphic::Color32Bit
219 const ecs::Entity::Identifier &identifier)
220 {
221 const auto &button =
222 registry.getComponent<components::Button>(identifier);
223 const auto &config =
224 registry.getComponent<components::IconButton>(identifier);
226 button,
227 getContentColor(config.getVariant(), button.isToggle(),
228 button.isSelected()));
229 }
230
232 const ecs::Entity::Identifier &identifier)
233 {
234 const auto &button =
235 registry.getComponent<components::Button>(identifier);
236 const auto &config =
237 registry.getComponent<components::IconButton>(identifier);
238 const Button::Child icon(registry, button.getIcon());
239 const bool disabled = Button::isDisabled(registry, identifier);
240 const bool hovered = Button::isHovered(registry, identifier);
241 const bool pressed = Button::isPressed(registry, identifier);
242 const bool toggle = button.isToggle();
243 const bool selected = button.isSelected();
244 const auto variant = config.getVariant();
245 if (disabled) {
246 registry.getComponent<components::Color>(identifier)
247 .setColor(
248 (variant == Variant::Filled || variant == Variant::Tonal)
251
252 registry.getComponent<components::Borders>(identifier)
253 .setColor(variant == Variant::Outlined
256 .setWidth(1.0f);
257
258 if (icon) {
259 icon.setColor(style::disabledContentColor());
260 }
261
262 return;
263 }
264
265 const auto baseContainerColor = Button::resolveContainerColor(
266 button, getContainerColorFor(variant, toggle, selected));
268 button, getContentColor(variant, toggle, selected));
269
270 utility::graphic::Color32Bit containerColor = baseContainerColor;
271
272 if (pressed) {
273 containerColor = stateLayerOver(baseContainerColor, contentColor,
275 } else if (hovered) {
276 containerColor = stateLayerOver(baseContainerColor, contentColor,
278 }
279
280 registry.getComponent<components::Color>(identifier)
281 .setColor(containerColor);
282
283 registry.getComponent<components::Borders>(identifier)
284 .setColor(getBorderColor(variant, selected))
285 .setWidth(1.0f);
286
287 if (icon) {
288 icon.setColor(contentColor);
289 }
290 }
291
293 const ecs::Entity::Identifier &identifier)
294 {
295 const auto &button =
296 registry.getComponent<components::Button>(identifier);
297 const auto &config =
298 registry.getComponent<components::IconButton>(identifier);
299 const Button::Child icon(registry, button.getIcon());
300 const auto layer = Layout::layerOf(registry, identifier);
301 const auto size = config.getSize();
302 const float iconSize = iconFontSize(registry, identifier);
303 const float containerSize = getContainerSize(size);
304
305 registry.getComponent<components::Bound>(identifier)
306 .setWidth(containerSize)
307 .setHeight(containerSize);
308 Button::applyTouchTarget(registry, identifier, containerSize,
309 containerSize);
310
311 registry.getComponent<components::Borders>(identifier)
312 .setBorderRadius(getBorderRadius(size));
313
314 if (icon) {
315 icon.setFontSize(iconSize);
316
317 const auto &buttonPose =
318 registry.getComponent<components::Transform>(identifier)
319 .getPose();
320 const auto &iconBound =
321 registry.getComponent<components::Bound>(icon.getIdentifier());
322
323 const float iconX = buttonPose.getPosition().x
324 + (containerSize - iconBound.getWidth()) / 2.0f;
325 const float iconY = buttonPose.getPosition().y
326 - (containerSize - iconBound.getHeight()) / 2.0f;
327
328 const auto iconPose = Layout::contentLayerPose(
329 utility::graphic::PositionF(iconX, iconY,
330 buttonPose.getPosition().z),
331 buttonPose.getOrientation(), layer);
332
333 registry.getComponent<components::Transform>(icon.getIdentifier())
334 .setPose(iconPose);
335 }
336 }
337
339 const ecs::Entity::Identifier &identifier)
340 {
341 styleState(registry, identifier);
342 geometry(registry, identifier);
343 }
344
345 void IconButton::update(const ecs::Entity::Identifier &entityIdentifier)
346 {
347 if (!requireComponent<components::IconButton>(entityIdentifier)
348 || !requireComponent<components::Button>(entityIdentifier)) {
349 return;
350 }
351
352 apply(getComponentRegistry(), entityIdentifier);
353 }
354} // namespace guillaume::systems
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Borders & setWidth(float width)
Set the width of the stroke painted by systems::RectangleRender.
Definition borders.cpp:122
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Bound & setHeight(float height) noexcept
Set the height.
Definition bound.cpp:42
State shared by every pressable button, whatever its look.
Definition button.hpp:77
bool isToggle(void) const noexcept
Check whether the button is a toggle.
Definition button.cpp:113
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
Look of a Material 3 icon button: color variant and size.
Variant
Which theme colors fill the container and the icon.
Size
Size step: a 32, 40 or 48 dp container around an 18, 24 or 30 dp icon.
Where an entity is in space: its pose (position and orientation) and its scale.
Definition transform.hpp:64
Transform & setPose(const utility::graphic::PoseF &pose)
Move and orient the entity.
Definition transform.cpp:27
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.
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
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
Input and content rules shared by every button variant: press, release, toggle, click and long press.
Definition button.hpp:75
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 bool isPressed(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Whether the button is being held down.
Definition button.cpp:157
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 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
IconButton(void)
Create the system for the Layout phase, matching entities that have both a components::IconButton and...
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.
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Style and lay out one button at once: styleState, then geometry.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one button.
static void geometry(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Second half of apply: write the size and the corner radius of the button and center its icon.
static void styleState(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
First half of apply: write the colors.
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static const utility::graphic::PoseF contentLayerPose(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation, const std::uint32_t &containerLayer)
Compute the pose of content placed by hand on a container.
Definition layout.cpp:118
static std::uint32_t layerOf(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Definition layout.cpp:90
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
float getBorderRadius(components::CommonButton::Size size, components::CommonButton::Shape shape, bool isButtonPressed)
Corner radius of the container.
utility::graphic::Color32Bit getBorderColor(components::CommonButton::Color style, bool isToggle, bool isSelected)
Color of the outline of an enabled button.
utility::graphic::Color32Bit getContentColor(components::CommonButton::Color style, bool isToggle, bool isSelected)
Color of the icon and of the label of an enabled button.
float getIconFontSize(components::CommonButton::Size size)
Font size of the leading icon of a button.
float getContainerSize(SizeType size)
Width and height of the container of a FAB at its default size: 40 dp for Small, 56 dp for Medium,...
utility::graphic::Color32Bit disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit stateLayer(const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &overlay, std::uint8_t alpha)
Blend a translucent tint over a base color.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit withAlpha(const utility::graphic::Color32Bit &color, std::uint8_t alpha)
Copy a color with another alpha channel.
Rules. Every class here runs each frame on the entities that have the components it declares.
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
StateLayer
The interaction states that change how a widget is drawn.
Definition tokens.hpp:223
@ Hovered
0.08 overlay of the content color.
Definition tokens.hpp:224
@ Pressed
0.12 overlay of the content color.
Definition tokens.hpp:226
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
std::uint8_t getStateLayerAlpha(theme::StateLayer layer)
Get the opacity the active theme gives to a state layer, as an alpha channel value.
SchemeColorRole
The named colors of a scheme: what a color is used for, not what it looks like.
Definition theme.hpp:58
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
utility::graphic::Color32Bit getActiveColor(SchemeColorRole role)
Get the color the active scheme gives to a role.
const Scheme & getActiveScheme(void)
Get the color scheme in use: the scheme of the active theme selected by setActiveScheme().
Style tables of the common button: paddings, icon size, corner radius and colors for each size,...
Style tables of the floating action buttons (FAB): container size, font sizes, corner radius and colo...
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.