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 "guillaume/systems/floating_action_button_menu.hpp"
24
25#include <string>
26#include <vector>
27
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/button.hpp"
30#include "guillaume/components/layout.hpp"
31#include "guillaume/systems/button.hpp"
32#include "guillaume/systems/layout.hpp"
33
34namespace guillaume::systems
35{
36 namespace
37 {
44 bool isButton(const ecs::ComponentRegistry &registry,
46 {
47 return registry.hasComponent<components::Button>(button);
48 }
49 } // namespace
50
56
58 ecs::ComponentRegistry &componentRegistry,
59 const ecs::Entity::Identifier &entityIdentifier)
60 {
61 ecs::ComponentRegistry *registry = &componentRegistry;
62 const ecs::Entity::Identifier identifier = entityIdentifier;
63
64 componentRegistry.getComponent<components::Layout>(identifier)
70 .setPadding(0.0f);
71
72 const auto &menu =
73 componentRegistry
75 const auto trigger = menu.getTrigger();
76
77 if (isButton(componentRegistry, trigger)) {
78 componentRegistry.getComponent<components::Button>(trigger)
79 .setOnClick([registry, identifier]() {
80 toggle(*registry, identifier);
81 });
82 }
83 if (componentRegistry.hasComponent<components::Accessibility>(
84 trigger)) {
85 componentRegistry.getComponent<components::Accessibility>(trigger)
86 .setLabel(menu.getAccessibilityLabel());
87 }
88
89 const auto &actions = menu.getActions();
90 const auto &buttons = menu.getActionButtons();
91 for (std::size_t index = 0;
92 index < buttons.size() && index < actions.size(); ++index) {
93 if (isButton(componentRegistry, buttons[index])) {
94 componentRegistry
95 .getComponent<components::Button>(buttons[index])
96 .setOnClick(actions[index].onClick);
97 }
98 }
99 }
100
101 void FloatingActionButtonMenu::setOpen(
102 ecs::ComponentRegistry &componentRegistry,
103 const ecs::Entity::Identifier &entityIdentifier, bool open)
104 {
105 auto &menu = componentRegistry
107 entityIdentifier);
108
109 if (menu.isOpen() == open) {
110 return;
111 }
112 menu.setOpen(open);
113 apply(componentRegistry, entityIdentifier);
114
115 // Copy: the callback may replace itself.
116 const auto onStateChange = menu.getOnStateChange();
117 if (onStateChange) {
118 onStateChange(open);
119 }
120 }
121
123 ecs::ComponentRegistry &componentRegistry,
124 const ecs::Entity::Identifier &entityIdentifier)
125 {
126 setOpen(componentRegistry, entityIdentifier, true);
127 }
128
130 ecs::ComponentRegistry &componentRegistry,
131 const ecs::Entity::Identifier &entityIdentifier)
132 {
133 setOpen(componentRegistry, entityIdentifier, false);
134 }
135
137 ecs::ComponentRegistry &componentRegistry,
138 const ecs::Entity::Identifier &entityIdentifier)
139 {
140 setOpen(componentRegistry, entityIdentifier,
141 !componentRegistry
143 entityIdentifier)
144 .isOpen());
145 }
146
148 ecs::ComponentRegistry &componentRegistry,
149 const ecs::Entity::Identifier &entityIdentifier)
150 {
151 const auto &menu =
152 componentRegistry
154 entityIdentifier);
155 const bool open = menu.isOpen();
156 const auto trigger = menu.getTrigger();
157 std::vector<ecs::Entity::Identifier> children;
158
159 // Closed actions are hidden and ignore activation, so they cannot be
160 // clicked through while the menu is collapsed.
161 for (const auto action: menu.getActionButtons()) {
162 if (!isButton(componentRegistry, action)) {
163 continue;
164 }
165
166 auto &button =
167 componentRegistry.getComponent<components::Button>(action);
168 if (componentRegistry.hasComponent<components::Accessibility>(
169 action)) {
170 componentRegistry
172 .setFocusable(open);
173 }
174 if (button.isVisible() != open) {
175 button.setVisible(open);
176 Button::restyle(componentRegistry, action);
177 }
178 if (open) {
179 children.push_back(action);
180 }
181 }
182
183 if (isButton(componentRegistry, trigger)) {
184 auto &button =
185 componentRegistry.getComponent<components::Button>(trigger);
186 const std::string icon =
187 open ? std::string(CloseGlyph) : menu.getIconGlyphName();
188
189 componentRegistry.getComponent<components::Accessibility>(trigger)
190 .setExpanded(open);
191 if (button.getIconGlyphName() != icon) {
192 button.setIconGlyphName(icon);
193 Button::syncContent(componentRegistry, trigger);
194 }
195 children.push_back(trigger);
196 }
197
198 // The open actions sit above the trigger; closed ones are not laid
199 // out at all.
200 auto &layout = componentRegistry.getComponent<components::Layout>(
201 entityIdentifier);
202 layout.setChildren(children);
203 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
204 Layout::layerOf(componentRegistry, entityIdentifier));
205 }
206
208 const ecs::Entity::Identifier &entityIdentifier)
209 {
211 entityIdentifier)) {
212 return;
213 }
214
215 apply(getComponentRegistry(), entityIdentifier);
216 }
217} // namespace guillaume::systems
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
State shared by every pressable button, whatever its look.
Definition button.hpp:77
State of a floating action button menu (speed dial): a trigger button that expands into a column of a...
static constexpr float Spacing
Gap between the buttons of the column, in dp.
FloatingActionButtonMenu & setOpen(bool open)
Store whether the menu is open.
ecs::Entity::Identifier getTrigger(void) const noexcept
Get the child entity of the trigger button.
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
Layout & setChildren(const std::vector< ecs::Entity::Identifier > &children)
Replace the list of children to arrange.
Definition layout.cpp:171
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Definition layout.cpp:92
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
Definition layout.cpp:47
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
@ End
Packed at the right (row) or bottom (column).
Definition layout.hpp:89
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Definition layout.cpp:77
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.
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
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
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...
static void restyle(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Recompute the colors and geometry of the button at once.
Definition button.cpp:228
static void syncContent(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Copy what components::Button says onto the icon child and the accessibility state.
Definition button.cpp:213
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.
FloatingActionButtonMenu(void)
Create the system for the Layout phase, matching entities that have a components::FloatingActionButto...
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.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one menu.
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...
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, std::uint32_t layer=0)
Arrange children inside a container and size the container.
Definition layout.cpp:126
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
Rules. Every class here runs each frame on the entities that have the components it declares.