guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
switch.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 <functional>
26#include <string>
27
28#include "guillaume/ecs/component.hpp"
29#include "guillaume/ecs/entity.hpp"
30
32{
33
55 class Switch: public ecs::Component
56 {
57 public:
65 enum class Variant { Enabled, Disabled, WithIcon };
66
69 using ValueChangeHandler = std::function<void(bool)>;
70
72 static constexpr float ThumbSize = 24.0f;
73
75 static constexpr float ThumbInset = 4.0f;
76
78 static constexpr float LabelGap = 16.0f;
79
80 private:
81 Variant _variant { Variant::Enabled };
82 std::string _iconGlyph {};
83 std::string _label {};
86 };
87
90 };
91
92 ecs::Entity::Identifier _labelEntity {
94 };
95
96 ValueChangeHandler _onValueChange {};
97
98 public:
103 Switch(void) = default;
104
108 ~Switch(void) = default;
109
114 Variant getVariant(void) const noexcept;
115
124 Switch &setVariant(Variant variant);
125
130 const std::string &getIconGlyph(void) const noexcept;
131
141 Switch &setIconGlyph(const std::string &glyph);
142
147 const std::string &getLabel(void) const noexcept;
148
158 Switch &setLabel(const std::string &label);
159
165 ecs::Entity::Identifier getThumb(void) const noexcept;
166
175
181 ecs::Entity::Identifier getIcon(void) const noexcept;
182
192
198 ecs::Entity::Identifier getLabelEntity(void) const noexcept;
199
208
213 const ValueChangeHandler &getOnValueChange(void) const noexcept;
214
225 };
226
227} // namespace guillaume::components
static constexpr float ThumbInset
Inset of the thumb from the track ends, in dp.
Definition switch.hpp:75
ecs::Entity::Identifier getIcon(void) const noexcept
Get the child entity that draws the thumb icon.
Definition switch.cpp:85
static constexpr float LabelGap
Gap between the track and the label, in dp.
Definition switch.hpp:78
Switch & setOnValueChange(ValueChangeHandler handler)
Set the callback run when the user toggles the switch.
Definition switch.cpp:119
Variant getVariant(void) const noexcept
Get the visual variant.
Definition switch.cpp:29
Switch(void)=default
Create the configuration of an enabled switch with no icon and no label.
Switch & setThumb(ecs::Entity::Identifier identifier)
Record which child entity is the thumb.
Definition switch.cpp:76
Switch & setLabelEntity(ecs::Entity::Identifier identifier)
Record which child entity draws the label.
Definition switch.cpp:104
~Switch(void)=default
Default destructor.
Switch & setIconGlyph(const std::string &glyph)
Set the name of the glyph drawn on the thumb.
Definition switch.cpp:48
const std::string & getIconGlyph(void) const noexcept
Get the name of the glyph drawn on the thumb.
Definition switch.cpp:43
const std::string & getLabel(void) const noexcept
Get the label text.
Definition switch.cpp:57
ecs::Entity::Identifier getThumb(void) const noexcept
Get the child entity drawn as the thumb.
Definition switch.cpp:71
ecs::Entity::Identifier getLabelEntity(void) const noexcept
Get the child entity that draws the label.
Definition switch.cpp:99
const ValueChangeHandler & getOnValueChange(void) const noexcept
Get the callback run when the user toggles the switch.
Definition switch.cpp:114
Switch & setLabel(const std::string &label)
Set the label text.
Definition switch.cpp:62
Switch & setVariant(Variant variant)
Choose the visual variant.
Definition switch.cpp:34
Switch & setIcon(ecs::Entity::Identifier identifier)
Record which child entity draws the thumb icon.
Definition switch.cpp:90
std::function< void(bool)> ValueChangeHandler
Definition switch.hpp:69
static constexpr float ThumbSize
Diameter of the thumb, in dp.
Definition switch.hpp:72
Base class of every component: a piece of state attached to an entity, plus a "changed" flag.
Definition component.hpp:72
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
State. Every class here is plain data attached to an entity.