guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
common_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/common_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 {
51 } // namespace
52
58
60 const ecs::Entity::Identifier &identifier)
61 {
62 if (!registry.hasComponent<components::Elevation>(identifier)) {
63 registry.addComponent<components::Elevation>(identifier)
64 .setElevationLevel(theme::ElevationLevel::Level0);
65 }
66 Button::install(registry, identifier);
67 registry.getComponent<components::Button>(identifier)
68 .setRestyle(&CommonButton::apply);
69 }
70
72 const ecs::Entity::Identifier &identifier)
73 {
74 const auto &config =
75 registry.getComponent<components::CommonButton>(identifier);
76 return button_style::getIconFontSize(config.getSize());
77 }
78
79 utility::graphic::Color32Bit
81 const ecs::Entity::Identifier &identifier)
82 {
83 const auto &button =
84 registry.getComponent<components::Button>(identifier);
85 const auto &config =
86 registry.getComponent<components::CommonButton>(identifier);
88 config.getColorStyle(), button.isToggle(), button.isSelected());
89 }
90
92 const ecs::Entity::Identifier &identifier)
93 {
94 const auto &button =
95 registry.getComponent<components::Button>(identifier);
96 const auto &config =
97 registry.getComponent<components::CommonButton>(identifier);
98 const Button::Child icon(registry, button.getIcon());
99 const Button::Child label(registry, button.getLabel());
100 const bool disabled = Button::isDisabled(registry, identifier);
101 const bool hovered = Button::isHovered(registry, identifier);
102 const bool pressed = Button::isPressed(registry, identifier);
103 const bool toggle = button.isToggle();
104 const bool selected = button.isSelected();
105 const auto colorStyle = config.getColorStyle();
106 // M3 elevated buttons rest at level 1 and lift to level 2 on hover;
107 // every other style, and any disabled button, is flat.
109 if (colorStyle == Color::Elevated && !disabled) {
110 level = (hovered && !pressed) ? theme::ElevationLevel::Level2
112 }
113 registry.getComponent<components::Elevation>(identifier)
114 .setElevationLevel(level);
115
116 if (disabled) {
117 registry.getComponent<components::Color>(identifier)
119
120 registry.getComponent<components::Borders>(identifier)
122 .setWidth(1.0f);
123
124 if (icon) {
125 icon.setColor(style::disabledContentColor());
126 }
127
128 if (label) {
130 }
131
132 return;
133 }
134
135 registry.getComponent<components::Color>(identifier)
137 button,
138 button_style::getContainerColor(colorStyle, toggle, selected,
139 hovered, pressed)));
140
141 registry.getComponent<components::Borders>(identifier)
142 .setColor(
143 button_style::getBorderColor(colorStyle, toggle, selected))
144 .setWidth(1.0f);
145
146 if (icon) {
147 icon.setColor(Button::resolveContentColor(
148 button,
149 button_style::getContentColor(colorStyle, toggle, selected)));
150 }
151
152 if (label) {
154 button,
155 button_style::getContentColor(colorStyle, toggle, selected)));
156 }
157 }
158
160 const ecs::Entity::Identifier &identifier)
161 {
162 const auto &button =
163 registry.getComponent<components::Button>(identifier);
164 const auto &config =
165 registry.getComponent<components::CommonButton>(identifier);
166 const Button::Child icon(registry, button.getIcon());
167 const Button::Child label(registry, button.getLabel());
168 const bool pressed = Button::isPressed(registry, identifier);
169 const bool toggle = button.isToggle();
170 const bool selected = button.isSelected();
171 const bool morph = button.isMorph();
172 const bool trailingIcon = button.isTrailingIcon();
173 const auto layer = Layout::layerOf(registry, identifier);
174 const auto shape = config.getShape();
175 const auto size = config.getSize();
176 const bool compact = config.isCompact();
177 const float minWidth = config.getMinWidth();
178 const bool uppercase = config.isUppercase();
179 const float iconSize = iconFontSize(registry, identifier);
180 const float radius = button_style::getBorderRadius(
181 size, button_style::getRestingShape(shape, toggle, selected, morph),
182 pressed);
183
184 // A button joined to its neighbours keeps the corners its group gave
185 // it; writing the uniform radius first would flip Borders each frame.
186 auto &borders = registry.getComponent<components::Borders>(identifier);
187 if (const auto &corners = config.getCorners()) {
188 borders.setTopLeftRadius(corners->topLeft)
189 .setTopRightRadius(corners->topRight)
190 .setBottomRightRadius(corners->bottomRight)
191 .setBottomLeftRadius(corners->bottomLeft);
192 } else {
193 borders.setBorderRadius(radius);
194 }
195
196 if (!icon && !label) {
197 return;
198 }
199
200 const float fontHeight = iconSize;
201
202 if (icon) {
203 icon.setFontSize(fontHeight);
204 }
205
206 if (label) {
208 std::string content = button.getLabelContent();
209 if (uppercase) {
210 std::transform(content.begin(), content.end(), content.begin(),
211 [](unsigned char character) {
212 return static_cast<char>(
213 std::toupper(character));
214 });
215 }
216 label.setContent(content);
217 }
218
219 const auto &buttonPose =
220 registry.getComponent<components::Transform>(identifier).getPose();
221
222 const bool hasIcon = static_cast<bool>(icon);
223 const bool hasLabel = static_cast<bool>(label);
224
225 const float iconWidth = hasIcon
226 ? registry.getComponent<components::Bound>(icon.getIdentifier())
227 .getWidth()
228 : 0.0f;
229 const float iconHeight = hasIcon
230 ? registry.getComponent<components::Bound>(icon.getIdentifier())
231 .getHeight()
232 : 0.0f;
233 const float labelWidth = hasLabel
234 ? registry.getComponent<components::Bound>(label.getIdentifier())
235 .getWidth()
236 : 0.0f;
237 const float labelHeight = hasLabel
238 ? registry.getComponent<components::Bound>(label.getIdentifier())
239 .getHeight()
240 : 0.0f;
241
242 const float gap = (!button.getIconGlyphName().empty()
243 && !button.getLabelContent().empty())
245 : 0.0f;
246 const float contentWidth = iconWidth + gap + labelWidth;
247 const float contentHeight = std::max(iconHeight, labelHeight);
248
249 // The label typography (labelLarge) is independent of the icon size;
250 // the container grows to the larger child and to the M3 minimums.
251 const float minHeight = (size == Size::ExtraSmall)
252 ? 0.0f
254 const float buttonWidth = std::max(
255 { contentWidth
256 + button_style::getWidthPadding(size, compact) * 2.0f,
258 const float buttonHeight = std::max(
259 contentHeight + button_style::getHeightPadding(size) * 2.0f,
260 minHeight);
261
262 const auto place = [&](const Button::Child &child, float x,
263 float childHeight) {
264 const float y = (buttonHeight - childHeight) / 2.0f;
267 utility::graphic::PositionF(buttonPose.getPosition().x + x,
268 buttonPose.getPosition().y - y,
269 buttonPose.getPosition().z),
270 buttonPose.getOrientation(), layer));
271 };
272
273 float cursor = (buttonWidth - contentWidth) / 2.0f;
274 if (trailingIcon) {
275 if (hasLabel) {
276 place(label, cursor, labelHeight);
277 cursor += labelWidth + gap;
278 }
279 if (hasIcon) {
280 place(icon, cursor, iconHeight);
281 }
282 } else {
283 if (hasIcon) {
284 place(icon, cursor, iconHeight);
285 cursor += iconWidth + gap;
286 }
287 if (hasLabel) {
288 place(label, cursor, labelHeight);
289 }
290 }
291
292 registry.getComponent<components::Bound>(identifier)
293 .setWidth(buttonWidth)
294 .setHeight(buttonHeight);
295 Button::applyTouchTarget(registry, identifier, buttonWidth,
296 buttonHeight);
297 }
298
300 const ecs::Entity::Identifier &identifier)
301 {
302 styleState(registry, identifier);
303 geometry(registry, identifier);
304 }
305
306 void CommonButton::update(const ecs::Entity::Identifier &entityIdentifier)
307 {
308 if (!requireComponent<components::CommonButton>(entityIdentifier)
309 || !requireComponent<components::Button>(entityIdentifier)) {
310 return;
311 }
312
313 apply(getComponentRegistry(), entityIdentifier);
314 }
315} // 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
Look of a Material 3 common button: color style, corner shape, size and padding options.
Color
Which theme colors fill the container, the content and the outline.
static constexpr float MinWidth
Minimum width of the container, in dp.
Size
Size step. It sets the padding, the icon size, the gap between icon and label and the corner radius; ...
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
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
const Child & setContent(const std::string &content) const
Write the string of the child's components::Text.
Definition button.cpp:92
ecs::Entity::Identifier getIdentifier(void) const noexcept
Get the entity this handle points to.
Definition button.cpp:55
const Child & setColor(const utility::graphic::Color32Bit &color) const
Write the color of the child's components::Color.
Definition button.cpp:61
const Child & setTypeRole(theme::TypeRole role) const
Give the label a role of the type scale and resolve it at once.
Definition button.cpp:79
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
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 void geometry(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Second half of apply: write the corner radius, the size of the button and the position of its icon an...
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.
CommonButton(void)
Create the system for the Layout phase, matching entities that have both a components::CommonButton a...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one button.
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Style and lay out one button at once: styleState, then geometry.
static void styleState(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
First half of apply: write the colors and the elevation.
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.
float getWidthPadding(components::CommonButton::Size size, bool compact=false)
Space between the content and the left edge of the container (and, equally, the right edge).
float getHeightPadding(components::CommonButton::Size size)
Space between the content and the top edge of the container (and, equally, the bottom edge).
float getSpaceBetweenIconAndLabel(components::CommonButton::Size size)
Horizontal gap between the icon and the label.
utility::graphic::Color32Bit getContainerColor(components::CommonButton::Color style, bool isToggle, bool isSelected, bool isHovered, bool isButtonPressed)
Fill color of the container, hover or press tint included.
utility::graphic::Color32Bit getContentColor(components::CommonButton::Color style, bool isToggle, bool isSelected)
Color of the icon and of the label of an enabled button.
components::CommonButton::Shape getRestingShape(components::CommonButton::Shape baseShape, bool isToggle, bool isSelected, bool isMorph)
Shape a button shows while it is not pressed.
float getIconFontSize(components::CommonButton::Size size)
Font size of the leading icon of a button.
utility::graphic::Color32Bit disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
Rules. Every class here runs each frame on the entities that have the components it declares.
@ Level2
Second raised level.
Definition tokens.hpp:133
@ Level1
Lowest raised level.
Definition tokens.hpp:132
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
@ LabelLarge
14 / 20, medium weight.
Definition typescale.hpp:57
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.