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 <cstddef>
26#include <functional>
27#include <string>
28#include <vector>
29
30#include "guillaume/ecs/component.hpp"
31#include "guillaume/ecs/entity.hpp"
32
34{
35
61 {
62 public:
70 enum class SelectionMode { Single, Multi };
71
75 struct Segment {
76 std::string label {};
79 std::string icon {};
80 bool disabled { false };
83 std::string accessibilityLabel {};
84
90 bool operator==(const Segment &other) const = default;
91 };
92
94 using ChangeHandler = std::function<void(std::size_t)>;
95
97 static constexpr float MinSegmentWidth = 76.0f;
98
99 private:
100 std::vector<Segment> _segments {};
101 SelectionMode _selectionMode {
102 SelectionMode::Single
103 };
104
105 std::vector<ecs::Entity::Identifier>
106 _buttons {};
107 ChangeHandler _onChange {};
108
109 public:
114 SegmentedButton(void) = default;
115
119 ~SegmentedButton(void) = default;
120
125 const std::vector<Segment> &getSegments(void) const noexcept;
126
135 SegmentedButton &setSegments(const std::vector<Segment> &segments);
136
148 SegmentedButton &setSegmentDisabled(std::size_t index, bool disabled);
149
154 SelectionMode getSelectionMode(void) const noexcept;
155
165
171 const std::vector<ecs::Entity::Identifier> &
172 getButtons(void) const noexcept;
173
182 setButtons(const std::vector<ecs::Entity::Identifier> &buttons);
183
188 const ChangeHandler &getOnChange(void) const noexcept;
189
200 };
201
202} // namespace guillaume::components
SelectionMode
How many segments can be selected at once.
const ChangeHandler & getOnChange(void) const noexcept
Get the callback run when the user presses a segment.
SegmentedButton & setSegments(const std::vector< Segment > &segments)
Replace the options of every segment.
std::function< void(std::size_t)> ChangeHandler
Called with the index of the segment the user just pressed.
static constexpr float MinSegmentWidth
Minimum width of a segment, in dp.
const std::vector< Segment > & getSegments(void) const noexcept
Get the options of every segment.
SegmentedButton & setSegmentDisabled(std::size_t index, bool disabled)
Record whether one segment is disabled.
SelectionMode getSelectionMode(void) const noexcept
Get the selection mode.
SegmentedButton(void)=default
Create the state of a single-selection segmented button with no segment.
SegmentedButton & setButtons(const std::vector< ecs::Entity::Identifier > &buttons)
Record the toggle button child of each segment.
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.
~SegmentedButton(void)=default
Default destructor.
const std::vector< ecs::Entity::Identifier > & getButtons(void) const noexcept
Get the toggle button child of each segment.
Base class of every component: a piece of state attached to an entity, plus a "changed" flag.
Definition component.hpp:72
State. Every class here is plain data attached to an entity.
What one segment shows and whether it can be pressed.
bool operator==(const Segment &other) const =default
Compare two segment configurations.