guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
radio_button.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/radio_button.hpp"
24
25#include <memory>
26#include <vector>
27
28#include "guillaume/ecs/entity_filler.hpp"
29
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/ellipse.hpp"
33#include "guillaume/components/transform.hpp"
34
35#include "guillaume/entities/content_helpers.hpp"
37#include "guillaume/entities/builder_base.hpp"
39#include "guillaume/components/hand_hover_interaction.hpp"
40#include "guillaume/components/mouse_hover_interaction.hpp"
42#include "guillaume/systems/radio_button.hpp"
43#include "guillaume/theme.hpp"
44
45namespace guillaume::entities
46{
47 namespace
48 {
52 class RadioDot:
53 public ecs::EntityFiller<components::Transform, components::Bound,
54 components::Color, components::Ellipse>
55 {
56 public:
62 RadioDot(ecs::ComponentRegistry &registry,
63 const utility::graphic::Color32Bit &color)
64 : ecs::EntityFiller<components::Transform, components::Bound,
65 components::Color, components::Ellipse>(
66 registry)
67 {
68 getComponentRegistry()
69 .getComponent<components::Bound>(getIdentifier())
70 .setWidth(RadioButton::DotSize)
71 .setHeight(RadioButton::DotSize);
72
73 getComponentRegistry()
74 .getComponent<components::Color>(getIdentifier())
75 .setColor(color);
76 }
77
81 ~RadioDot(void) override = default;
82 };
83 } // namespace
84
86 ecs::EntityRegistry &entityRegistry)
87 : EntityBuilderBase<RadioButton>(componentRegistry, entityRegistry)
88 {
89 reset();
90 }
91
92 std::shared_ptr<RadioButton> RadioButton::Builder::buildEntity(void)
93 {
94 auto entity = RadioButton::create(this->getComponentRegistry(), _config,
95 _variant, _label);
96 return entity;
97 }
98
100 {
101 _config = SurfaceConfig();
102 _variant = Variant::Enabled;
103 _label.clear();
104
105 _config.borderRadius = getShapeRadius(theme::ShapeRole::Full);
107 _config.mainAxisAlignment =
109 _config.crossAxisAlignment =
111 _config.spacing = 8.0f;
112 _config.padding = 0.0f;
113 _config.setFixedWidth(20.0f);
114 _config.setFixedHeight(20.0f);
115 }
116
118 RadioButton::Builder::withPose(const utility::graphic::PoseF &pose)
119 {
120 _config.pose = pose;
121 return *this;
122 }
123
125 {
126 _variant = variant;
127 return *this;
128 }
129
131 RadioButton::Builder::withLabel(const std::string &label)
132 {
133 _label = label;
134 return *this;
135 }
136
137 std::shared_ptr<RadioButton> RadioButton::Builder::makeRadioButton(
138 std::shared_ptr<ecs::Entity> parent, Variant variant,
139 const std::string &label)
140 {
141 return withVariant(variant).withLabel(label).registerEntity(parent);
142 }
143
144 RadioButton::RadioButton(ecs::ComponentRegistry &registry,
145 const SurfaceConfig &config, Variant variant,
146 const std::string &label)
147 : SurfaceBase<components::Selectable, components::Ring,
148 components::RadioButton>(registry, config)
149 {
152 .setVariant(variant)
153 .setLabel(label);
156 }
157
163
164 RadioButton &RadioButton::setOnPress(std::function<void(void)> onPress)
165 {
168 .setOnPress(std::move(onPress));
169 return *this;
170 }
171
180
187
188 RadioButton &RadioButton::setLabel(const std::string &label)
189 {
190 auto &registry = getComponentRegistry();
191 auto &radio =
193
194 radio.setLabel(label);
195 if (registry.hasComponent<components::Text>(radio.getLabelEntity())) {
196 registry.getComponent<components::Text>(radio.getLabelEntity())
197 .setContent(label);
198 }
200 return *this;
201 }
202
209
210 RadioButton &RadioButton::select(bool selected)
211 {
213 selected);
214 return *this;
215 }
216
217 void RadioButton::build(void)
218 {
219 auto &registry = getComponentRegistry();
220 auto &radio =
222
223 const auto dot = std::make_shared<RadioDot>(
224 registry, schemeColor(SchemeColorRole::Primary));
225 dot->setParent(shared_from_this());
226 this->addEntity(dot);
227 radio.setDot(dot->getIdentifier());
228
229 if (!radio.getLabel().empty()) {
230 const auto label =
231 buildText(registry, *this, shared_from_this(), radio.getLabel(),
233 schemeColor(SchemeColorRole::OnSurface));
234 if (label != nullptr) {
235 radio.setLabelEntity(label->getIdentifier());
236 }
237 }
238
239 // The dot is centered and the label placed beside the ring by
240 // systems::RadioButton; neither is a layout child of the surface.
241 setChildren({});
243 }
244
245} // 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 radio button: its variant, its label and the identifiers of its dot and label chil...
RadioButton & setLabel(const std::string &label)
Set the label text.
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.
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, creates and registers RadioButton widgets.
Builder & withLabel(const std::string &label)
Set the label shown to the right of the ring.
void reset(void) override
Restore the default configuration.
Builder & withPose(const utility::graphic::PoseF &pose)
Set the position and orientation of the ring.
std::shared_ptr< RadioButton > buildEntity(void) override
Build a RadioButton, with its dot and label children, from the current configuration,...
Builder & withVariant(Variant variant)
Set the visual variant.
std::shared_ptr< RadioButton > makeRadioButton(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &label)
Build a radio button, register it under parent and reset the builder.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
bool isSelected(void) const
Check whether the radio button is selected.
bool isDisabled(void) const
Check whether the radio button ignores input.
static std::shared_ptr< RadioButton > create(Arguments &&...arguments)
Create a radio button and its dot and label children, without registering it in an entity registry.
Variant getVariant(void) const
Get the visual variant.
RadioButton & setVariant(Variant variant)
Set the visual variant. Restyles at once.
RadioButton & select(bool selected)
Select or deselect the radio button, even when it is disabled. Restyles at once.
RadioButton & setLabel(const std::string &label)
Set the label text and the accessibility label. Restyles at once.
static constexpr float DotSize
Diameter of the dot shown while selected, in dp (M3: 10 dp).
components::RadioButton::Variant Variant
RadioButton & setOnPress(std::function< void(void)> onPress)
Set the handler called when the user presses the radio button (Paper onPress).
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool selected)
Select or deselect a radio button from code and show it at once.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one radio button from its components: colors, accessibility, dot and label position.
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the radio button ignores input and is drawn disabled.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a radio button.
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.
@ BodyLarge
16 / 24, regular.
Definition typescale.hpp:60
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
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