guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
chip.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/chip.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/systems/chip.hpp"
33#include "guillaume/theme.hpp"
34
35namespace guillaume::entities
36{
38 ecs::EntityRegistry &entityRegistry)
39 : EntityBuilderBase<Chip>(componentRegistry, entityRegistry)
40 {
41 reset();
42 }
43
44 std::shared_ptr<Chip> Chip::Builder::buildEntity(void)
45 {
46 auto entity = Chip::create(this->getComponentRegistry(), _config,
47 _variant, _label, _iconGlyph, _selected);
48 return entity;
49 }
50
52 {
53 _config = SurfaceConfig();
54 _variant = Variant::Assist;
55 _label.clear();
56 _iconGlyph.clear();
57 _selected = false;
58
59 _config.borderRadius = getShapeRadius(theme::ShapeRole::Small);
61 _config.mainAxisAlignment =
63 _config.crossAxisAlignment =
65 _config.spacing = 8.0f;
66 _config.padding = 12.0f;
67 _config.setFixedHeight(32.0f);
68 }
69
70 Chip::Builder &Chip::Builder::withPose(const utility::graphic::PoseF &pose)
71 {
72 _config.pose = pose;
73 return *this;
74 }
75
77 {
78 _variant = variant;
79 return *this;
80 }
81
82 Chip::Builder &Chip::Builder::withLabel(const std::string &label)
83 {
84 _label = label;
85 return *this;
86 }
87
88 Chip::Builder &Chip::Builder::withIcon(const std::string &iconGlyph)
89 {
90 _iconGlyph = iconGlyph;
91 return *this;
92 }
93
95 {
96 _selected = selected;
97 return *this;
98 }
99
100 std::shared_ptr<Chip>
101 Chip::Builder::makeChip(std::shared_ptr<ecs::Entity> parent,
102 Variant variant, const std::string &label,
103 const std::string &iconGlyph, bool selected)
104 {
105 return withVariant(variant)
106 .withLabel(label)
107 .withIcon(iconGlyph)
108 .withSelected(selected)
109 .registerEntity(parent);
110 }
111
112 Chip::Chip(ecs::ComponentRegistry &registry, const SurfaceConfig &config,
113 Variant variant, const std::string &label,
114 const std::string &iconGlyph, bool selected)
115 : SurfaceBase<components::Selectable, components::Elevation,
116 components::Chip>(registry, config)
117 {
120 .setVariant(variant)
121 .setLabel(label)
122 .setIconGlyph(iconGlyph);
125 .setSelected(selected);
128 }
129
131 {
132 }
133
142
149
150 Chip &Chip::setDisabled(bool disabled)
151 {
153 disabled);
154 return *this;
155 }
156
157 bool Chip::isDisabled(void) const
158 {
160 getIdentifier());
161 }
162
163 Chip &Chip::setCompact(bool compact)
164 {
167 .setCompact(compact);
169 return *this;
170 }
171
172 Chip &Chip::setElevated(bool elevated)
173 {
176 .setElevated(elevated);
178 return *this;
179 }
180
189
198
199 Chip &Chip::setOnPress(std::function<void(void)> onPress)
200 {
203 .setOnPress(std::move(onPress));
204 return *this;
205 }
206
207 Chip &Chip::setOnClose(std::function<void(void)> onClose)
208 {
211 .setOnClose(std::move(onClose));
212 return *this;
213 }
214
221
230
237
238 Chip &Chip::setLabel(const std::string &label)
239 {
242 .setLabel(label);
244 return *this;
245 }
246
247 Chip &Chip::setSelected(bool selected)
248 {
250 selected);
251 return *this;
252 }
253
254 bool Chip::isSelected(void) const
255 {
257 getIdentifier());
258 }
259
260 void Chip::build(void)
261 {
262 auto &registry = getComponentRegistry();
263 auto &chip = registry.getComponent<components::Chip>(getIdentifier());
264 const auto contentColor =
265 schemeColor(SchemeColorRole::OnSecondaryContainer);
266 std::vector<std::shared_ptr<ecs::Entity>> children;
267
268 // Which children exist is decided by systems::Chip; they are only
269 // built here because that needs shared_from_this().
270 const auto leadingIcon =
271 buildIcon(registry, *this, shared_from_this(),
273 components::Chip::IconSize, contentColor);
274 if (leadingIcon != nullptr) {
275 chip.setLeadingIcon(leadingIcon->getIdentifier());
276 children.push_back(leadingIcon);
277 }
278
280 const auto checkIcon =
281 buildIcon(registry, *this, shared_from_this(),
283 components::Chip::IconSize, contentColor);
284 if (checkIcon != nullptr) {
285 chip.setCheckIcon(checkIcon->getIdentifier());
286 children.push_back(checkIcon);
287 }
288 }
289
290 const auto label =
291 buildText(registry, *this, shared_from_this(), chip.getLabel(),
292 theme::TypeRole::LabelLarge, contentColor);
293 if (label != nullptr) {
294 chip.setLabelEntity(label->getIdentifier());
295 children.push_back(label);
296 }
297
299 const auto closeIcon =
300 buildIcon(registry, *this, shared_from_this(),
302 components::Chip::IconSize, contentColor);
303 if (closeIcon != nullptr) {
304 chip.setTrailingIcon(closeIcon->getIdentifier());
305 children.push_back(closeIcon);
307 }
308 }
309
310 // Every child is attached; systems::Chip then narrows the layout
311 // children to the ones currently shown.
312 if (!children.empty()) {
313 setChildren(children);
314 }
316 }
317
318} // namespace guillaume::entities
Configuration of a chip: a compact pill with a label and optional leading, check and close icons.
Definition chip.hpp:60
static constexpr float IconSize
Font size of the icons, in dp.
Definition chip.hpp:92
static constexpr const char * CloseGlyph
Glyph of the trailing close icon.
Definition chip.hpp:89
static constexpr const char * CheckGlyph
Glyph of the selected check.
Definition chip.hpp:86
@ 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
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.
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
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
Configures, creates and registers Chip widgets.
Definition chip.hpp:113
Builder & withLabel(const std::string &label)
Set the label text.
Definition chip.cpp:82
Builder & withIcon(const std::string &iconGlyph)
Set the leading icon.
Definition chip.cpp:88
Builder & withVariant(Variant variant)
Set the variant, which decides the children built and whether a press toggles the selection.
Definition chip.cpp:76
std::shared_ptr< Chip > makeChip(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &label, const std::string &iconGlyph, bool selected)
Build a chip, register it under parent and reset the builder.
Definition chip.cpp:101
Builder & withSelected(bool selected)
Choose whether the chip starts selected.
Definition chip.cpp:94
void reset(void) override
Restore the default configuration.
Definition chip.cpp:51
Builder & withPose(const utility::graphic::PoseF &pose)
Set the position and orientation of the chip.
Definition chip.cpp:70
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Definition chip.cpp:37
std::shared_ptr< Chip > buildEntity(void) override
Build a Chip, with its icon and label children, from the current configuration, without registering i...
Definition chip.cpp:44
Chip & setSelected(bool selected)
Select or deselect the chip, whatever its variant. Restyles at once.
Definition chip.cpp:247
bool isSelected(void) const
Check whether the chip is selected.
Definition chip.cpp:254
Mode getMode(void) const
Get the mode.
Definition chip.cpp:143
Chip & setOnClose(std::function< void(void)> onClose)
Set the handler called when the user presses the trailing close icon (Paper onClose).
Definition chip.cpp:207
Variant getVariant(void) const
Get the variant.
Definition chip.cpp:231
Chip & setMode(Mode mode)
Set the mode. Restyles at once.
Definition chip.cpp:134
Chip & setShowSelectedCheck(bool show)
Show the check icon while the chip is selected (Paper showSelectedCheck). Restyles at once.
Definition chip.cpp:190
Chip & setCompact(bool compact)
Use the compact padding (Paper compact). Restyles at once.
Definition chip.cpp:163
~Chip(void) override
Destructor.
Definition chip.cpp:130
Chip & setShowSelectedOverlay(bool show)
Darken the chip with a pressed state layer while it is selected (Paper showSelectedOverlay)....
Definition chip.cpp:181
bool isDisabled(void) const
Check whether the chip is disabled.
Definition chip.cpp:157
Chip & setVariant(Variant variant)
Set the variant. Restyles at once.
Definition chip.cpp:222
Chip & setElevated(bool elevated)
Paint a level 1 shadow under the chip (Paper elevated). Restyles at once.
Definition chip.cpp:172
Chip & setOnPress(std::function< void(void)> onPress)
Set the handler called when the user presses the chip (Paper onPress).
Definition chip.cpp:199
ecs::Entity::Identifier getCloseIconIdentifier(void) const
Get the identifier of the trailing close icon child.
Definition chip.cpp:215
Chip & setLabel(const std::string &label)
Set the label text. Restyles at once.
Definition chip.cpp:238
components::Chip::Variant Variant
Definition chip.hpp:101
Chip & setDisabled(bool disabled)
Enable or disable the chip. Restyles at once.
Definition chip.cpp:150
static std::shared_ptr< Chip > create(Arguments &&...arguments)
Create a chip and its icon and label children, without registering it in an entity registry.
Definition chip.hpp:227
components::Chip::Mode Mode
Paper chip mode: Flat (tonal fill, the default) or Outlined.
Definition chip.hpp:104
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the chip and show it at once.
Definition chip.cpp:212
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one chip from its components and arrange its children.
Definition chip.cpp:256
static bool needsCheckIcon(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the widget class should build a selected check child for this chip.
Definition chip.cpp:184
static bool needsCloseIcon(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the widget class should build a trailing close icon child for this chip.
Definition chip.cpp:193
static std::string leadingGlyph(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the name of the glyph the leading icon of the chip should show.
Definition chip.cpp:171
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a chip.
Definition chip.cpp:112
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the chip ignores input and is drawn disabled.
Definition chip.cpp:164
static void installCloseIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the trailing icon of a chip a close button.
Definition chip.cpp:134
static bool isSelected(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the chip is selected.
Definition chip.cpp:156
static void setSelected(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool selected)
Select or deselect the chip from code and show it at once.
Definition chip.cpp:203
Makes the interaction 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.
@ LabelLarge
14 / 20, medium weight.
Definition typescale.hpp:57
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