guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
floating_action_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/floating_action_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 {
50 } // namespace
51
57
58 void
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(&FloatingActionButton::apply);
69 }
70
72 const ecs::ComponentRegistry &registry,
73 const ecs::Entity::Identifier &identifier)
74 {
75 const auto &config =
77 return fab_style::getIconFontSize(config.getSize());
78 }
79
80 utility::graphic::Color32Bit FloatingActionButton::contentColor(
81 const ecs::ComponentRegistry &registry,
82 const ecs::Entity::Identifier &identifier)
83 {
84 const auto &button =
85 registry.getComponent<components::Button>(identifier);
86 const auto &config =
89 button, fab_style::getContentColor(config.getVariant()));
90 }
91
93 ecs::ComponentRegistry &registry,
94 const ecs::Entity::Identifier &identifier)
95 {
96 const auto &button =
97 registry.getComponent<components::Button>(identifier);
98 const auto &config =
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 visible = button.isVisible();
104 const auto mode = config.getMode();
105 const bool lowered = config.isLowered();
106 // M3 FAB: level 3 at rest (1 when lowered), one level up on hover,
107 // and flat when disabled.
108 auto level = lowered ? theme::ElevationLevel::Level1
110 if (disabled || !visible || mode == Mode::Flat) {
112 } else if (hovered && !pressed) {
113 level = static_cast<theme::ElevationLevel>(
114 static_cast<std::size_t>(level) + 1);
115 }
116 registry.getComponent<components::Elevation>(identifier)
117 .setElevationLevel(level);
118 }
119
121 ecs::ComponentRegistry &registry,
122 const ecs::Entity::Identifier &identifier)
123 {
124 const auto &button =
125 registry.getComponent<components::Button>(identifier);
126 const auto &config =
128 const Button::Child icon(registry, button.getIcon());
129 const Button::Child label(registry, button.getLabel());
130 const bool disabled = Button::isDisabled(registry, identifier);
131 const bool hovered = Button::isHovered(registry, identifier);
132 const bool pressed = Button::isPressed(registry, identifier);
133 const bool visible = button.isVisible();
134 const auto variant = config.getVariant();
135 const bool lowered = config.isLowered();
136 elevation(registry, identifier);
137
138 if (!visible) {
139 registry.getComponent<components::Color>(identifier)
140 .setColor(style::transparentColor());
141 if (icon) {
142 icon.setColor(style::transparentColor());
143 }
144 if (label) {
146 }
147 return;
148 }
149
150 if (disabled) {
151 registry.getComponent<components::Color>(identifier)
153
154 if (icon) {
155 icon.setColor(style::disabledContentColor());
156 }
157
158 if (label) {
160 }
161
162 return;
163 }
164
165 const auto baseContainerColor = Button::resolveContainerColor(
166 button, fab_style::getContainerColor(variant, lowered));
168 button, fab_style::getContentColor(variant));
169
170 utility::graphic::Color32Bit containerColor = baseContainerColor;
171
172 if (pressed) {
173 containerColor = style::stateLayer(baseContainerColor, contentColor,
175 } else if (hovered) {
176 containerColor = style::stateLayer(baseContainerColor, contentColor,
178 }
179
180 registry.getComponent<components::Color>(identifier)
181 .setColor(containerColor);
182
183 if (icon) {
184 icon.setColor(contentColor);
185 }
186
187 if (label) {
188 label.setColor(contentColor);
189 }
190 }
191
193 ecs::ComponentRegistry &registry,
194 const ecs::Entity::Identifier &identifier)
195 {
196 const auto &button =
197 registry.getComponent<components::Button>(identifier);
198 const auto &config =
200 const Button::Child icon(registry, button.getIcon());
201 const Button::Child label(registry, button.getLabel());
202 const auto layer = Layout::layerOf(registry, identifier);
203 const auto size = config.getSize();
204 const auto &customSize = config.getCustomSize();
205 const float iconSize = iconFontSize(registry, identifier);
206 const float containerSize =
207 customSize.value_or(fab_style::getContainerSize(size));
208 const float fontHeight = iconSize;
209
210 registry.getComponent<components::Bound>(identifier)
211 .setWidth(containerSize)
212 .setHeight(containerSize);
213 Button::applyTouchTarget(registry, identifier, containerSize,
214 containerSize);
215
216 registry.getComponent<components::Borders>(identifier)
217 .setBorderRadius(customSize.has_value()
218 // Paper: customSize / 3.5 (16 dp at 56 dp).
219 ? *customSize / 3.5f
221
222 if (icon) {
223 icon.setFontSize(fontHeight);
224 }
225
226 if (label) {
228 label.setContent(button.getLabelContent());
229 }
230
231 if (!icon && !label) {
232 return;
233 }
234
235 const auto &buttonPose =
236 registry.getComponent<components::Transform>(identifier).getPose();
237
238 const float iconWidth = icon
239 ? registry.getComponent<components::Bound>(icon.getIdentifier())
240 .getWidth()
241 : 0.0f;
242 const float iconHeight = icon
243 ? registry.getComponent<components::Bound>(icon.getIdentifier())
244 .getHeight()
245 : 0.0f;
246 const float labelWidth = label
247 ? registry.getComponent<components::Bound>(label.getIdentifier())
248 .getWidth()
249 : 0.0f;
250 const float labelHeight = label
251 ? registry.getComponent<components::Bound>(label.getIdentifier())
252 .getHeight()
253 : 0.0f;
254
255 const float gap = (icon && label) ? 8.0f : 0.0f;
256 const float contentWidth = iconWidth + gap + labelWidth;
257
258 // Content is centered in the container; when the row is wider than the
259 // container the bound grows so nothing is clipped.
260 const float width = std::max(containerSize, contentWidth);
261 const float startX =
262 buttonPose.getPosition().x + (width - contentWidth) / 2.0f;
263
264 if (icon) {
265 const float iconY = buttonPose.getPosition().y
266 - (containerSize - iconHeight) / 2.0f;
267
268 const auto iconPose = Layout::contentLayerPose(
269 utility::graphic::PositionF(startX, iconY,
270 buttonPose.getPosition().z),
271 buttonPose.getOrientation(), layer);
272
273 registry.getComponent<components::Transform>(icon.getIdentifier())
274 .setPose(iconPose);
275 }
276
277 if (label) {
278 const float labelX = startX + iconWidth + gap;
279 const float labelY = buttonPose.getPosition().y
280 - (containerSize - labelHeight) / 2.0f;
281
282 const auto labelPose = Layout::contentLayerPose(
283 utility::graphic::PositionF(labelX, labelY,
284 buttonPose.getPosition().z),
285 buttonPose.getOrientation(), layer);
286
288 .setPose(labelPose);
289 }
290
291 if (width > containerSize) {
292 registry.getComponent<components::Bound>(identifier)
293 .setWidth(width);
294 }
295 }
296
298 const ecs::Entity::Identifier &identifier)
299 {
300 styleState(registry, identifier);
301 geometry(registry, identifier);
302 }
303
305 const ecs::Entity::Identifier &entityIdentifier)
306 {
308 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
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
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 floating action button (FAB): color variant, size and shadow.
Mode
Whether the button casts a shadow (Elevated) or not (Flat).
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 void styleState(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
First half of apply: write the elevation and the colors.
FloatingActionButton(void)
Create the system for the Layout phase, matching entities that have both a components::FloatingAction...
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 and visible.
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 place its icon and label...
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Style and lay out one button at once: styleState, then geometry.
static void elevation(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Write the shadow level of the button into its components::Elevation.
static float iconFontSize(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the font size of the icon for the configured size.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one button.
static void install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity behave as a floating action button.
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
utility::graphic::Color32Bit getContentColor(VariantType variant)
Color of the icon and of the label of a FAB: Primary for the Surface variant, the "on container" role...
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 getContainerColor(VariantType variant, bool lowered)
Fill color of the container of a FAB.
float getBorderRadius(SizeType size)
Corner radius of the container: the Medium, Large and ExtraLarge shape of the active theme for a Smal...
float getIconFontSize(SizeType size)
Font size of the icon of a regular FAB: 24 dp for Small and Medium, 36 dp for Large.
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.
Rules. Every class here runs each frame on the entities that have the components it declares.
ElevationLevel
How far a surface is raised above the background, in six steps.
Definition tokens.hpp:130
@ Level1
Lowest raised level.
Definition tokens.hpp:132
@ Level3
Third raised level.
Definition tokens.hpp:134
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
@ LabelLarge
14 / 20, medium weight.
Definition typescale.hpp:57
@ Hovered
0.08 overlay of the content color.
Definition tokens.hpp:224
@ Pressed
0.12 overlay of the content color.
Definition tokens.hpp:226
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.