guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
search.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 <memory>
24#include <utility>
25#include <vector>
26
27#include "guillaume/entities/search.hpp"
28
29#include "guillaume/entities/builder_base.hpp"
30#include "guillaume/entities/content_helpers.hpp"
32#include "guillaume/systems/search.hpp"
33#include "guillaume/theme.hpp"
34
35namespace guillaume::entities
36{
38 ecs::EntityRegistry &entityRegistry)
39 : EntityBuilderBase<Search>(componentRegistry, entityRegistry)
40 {
41 reset();
42 }
43
44 std::shared_ptr<Search> Search::Builder::buildEntity(void)
45 {
46 auto entity = Search::create(this->getComponentRegistry(), _config,
47 _variant, _placeholder, _text);
48 return entity;
49 }
50
52 {
53 _config = SurfaceConfig();
54 _variant = Variant::SearchBar;
55 _placeholder.clear();
56 _text.clear();
57
59 _config.mainAxisAlignment =
61 _config.crossAxisAlignment =
63 _config.spacing = theme::SpacingScale::get(4);
64 _config.padding = theme::SpacingScale::get(4);
65 _config.borderRadius = getShapeRadius(theme::ShapeRole::ExtraLarge);
66 _config.setFixedWidth(components::Search::DefaultWidth);
67 _config.setFixedHeight(components::Search::BarHeight);
68 }
69
71 Search::Builder::withPose(const utility::graphic::PoseF &pose)
72 {
73 _config.pose = pose;
74 return *this;
75 }
76
78 {
79 _variant = variant;
80 return *this;
81 }
82
84 Search::Builder::withPlaceholder(const std::string &placeholder)
85 {
86 _placeholder = placeholder;
87 return *this;
88 }
89
91 {
92 _text = text;
93 return *this;
94 }
95
96 std::shared_ptr<Search> Search::Builder::makeSearch(
97 std::shared_ptr<ecs::Entity> parent, Variant variant,
98 const std::string &placeholder, const std::string &text)
99 {
100 return withVariant(variant)
101 .withPlaceholder(placeholder)
102 .withText(text)
103 .registerEntity(parent);
104 }
105
106 Search::Search(ecs::ComponentRegistry &registry,
107 const SurfaceConfig &config, Variant variant,
108 const std::string &placeholder, const std::string &text)
110 components::Elevation, components::Line,
111 components::Focus, components::Accessibility,
112 components::Search>(registry, config)
113 {
116 .setVariant(variant)
117 .setPlaceholder(placeholder)
118 .setText(text);
121 }
122
124 {
125 }
126
135
142
143 Search &Search::setPlaceholder(const std::string &placeholder)
144 {
147 .setPlaceholder(placeholder);
148 buildChildren();
150 return *this;
151 }
152
153 Search &Search::setText(const std::string &text)
154 {
156 return *this;
157 }
158
159 std::string Search::getText(void) const
160 {
162 }
163
164 Search &Search::clear(void)
165 {
167 return *this;
168 }
169
170 Search &Search::setIcon(const std::string &iconGlyph)
171 {
174 .setIconGlyph(iconGlyph);
175 buildChildren();
177 return *this;
178 }
179
180 Search &Search::setOnIconPress(std::function<void(void)> onIconPress)
181 {
184 .setOnIconPress(std::move(onIconPress));
185 return *this;
186 }
187
188 Search &
189 Search::setOnClearIconPress(std::function<void(void)> onClearIconPress)
190 {
193 .setOnClearIconPress(std::move(onClearIconPress));
194 return *this;
195 }
196
197 Search &Search::setTrailingIcon(const std::string &iconGlyph,
198 std::function<void(void)> onPress)
199 {
202 .setTrailingGlyph(iconGlyph)
203 .setOnTrailingIconPress(std::move(onPress));
204 buildChildren();
206 return *this;
207 }
208
209 Search &Search::setLoading(bool loading)
210 {
213 .setLoading(loading);
215 return *this;
216 }
217
218 bool Search::isLoading(void) const
219 {
220 return getComponentRegistry()
222 .isLoading();
223 }
224
226 {
229 .setElevationLevel(level);
231 return *this;
232 }
233
240
247
249 {
250 return getComponentRegistry()
252 .getPlaceholderEntity();
253 }
254
261
262 Search &Search::show(void)
263 {
265 return *this;
266 }
267
268 Search &Search::hide(void)
269 {
271 return *this;
272 }
273
274 bool Search::isVisible(void) const
275 {
277 getIdentifier());
278 }
279
280 Search &Search::open(void)
281 {
282 return show();
283 }
284
285 Search &Search::close(void)
286 {
287 return hide();
288 }
289
290 bool Search::isOpen(void) const
291 {
292 return isVisible();
293 }
294
295 void Search::build(void)
296 {
297 buildChildren();
299 }
300
301 void Search::buildChildren(void)
302 {
303 // Children need the owning handle as their parent.
304 if (weak_from_this().expired()) {
305 return;
306 }
307
308 auto &registry = getComponentRegistry();
309 const auto identifier = getIdentifier();
310 const auto self = shared_from_this();
311 const auto iconColor = schemeColor(SchemeColorRole::OnSurfaceVariant);
312 // Copy: the configuration is read while the children are created.
313 const components::Search config =
314 registry.getComponent<components::Search>(identifier);
315 const auto search = [&registry, identifier]() -> components::Search & {
316 return registry.getComponent<components::Search>(identifier);
317 };
318 bool built = false;
319
320 if (config.getSearchIcon() == ecs::Entity::InvalidIdentifier) {
321 if (const auto icon =
322 buildIcon(registry, *this, self, config.getIconGlyph(),
323 components::Search::IconSize, iconColor)) {
324 search().setSearchIcon(icon->getIdentifier());
325 systems::Search::installIcon(registry, identifier);
326 built = true;
327 }
328 }
329
330 if (config.getTextEntity() == ecs::Entity::InvalidIdentifier) {
331 // Built even when empty so the text-input systems can edit it.
332 Text::Builder builder(registry, *this);
333 const auto text = builder.makeText(
334 self, config.getText(), theme::TypeRole::BodyLarge,
335 schemeColor(SchemeColorRole::OnSurface));
336 text->setContent(config.getText());
337 registry.addComponent<components::Focus>(text->getIdentifier());
338 search().setTextEntity(text->getIdentifier());
339 built = true;
340 }
341
342 if (config.getPlaceholderEntity() == ecs::Entity::InvalidIdentifier) {
343 if (const auto placeholder =
344 buildText(registry, *this, self, config.getPlaceholder(),
345 theme::TypeRole::BodyLarge, iconColor)) {
346 search().setPlaceholderEntity(placeholder->getIdentifier());
347 built = true;
348 }
349 }
350
351 if (config.getClearIcon() == ecs::Entity::InvalidIdentifier) {
352 if (const auto icon = buildIcon(
353 registry, *this, self, components::Search::ClearGlyph,
354 components::Search::IconSize, iconColor)) {
355 search().setClearIcon(icon->getIdentifier());
356 systems::Search::installClearIcon(registry, identifier);
357 built = true;
358 }
359 }
360
361 if (config.getLoadingIcon() == ecs::Entity::InvalidIdentifier) {
362 if (const auto icon = buildIcon(
363 registry, *this, self, components::Search::LoadingGlyph,
365 schemeColor(SchemeColorRole::Primary))) {
366 search().setLoadingIcon(icon->getIdentifier());
367 built = true;
368 }
369 }
370
371 if (config.getTrailingIcon() == ecs::Entity::InvalidIdentifier) {
372 if (const auto icon =
373 buildIcon(registry, *this, self, config.getTrailingGlyph(),
374 components::Search::IconSize, iconColor)) {
375 search().setTrailingIcon(icon->getIdentifier());
376 systems::Search::installTrailingIcon(registry, identifier);
377 built = true;
378 }
379 }
380
381 if (!built) {
382 return;
383 }
384
385 // Hand every child to the surface; systems::Search then narrows the
386 // layout children to the ones currently shown.
387 const auto &current = search();
388 Children children;
389
390 for (const auto child:
391 { current.getSearchIcon(), current.getTextEntity(),
392 current.getPlaceholderEntity(), current.getClearIcon(),
393 current.getLoadingIcon(), current.getTrailingIcon() }) {
394 if (const auto entity = getEntity<ecs::Entity>(child)) {
395 children.push_back(entity);
396 }
397 }
398 setChildren(children);
399 }
400
401} // namespace guillaume::entities
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
State of a search field: a leading icon, the query the user types (or a placeholder),...
Definition search.hpp:59
static constexpr float DefaultWidth
Width given to a search field that has no fixed width, in dp.
Definition search.hpp:72
static constexpr float IconSize
Size of the icons, in dp.
Definition search.hpp:81
static constexpr const char * LoadingGlyph
Glyph name of the icon shown while loading.
Definition search.hpp:90
Search & setOnTrailingIconPress(std::function< void(void)> handler)
Set the callback run when the trailing icon is pressed.
Definition search.cpp:118
static constexpr const char * ClearGlyph
Glyph name of the clear button.
Definition search.hpp:87
static constexpr float BarHeight
Height of the SearchBar variant, in dp.
Definition search.hpp:75
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.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
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
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
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a Search, creates it and registers it in an entity registry.
Definition search.hpp:112
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Definition search.cpp:37
std::shared_ptr< Search > makeSearch(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &placeholder, const std::string &text="")
Build a search field, register it under parent and reset the builder.
Definition search.cpp:96
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the search field is placed.
Definition search.cpp:71
std::shared_ptr< Search > buildEntity(void) override
Build a search field and its children from the current configuration, without registering it or setti...
Definition search.cpp:44
Builder & withText(const std::string &text)
Set the initial query.
Definition search.cpp:90
void reset(void) override
Restore the defaults.
Definition search.cpp:51
Builder & withVariant(Variant variant)
Set the variant.
Definition search.cpp:77
Builder & withPlaceholder(const std::string &placeholder)
Set the placeholder: the hint shown while the query is empty.
Definition search.cpp:84
A search field: a leading icon, an editable query with a placeholder, a clear button and an optional ...
Definition search.hpp:103
Search & setLoading(bool loading)
Show the loading glyph in place of the clear button, or the reverse. Restyles at once.
Definition search.cpp:209
Search & setOnIconPress(std::function< void(void)> onIconPress)
Set the handler called when the user presses the leading icon (Paper onIconPress).
Definition search.cpp:180
Search & setPlaceholder(const std::string &placeholder)
Change the placeholder: the hint shown while the query is empty.
Definition search.cpp:143
components::Search::Variant Variant
Search variant.
Definition search.hpp:105
static std::shared_ptr< Search > create(Arguments &&...arguments)
Create a search field and its children, without registering it in an entity registry.
Definition search.hpp:209
Search & hide(void)
Hide the search field: set its components::Overlay not visible.
Definition search.cpp:268
Search & setElevation(theme::ElevationLevel level)
Set the elevation level (Paper elevation). Restyles at once.
Definition search.cpp:225
Search & setIcon(const std::string &iconGlyph)
Change the glyph of the leading icon (Paper icon). Restyles at once.
Definition search.cpp:170
~Search(void) override
Default destructor.
Definition search.cpp:123
ecs::Entity::Identifier getTextIdentifier(void) const
Get the query text entity: the text the user edits.
Definition search.cpp:255
ecs::Entity::Identifier getPlaceholderIdentifier(void) const
Get the placeholder text entity.
Definition search.cpp:248
ecs::Entity::Identifier getIconIdentifier(void) const
Get the leading icon: a pressable icon entity.
Definition search.cpp:234
Search & setOnClearIconPress(std::function< void(void)> onClearIconPress)
Set the handler called after the query was cleared, by the clear button or by clear() (Paper onClearI...
Definition search.cpp:189
Search & open(void)
Open the search field. Same as show().
Definition search.cpp:280
bool isVisible(void) const
Whether the search field is shown.
Definition search.cpp:274
bool isLoading(void) const
Whether the loading glyph replaces the clear button.
Definition search.cpp:218
ecs::Entity::Identifier getClearIdentifier(void) const
Get the clear button: a pressable icon entity.
Definition search.cpp:241
Search & clear(void)
Empty the query, as the clear button does, then call the clear handler.
Definition search.cpp:164
Search & show(void)
Show the search field: set its components::Overlay visible.
Definition search.cpp:262
Search & close(void)
Close the search field. Same as hide().
Definition search.cpp:285
Search & setText(const std::string &text)
Replace the query. Restyles at once.
Definition search.cpp:153
Search & setTrailingIcon(const std::string &iconGlyph, std::function< void(void)> onPress={})
Set or hide the trailing icon (Paper right and traileringIcon).
Definition search.cpp:197
Search & setVariant(Variant variant)
Change the variant. Restyles at once.
Definition search.cpp:127
bool isOpen(void) const
Whether the search field is open. Same as isVisible().
Definition search.cpp:290
Variant getVariant(void) const
Get the variant.
Definition search.cpp:136
std::string getText(void) const
Get the query, including what the user typed.
Definition search.cpp:159
A box the user types text into, with a label, optional icons, prefix and suffix, and a helper or erro...
static void clear(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Empty the query as a press on the clear icon would.
Definition search.cpp:269
static bool isVisible(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the search field is shown.
Definition search.cpp:322
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the search field.
Definition search.cpp:315
static void installClearIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the clear icon a button that calls clear().
Definition search.cpp:204
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one search field and lay out its children.
Definition search.cpp:330
static void installIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the leading icon a button that calls pressIcon().
Definition search.cpp:189
static void setText(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const std::string &text)
Replace the query and restyle the field.
Definition search.cpp:252
static std::string text(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the query the field holds.
Definition search.cpp:237
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a search field its accessibility role and make its icons pressable.
Definition search.cpp:173
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the search field.
Definition search.cpp:308
static void installTrailingIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the trailing icon a button that calls pressTrailingIcon().
Definition search.cpp:220
static constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
Definition tokens.hpp:441
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.
ElevationLevel
How far a surface is raised above the background, in six steps.
Definition tokens.hpp:130
@ BodyLarge
16 / 24, regular.
Definition typescale.hpp:60
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
Definition tokens.hpp:48
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