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 <algorithm>
24#include <memory>
25#include <utility>
26
27#include "guillaume/entities/segmented_button.hpp"
28#include "guillaume/entities/builder_base.hpp"
29#include "guillaume/entities/button.hpp"
30#include "guillaume/systems/segmented_button.hpp"
31
32namespace guillaume::entities
33{
34 namespace
35 {
41 std::vector<SegmentedButton::Segment>
42 segmentsFromLabels(const std::vector<std::string> &labels)
43 {
44 std::vector<SegmentedButton::Segment> segments;
45
46 segments.reserve(labels.size());
47 for (const auto &label: labels) {
48 segments.push_back(
49 SegmentedButton::Segment { label, {}, false, {} });
50 }
51 return segments;
52 }
53 } // namespace
54
56 ecs::EntityRegistry &entityRegistry)
57 : EntityBuilderBase<SegmentedButton>(componentRegistry, entityRegistry)
58 {
59 reset();
60 }
61
62 std::shared_ptr<SegmentedButton> SegmentedButton::Builder::buildEntity(void)
63 {
64 if (!_segmentConfigs.empty()) {
66 _segmentConfigs, _selectionMode,
67 _onChange);
68 }
69 auto entity = SegmentedButton::create(
70 this->getComponentRegistry(), _labels, _selectionMode, _onChange);
71 return entity;
72 }
73
75 {
76 _labels.clear();
77 _segmentConfigs.clear();
78 _selectionMode = SelectionMode::Single;
79 _onChange = {};
80 }
81
83 const std::vector<std::string> &labels)
84 {
85 _labels = labels;
86 return *this;
87 }
88
90 const SelectionMode &selectionMode)
91 {
92 _selectionMode = selectionMode;
93 return *this;
94 }
95
97 const std::vector<Segment> &segments)
98 {
99 _segmentConfigs = segments;
100 return *this;
101 }
102
104 std::function<void(std::size_t)> onChange)
105 {
106 _onChange = std::move(onChange);
107 return *this;
108 }
109
110 std::shared_ptr<SegmentedButton>
112 std::shared_ptr<Entity> parent,
113 const std::vector<std::string> &labels, SelectionMode selectionMode,
114 std::function<void(std::size_t)> onChange)
115 {
116 return withLabels(labels)
117 .withSelectionMode(selectionMode)
118 .withOnChange(onChange)
119 .registerEntity(parent);
120 }
121
122 SegmentedButton::SegmentedButton(ecs::ComponentRegistry &registry,
123 const std::vector<std::string> &labels,
124 SelectionMode selectionMode,
125 std::function<void(std::size_t)> onChange)
126 : SegmentedButton(registry, segmentsFromLabels(labels), selectionMode,
127 std::move(onChange))
128 {
129 }
130
131 SegmentedButton::SegmentedButton(ecs::ComponentRegistry &registry,
132 const std::vector<Segment> &segments,
133 SelectionMode selectionMode,
134 std::function<void(std::size_t)> onChange)
135 : ecs::ParentEntityFiller<components::Transform, components::Bound,
136 components::Layout,
137 components::SegmentedButton>(registry)
138 {
141 .setSegments(segments)
142 .setSelectionMode(selectionMode)
143 .setOnChange(std::move(onChange));
145 getIdentifier());
146 }
147
148 SegmentedButton &SegmentedButton::setSegmentDisabled(std::size_t index,
149 bool disabled)
150 {
152 getComponentRegistry(), getIdentifier(), index, disabled);
153 return *this;
154 }
155
159
160 SegmentedButton &SegmentedButton::select(std::size_t index)
161 {
163 getIdentifier(), index);
164 return *this;
165 }
166
167 SegmentedButton &SegmentedButton::deselect(std::size_t index)
168 {
170 getIdentifier(), index);
171 return *this;
172 }
173
174 bool SegmentedButton::isSelected(std::size_t index) const
175 {
177 getIdentifier(), index);
178 }
179
180 std::vector<std::size_t> SegmentedButton::getSelectedIndices(void) const
181 {
183 getIdentifier());
184 }
185
186 std::size_t SegmentedButton::getSegmentCount(void) const
187 {
188 return getComponentRegistry()
190 .getButtons()
191 .size();
192 }
193
196 {
197 const auto &buttons =
200 .getButtons();
201
202 return index < buttons.size() ? buttons[index]
204 }
205
206 void SegmentedButton::build(void)
207 {
208 auto &registry = getComponentRegistry();
209 // Copy: building children may move the component storage.
210 const std::vector<Segment> segments =
212 .getSegments();
213
214 std::vector<ecs::Entity::Identifier> identifiers;
215 Button::Builder buttonBuilder(registry, *this);
216 for (const auto &segment: segments) {
217 // systems::SegmentedButton::install wires the click and applies
218 // the segment options.
219 const auto button = buttonBuilder.makeButton(
220 shared_from_this(), segment.label, std::function<void(void)>(),
221 Button::Color::Segmented, Button::Shape::Square,
222 Button::Size::Medium, false);
223 identifiers.push_back(button->getIdentifier());
224 }
225
227 .setButtons(identifiers);
230 }
231} // namespace guillaume::entities
State of a segmented button: a row of joined toggle buttons of which one (or several) can be selected...
SegmentedButton & setOnChange(ChangeHandler handler)
Set the callback run when the user presses a segment.
SegmentedButton & setSelectionMode(SelectionMode mode)
Choose how many segments can be selected at once.
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
Configures, creates and registers Button widgets.
Definition button.hpp:97
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures, creates and registers SegmentedButton widgets.
std::shared_ptr< SegmentedButton > buildEntity(void) override
Build a SegmentedButton, with one button per segment, from the current configuration,...
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Builder & withLabels(const std::vector< std::string > &labels)
Describe the segments by their labels only.
void reset(void) override
Restore the default configuration.
Builder & withSelectionMode(const SelectionMode &selectionMode)
Set the selection mode.
std::shared_ptr< SegmentedButton > makeSegmentedButton(std::shared_ptr< Entity > parent, const std::vector< std::string > &labels, SelectionMode selectionMode, std::function< void(std::size_t)> onChange)
Build a segmented button from labels, register it under parent and reset the builder.
Builder & withOnChange(std::function< void(std::size_t)> onChange)
Set the change handler.
Builder & withSegments(const std::vector< Segment > &segments)
Describe the segments one by one (label, icon, disabled, accessibility label).
A row of joined toggle buttons (segments) from which the user picks one option, or several.
SegmentedButton & setSegmentDisabled(std::size_t index, bool disabled)
Enable or disable one segment. Restyles at once.
SegmentedButton & select(std::size_t index)
Select a segment. Restyles at once.
ecs::Entity::Identifier getSegmentIdentifier(std::size_t index) const
Get the identifier of the Button child of one segment.
SegmentedButton & deselect(std::size_t index)
Deselect a segment. Restyles at once.
components::SegmentedButton::SelectionMode SelectionMode
std::size_t getSegmentCount(void) const
Get the number of segments.
static std::shared_ptr< SegmentedButton > create(Arguments &&...arguments)
Create a segmented button and one button per segment, without registering it in an entity registry.
components::SegmentedButton::Segment Segment
bool isSelected(std::size_t index) const
Get whether a segment is selected.
std::vector< std::size_t > getSelectedIndices(void) const
Get the indices of all selected segments.
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 apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Lay out the row from the current selection of the buttons.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a segmented button.
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.
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
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46