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/systems/chip.hpp"
24
25#include <vector>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/borders.hpp"
29#include "guillaume/components/color.hpp"
30#include "guillaume/components/elevation.hpp"
31#include "guillaume/components/layout.hpp"
32#include "guillaume/components/selection.hpp"
33#include "guillaume/components/text.hpp"
36#include "guillaume/systems/layout.hpp"
37#include "guillaume/theme.hpp"
38
39namespace guillaume::systems
40{
41 namespace
42 {
43 using Mode = components::Chip::Mode;
44 using Variant = components::Chip::Variant;
49
56 void colorChild(ecs::ComponentRegistry &registry,
58 const utility::graphic::Color32Bit &color)
59 {
60 if (registry.hasComponent<components::Color>(child)) {
61 registry.getComponent<components::Color>(child).setColor(color);
62 }
63 }
64
72 void attachChildren(ecs::ComponentRegistry &registry,
73 ecs::Entity::Identifier identifier, bool showCheck)
74 {
75 const auto &chip =
76 registry.getComponent<components::Chip>(identifier);
77 const auto invalid = ecs::Entity::InvalidIdentifier;
78 const auto leading = chip.getLeadingIcon();
79 const auto check = chip.getCheckIcon();
80 const auto label = chip.getLabelEntity();
81 const auto trailing = chip.getTrailingIcon();
82
83 // A chip without content of its own keeps whatever children it
84 // was given.
85 if (leading == invalid && check == invalid && label == invalid
86 && trailing == invalid) {
87 return;
88 }
89
90 std::vector<ecs::Entity::Identifier> children;
91 if (check != invalid && showCheck) {
92 children.push_back(check);
93 } else if (leading != invalid) {
94 children.push_back(leading);
95 }
96 if (label != invalid) {
97 children.push_back(label);
98 }
99 if (trailing != invalid) {
100 children.push_back(trailing);
101 }
102 registry.getComponent<components::Layout>(identifier)
103 .setChildren(children);
104 }
105 } // namespace
106
109 {
110 }
111
112 void Chip::install(ecs::ComponentRegistry &componentRegistry,
113 const ecs::Entity::Identifier &entityIdentifier)
114 {
115 ecs::ComponentRegistry *registry = &componentRegistry;
116 const ecs::Entity::Identifier identifier = entityIdentifier;
117
119 componentRegistry, identifier,
121 [registry, identifier]() {
122 press(*registry, identifier);
123 },
124 [registry, identifier]() {
125 apply(*registry, identifier);
126 });
127
128 componentRegistry.getComponent<components::Selectable>(identifier)
129 .setOnSelectionChangedHandler([registry, identifier](bool) {
130 apply(*registry, identifier);
131 });
132 }
133
135 const ecs::Entity::Identifier &entityIdentifier)
136 {
137 ecs::ComponentRegistry *registry = &componentRegistry;
138 const ecs::Entity::Identifier identifier = entityIdentifier;
139 const ecs::Entity::Identifier closeIcon =
140 componentRegistry.getComponent<components::Chip>(identifier)
141 .getTrailingIcon();
142
143 if (closeIcon == ecs::Entity::InvalidIdentifier) {
144 return;
145 }
146
147 interaction::makeInteractive(componentRegistry, closeIcon,
149 [registry, identifier]() {
150 close(*registry, identifier);
151 });
152 componentRegistry.getComponent<components::Accessibility>(closeIcon)
153 .setLabel("Close");
154 }
155
156 bool Chip::isSelected(const ecs::ComponentRegistry &componentRegistry,
157 const ecs::Entity::Identifier &entityIdentifier)
158 {
159 return componentRegistry
160 .getComponent<components::Selectable>(entityIdentifier)
161 .isSelected();
162 }
163
164 bool Chip::isDisabled(const ecs::ComponentRegistry &componentRegistry,
165 const ecs::Entity::Identifier &entityIdentifier)
166 {
167 return !interaction::isEnabled(componentRegistry, entityIdentifier);
168 }
169
170 std::string
172 const ecs::Entity::Identifier &entityIdentifier)
173 {
174 const auto &chip =
175 componentRegistry.getComponent<components::Chip>(entityIdentifier);
176
177 if (chip.getVariant() == Variant::Assist
178 && chip.getIconGlyph().empty()) {
180 }
181 return chip.getIconGlyph();
182 }
183
184 bool Chip::needsCheckIcon(const ecs::ComponentRegistry &componentRegistry,
185 const ecs::Entity::Identifier &entityIdentifier)
186 {
187 return componentRegistry
188 .getComponent<components::Chip>(entityIdentifier)
189 .getVariant()
190 == Variant::Filter;
191 }
192
193 bool Chip::needsCloseIcon(const ecs::ComponentRegistry &componentRegistry,
194 const ecs::Entity::Identifier &entityIdentifier)
195 {
196 const auto &chip =
197 componentRegistry.getComponent<components::Chip>(entityIdentifier);
198
199 return chip.getVariant() == Variant::Input
200 || static_cast<bool>(chip.getOnClose());
201 }
202
204 const ecs::Entity::Identifier &entityIdentifier,
205 bool selected)
206 {
207 componentRegistry.getComponent<components::Selectable>(entityIdentifier)
208 .setSelected(selected);
209 apply(componentRegistry, entityIdentifier);
210 }
211
213 const ecs::Entity::Identifier &entityIdentifier,
214 bool disabled)
215 {
216 interaction::setEnabled(componentRegistry, entityIdentifier, !disabled);
217 apply(componentRegistry, entityIdentifier);
218 }
219
220 void Chip::press(ecs::ComponentRegistry &componentRegistry,
221 const ecs::Entity::Identifier &entityIdentifier)
222 {
223 if (isDisabled(componentRegistry, entityIdentifier)) {
224 return;
225 }
226
227 const auto &chip =
228 componentRegistry.getComponent<components::Chip>(entityIdentifier);
229 if (chip.getVariant() == Variant::Filter) {
230 setSelected(componentRegistry, entityIdentifier,
231 !isSelected(componentRegistry, entityIdentifier));
232 }
233 // Copy: the callback may replace itself.
234 const auto onPress = chip.getOnPress();
235 if (onPress) {
236 onPress();
237 }
238 }
239
240 void Chip::close(ecs::ComponentRegistry &componentRegistry,
241 const ecs::Entity::Identifier &entityIdentifier)
242 {
243 if (isDisabled(componentRegistry, entityIdentifier)) {
244 return;
245 }
246
247 // Copy: the callback may replace itself.
248 const auto onClose =
249 componentRegistry.getComponent<components::Chip>(entityIdentifier)
250 .getOnClose();
251 if (onClose) {
252 onClose();
253 }
254 }
255
256 void Chip::apply(ecs::ComponentRegistry &componentRegistry,
257 const ecs::Entity::Identifier &entityIdentifier)
258 {
259 const auto &chip =
260 componentRegistry.getComponent<components::Chip>(entityIdentifier);
261 auto &borders = componentRegistry.getComponent<components::Borders>(
262 entityIdentifier);
263 auto &layout = componentRegistry.getComponent<components::Layout>(
264 entityIdentifier);
265
266 const bool selected = isSelected(componentRegistry, entityIdentifier);
267 const bool disabled = isDisabled(componentRegistry, entityIdentifier);
268 const bool outlined = (chip.getMode() == Mode::Outlined);
269 const bool showCheck = selected && chip.isShowSelectedCheck();
270
271 componentRegistry
272 .getComponent<components::Accessibility>(entityIdentifier)
273 .setLabel(chip.getLabel())
274 .setSelected(selected);
275
276 // Paper v5 Chip/helpers.tsx: flat chips sit on secondaryContainer;
277 // outlined chips are transparent with an outline until selected.
278 utility::graphic::Color32Bit container = transparentColor();
279 if (!outlined || selected) {
280 container = schemeColor(SchemeColorRole::SecondaryContainer);
281 }
282 auto content = (outlined && !selected)
283 ? schemeColor(SchemeColorRole::OnSurfaceVariant)
284 : schemeColor(SchemeColorRole::OnSecondaryContainer);
285 auto iconColor = selected
286 ? schemeColor(SchemeColorRole::OnSecondaryContainer)
287 : schemeColor(SchemeColorRole::Primary);
288
289 if (disabled) {
290 container =
292 content = disabledContentColor();
293 iconColor = disabledContentColor();
294 } else {
295 if (selected && chip.isShowSelectedOverlay()) {
296 container = style::stateLayer(container, content,
298 }
300 componentRegistry, entityIdentifier, container, content);
301 }
302
303 componentRegistry.getComponent<components::Color>(entityIdentifier)
304 .setColor(container);
305
306 if (outlined && !selected) {
307 borders
308 .setColor(disabled ? disabledContainerColor()
309 : schemeColor(SchemeColorRole::Outline))
310 .setWidth(1.0f);
311 } else {
312 borders.setColor(transparentColor()).setWidth(0.0f);
313 }
314
315 componentRegistry.getComponent<components::Elevation>(entityIdentifier)
316 .setElevationLevel((chip.isElevated() && !disabled)
319
320 layout.setPadding(chip.isCompact() ? components::Chip::CompactPadding
322
323 colorChild(componentRegistry, chip.getLeadingIcon(), iconColor);
324 colorChild(componentRegistry, chip.getCheckIcon(),
325 showCheck ? iconColor : transparentColor());
326 colorChild(componentRegistry, chip.getTrailingIcon(), content);
327 colorChild(componentRegistry, chip.getLabelEntity(), content);
328 if (componentRegistry.hasComponent<components::Text>(
329 chip.getLabelEntity())) {
330 componentRegistry
331 .getComponent<components::Text>(chip.getLabelEntity())
332 .setContent(chip.getLabel());
333 }
334
335 attachChildren(componentRegistry, entityIdentifier, showCheck);
336 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
337 Layout::layerOf(componentRegistry, entityIdentifier));
338 }
339
340 void Chip::update(const ecs::Entity::Identifier &entityIdentifier)
341 {
342 if (!requireComponent<components::Chip>(entityIdentifier)) {
343 return;
344 }
345
346 apply(getComponentRegistry(), entityIdentifier);
347 }
348} // 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 & setSelected(bool selected)
Set whether the entity is the selected one of its group.
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Configuration of a chip: a compact pill with a label and optional leading, check and close icons.
Definition chip.hpp:60
static constexpr float Padding
Padding around the content, in dp.
Definition chip.hpp:95
static constexpr const char * DefaultAssistGlyph
Glyph of the leading icon of an Assist chip without one.
Definition chip.hpp:83
Variant getVariant(void) const noexcept
Get the kind of chip.
Definition chip.cpp:29
const std::string & getIconGlyph(void) const noexcept
Get the name of the leading icon glyph.
Definition chip.cpp:71
Mode
Container style: Flat is filled with the secondary container color; Outlined is transparent with an o...
Definition chip.hpp:77
const Handler & getOnPress(void) const noexcept
Get the callback run when the chip is pressed.
Definition chip.cpp:197
static constexpr float CompactPadding
Padding around the content of a compact chip, in dp.
Definition chip.hpp:98
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
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.
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
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...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one chip.
Definition chip.cpp:340
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
Chip(void)
Create the system for the Layout phase, matching entities that have a components::Chip.
Definition chip.cpp:107
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 void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Press the chip as a click does.
Definition chip.cpp:220
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 close(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Press the close icon of the chip as a click does.
Definition chip.cpp:240
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
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, std::uint32_t layer=0)
Arrange children inside a container and size the container.
Definition layout.cpp:126
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.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
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 stateLayer(const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &overlay, std::uint8_t alpha)
Blend a translucent tint over a base color.
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.
@ Level1
Lowest raised level.
Definition tokens.hpp:132
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
@ Pressed
0.12 overlay of the content color.
Definition tokens.hpp:226
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.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.