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#include "guillaume/systems/switch.hpp"
23
24#include "guillaume/components/accessibility.hpp"
25#include "guillaume/components/borders.hpp"
26#include "guillaume/components/bound.hpp"
27#include "guillaume/components/color.hpp"
28#include "guillaume/components/value.hpp"
32#include "guillaume/systems/layout.hpp"
33#include "guillaume/theme.hpp"
34
35namespace guillaume::systems
36{
37 namespace
38 {
39 using Variant = components::Switch::Variant;
44
51 void colorChild(ecs::ComponentRegistry &registry,
53 const utility::graphic::Color32Bit &color)
54 {
55 if (registry.hasComponent<components::Color>(child)) {
56 registry.getComponent<components::Color>(child).setColor(color);
57 }
58 }
59 } // namespace
60
63 {
64 }
65
66 void Switch::install(ecs::ComponentRegistry &componentRegistry,
67 const ecs::Entity::Identifier &entityIdentifier)
68 {
69 ecs::ComponentRegistry *registry = &componentRegistry;
70 const ecs::Entity::Identifier identifier = entityIdentifier;
71
73 componentRegistry, identifier,
75 [registry, identifier]() {
76 toggle(*registry, identifier);
77 },
78 [registry, identifier]() {
79 apply(*registry, identifier);
80 });
81
82 auto &value =
83 componentRegistry.getComponent<components::Value>(identifier);
84 value.setMin(0.0f).setMax(1.0f).setStep(1.0f);
85 value.setOnChangedHandler([registry, identifier](float) {
86 apply(*registry, identifier);
87 });
88 }
89
90 bool Switch::isDisabled(const ecs::ComponentRegistry &componentRegistry,
91 const ecs::Entity::Identifier &entityIdentifier)
92 {
93 return componentRegistry
94 .getComponent<components::Switch>(entityIdentifier)
95 .getVariant()
96 == Variant::Disabled
97 || !interaction::isEnabled(componentRegistry, entityIdentifier);
98 }
99
100 bool Switch::isChecked(const ecs::ComponentRegistry &componentRegistry,
101 const ecs::Entity::Identifier &entityIdentifier)
102 {
103 return componentRegistry
104 .getComponent<components::Value>(entityIdentifier)
105 .getValue()
106 >= 0.5f;
107 }
108
110 const ecs::Entity::Identifier &entityIdentifier,
111 bool checked)
112 {
113 componentRegistry.getComponent<components::Value>(entityIdentifier)
114 .setValue(checked ? 1.0f : 0.0f);
115 }
116
117 void Switch::toggle(ecs::ComponentRegistry &componentRegistry,
118 const ecs::Entity::Identifier &entityIdentifier)
119 {
120 if (isDisabled(componentRegistry, entityIdentifier)) {
121 return;
122 }
123
124 setChecked(componentRegistry, entityIdentifier,
125 !isChecked(componentRegistry, entityIdentifier));
126 // Copy: the callback may replace itself.
127 const auto onValueChange =
128 componentRegistry.getComponent<components::Switch>(entityIdentifier)
129 .getOnValueChange();
130 if (onValueChange) {
131 onValueChange(isChecked(componentRegistry, entityIdentifier));
132 }
133 }
134
135 void Switch::apply(ecs::ComponentRegistry &componentRegistry,
136 const ecs::Entity::Identifier &entityIdentifier)
137 {
138 const auto &config = componentRegistry.getComponent<components::Switch>(
139 entityIdentifier);
140 const bool on = isChecked(componentRegistry, entityIdentifier);
141 const auto layer = Layout::layerOf(componentRegistry, entityIdentifier);
142 const auto thumb = config.getThumb();
143 const auto label = config.getLabelEntity();
144
145 // Placement: the thumb slides along the track, the icon stays
146 // centered on the thumb, the label sits beside the track.
147 if (componentRegistry.hasComponent<components::Color>(thumb)) {
148 placement::placeNormalized(componentRegistry, entityIdentifier,
149 thumb, on ? 1.0f : 0.0f,
151 colorChild(componentRegistry, thumb,
152 config.getVariant() == Variant::Disabled
154 : schemeColor(on ? SchemeColorRole::OnPrimary
155 : SchemeColorRole::Outline));
156 placement::centerChild(componentRegistry, thumb, config.getIcon(),
157 Layout::layerOf(componentRegistry, thumb));
158 }
159 if (componentRegistry.hasComponent<components::Bound>(label)) {
160 const auto &track =
161 componentRegistry.getComponent<components::Bound>(
162 entityIdentifier);
163 const auto &labelBound =
164 componentRegistry.getComponent<components::Bound>(label);
166 componentRegistry, entityIdentifier, label,
167 utility::graphic::PositionF(
168 track.getWidth() + components::Switch::LabelGap,
169 (track.getHeight() - labelBound.getHeight()) / 2.0f, 0.0f),
170 layer);
171 placement::reserveTrailingWidth(componentRegistry, entityIdentifier,
173 + labelBound.getWidth());
174 } else {
175 placement::reserveTrailingWidth(componentRegistry, entityIdentifier,
176 0.0f);
177 }
178
179 // Style.
180 interaction::setEnabled(componentRegistry, entityIdentifier,
181 config.getVariant() != Variant::Disabled);
182 componentRegistry
183 .getComponent<components::Accessibility>(entityIdentifier)
186 .setLabel(config.getLabel());
187
188 const bool disabled = isDisabled(componentRegistry, entityIdentifier);
189 auto &color =
190 componentRegistry.getComponent<components::Color>(entityIdentifier);
191 if (disabled) {
192 color.setColor(disabledContainerColor());
193 } else {
194 color.setColor(schemeColor(on ? SchemeColorRole::Primary
195 : SchemeColorRole::SurfaceVariant));
196 }
197 componentRegistry.getComponent<components::Borders>(entityIdentifier)
198 .setColor(on ? transparentColor()
199 : schemeColor(SchemeColorRole::Outline));
200
201 colorChild(componentRegistry, config.getIcon(),
202 disabled
204 : schemeColor(on ? SchemeColorRole::OnPrimary
205 : SchemeColorRole::OnSurfaceVariant));
206 colorChild(componentRegistry, label,
207 disabled ? disabledContentColor()
208 : schemeColor(SchemeColorRole::OnSurface));
209 }
210
211 void Switch::update(const ecs::Entity::Identifier &entityIdentifier)
212 {
213 if (!requireComponent<components::Switch>(entityIdentifier)) {
214 return;
215 }
216
217 apply(getComponentRegistry(), entityIdentifier);
218 }
219} // namespace guillaume::systems
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setLabel(const std::string &label)
Set the name announced for the entity, such as "Close".
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Configuration of a switch: its variant, its thumb icon, its label and the identifiers of its thumb,...
Definition switch.hpp:56
static constexpr float ThumbInset
Inset of the thumb from the track ends, in dp.
Definition switch.hpp:75
static constexpr float LabelGap
Gap between the track and the label, in dp.
Definition switch.hpp:78
A number kept inside a range, with a callback run when it changes.
Definition value.hpp:56
Value & setMin(float min)
Set the lower bound of the range.
Definition value.cpp:35
Value & setOnChangedHandler(const Handler &handler)
Set the callback run when the value changes.
Definition value.cpp:111
Value & setMax(float max)
Set the upper bound of the range.
Definition value.cpp:51
Value & setStep(float step)
Set the snapping step.
Definition value.cpp:86
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.
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
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static std::uint32_t layerOf(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Definition layout.cpp:90
Switch(void)
Create the system for the Layout phase, matching entities that have a components::Switch.
Definition switch.cpp:61
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
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one switch.
Definition switch.cpp:211
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
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
void centerChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, std::uint32_t parentLayer)
Center a child inside the bound of its parent, on both axes.
void placeChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, const utility::graphic::PositionF &offset, std::uint32_t parentLayer)
Place a child at an offset from the position of its parent.
void reserveTrailingWidth(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, float width)
Reserve layout room after an entity's Bound.
void placeNormalized(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, float normalized, float inset, std::uint32_t parentLayer)
Place a child along the width of its parent from a 0 to 1 value, vertically centered.
utility::graphic::Color32Bit disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Rules. Every class here runs each frame on the entities that have the components it declares.
utility::graphic::Color32Bit disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isEnabled(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
void makeInteractive(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
void setEnabled(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, bool enabled)
Enable or disable an entity.
Placement helpers for the children systems::Layout does not arrange.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.