guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
extended_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/extended_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
59 ecs::ComponentRegistry &registry,
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)
69 }
70
72 const ecs::ComponentRegistry &registry,
73 const ecs::Entity::Identifier &identifier)
74 {
75 const auto &config =
77 identifier);
78 return fab_style::getLabelFontSize(config.getSize());
79 }
80
81 utility::graphic::Color32Bit ExtendedFloatingActionButton::contentColor(
82 const ecs::ComponentRegistry &registry,
83 const ecs::Entity::Identifier &identifier)
84 {
85 const auto &button =
86 registry.getComponent<components::Button>(identifier);
87 const auto &config =
89 identifier);
91 button, fab_style::getContentColor(config.getVariant()));
92 }
93
95 ecs::ComponentRegistry &registry,
96 const ecs::Entity::Identifier &identifier)
97 {
98 const auto &button =
99 registry.getComponent<components::Button>(identifier);
100 const auto &config =
102 identifier);
103 const bool disabled = Button::isDisabled(registry, identifier);
104 const bool hovered = Button::isHovered(registry, identifier);
105 const bool pressed = Button::isPressed(registry, identifier);
106 const bool visible = button.isVisible();
107 const auto mode = config.getMode();
108 const bool lowered = config.isLowered();
109 // M3 FAB: level 3 at rest (1 when lowered), one level up on hover,
110 // and flat when disabled.
111 auto level = lowered ? theme::ElevationLevel::Level1
113 if (disabled || !visible || mode == Mode::Flat) {
115 } else if (hovered && !pressed) {
116 level = static_cast<theme::ElevationLevel>(
117 static_cast<std::size_t>(level) + 1);
118 }
119 registry.getComponent<components::Elevation>(identifier)
120 .setElevationLevel(level);
121 }
122
124 ecs::ComponentRegistry &registry,
125 const ecs::Entity::Identifier &identifier)
126 {
127 const auto &button =
128 registry.getComponent<components::Button>(identifier);
129 const auto &config =
131 identifier);
132 const Button::Child icon(registry, button.getIcon());
133 const Button::Child label(registry, button.getLabel());
134 // A collapsed FAB, or one whose label measures nothing, keeps its
135 // label child but hides it. geometry() uses the same rule; deciding
136 // it here too keeps the two from rewriting the color every frame.
137 const bool labelShown = config.isExtended() && label
138 && registry.getComponent<components::Bound>(label.getIdentifier())
139 .getWidth()
140 > 0.0f;
141 const bool disabled = Button::isDisabled(registry, identifier);
142 const bool hovered = Button::isHovered(registry, identifier);
143 const bool pressed = Button::isPressed(registry, identifier);
144 const bool visible = button.isVisible();
145 const auto variant = config.getVariant();
146 const bool lowered = config.isLowered();
147 elevation(registry, identifier);
148
149 if (!visible) {
150 registry.getComponent<components::Color>(identifier)
151 .setColor(style::transparentColor());
152 if (icon) {
153 icon.setColor(style::transparentColor());
154 }
155 if (label) {
157 }
158 return;
159 }
160
161 if (disabled) {
162 registry.getComponent<components::Color>(identifier)
164
165 if (icon) {
166 icon.setColor(style::disabledContentColor());
167 }
168
169 if (label) {
170 label.setColor(labelShown ? style::disabledContentColor()
172 }
173
174 return;
175 }
176
177 const auto baseContainerColor = Button::resolveContainerColor(
178 button, fab_style::getContainerColor(variant, lowered));
180 button, fab_style::getContentColor(variant));
181
182 utility::graphic::Color32Bit containerColor = baseContainerColor;
183
184 if (pressed) {
185 containerColor = style::stateLayer(baseContainerColor, contentColor,
187 } else if (hovered) {
188 containerColor = style::stateLayer(baseContainerColor, contentColor,
190 }
191
192 registry.getComponent<components::Color>(identifier)
193 .setColor(containerColor);
194
195 if (icon) {
196 icon.setColor(contentColor);
197 }
198
199 if (label) {
200 label.setColor(labelShown ? contentColor
202 }
203 }
204
206 ecs::ComponentRegistry &registry,
207 const ecs::Entity::Identifier &identifier)
208 {
209 const auto &button =
210 registry.getComponent<components::Button>(identifier);
211 const auto &config =
213 identifier);
214 const Button::Child icon(registry, button.getIcon());
215 const Button::Child label(registry, button.getLabel());
216 const bool disabled = Button::isDisabled(registry, identifier);
217 const bool visible = button.isVisible();
218 const auto layer = Layout::layerOf(registry, identifier);
219 const auto variant = config.getVariant();
220 const auto size = config.getSize();
221 const auto &customSize = config.getCustomSize();
222 const bool extended = config.isExtended();
223 const float baseHeight =
224 customSize.value_or(fab_style::getContainerSize(size));
225 const float fontHeight = fab_style::getLabelFontSize(size);
226
227 registry.getComponent<components::Borders>(identifier)
228 .setBorderRadius(customSize.has_value()
229 ? *customSize / 3.5f
231
232 if (!icon && !label) {
233 return;
234 }
235
236 if (icon) {
237 icon.setFontSize(fontHeight);
238 }
239
240 if (label) {
242 label.setContent(button.getLabelContent());
243 }
244
245 // M3 extended FAB: 16 dp leading, 12 dp icon-label gap, 20 dp
246 // trailing; collapsed it is a square icon-only FAB.
247 constexpr float leading = 16.0f;
248 constexpr float gap = 12.0f;
249 constexpr float trailing = 20.0f;
250
251 const auto &buttonPose =
252 registry.getComponent<components::Transform>(identifier).getPose();
253
254 const auto sizeOf = [&registry](const Button::Child &child) {
255 const auto &bound =
256 registry.getComponent<components::Bound>(child.getIdentifier());
257 return std::make_pair(bound.getWidth(), bound.getHeight());
258 };
259
260 const auto place = [&](const Button::Child &child, float x,
261 float height) {
264 utility::graphic::PositionF(buttonPose.getPosition().x + x,
265 buttonPose.getPosition().y
266 - (baseHeight - height)
267 / 2.0f,
268 buttonPose.getPosition().z),
269 buttonPose.getOrientation(), layer));
270 };
271
272 const auto [iconWidth, iconHeight] =
273 icon ? sizeOf(icon) : std::make_pair(0.0f, 0.0f);
274 const auto [labelWidth, labelHeight] =
275 label ? sizeOf(label) : std::make_pair(0.0f, 0.0f);
276
277 const bool showLabel = extended && labelWidth > 0.0f;
278 float width = baseHeight;
279
280 if (showLabel) {
281 width = leading + labelWidth + trailing;
282 float cursor = leading;
283 if (iconWidth > 0.0f) {
284 place(icon, cursor, iconHeight);
285 cursor += iconWidth + gap;
286 width += iconWidth + gap;
287 }
288 place(label, cursor, labelHeight);
289 } else {
290 if (icon) {
291 place(icon, (baseHeight - iconWidth) / 2.0f, iconHeight);
292 }
293 if (label) {
294 place(label, (baseHeight - labelWidth) / 2.0f, labelHeight);
295 }
296 }
297
298 if (label) {
299 // A collapsed FAB keeps its label child but hides it.
301 button, fab_style::getContentColor(variant));
302 if (!showLabel) {
304 } else if (!disabled && visible) {
305 label.setColor(contentColor);
306 }
307 }
308
309 registry.getComponent<components::Bound>(identifier)
310 .setWidth(width)
311 .setHeight(baseHeight);
312 Button::applyTouchTarget(registry, identifier, width, baseHeight);
313 }
314
316 ecs::ComponentRegistry &registry,
317 const ecs::Entity::Identifier &identifier)
318 {
319 styleState(registry, identifier);
320 geometry(registry, identifier);
321 }
322
324 const ecs::Entity::Identifier &entityIdentifier)
325 {
327 entityIdentifier)
328 || !requireComponent<components::Button>(entityIdentifier)) {
329 return;
330 }
331
332 apply(getComponentRegistry(), entityIdentifier);
333 }
334} // 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 extended floating action button: a FAB that shows a label next to its icon and c...
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.
ExtendedFloatingActionButton(void)
Create the system for the Layout phase, matching entities that have both a components::ExtendedFloati...
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 float iconFontSize(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the font size of the icon for the configured size.
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 install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity behave as an extended floating action button.
static void elevation(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Write the shadow level of the button into its components::Elevation.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one 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 getLabelFontSize(SizeType size)
Font size shared by the label and the icon of an extended FAB: 18 dp for Small, 24 dp for Medium,...
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...
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.