guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
floating_action_button_menu.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 <memory>
24#include <utility>
25
26#include "guillaume/entities/floating_action_button_menu.hpp"
27#include "guillaume/entities/builder_base.hpp"
28#include "guillaume/entities/extended_floating_action_button.hpp"
29#include "guillaume/entities/floating_action_button.hpp"
30#include "guillaume/systems/floating_action_button_menu.hpp"
31
32namespace guillaume::entities
33{
35 ecs::ComponentRegistry &componentRegistry,
36 ecs::EntityRegistry &entityRegistry)
37 : EntityBuilderBase<FloatingActionButtonMenu>(componentRegistry,
38 entityRegistry)
39 {
40 reset();
41 }
42
43 std::shared_ptr<FloatingActionButtonMenu>
45 {
47 this->getComponentRegistry(), _iconGlyphName, _actions,
48 _accessibilityLabel);
49 return entity;
50 }
51
53 {
54 _iconGlyphName.clear();
55 _actions.clear();
56 _accessibilityLabel.clear();
57 }
58
61 const std::string &iconGlyphName)
62 {
63 _iconGlyphName = iconGlyphName;
64 return *this;
65 }
66
69 const std::string &iconGlyphName, std::function<void(void)> onClick)
70 {
71 _actions.push_back(Action { "", iconGlyphName, std::move(onClick) });
72 return *this;
73 }
74
77 const std::string &iconGlyphName, const std::string &label,
78 std::function<void(void)> onClick)
79 {
80 _actions.push_back(Action { label, iconGlyphName, std::move(onClick) });
81 return *this;
82 }
83
86 const std::string &accessibilityLabel)
87 {
88 _accessibilityLabel = accessibilityLabel;
89 return *this;
90 }
91
92 std::shared_ptr<FloatingActionButtonMenu>
94 std::shared_ptr<Entity> parent, const std::string &iconGlyphName)
95 {
96 return withIcon(iconGlyphName).registerEntity(parent);
97 }
98
99 FloatingActionButtonMenu::FloatingActionButtonMenu(
100 ecs::ComponentRegistry &registry, const std::string &iconGlyphName,
101 const std::vector<Action> &actions,
102 const std::string &accessibilityLabel)
103 : ecs::ParentEntityFiller<components::Transform, components::Bound,
104 components::Layout,
105 components::FloatingActionButtonMenu>(
106 registry)
107 {
108 state()
109 .setIconGlyphName(iconGlyphName)
110 .setActions(actions)
111 .setAccessibilityLabel(accessibilityLabel);
113 getIdentifier());
114 }
115
119
120 components::FloatingActionButtonMenu &FloatingActionButtonMenu::state(void)
121 {
122 return getComponentRegistry()
124 getIdentifier());
125 }
126
128 FloatingActionButtonMenu::state(void) const
129 {
130 return getComponentRegistry()
132 getIdentifier());
133 }
134
135 FloatingActionButtonMenu &FloatingActionButtonMenu::open(void)
136 {
138 getIdentifier());
139 return *this;
140 }
141
142 FloatingActionButtonMenu &FloatingActionButtonMenu::close(void)
143 {
145 getIdentifier());
146 return *this;
147 }
148
149 FloatingActionButtonMenu &FloatingActionButtonMenu::toggle(void)
150 {
152 getIdentifier());
153 return *this;
154 }
155
157 std::function<void(bool)> onStateChange)
158 {
159 state().setOnStateChange(std::move(onStateChange));
160 return *this;
161 }
162
164 {
165 return state().isOpen();
166 }
167
169 {
170 return state().getActionButtons().size();
171 }
172
175 {
176 return state().getTrigger();
177 }
178
181 {
182 const auto &buttons = state().getActionButtons();
183
184 return index < buttons.size() ? buttons[index]
186 }
187
188 void FloatingActionButtonMenu::build(void)
189 {
190 auto &registry = getComponentRegistry();
191 // Copies: building children may move the component storage.
192 const std::string iconGlyphName = state().getIconGlyphName();
193 const std::vector<Action> actions = state().getActions();
194
195 // systems::FloatingActionButtonMenu::install wires every click.
196 FloatingActionButton::Builder triggerBuilder(registry, *this);
197 const auto trigger = triggerBuilder.makeFloatingActionButton(
198 shared_from_this(), iconGlyphName, std::function<void(void)>(),
199 FloatingActionButton::Variant::Primary,
200 FloatingActionButton::Size::Medium);
201
202 FloatingActionButton::Builder actionBuilder(registry, *this);
203 ExtendedFloatingActionButton::Builder labelledBuilder(registry, *this);
204 std::vector<ecs::Entity::Identifier> identifiers;
205
206 for (const auto &action: actions) {
207 std::shared_ptr<ButtonBase> button;
208
209 if (action.label.empty()) {
210 button = actionBuilder.makeFloatingActionButton(
211 shared_from_this(), action.iconGlyphName,
212 std::function<void(void)>(),
213 FloatingActionButton::Variant::Surface,
214 FloatingActionButton::Size::Small);
215 } else {
216 // A labelled action shows its label beside the icon.
217 button =
218 labelledBuilder.withIcon(action.iconGlyphName)
219 .withLabel(action.label)
220 .withVariant(
221 ExtendedFloatingActionButton::Variant::Surface)
222 .withSize(ExtendedFloatingActionButton::Size::Small)
223 .withAccessibilityLabel(action.label)
224 .registerEntity(shared_from_this());
225 }
226
227 identifiers.push_back(button->getIdentifier());
228 }
229
230 state()
231 .setTrigger(trigger->getIdentifier())
232 .setActionButtons(identifiers);
235 }
236} // namespace guillaume::entities
State of a floating action button menu (speed dial): a trigger button that expands into a column of a...
const std::vector< Action > & getActions(void) const noexcept
Get the actions of the menu.
const std::string & getIconGlyphName(void) const noexcept
Get the icon the trigger shows while the menu is closed.
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.
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::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures, creates and registers ExtendedFloatingActionButton widgets.
Configures, creates and registers FloatingActionButtonMenu widgets.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Builder & withAccessibilityLabel(const std::string &accessibilityLabel)
Set the label of the trigger read by assistive technology.
std::shared_ptr< FloatingActionButtonMenu > buildEntity(void) override
Build a FloatingActionButtonMenu, with its trigger and action buttons, from the current configuration...
std::shared_ptr< FloatingActionButtonMenu > makeFloatingActionButtonMenu(std::shared_ptr< Entity > parent, const std::string &iconGlyphName)
Build a menu with the actions added so far, register it under parent and reset the builder.
Builder & withIcon(const std::string &iconGlyphName)
Set the icon of the trigger while the menu is closed.
Builder & addAction(const std::string &iconGlyphName, std::function< void(void)> onClick)
Add an action shown as a small icon-only FAB.
void reset(void) override
Restore the default configuration.
components::FloatingActionButtonMenu::Action Action
FloatingActionButtonMenu & toggle(void)
Open the menu when it is closed, close it when it is open.
ecs::Entity::Identifier getTriggerIdentifier(void) const
Get the identifier of the trigger button, a FloatingActionButton child of the menu.
std::size_t getActionCount(void) const
Get the number of actions.
FloatingActionButtonMenu & close(void)
Close the menu: hide the action buttons and restore the trigger icon, at once.
FloatingActionButtonMenu & setOnStateChange(std::function< void(bool)> onStateChange)
Set the handler called when the menu opens or closes (Paper onStateChange).
static std::shared_ptr< FloatingActionButtonMenu > create(Arguments &&...arguments)
Create a menu with its trigger and action buttons, without registering it in an entity registry.
FloatingActionButtonMenu & open(void)
Open the menu: show the action buttons and the close glyph on the trigger, at once.
ecs::Entity::Identifier getActionIdentifier(std::size_t index) const
Get the identifier of the button of one action.
bool isOpen(void) const
Get whether the menu is open.
Configures, creates and registers FloatingActionButton widgets.
static void open(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Open the menu: show its actions at once, then call the state-change callback with true.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Bring the buttons in line with the open flag of the component, then lay out the column.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a floating action button menu.
static void close(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Close the menu: hide its actions at once, then call the state-change callback with false.
static void toggle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Open the menu when it is closed, close it when it is open, and call the state-change callback with th...
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46