guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
segmented_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/segmented_button.hpp"
24
25#include <algorithm>
26#include <string>
27
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/borders.hpp"
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/button.hpp"
32#include "guillaume/components/common_button.hpp"
33#include "guillaume/components/layout.hpp"
34#include "guillaume/components/style_state.hpp"
35#include "guillaume/systems/button.hpp"
36#include "guillaume/systems/common_button.hpp"
37#include "guillaume/systems/layout.hpp"
38
39namespace guillaume::systems
40{
41 namespace
42 {
44
51 bool isButton(const ecs::ComponentRegistry &registry,
53 {
54 return registry.hasComponent<components::Button>(button);
55 }
56 } // namespace
57
62
64 ecs::ComponentRegistry &componentRegistry,
65 const ecs::Entity::Identifier &entityIdentifier)
66 {
67 ecs::ComponentRegistry *registry = &componentRegistry;
68 const ecs::Entity::Identifier identifier = entityIdentifier;
69
70 componentRegistry.getComponent<components::Layout>(identifier)
75 .setSpacing(0.0f)
76 .setPadding(0.0f);
77
78 const auto &config =
80 identifier);
81 const auto &buttons = config.getButtons();
82 const auto &segments = config.getSegments();
83
84 for (std::size_t index = 0; index < buttons.size(); ++index) {
85 const auto button = buttons[index];
86 if (!isButton(componentRegistry, button)) {
87 continue;
88 }
89
90 componentRegistry.getComponent<components::Button>(button)
91 .setToggle(true)
92 .setOnClick([registry, identifier, index]() {
93 segmentChanged(*registry, identifier, index);
94 });
95
96 if (index < segments.size()) {
97 const auto &segment = segments[index];
98 if (componentRegistry.hasComponent<components::StyleState>(
99 button)) {
100 componentRegistry
102 .setEnabled(!segment.disabled);
103 }
104 if (!segment.accessibilityLabel.empty()
105 && componentRegistry
107 componentRegistry
109 .setLabel(segment.accessibilityLabel);
110 }
111 }
112
113 Button::restyle(componentRegistry, button);
114 }
115 }
116
118 const ecs::ComponentRegistry &componentRegistry,
119 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
120 {
121 const auto &buttons =
122 componentRegistry
123 .getComponent<components::SegmentedButton>(entityIdentifier)
124 .getButtons();
125
126 return index < buttons.size()
127 && isButton(componentRegistry, buttons[index])
128 && componentRegistry
129 .getComponent<components::Button>(buttons[index])
130 .isSelected();
131 }
132
133 std::vector<std::size_t> SegmentedButton::selectedIndices(
134 const ecs::ComponentRegistry &componentRegistry,
135 const ecs::Entity::Identifier &entityIdentifier)
136 {
137 const std::size_t count =
138 componentRegistry
139 .getComponent<components::SegmentedButton>(entityIdentifier)
140 .getButtons()
141 .size();
142 std::vector<std::size_t> indices;
143
144 for (std::size_t index = 0; index < count; ++index) {
145 if (isSelected(componentRegistry, entityIdentifier, index)) {
146 indices.push_back(index);
147 }
148 }
149 return indices;
150 }
151
152 void
154 const ecs::Entity::Identifier &entityIdentifier,
155 std::size_t index)
156 {
157 const auto &config =
158 componentRegistry.getComponent<components::SegmentedButton>(
159 entityIdentifier);
160 const auto &buttons = config.getButtons();
161
162 if (index >= buttons.size()) {
163 return;
164 }
165
166 if (config.getSelectionMode() == SelectionMode::Single) {
167 for (std::size_t other = 0; other < buttons.size(); ++other) {
168 if (isButton(componentRegistry, buttons[other])) {
169 Button::setSelected(componentRegistry, buttons[other],
170 other == index);
171 }
172 }
173 } else if (isButton(componentRegistry, buttons[index])) {
174 Button::setSelected(componentRegistry, buttons[index], true);
175 }
176
177 apply(componentRegistry, entityIdentifier);
178 }
179
181 ecs::ComponentRegistry &componentRegistry,
182 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
183 {
184 const auto &buttons =
185 componentRegistry
186 .getComponent<components::SegmentedButton>(entityIdentifier)
187 .getButtons();
188
189 if (index >= buttons.size()) {
190 return;
191 }
192
193 if (isButton(componentRegistry, buttons[index])) {
194 Button::setSelected(componentRegistry, buttons[index], false);
195 }
196
197 apply(componentRegistry, entityIdentifier);
198 }
199
201 ecs::ComponentRegistry &componentRegistry,
202 const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
203 {
204 const auto &config =
205 componentRegistry.getComponent<components::SegmentedButton>(
206 entityIdentifier);
207 const auto &buttons = config.getButtons();
208
209 if (index >= buttons.size()
210 || !isButton(componentRegistry, buttons[index])) {
211 return;
212 }
213
214 if (config.getSelectionMode() == SelectionMode::Single) {
215 // Paper single-select commits the pressed value: pressing the
216 // selected segment keeps it selected instead of clearing it.
217 if (!isSelected(componentRegistry, entityIdentifier, index)) {
218 Button::setSelected(componentRegistry, buttons[index], true);
219 }
220 for (std::size_t other = 0; other < buttons.size(); ++other) {
221 if (other != index
222 && isButton(componentRegistry, buttons[other])) {
223 Button::setSelected(componentRegistry, buttons[other],
224 false);
225 }
226 }
227 }
228
229 apply(componentRegistry, entityIdentifier);
230
231 // Copy: the callback may replace itself.
232 const auto onChange = config.getOnChange();
233 if (onChange) {
234 onChange(index);
235 }
236 }
237
239 ecs::ComponentRegistry &componentRegistry,
240 const ecs::Entity::Identifier &entityIdentifier, std::size_t index,
241 bool disabled)
242 {
243 auto &config =
244 componentRegistry.getComponent<components::SegmentedButton>(
245 entityIdentifier);
246 const auto &buttons = config.getButtons();
247
248 config.setSegmentDisabled(index, disabled);
249 if (index < buttons.size()
250 && componentRegistry.hasComponent<components::StyleState>(
251 buttons[index])) {
252 componentRegistry
253 .getComponent<components::StyleState>(buttons[index])
254 .setEnabled(!disabled);
255 if (isButton(componentRegistry, buttons[index])) {
256 Button::restyle(componentRegistry, buttons[index]);
257 }
258 }
259
260 // Lay the row out again so the change shows before the next frame.
261 apply(componentRegistry, entityIdentifier);
262 }
263
265 const ecs::Entity::Identifier &entityIdentifier)
266 {
267 const auto &config =
268 componentRegistry.getComponent<components::SegmentedButton>(
269 entityIdentifier);
270 const auto &buttons = config.getButtons();
271 const auto &segments = config.getSegments();
272
273 // A selected segment shows the check glyph, the others their icon.
274 for (std::size_t index = 0; index < buttons.size(); ++index) {
275 if (!isButton(componentRegistry, buttons[index])) {
276 continue;
277 }
278
279 auto &button = componentRegistry.getComponent<components::Button>(
280 buttons[index]);
281 const std::string icon = button.isSelected()
282 ? std::string(SelectedGlyph)
283 : (index < segments.size() ? segments[index].icon
284 : std::string());
285 if (button.getIconGlyphName() != icon) {
286 button.setIconGlyphName(icon);
287 Button::syncContent(componentRegistry, buttons[index]);
288 Button::restyle(componentRegistry, buttons[index]);
289 }
290 }
291
292 // Equalize the segment widths (at least MinSegmentWidth).
294 for (const auto button: buttons) {
295 if (componentRegistry.hasComponent<components::Bound>(button)) {
296 width = std::max(
297 width,
298 componentRegistry.getComponent<components::Bound>(button)
299 .getWidth());
300 }
301 }
302 for (const auto button: buttons) {
303 if (!componentRegistry.hasComponent<components::CommonButton>(
304 button)) {
305 continue;
306 }
307
308 auto &common =
309 componentRegistry.getComponent<components::CommonButton>(
310 button);
311 if (common.getMinWidth() != width) {
312 common.setMinWidth(width);
313 Button::restyle(componentRegistry, button);
314 }
315 }
316
317 // The segments are the children the Layout system places in a row.
318 auto &layout = componentRegistry.getComponent<components::Layout>(
319 entityIdentifier);
320 layout.setChildren(buttons);
321 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
322 Layout::layerOf(componentRegistry, entityIdentifier));
323
324 if (buttons.empty()) {
325 return;
326 }
327
328 // Connected corners: only the outer corners of the row are rounded.
329 const float radius =
330 componentRegistry.getComponent<components::Bound>(entityIdentifier)
331 .getHeight()
332 * 0.5f;
333 const std::size_t count = buttons.size();
334
335 for (std::size_t index = 0; index < count; ++index) {
336 if (!componentRegistry.hasComponent<components::CommonButton>(
337 buttons[index])) {
338 continue;
339 }
340
341 const bool isFirst = (index == 0);
342 const bool isLast = (index == count - 1);
344 isFirst ? radius : 0.0f, isLast ? radius : 0.0f,
345 isLast ? radius : 0.0f, isFirst ? radius : 0.0f
346 };
347
348 // systems::CommonButton copies these to Borders; run its
349 // geometry here so the result is visible at once.
350 componentRegistry
351 .getComponent<components::CommonButton>(buttons[index])
352 .setCorners(corners);
353 CommonButton::geometry(componentRegistry, buttons[index]);
354 }
355 }
356
357 void
359 {
360 if (!requireComponent<components::SegmentedButton>(entityIdentifier)) {
361 return;
362 }
363
364 apply(getComponentRegistry(), entityIdentifier);
365 }
366} // 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...
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
State shared by every pressable button, whatever its look.
Definition button.hpp:77
bool isSelected(void) const noexcept
Check whether the button is selected.
Definition button.cpp:127
Button & setOnClick(Handler handler)
Set the callback run when the button is released.
Definition button.cpp:202
Look of a Material 3 common button: color style, corner shape, size and padding options.
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
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Definition layout.cpp:77
State of a segmented button: a row of joined toggle buttons of which one (or several) can be selected...
SelectionMode
How many segments can be selected at once.
static constexpr float MinSegmentWidth
Minimum width of a segment, in dp.
const std::vector< ecs::Entity::Identifier > & getButtons(void) const noexcept
Get the toggle button child of each segment.
The interaction state of an entity in one place: enabled or disabled, and whether it is hovered,...
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
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 setSelected(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, bool selected)
Select or unselect a toggle button from code and show it at once.
Definition button.cpp:239
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 geometry(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Second half of apply: write the corner radius, the size of the button and the position of its icon an...
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
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select a segment from code and show it at once.
static void segmentChanged(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
React to the user clicking a segment.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Lay out the row from the current selection of the buttons.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one segmented button.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a segmented button.
static constexpr const char * SelectedGlyph
static std::vector< std::size_t > selectedIndices(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
List the selected segments.
static bool isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Whether a segment is selected.
static void deselect(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Deselect a segment from code and show it at once.
SegmentedButton(void)
Create the system for the Layout phase, matching entities that have a components::SegmentedButton.
static void setSegmentDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, bool disabled)
Enable or disable one segment and show it at once.
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.
Radius of each corner, for a button joined to its neighbours (a segment of a segmented button).