guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
segmented_button.hpp
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#pragma once
24
25#include <utility>
26#include <functional>
27#include <memory>
28#include <string>
29#include <vector>
30
31#include "guillaume/ecs/component_registry.hpp"
32#include "guillaume/entities/builder_base.hpp"
33#include "guillaume/ecs/entity_builder.hpp"
34#include "guillaume/ecs/parent_entity_filler.hpp"
35
36#include "guillaume/components/transform.hpp"
37#include "guillaume/components/bound.hpp"
38#include "guillaume/components/borders.hpp"
39#include "guillaume/components/layout.hpp"
40#include "guillaume/components/segmented_button.hpp"
41
42#include "guillaume/entities/button_base.hpp"
43
44namespace guillaume::entities
45{
46
85 class SegmentedButton:
86 public std::enable_shared_from_this<SegmentedButton>,
87 public ecs::ParentEntityFiller<components::Transform, components::Bound,
88 components::Layout,
89 components::SegmentedButton>
90 {
91 public:
95
99
104 static constexpr float MinSegmentWidth =
106
114 class Builder: public EntityBuilderBase<SegmentedButton>
115 {
116 private:
117 std::shared_ptr<SegmentedButton>
118 _segmentedButton;
119 std::vector<std::string>
120 _labels;
122 SelectionMode _selectionMode;
123 std::vector<Segment>
124 _segmentConfigs;
126 std::function<void(std::size_t)>
127 _onChange;
129
130 public:
139 Builder(ecs::ComponentRegistry &componentRegistry,
140 ecs::EntityRegistry &entityRegistry);
141
152 std::shared_ptr<SegmentedButton> buildEntity(void) override;
153
160 void reset(void) override;
161
169 Builder &withLabels(const std::vector<std::string> &labels);
170
178 Builder &withSelectionMode(const SelectionMode &selectionMode);
179
187 Builder &withOnChange(std::function<void(std::size_t)> onChange);
188
197 Builder &withSegments(const std::vector<Segment> &segments);
198
214 std::shared_ptr<SegmentedButton>
215 makeSegmentedButton(std::shared_ptr<Entity> parent,
216 const std::vector<std::string> &labels,
217 SelectionMode selectionMode,
218 std::function<void(std::size_t)> onChange);
219 };
220
230 SegmentedButton &setSegmentDisabled(std::size_t index, bool disabled);
231
246 template<typename... Arguments>
247 static std::shared_ptr<SegmentedButton> create(Arguments &&...arguments)
248 {
249 std::shared_ptr<SegmentedButton> entity(
250 new SegmentedButton(std::forward<Arguments>(arguments)...));
251 entity->build();
252 return entity;
253 }
254
258 ~SegmentedButton(void);
259
269 SegmentedButton &select(std::size_t index);
270
279 SegmentedButton &deselect(std::size_t index);
280
287 bool isSelected(std::size_t index) const;
288
294 std::vector<std::size_t> getSelectedIndices(void) const;
295
300 std::size_t getSegmentCount(void) const;
301
308 ecs::Entity::Identifier getSegmentIdentifier(std::size_t index) const;
309
310 private:
322 const std::vector<std::string> &labels,
323 SelectionMode selectionMode,
324 std::function<void(std::size_t)> onChange);
325
336 const std::vector<Segment> &segments,
337 SelectionMode selectionMode,
338 std::function<void(std::size_t)> onChange);
339
348 void build(void);
349 };
350
351} // namespace guillaume::entities
SelectionMode
How many segments can be selected at once.
static constexpr float MinSegmentWidth
Minimum width of a segment, in dp.
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Owns entities and gives the systems the list of entities to visit.
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
The base class of a widget with children: an EntityFiller that is also a ParentEntity.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
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.
static constexpr float MinSegmentWidth
Minimum width of a segment, in dp (Paper SegmentedButtons: 76 dp).
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.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
What one segment shows and whether it can be pressed.