guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
checkbox.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/checkbox.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/hand_hover_interaction.hpp"
29#include "guillaume/components/layout.hpp"
30#include "guillaume/components/line.hpp"
31#include "guillaume/components/mouse_hover_interaction.hpp"
32#include "guillaume/components/selection.hpp"
36#include "guillaume/systems/layout.hpp"
37#include "guillaume/theme.hpp"
38
39namespace guillaume::systems
40{
41 namespace
42 {
43 using State = components::Checkbox::State;
44 using Variant = components::Checkbox::Variant;
48
55 void colorChild(ecs::ComponentRegistry &registry,
57 const utility::graphic::Color32Bit &color)
58 {
59 if (registry.hasComponent<components::Color>(child)) {
60 registry.getComponent<components::Color>(child).setColor(color);
61 }
62 }
63
70 void placeLabel(ecs::ComponentRegistry &registry,
71 ecs::Entity::Identifier identifier,
73 {
74 if (!registry.hasComponent<components::Bound>(label)) {
75 placement::reserveTrailingWidth(registry, identifier, 0.0f);
76 return;
77 }
78
79 const auto &box =
80 registry.getComponent<components::Bound>(identifier);
81 const auto &labelBound =
82 registry.getComponent<components::Bound>(label);
83
84 // The label starts after the 40 dp target, vertically centered
85 // on the box, so it never overlaps the 18 dp surface.
86 const float x =
87 (box.getWidth() + components::Checkbox::TouchTarget) / 2.0f
89 const float y = (box.getHeight() - labelBound.getHeight()) / 2.0f;
90 placement::placeChild(registry, identifier, label,
91 utility::graphic::PositionF(x, y, 0.0f),
92 Layout::layerOf(registry, identifier));
93 placement::reserveTrailingWidth(registry, identifier,
94 x + labelBound.getWidth()
95 - box.getWidth());
96 }
97 } // namespace
98
101 {
102 }
103
105 const ecs::Entity::Identifier &entityIdentifier)
106 {
107 ecs::ComponentRegistry *registry = &componentRegistry;
108 const ecs::Entity::Identifier identifier = entityIdentifier;
109
111 componentRegistry, identifier,
113 [registry, identifier]() {
114 toggle(*registry, identifier);
115 },
116 [registry, identifier]() {
117 apply(*registry, identifier);
118 });
119
120 // Grow the 18 dp box to the 40 dp target without growing it visually.
121 const float width =
122 componentRegistry.getComponent<components::Layout>(identifier)
123 .getFixedWidth();
124 const auto margin = static_cast<unsigned int>(
125 (components::Checkbox::TouchTarget - width) / 2.0f);
126 componentRegistry
128 .setAccessibilityMargin(utility::graphic::SizeUI(margin, margin));
129 componentRegistry
131 .setAccessibilityMargin(utility::graphic::SizeUI(margin, margin));
132
133 componentRegistry.getComponent<components::Selectable>(identifier)
134 .setOnSelectionChangedHandler([registry,
135 identifier](bool selected) {
136 auto &checkbox =
137 registry->getComponent<components::Checkbox>(identifier);
138 if (checkbox.getState() != State::Indeterminate) {
139 checkbox.setState(selected ? State::Checked
140 : State::Unchecked);
141 }
142 apply(*registry, identifier);
143 });
144 }
145
146 bool Checkbox::isDisabled(const ecs::ComponentRegistry &componentRegistry,
147 const ecs::Entity::Identifier &entityIdentifier)
148 {
149 return componentRegistry
150 .getComponent<components::Checkbox>(entityIdentifier)
151 .getVariant()
152 == Variant::Disabled
153 || !interaction::isEnabled(componentRegistry, entityIdentifier);
154 }
155
157 const ecs::Entity::Identifier &entityIdentifier,
159 {
160 componentRegistry.getComponent<components::Checkbox>(entityIdentifier)
161 .setState(state);
162 componentRegistry.getComponent<components::Selectable>(entityIdentifier)
163 .setSelected(state != State::Unchecked);
164 apply(componentRegistry, entityIdentifier);
165 }
166
168 const ecs::Entity::Identifier &entityIdentifier)
169 {
170 if (isDisabled(componentRegistry, entityIdentifier)) {
171 return;
172 }
173
174 const auto &checkbox =
175 componentRegistry.getComponent<components::Checkbox>(
176 entityIdentifier);
177 setState(componentRegistry, entityIdentifier,
178 checkbox.getState() == State::Checked ? State::Unchecked
179 : State::Checked);
180 // Copy: the callback may replace itself.
181 const auto onValueChange = checkbox.getOnValueChange();
182 if (onValueChange) {
183 onValueChange(checkbox.getState());
184 }
185 }
186
188 const ecs::Entity::Identifier &entityIdentifier)
189 {
190 const auto &checkbox =
191 componentRegistry.getComponent<components::Checkbox>(
192 entityIdentifier);
193 const State state = checkbox.getState();
194 const Variant variant = checkbox.getVariant();
195 auto &color =
196 componentRegistry.getComponent<components::Color>(entityIdentifier);
197 auto &borders = componentRegistry.getComponent<components::Borders>(
198 entityIdentifier);
199 auto &line =
200 componentRegistry.getComponent<components::Line>(entityIdentifier);
201
202 interaction::setEnabled(componentRegistry, entityIdentifier,
203 variant != Variant::Disabled);
204
205 const bool disabled = isDisabled(componentRegistry, entityIdentifier);
206 const bool errored = (variant == Variant::Error);
207
208 componentRegistry
209 .getComponent<components::Accessibility>(entityIdentifier)
210 .setLabel(checkbox.getLabel())
211 .setChecked(state == State::Checked
213 : (state == State::Indeterminate
216
217 line.setThickness(state == State::Indeterminate ? 2.0f : 0.0f);
218
219 const auto onColorRole =
220 errored ? SchemeColorRole::OnError : SchemeColorRole::OnPrimary;
221 // The indeterminate dash shares the box entity, so it must carry its
222 // own color instead of the container fill.
223 line.setColor(schemeColor(onColorRole));
224 const auto containerRole =
225 errored ? SchemeColorRole::Error : SchemeColorRole::Primary;
226
227 switch (state) {
228 case State::Checked:
229 case State::Indeterminate: {
230 if (disabled) {
231 color.setColor(disabledContentColor());
232 } else {
233 color.setColor(interaction::stateLayerColor(
234 componentRegistry, entityIdentifier,
235 errored ? schemeColor(containerRole)
236 : checkbox.getCheckedColor().value_or(
237 schemeColor(containerRole)),
238 schemeColor(onColorRole)));
239 }
240 borders.setColor(transparentColor()).setWidth(0.0f);
241 break;
242 }
243 case State::Unchecked:
244 default: {
245 const auto outline = errored
246 ? schemeColor(SchemeColorRole::Error)
247 : checkbox.getUncheckedColor().value_or(
248 schemeColor(SchemeColorRole::OnSurfaceVariant));
249 color.setColor(disabled
252 componentRegistry, entityIdentifier,
253 transparentColor(), outline));
254 borders.setWidth(2.0f);
255 borders.setColor(disabled ? disabledContentColor() : outline);
256 break;
257 }
258 }
259
260 colorChild(componentRegistry, checkbox.getCheckIcon(),
261 state == State::Checked ? schemeColor(onColorRole)
262 : transparentColor());
263 colorChild(componentRegistry, checkbox.getLabelEntity(),
264 disabled
266 : schemeColor(errored ? SchemeColorRole::Error
267 : SchemeColorRole::OnSurface));
268
269 placeLabel(componentRegistry, entityIdentifier,
270 checkbox.getLabelEntity());
271 }
272
273 void Checkbox::update(const ecs::Entity::Identifier &entityIdentifier)
274 {
275 if (!requireComponent<components::Checkbox>(entityIdentifier)) {
276 return;
277 }
278
279 apply(getComponentRegistry(), entityIdentifier);
280 }
281} // 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 & setChecked(std::optional< CheckedState > checked)
Set whether the entity is ticked.
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Borders & setColor(const utility::graphic::Color32Bit &color)
Set the color of the stroke.
Definition borders.cpp:107
State of a checkbox: checked state, variant, label, color overrides and the identifiers of its check ...
Definition checkbox.hpp:63
static constexpr float LabelGap
Gap between the touch target and the label, in dp.
Definition checkbox.hpp:89
static constexpr float TouchTarget
Edge of the square touch target centered on the box, in dp.
Definition checkbox.hpp:86
State getState(void) const noexcept
Get the checked state.
Definition checkbox.cpp:29
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Color & setColor(const utility::graphic::Color32Bit &color)
Set the color.
Definition color.cpp:37
Whether an XR hand (controller) aims at an entity, what to call when the aim enters or leaves,...
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
A straight stroke across the width of the entity: a divider, an indicator, the underline of a text fi...
Definition line.hpp:62
Whether the mouse pointer is over an entity, what to call when it enters or leaves,...
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
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.
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...
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one checkbox from its component: colors, accessibility and label position.
Definition checkbox.cpp:187
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a checkbox.
Definition checkbox.cpp:104
Checkbox(void)
Create the system for the Layout phase, matching entities that have a components::Checkbox.
Definition checkbox.cpp:99
static void setState(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, components::Checkbox::State state)
Set the state of a checkbox from code and show it at once.
Definition checkbox.cpp:156
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the checkbox ignores input and is drawn disabled.
Definition checkbox.cpp:146
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one checkbox.
Definition checkbox.cpp:273
static void toggle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Toggle the checkbox as a click does.
Definition checkbox.cpp:167
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
State. Every class here is plain data attached to an entity.
@ Checkbox
Two- or three-state checkbox.
@ Mixed
Indeterminate: neither checked nor unchecked.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
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.
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 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.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &content)
Container color of a widget in its current input state.
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.