guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
badge.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/badge.hpp"
24
25#include <algorithm>
26#include <cstdint>
27
28#include "guillaume/components/animation.hpp"
29#include "guillaume/components/borders.hpp"
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/layout.hpp"
33#include "guillaume/components/text.hpp"
34#include "guillaume/components/typography.hpp"
36#include "guillaume/systems/layout.hpp"
37#include "guillaume/systems/typography.hpp"
38#include "guillaume/theme.hpp"
39
40namespace guillaume::systems
41{
42 namespace
43 {
44 using Variant = components::Badge::Variant;
46
53 utility::graphic::Color32Bit
54 faded(const utility::graphic::Color32Bit &color, float opacity)
55 {
56 return style::withAlpha(
57 color,
58 static_cast<std::uint8_t>(
59 static_cast<float>(color.getAlpha()) * opacity + 0.5f));
60 }
61
70 void styleLabel(ecs::ComponentRegistry &registry,
72 const std::string &content, float fontSize,
73 const utility::graphic::Color32Bit &color)
74 {
75 if (registry.hasComponent<components::Text>(label)) {
76 registry.getComponent<components::Text>(label).setContent(
77 content);
78 if (registry.hasComponent<components::Typography>(label)) {
79 registry.getComponent<components::Typography>(label)
80 .setFontSize(fontSize);
81 Typography::apply(registry, label);
82 }
83 }
84 if (registry.hasComponent<components::Color>(label)) {
85 registry.getComponent<components::Color>(label).setColor(color);
86 }
87 }
88 } // namespace
89
92 {
93 }
94
95 void Badge::install(ecs::ComponentRegistry &componentRegistry,
96 const ecs::Entity::Identifier &entityIdentifier)
97 {
98 ecs::ComponentRegistry *registry = &componentRegistry;
99 const ecs::Entity::Identifier identifier = entityIdentifier;
100
101 componentRegistry.getComponent<components::Animation>(identifier)
104 .setOnSampleHandler([registry, identifier](float progress) {
105 fade(*registry, identifier, progress);
106 });
107 }
108
109 float Badge::sizeOf(const ecs::ComponentRegistry &componentRegistry,
110 const ecs::Entity::Identifier &entityIdentifier)
111 {
112 const auto &badge =
113 componentRegistry.getComponent<components::Badge>(entityIdentifier);
114
115 if (badge.getSize().has_value()) {
116 return *badge.getSize();
117 }
118 switch (badge.getVariant()) {
119 case Variant::Dot:
121 case Variant::Small:
123 case Variant::Large:
124 default:
126 }
127 }
128
129 bool Badge::needsLabel(const ecs::ComponentRegistry &componentRegistry,
130 const ecs::Entity::Identifier &entityIdentifier)
131 {
132 const auto &badge =
133 componentRegistry.getComponent<components::Badge>(entityIdentifier);
134
135 return badge.getVariant() != Variant::Dot && !badge.getLabel().empty();
136 }
137
139 const ecs::Entity::Identifier &entityIdentifier,
140 bool visible)
141 {
142 auto &badge =
143 componentRegistry.getComponent<components::Badge>(entityIdentifier);
144
145 if (badge.isVisible() == visible) {
146 return;
147 }
148 badge.setVisible(visible).setFadeFrom(badge.getOpacity());
149 componentRegistry.getComponent<components::Animation>(entityIdentifier)
150 .play();
151 }
152
153 void Badge::fade(ecs::ComponentRegistry &componentRegistry,
154 const ecs::Entity::Identifier &entityIdentifier,
155 float progress)
156 {
157 auto &badge =
158 componentRegistry.getComponent<components::Badge>(entityIdentifier);
159 const float target = badge.isVisible() ? 1.0f : 0.0f;
160 const float from = badge.getFadeFrom();
161
162 badge.setOpacity(from + (target - from) * progress);
163 apply(componentRegistry, entityIdentifier);
164 }
165
166 void Badge::apply(ecs::ComponentRegistry &componentRegistry,
167 const ecs::Entity::Identifier &entityIdentifier)
168 {
169 const auto &badge =
170 componentRegistry.getComponent<components::Badge>(entityIdentifier);
171 const float size = sizeOf(componentRegistry, entityIdentifier);
172 const float opacity = badge.getOpacity();
173 const auto label = badge.getLabelEntity();
174 auto &layout = componentRegistry.getComponent<components::Layout>(
175 entityIdentifier);
176
177 componentRegistry.getComponent<components::Borders>(entityIdentifier)
178 .setBorderRadius(getShapeRadius(theme::ShapeRole::Full));
179
180 // A label wider than the badge grows it into a pill.
181 float width = size;
182 if (badge.getVariant() != Variant::Dot
183 && componentRegistry.hasComponent<components::Bound>(label)) {
184 width = std::max(
185 size,
186 componentRegistry.getComponent<components::Bound>(label)
187 .getWidth()
189 }
190 layout.setPadding(0.0f)
191 .setSpacing(0.0f)
192 .setFixedWidth(width)
193 .setFixedHeight(size);
194
195 componentRegistry.getComponent<components::Color>(entityIdentifier)
196 .setColor(faded(badge.getContainerColor().value_or(
197 schemeColor(SchemeColorRole::Error)),
198 opacity));
199
200 styleLabel(componentRegistry, label, badge.getLabel(),
202 faded(badge.getContentColor().value_or(
203 schemeColor(SchemeColorRole::OnError)),
204 opacity));
205
206 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
207 Layout::layerOf(componentRegistry, entityIdentifier));
208 }
209
210 void Badge::update(const ecs::Entity::Identifier &entityIdentifier)
211 {
212 if (!requireComponent<components::Badge>(entityIdentifier)) {
213 return;
214 }
215
216 apply(getComponentRegistry(), entityIdentifier);
217 }
218} // namespace guillaume::systems
A timer that runs from 0 to a duration and reports its eased progress, from 0 to 1,...
Definition animation.hpp:61
Animation & setEasing(motion::Easing easing)
Set the curve that turns the linear progress into the value passed to the sample handler.
Animation & setOnSampleHandler(const SampleHandler &handler)
Store the callback that applies the animation.
State of a badge: a small dot or pill, usually holding a count, drawn over another widget.
Definition badge.hpp:64
static constexpr float SmallSize
Height of the Small variant, in dp.
Definition badge.hpp:80
Badge & setVisible(bool visible)
Store the visibility the fade heads to.
Definition badge.cpp:122
Variant
Kind of badge; it sets the height and whether a label is shown.
Definition badge.hpp:70
Badge & setFadeFrom(float opacity)
Record the opacity a fade starts from.
Definition badge.cpp:150
bool isVisible(void) const noexcept
Check whether the badge is shown or fading in.
Definition badge.cpp:117
static constexpr float LargeSize
Height of the Large variant, in dp.
Definition badge.hpp:83
static constexpr float LabelScale
Label font size as a fraction of the badge height.
Definition badge.hpp:89
static constexpr float DotSize
Height of the Dot variant, in dp.
Definition badge.hpp:77
Variant getVariant(void) const noexcept
Get the kind of badge.
Definition badge.cpp:29
static constexpr float HorizontalPadding
Space on each side of the label, in dp.
Definition badge.hpp:86
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
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
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)
Size and color one badge and its label.
Definition badge.cpp:166
static void fade(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, float progress)
Move the visibility fade to a given progress and restyle.
Definition badge.cpp:153
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one badge; called by the frame loop.
Definition badge.cpp:210
static void setVisible(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool visible)
Show or hide the badge with a fade.
Definition badge.cpp:138
static float sizeOf(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the height of a badge, in dp.
Definition badge.cpp:109
Badge(void)
Create the system in the Layout phase.
Definition badge.cpp:90
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Prepare the visibility fade of a badge.
Definition badge.cpp:95
static bool needsLabel(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the badge should have a label child.
Definition badge.cpp:129
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
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Resolve the metrics of one text and write them into its components::Text.
static constexpr motion::Easing standard(void)
Get the easing for elements that move or change while staying on screen.
Definition tokens.hpp:380
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 withAlpha(const utility::graphic::Color32Bit &color, std::uint8_t alpha)
Copy a color with another alpha channel.
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 schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
float getMotionDuration(theme::MotionDuration duration)
Get the length the active theme gives to a duration token.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.