guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
switch.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/entities/switch.hpp"
25
26#include <functional>
27#include <memory>
28#include <vector>
29
30#include <utility/event/hand_button_event.hpp>
31#include <utility/event/mouse_button_event.hpp>
32
33#include "guillaume/ecs/entity_filler.hpp"
34
35#include "guillaume/components/bound.hpp"
36#include "guillaume/components/color.hpp"
37#include "guillaume/components/ellipse.hpp"
38#include "guillaume/components/selection.hpp"
39#include "guillaume/components/transform.hpp"
40
41#include "guillaume/entities/content_helpers.hpp"
43#include "guillaume/entities/builder_base.hpp"
45#include "guillaume/systems/switch.hpp"
46#include "guillaume/theme.hpp"
47
48namespace guillaume::entities
49{
50 namespace
51 {
52 constexpr float TrackWidth = 52.0f;
53 constexpr float TrackHeight = 32.0f;
54 constexpr float ThumbSize = components::Switch::ThumbSize;
55
59 class SwitchThumb:
60 public ecs::EntityFiller<components::Transform, components::Bound,
61 components::Color, components::Ellipse>
62 {
63 public:
69 SwitchThumb(ecs::ComponentRegistry &registry,
70 const utility::graphic::Color32Bit &color)
71 : ecs::EntityFiller<components::Transform, components::Bound,
72 components::Color, components::Ellipse>(
73 registry)
74 {
75 getComponentRegistry()
76 .getComponent<components::Bound>(getIdentifier())
77 .setWidth(ThumbSize)
78 .setHeight(ThumbSize);
79
80 getComponentRegistry()
81 .getComponent<components::Color>(getIdentifier())
82 .setColor(color);
83 }
84
88 ~SwitchThumb(void) override = default;
89 };
90 } // namespace
91
93 ecs::EntityRegistry &entityRegistry)
94 : EntityBuilderBase<Switch>(componentRegistry, entityRegistry)
95 {
96 reset();
97 }
98
99 std::shared_ptr<Switch> Switch::Builder::buildEntity(void)
100 {
101 auto entity = Switch::create(this->getComponentRegistry(), _config,
102 _variant, _checked, _iconGlyph, _label);
103 return entity;
104 }
105
107 {
108 _config = SurfaceConfig();
109 _variant = Variant::Enabled;
110 _checked = false;
111 _iconGlyph.clear();
112 _label.clear();
113
114 _config.borderRadius = TrackHeight * 0.5f;
116 _config.mainAxisAlignment =
118 _config.crossAxisAlignment =
120 _config.spacing = 8.0f;
121 _config.padding = 0.0f;
122 _config.setFixedWidth(TrackWidth);
123 _config.setFixedHeight(TrackHeight);
124 }
125
127 Switch::Builder::withPose(const utility::graphic::PoseF &pose)
128 {
129 _config.pose = pose;
130 return *this;
131 }
132
134 {
135 _variant = variant;
136 return *this;
137 }
138
140 {
141 _checked = checked;
142 return *this;
143 }
144
145 Switch::Builder &Switch::Builder::withIcon(const std::string &iconGlyph)
146 {
147 _iconGlyph = iconGlyph;
148 return *this;
149 }
150
152 {
153 _label = label;
154 return *this;
155 }
156
157 std::shared_ptr<Switch> Switch::Builder::makeSwitch(
158 std::shared_ptr<ecs::Entity> parent, Variant variant, bool checked,
159 const std::string &iconGlyph, const std::string &label)
160 {
161 return withVariant(variant)
162 .withChecked(checked)
163 .withIcon(iconGlyph)
164 .withLabel(label)
165 .registerEntity(parent);
166 }
167
168 Switch::Switch(ecs::ComponentRegistry &registry,
169 const SurfaceConfig &config, Variant variant, bool checked,
170 const std::string &iconGlyph, const std::string &label)
171 : SurfaceBase<components::Value, components::MouseButtonInteraction,
172 components::HandButtonInteraction, components::Switch>(
173 registry, config)
174 {
177 .setVariant(variant)
178 .setIconGlyph(iconGlyph)
179 .setLabel(label);
182 checked);
184 }
185
194
201
202 Switch &Switch::setChecked(bool checked)
203 {
205 checked);
206 return *this;
207 }
208
209 bool Switch::isChecked(void) const
210 {
212 getIdentifier());
213 }
214
215 Switch &Switch::toggle(void)
216 {
218 return *this;
219 }
220
221 Switch &Switch::setOnValueChange(std::function<void(bool)> onValueChange)
222 {
225 .setOnValueChange(std::move(onValueChange));
226 return *this;
227 }
228
229 bool Switch::isDisabled(void) const
230 {
232 getIdentifier());
233 }
234
235 Switch &Switch::setLabel(const std::string &label)
236 {
237 auto &registry = getComponentRegistry();
238 auto &config =
240
241 config.setLabel(label);
242 if (registry.hasComponent<components::Text>(config.getLabelEntity())) {
243 registry.getComponent<components::Text>(config.getLabelEntity())
244 .setContent(label);
245 }
247 return *this;
248 }
249
250 void Switch::build(void)
251 {
252 auto &registry = getComponentRegistry();
253 auto &config =
255
256 const auto thumb = std::make_shared<SwitchThumb>(
257 registry, schemeColor(SchemeColorRole::Outline));
258 thumb->setParent(shared_from_this());
259 this->addEntity(thumb);
260 config.setThumb(thumb->getIdentifier());
261
262 if (!config.getIconGlyph().empty()) {
263 const auto icon =
264 buildIcon(registry, *this, thumb, config.getIconGlyph(), 16.0f,
265 schemeColor(SchemeColorRole::OnPrimary));
266 if (icon != nullptr) {
267 config.setIcon(icon->getIdentifier());
268 }
269 }
270
271 if (!config.getLabel().empty()) {
272 const auto label =
273 buildText(registry, *this, shared_from_this(),
274 config.getLabel(), theme::TypeRole::BodyLarge,
275 schemeColor(SchemeColorRole::OnSurface));
276 if (label != nullptr) {
277 config.setLabelEntity(label->getIdentifier());
278 }
279 }
280
281 // The thumb and the label are positioned by systems::Switch; neither
282 // is a layout child of the track.
283 setChildren({});
285 }
286
287} // namespace guillaume::entities
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Configuration of a switch: its variant, its thumb icon, its label and the identifiers of its thumb,...
Definition switch.hpp:56
static constexpr float ThumbSize
Diameter of the thumb, in dp.
Definition switch.hpp:72
A run of text: its content, its font metrics and the position of the editing cursor.
Definition text.hpp:66
Text & setContent(const std::string &content)
Replace the text.
Definition text.cpp:34
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.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures Switch widgets and registers them in the scene's entity registry.
Definition switch.hpp:109
Builder & withChecked(bool checked)
Choose whether the switch starts on.
Definition switch.cpp:139
Builder & withVariant(Variant variant)
Choose whether the switch reacts to input.
Definition switch.cpp:133
Builder & withIcon(const std::string &iconGlyph)
Draw an icon on the thumb.
Definition switch.cpp:145
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Definition switch.cpp:92
std::shared_ptr< Switch > makeSwitch(std::shared_ptr< ecs::Entity > parent, Variant variant, bool checked, const std::string &iconGlyph, const std::string &label)
Build a switch and its children, register it under parent and reset the builder.
Definition switch.cpp:157
Builder & withLabel(const std::string &label)
Set the text shown to the right of the track.
Definition switch.cpp:151
void reset(void) override
Restore the defaults: default pose, the Enabled variant, off, no icon, no label, and a 52 x 32 dp tra...
Definition switch.cpp:106
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the switch is.
Definition switch.cpp:127
std::shared_ptr< Switch > buildEntity(void) override
Build a switch and its children from the current configuration, without registering it or giving it a...
Definition switch.cpp:99
Switch & setOnValueChange(std::function< void(bool)> onValueChange)
Set the handler called when the user toggles the switch (Paper onValueChange).
Definition switch.cpp:221
Switch & setVariant(Variant variant)
Enable or disable the switch. Restyles at once.
Definition switch.cpp:186
Switch & toggle(void)
Flip the checked state, as a user activation would.
Definition switch.cpp:215
components::Switch::Variant Variant
Definition switch.hpp:102
bool isChecked(void) const
Whether the switch is on.
Definition switch.cpp:209
Variant getVariant(void) const
Get the variant of the switch.
Definition switch.cpp:195
static std::shared_ptr< Switch > create(Arguments &&...arguments)
Build a switch without registering it in an entity registry.
Definition switch.hpp:216
bool isDisabled(void) const
Check whether the switch ignores input.
Definition switch.cpp:229
Switch & setLabel(const std::string &label)
Change the text of the label. Restyles at once.
Definition switch.cpp:235
Switch & setChecked(bool checked)
Turn the switch on or off from code.
Definition switch.cpp:202
static void setChecked(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool checked)
Turn the switch on or off from code.
Definition switch.cpp:109
static bool isChecked(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the switch is on.
Definition switch.cpp:100
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a switch.
Definition switch.cpp:66
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the switch ignores input and is drawn disabled.
Definition switch.cpp:90
static void toggle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Flip the switch as a click does.
Definition switch.cpp:117
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one switch from its components: thumb position, colors and accessibility.
Definition switch.cpp:135
Makes the interaction helpers callable from guillaume::entities without qualification.
Makes the placement helpers callable from guillaume::entities without qualification.
Makes the color helpers callable from guillaume::entities without qualification.
State. Every class here is plain data attached to an entity.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
std::shared_ptr< Text > buildText(ecs::ComponentRegistry &registry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &content, float fontSize, const utility::graphic::Color32Bit &color)
Create a Text of a given font size as a child of a widget, or nothing when there is no content.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
std::shared_ptr< Icon > buildIcon(ecs::ComponentRegistry &registry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &glyphName, float fontSize, const utility::graphic::Color32Bit &color, components::Glyph::Style style=components::Glyph::Style::Outlined)
Create an Icon as a child of a widget, or nothing when there is no glyph to show.
@ BodyLarge
16 / 24, regular.
Definition typescale.hpp:60
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...
Definition surface.hpp:71