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 <algorithm>
24#include <memory>
25#include <stdexcept>
26#include <utility>
27
28#include "guillaume/entities/floating_action_button.hpp"
29#include "guillaume/entities/builder_base.hpp"
31#include "guillaume/components/elevation.hpp"
33#include "guillaume/theme.hpp"
34#include "guillaume/systems/floating_action_button.hpp"
35
36namespace guillaume::entities
37{
39 ecs::ComponentRegistry &componentRegistry,
40 ecs::EntityRegistry &entityRegistry)
41 : EntityBuilderBase<FloatingActionButton>(componentRegistry,
42 entityRegistry)
43 {
44 reset();
45 }
46
47 std::shared_ptr<FloatingActionButton>
49 {
50 auto entity = FloatingActionButton::create(
51 this->getComponentRegistry(), _iconGlyphName, _iconStyle,
52 _labelContent, _variant, _size, _lowered, _isDisabled,
53 _accessibilityLabel, _onClick);
54 return entity;
55 }
56
58 {
59 _iconGlyphName.clear();
60 _iconStyle = components::Glyph::Style::Outlined;
61 _labelContent.clear();
62 _variant = Variant::Primary;
63 _size = Size::Medium;
64 _lowered = false;
65 _isDisabled = false;
66 _accessibilityLabel.clear();
67 _onClick = {};
68 }
69
71 const std::string &iconGlyphName)
72 {
73 _iconGlyphName = iconGlyphName;
74 return *this;
75 }
76
78 const components::Glyph::Style &iconStyle)
79 {
80 _iconStyle = iconStyle;
81 return *this;
82 }
83
85 const std::string &labelContent)
86 {
87 _labelContent = labelContent;
88 return *this;
89 }
90
92 std::function<void(void)> onClick)
93 {
94 _onClick = std::move(onClick);
95 return *this;
96 }
97
100 {
101 _variant = variant;
102 return *this;
103 }
104
107 {
108 _size = size;
109 return *this;
110 }
111
114 {
115 _lowered = lowered;
116 return *this;
117 }
118
121 {
122 _isDisabled = isDisabled;
123 return *this;
124 }
125
128 const std::string &accessibilityLabel)
129 {
130 _accessibilityLabel = accessibilityLabel;
131 return *this;
132 }
133
134 std::shared_ptr<FloatingActionButton>
136 std::shared_ptr<Entity> parent, const std::string &iconGlyphName,
137 std::function<void(void)> onClick, Variant variant, Size size)
138 {
139 return withIcon(iconGlyphName)
140 .withOnClick(onClick)
141 .withVariant(variant)
142 .withSize(size)
143 .registerEntity(parent);
144 }
145
146 FloatingActionButton::FloatingActionButton(
147 ecs::ComponentRegistry &registry, const std::string &iconGlyphName,
148 const components::Glyph::Style &iconStyle,
149 const std::string &labelContent, Variant variant, Size size,
150 bool lowered, bool isDisabled, const std::string &accessibilityLabel,
151 std::function<void(void)> onClick)
152 : ButtonBase(registry, iconGlyphName, iconStyle, labelContent, false,
153 false, isDisabled, false, accessibilityLabel,
154 std::move(onClick))
155 {
158 .setVariant(variant)
159 .setSize(size)
160 .setLowered(lowered);
165 getIdentifier());
166 }
167
173
174 utility::graphic::Color32Bit
180
181 FloatingActionButton &FloatingActionButton::setMode(Mode mode)
182 {
185 .setMode(mode);
186 restyle();
187 return *this;
188 }
189
191 FloatingActionButton::setCustomSize(std::optional<float> size)
192 {
193 if (size.has_value() && *size <= 0.0f) {
194 throw std::invalid_argument("FAB custom size must be positive");
195 }
198 .setCustomSize(size);
199 restyle();
200 return *this;
201 }
202
205 {
208 .setVariant(variant);
209 restyle();
210 return *this;
211 }
212
213 FloatingActionButton &FloatingActionButton::setSize(const Size &size)
214 {
217 .setSize(size);
218 restyle();
219 return *this;
220 }
221
222 FloatingActionButton &FloatingActionButton::setLowered(const bool &lowered)
223 {
226 .setLowered(lowered);
227 restyle();
228 return *this;
229 }
230
237
244
251
258
259} // namespace guillaume::entities
Look of a Material 3 floating action button (FAB): color variant, size and shadow.
Style
Family of the Material Symbols font the icon belongs to.
Definition glyph.hpp:66
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.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures, creates and registers FloatingActionButton widgets.
Builder & withDisabled(const bool &isDisabled)
Choose whether the FAB starts disabled.
std::shared_ptr< FloatingActionButton > buildEntity(void) override
Build a FloatingActionButton, with its icon and label children, from the current configuration,...
Builder & withOnClick(std::function< void(void)> onClick)
Set the click handler.
Builder & withVariant(const Variant &variant)
Set the color variant.
Builder & withSize(const Size &size)
Set the size.
Builder & withAccessibilityLabel(const std::string &accessibilityLabel)
Set the label read by assistive technology.
Builder & withIcon(const std::string &iconGlyphName)
Set the icon.
Builder & withLowered(const bool &lowered)
Choose the lowered style: a weaker shadow (elevation level 1 instead of 3) and, for Variant::Surface,...
std::shared_ptr< FloatingActionButton > makeFloatingActionButton(std::shared_ptr< Entity > parent, const std::string &iconGlyphName, std::function< void(void)> onClick, Variant variant, Size size)
Build a FAB, register it under parent and reset the builder.
Builder & withLabel(const std::string &labelContent)
Set the label text, shown after the icon.
Builder & withIconStyle(const components::Glyph::Style &iconStyle)
Set the glyph style of the icon.
void reset(void) override
Restore the default configuration.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
A Material 3 floating action button (FAB): a raised, square button with an icon, for the main action ...
float getIconFontSize(void) const override
Get the font size of the icon child for the current size.
bool isLowered(void) const
Get whether the FAB uses the lowered style.
components::FloatingActionButton::Variant Variant
components::FloatingActionButton::Mode Mode
Variant getVariant(void) const
Get the color variant.
FloatingActionButton & setCustomSize(std::optional< float > size)
Override the side of the square container (Paper customSize). Restyles at once.
FloatingActionButton & setVariant(const Variant &variant)
Set the color variant. Restyles at once.
static std::shared_ptr< FloatingActionButton > create(Arguments &&...arguments)
Create a FAB and its icon and label children, without registering it in an entity registry.
FloatingActionButton & setSize(const Size &size)
Set the size, which decides the container size, the corner radius and the icon size....
components::FloatingActionButton::Size Size
utility::graphic::Color32Bit getInitialContentColor(void) const override
Get the color the icon and label children are created with.
Mode getMode(void) const
Get the shadow mode.
FloatingActionButton & setMode(Mode mode)
Set the shadow mode. Restyles at once.
FloatingActionButton & setLowered(const bool &lowered)
Choose the lowered style: a weaker shadow (elevation level 1 instead of 3) and, for Variant::Surface,...
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 install(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Make an entity behave as a floating action button.
Makes the floating action button style tables reachable as guillaume::entities::fab_style.
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