guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
tooltip.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/tooltip.hpp"
24
25#include <memory>
26#include <utility>
27#include <vector>
28
29#include "guillaume/entities/content_helpers.hpp"
30#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/systems/tooltip.hpp"
33#include "guillaume/theme.hpp"
34
35namespace guillaume::entities
36{
38 ecs::EntityRegistry &entityRegistry)
39 : EntityBuilderBase<Tooltip>(componentRegistry, entityRegistry)
40 {
41 reset();
42 }
43
44 std::shared_ptr<Tooltip> Tooltip::Builder::buildEntity(void)
45 {
46 auto entity = Tooltip::create(this->getComponentRegistry(), _config,
47 _variant, _text, _title);
48 return entity;
49 }
50
52 {
53 _config = SurfaceConfig();
54 _variant = Variant::Plain;
55 _text.clear();
56 _title.clear();
57
59 _config.mainAxisAlignment =
61 _config.crossAxisAlignment =
63 _config.padding = 8.0f;
64 _config.spacing = 2.0f;
65 }
66
68 Tooltip::Builder::withPose(const utility::graphic::PoseF &pose)
69 {
70 _config.pose = pose;
71 return *this;
72 }
73
75 {
76 _variant = variant;
77 return *this;
78 }
79
81 {
82 _text = text;
83 return *this;
84 }
85
87 {
88 _title = title;
89 return *this;
90 }
91
93 {
94 _config.hasFixedWidth = true;
95 _config.fixedWidth = width;
96 return *this;
97 }
98
99 std::shared_ptr<Tooltip>
100 Tooltip::Builder::makeTooltip(std::shared_ptr<ecs::Entity> parent,
101 Variant variant, const std::string &text,
102 const std::string &title)
103 {
104 return withVariant(variant)
105 .withText(text)
106 .withTitle(title)
107 .registerEntity(parent);
108 }
109
110 Tooltip::Tooltip(ecs::ComponentRegistry &registry,
111 const SurfaceConfig &config, Variant variant,
112 const std::string &text, const std::string &title)
113 : SurfaceBase<components::Overlay, components::Elevation,
114 components::Animation, components::Accessibility,
115 components::Tooltip>(registry, config)
116 {
119 .setVariant(variant)
120 .setText(text)
121 .setTitle(title);
124 }
125
126 Tooltip &Tooltip::setAnchor(std::shared_ptr<ecs::Entity> anchor)
127 {
129 anchor != nullptr
130 ? anchor->getIdentifier()
132 return *this;
133 }
134
135 Tooltip &Tooltip::setEnterDelay(float seconds)
136 {
139 .setEnterDelay(seconds);
140 return *this;
141 }
142
143 Tooltip &Tooltip::setLeaveDelay(float seconds)
144 {
147 .setLeaveDelay(seconds);
148 return *this;
149 }
150
157
164
165 Tooltip &Tooltip::setViewport(float width, float height)
166 {
168 width, height);
169 return *this;
170 }
171
172 Tooltip &Tooltip::show(void)
173 {
175 return *this;
176 }
177
178 Tooltip &Tooltip::hide(void)
179 {
181 return *this;
182 }
183
184 bool Tooltip::isVisible(void) const
185 {
186 return getComponentRegistry()
188 .isVisible();
189 }
190
191 void Tooltip::advance(float deltaTime)
192 {
194 deltaTime);
195 }
196
201
202 Tooltip &Tooltip::setVariant(Variant variant)
203 {
206 .setVariant(variant);
207 buildContent();
209 return *this;
210 }
211
218
219 Tooltip &Tooltip::setText(const std::string &text)
220 {
223 .setText(text);
224 buildContent();
226 return *this;
227 }
228
229 void Tooltip::build(void)
230 {
231 buildContent();
233 }
234
235 void Tooltip::buildContent(void)
236 {
237 // Children need the owning handle as their parent.
238 if (weak_from_this().expired()) {
239 return;
240 }
241
242 auto &registry = getComponentRegistry();
243 auto &tooltip =
244 registry.getComponent<components::Tooltip>(getIdentifier());
245
246 // Children are built transparent: systems::Tooltip colors them once
247 // the tooltip is shown.
248 if (tooltip.getVariant() == Variant::Rich
249 && tooltip.getTitleEntity() == ecs::Entity::InvalidIdentifier) {
250 const auto title = buildText(
251 registry, *this, shared_from_this(), tooltip.getTitle(),
253 if (title != nullptr) {
254 tooltip.setTitleEntity(title->getIdentifier());
255 }
256 }
257
258 if (tooltip.getTextEntity() == ecs::Entity::InvalidIdentifier) {
259 const auto text = buildText(
260 registry, *this, shared_from_this(), tooltip.getText(),
262 if (text != nullptr) {
263 tooltip.setTextEntity(text->getIdentifier());
264 }
265 }
266
267 // Hand the layout children to the surface: the title only takes part
268 // in the layout of a rich tooltip.
269 std::vector<std::shared_ptr<ecs::Entity>> children;
270 if (tooltip.getVariant() == Variant::Rich) {
271 if (auto title = getEntity<Text>(tooltip.getTitleEntity())) {
272 children.push_back(std::move(title));
273 }
274 }
275 if (auto text = getEntity<Text>(tooltip.getTextEntity())) {
276 children.push_back(std::move(text));
277 }
278 setChildren(children);
279 }
280
281} // namespace guillaume::entities
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
State of a tooltip: a small label that appears under an entity (its anchor) after the pointer has res...
Definition tooltip.hpp:55
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.
std::shared_ptr< EntityType > getEntity(Entity::Identifier identifier)
Find an entity owned directly by this registry, by identifier.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a Tooltip, then builds it and registers it in the entity registry given at construction.
Definition tooltip.hpp:133
Builder & withTitle(const std::string &title)
Set the title shown above the text.
Definition tooltip.cpp:86
std::shared_ptr< Tooltip > buildEntity(void) override
Build a tooltip and its children from the current configuration, without registering it or setting it...
Definition tooltip.cpp:44
Builder & withFixedWidth(float width)
Give the tooltip a fixed width.
Definition tooltip.cpp:92
Builder & withVariant(Variant variant)
Set the tooltip variant.
Definition tooltip.cpp:74
std::shared_ptr< Tooltip > makeTooltip(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &text, const std::string &title="")
Build a tooltip, register it under parent and reset the builder.
Definition tooltip.cpp:100
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Definition tooltip.cpp:37
Builder & withText(const std::string &text)
Set the body text.
Definition tooltip.cpp:80
Builder & withPose(const utility::graphic::PoseF &pose)
Set the pose the tooltip is created with.
Definition tooltip.cpp:68
void reset(void) override
Restore the defaults: Plain variant, no text, no title, a default pose and a width that follows the c...
Definition tooltip.cpp:51
void place(void)
Move the tooltip next to its anchor.
Definition tooltip.cpp:197
bool isVisible(void) const
Check whether the tooltip is shown.
Definition tooltip.cpp:184
float getEnterDelay(void) const
Get how long the anchor must be hovered before the tooltip shows.
Definition tooltip.cpp:151
Tooltip & setEnterDelay(float seconds)
Set how long the anchor must be hovered before the tooltip shows (Paper enterTouchDelay).
Definition tooltip.cpp:135
Tooltip & setVariant(Variant variant)
Change the tooltip variant.
Definition tooltip.cpp:202
Tooltip & setLeaveDelay(float seconds)
Set how long the tooltip stays shown after the anchor is left (Paper leaveTouchDelay).
Definition tooltip.cpp:143
void advance(float deltaTime)
Advance the hover delays by hand.
Definition tooltip.cpp:191
Tooltip & setViewport(float width, float height)
Set the area the tooltip must stay inside, usually the window.
Definition tooltip.cpp:165
float getLeaveDelay(void) const
Get how long the tooltip stays shown after the anchor is left.
Definition tooltip.cpp:158
Tooltip & setAnchor(std::shared_ptr< ecs::Entity > anchor)
Choose the entity whose hover shows the tooltip.
Definition tooltip.cpp:126
components::Tooltip::Variant Variant
Look of the tooltip: Plain (one text) or Rich (title and text).
Definition tooltip.hpp:112
Variant getVariant(void) const
Get the tooltip variant.
Definition tooltip.cpp:212
Tooltip & setText(const std::string &text)
Change the body text.
Definition tooltip.cpp:219
Tooltip & show(void)
Show the tooltip at once, without waiting for the enter delay.
Definition tooltip.cpp:172
Tooltip & hide(void)
Hide the tooltip at once, without waiting for the leave delay. Restyles at once.
Definition tooltip.cpp:178
static std::shared_ptr< Tooltip > create(Arguments &&...arguments)
Create a tooltip and its children, without registering it in an entity registry.
Definition tooltip.hpp:237
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a tooltip its accessibility semantics, its ticker and its dismissal.
Definition tooltip.cpp:94
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one tooltip and arrange its children.
Definition tooltip.cpp:272
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the tooltip next to its anchor at once, without waiting for the enter delay.
Definition tooltip.cpp:171
static void advance(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, float deltaTime)
Advance the hover timers by some elapsed time.
Definition tooltip.cpp:188
static void setViewport(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, float width, float height)
Set the area the tooltip must stay inside.
Definition tooltip.cpp:158
static void place(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Move the tooltip next to its anchor.
Definition tooltip.cpp:219
static void setAnchor(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, ecs::Entity::Identifier anchorIdentifier)
Attach the tooltip to the entity whose hover triggers it.
Definition tooltip.cpp:139
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the tooltip at once, without waiting for the leave delay.
Definition tooltip.cpp:180
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
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
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.
@ TitleSmall
14 / 20, medium weight.
Definition typescale.hpp:56
@ BodySmall
12 / 16, regular.
Definition typescale.hpp:62
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