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
23#include "guillaume/entities/checkbox.hpp"
24
25#include <memory>
26#include <vector>
27
28#include "guillaume/entities/content_helpers.hpp"
29#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/components/hand_hover_interaction.hpp"
33#include "guillaume/components/mouse_hover_interaction.hpp"
35#include "guillaume/systems/checkbox.hpp"
36#include "guillaume/theme.hpp"
37
38namespace guillaume::entities
39{
41 ecs::EntityRegistry &entityRegistry)
42 : EntityBuilderBase<Checkbox>(componentRegistry, entityRegistry)
43 {
44 reset();
45 }
46
47 std::shared_ptr<Checkbox> Checkbox::Builder::buildEntity(void)
48 {
49 auto entity = Checkbox::create(this->getComponentRegistry(), _config,
50 _state, _variant, _label);
51 return entity;
52 }
53
55 {
56 _config = SurfaceConfig();
57 _state = State::Unchecked;
58 _variant = Variant::Enabled;
59 _label.clear();
60
61 _config.borderRadius = 2.0f;
63 _config.mainAxisAlignment =
65 _config.crossAxisAlignment =
67 _config.spacing = 8.0f;
68 _config.padding = 0.0f;
69 _config.setFixedWidth(18.0f);
70 _config.setFixedHeight(18.0f);
71 }
72
74 Checkbox::Builder::withPose(const utility::graphic::PoseF &pose)
75 {
76 _config.pose = pose;
77 return *this;
78 }
79
81 {
82 _state = state;
83 return *this;
84 }
85
87 {
88 _variant = variant;
89 return *this;
90 }
91
93 {
94 _label = label;
95 return *this;
96 }
97
98 std::shared_ptr<Checkbox>
99 Checkbox::Builder::makeCheckbox(std::shared_ptr<ecs::Entity> parent,
100 State state, Variant variant,
101 const std::string &label)
102 {
103 return withState(state)
104 .withVariant(variant)
105 .withLabel(label)
106 .registerEntity(parent);
107 }
108
109 Checkbox::Checkbox(ecs::ComponentRegistry &registry,
110 const SurfaceConfig &config, State state,
111 Variant variant, const std::string &label)
112 : SurfaceBase<components::Selectable, components::Line,
113 components::Checkbox>(registry, config)
114 {
117 .setState(state)
118 .setVariant(variant)
119 .setLabel(label);
122 }
123
124 Checkbox &Checkbox::toggle(void)
125 {
127 return *this;
128 }
129
135
136 Checkbox &
137 Checkbox::setOnValueChange(std::function<void(State)> onValueChange)
138 {
141 .setOnValueChange(std::move(onValueChange));
142 return *this;
143 }
144
146 std::optional<utility::graphic::Color32Bit> color)
147 {
150 .setCheckedColor(color);
152 return *this;
153 }
154
156 std::optional<utility::graphic::Color32Bit> color)
157 {
160 .setUncheckedColor(color);
162 return *this;
163 }
164
165 Checkbox &Checkbox::setState(State state)
166 {
168 state);
169 return *this;
170 }
171
178
187
194
195 Checkbox &Checkbox::setLabel(const std::string &label)
196 {
197 auto &registry = getComponentRegistry();
198 auto &checkbox =
200
201 checkbox.setLabel(label);
202 if (registry.hasComponent<components::Text>(
203 checkbox.getLabelEntity())) {
204 registry.getComponent<components::Text>(checkbox.getLabelEntity())
205 .setContent(label);
206 }
208 return *this;
209 }
210
211 bool Checkbox::isChecked(void) const
212 {
213 return getComponentRegistry()
215 .isSelected();
216 }
217
218 void Checkbox::build(void)
219 {
220 auto &registry = getComponentRegistry();
221 auto &checkbox =
223
224 // Only the check glyph is laid out inside the box; the label is placed
225 // beside it by systems::Checkbox.
226 const auto checkIcon =
227 buildIcon(registry, *this, shared_from_this(), "check", 14.0f,
228 schemeColor(SchemeColorRole::OnPrimary));
229 std::vector<std::shared_ptr<ecs::Entity>> children;
230 if (checkIcon != nullptr) {
231 checkbox.setCheckIcon(checkIcon->getIdentifier());
232 children.push_back(checkIcon);
233 }
234
235 if (!checkbox.getLabel().empty()) {
236 const auto label =
237 buildText(registry, *this, shared_from_this(),
238 checkbox.getLabel(), theme::TypeRole::BodyLarge,
239 schemeColor(SchemeColorRole::OnSurface));
240 if (label != nullptr) {
241 checkbox.setLabelEntity(label->getIdentifier());
242 }
243 }
244
245 setChildren(children);
247 }
248
249} // namespace guillaume::entities
State of a checkbox: checked state, variant, label, color overrides and the identifiers of its check ...
Definition checkbox.hpp:63
Checkbox & setLabel(const std::string &label)
Set the label text.
Definition checkbox.cpp:62
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
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.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
Configures, creates and registers Checkbox widgets.
Definition checkbox.hpp:108
Builder & withState(State state)
Set the state the checkbox starts in.
Definition checkbox.cpp:80
void reset(void) override
Restore the default configuration.
Definition checkbox.cpp:54
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Definition checkbox.cpp:40
Builder & withVariant(Variant variant)
Set the visual variant.
Definition checkbox.cpp:86
Builder & withPose(const utility::graphic::PoseF &pose)
Set the position and orientation of the box.
Definition checkbox.cpp:74
Builder & withLabel(const std::string &label)
Set the label shown to the right of the box.
Definition checkbox.cpp:92
std::shared_ptr< Checkbox > makeCheckbox(std::shared_ptr< ecs::Entity > parent, State state, Variant variant, const std::string &label)
Build a checkbox, register it under parent and reset the builder.
Definition checkbox.cpp:99
std::shared_ptr< Checkbox > buildEntity(void) override
Build a Checkbox, with its check glyph and label children, from the current configuration,...
Definition checkbox.cpp:47
A box the user checks, unchecks or leaves indeterminate, with an optional label next to it.
Definition checkbox.hpp:92
bool isChecked(void) const
Check whether the checkbox's components::Selectable is selected.
Definition checkbox.cpp:211
components::Checkbox::Variant Variant
Definition checkbox.hpp:99
Checkbox & setCheckedColor(std::optional< utility::graphic::Color32Bit > color)
Override the fill color of the checked and indeterminate box (Paper color). Restyles at once.
Definition checkbox.cpp:145
State getState(void) const
Get the state.
Definition checkbox.cpp:172
Variant getVariant(void) const
Get the visual variant.
Definition checkbox.cpp:188
Checkbox & setVariant(Variant variant)
Set the visual variant. Restyles at once.
Definition checkbox.cpp:179
Checkbox & setOnValueChange(std::function< void(State)> onValueChange)
Set the handler called when the user toggles the box, or when toggle() is called.
Definition checkbox.cpp:137
Checkbox & toggle(void)
Toggle as a user activation would: a checked box becomes unchecked, an unchecked or indeterminate box...
Definition checkbox.cpp:124
bool isDisabled(void) const
Check whether the checkbox ignores input.
Definition checkbox.cpp:130
Checkbox & setState(State state)
Set the state. Restyles at once.
Definition checkbox.cpp:165
components::Checkbox::State State
Checked state: Unchecked, Checked or Indeterminate.
Definition checkbox.hpp:95
Checkbox & setLabel(const std::string &label)
Set the label text and the accessibility label. Restyles at once.
Definition checkbox.cpp:195
static std::shared_ptr< Checkbox > create(Arguments &&...arguments)
Create a checkbox and its check glyph and label children, without registering it in an entity registr...
Definition checkbox.hpp:209
Checkbox & setUncheckedColor(std::optional< utility::graphic::Color32Bit > color)
Override the outline color of the unchecked box (Paper uncheckedColor). Restyles at once.
Definition checkbox.cpp:155
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
SurfaceBase(ecs::ComponentRegistry &registry, const SurfaceConfig &config)
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
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
static void toggle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Toggle the checkbox as a click does.
Definition checkbox.cpp:167
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