guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
search.hpp
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#pragma once
24
25#include <functional>
26#include <string>
27
28#include "guillaume/ecs/component.hpp"
29#include "guillaume/ecs/entity.hpp"
30
32{
33
58 class Search: public ecs::Component
59 {
60 public:
70
72 static constexpr float DefaultWidth = 360.0f;
73
75 static constexpr float BarHeight = 56.0f;
76
78 static constexpr float ViewHeight = 72.0f;
79
81 static constexpr float IconSize = 24.0f;
82
84 static constexpr const char *SearchGlyph = "search";
85
87 static constexpr const char *ClearGlyph = "close";
88
90 static constexpr const char *LoadingGlyph = "progress_activity";
91
92 private:
93 Variant _variant { Variant::SearchBar };
94 std::string _placeholder {};
95 std::string _text {};
96 std::string _iconGlyph { SearchGlyph };
97 std::string _trailingGlyph {};
98 bool _loading { false };
99 std::function<void(void)> _onIconPress {};
100 std::function<void(void)> _onClearIconPress {};
101 std::function<void(void)>
102 _onTrailingIconPress {};
103 ecs::Entity::Identifier _searchIcon {
105 };
106
107 ecs::Entity::Identifier _textEntity {
109 };
110
111 ecs::Entity::Identifier _placeholderEntity {
113 };
114
115 ecs::Entity::Identifier _clearIcon {
117 };
118
119 ecs::Entity::Identifier _loadingIcon {
121 };
122
123 ecs::Entity::Identifier _trailingIcon {
125 };
126
127 public:
132 Search(void) = default;
133
137 ~Search(void) = default;
138
143 Variant getVariant(void) const noexcept;
144
152 Search &setVariant(Variant variant);
153
158 const std::string &getPlaceholder(void) const noexcept;
159
168 Search &setPlaceholder(const std::string &placeholder);
169
177 const std::string &getText(void) const noexcept;
178
189 Search &setText(const std::string &text);
190
195 const std::string &getIconGlyph(void) const noexcept;
196
205 Search &setIconGlyph(const std::string &glyph);
206
212 const std::string &getTrailingGlyph(void) const noexcept;
213
224 Search &setTrailingGlyph(const std::string &glyph);
225
230 bool isLoading(void) const noexcept;
231
240 Search &setLoading(bool loading);
241
246 const std::function<void(void)> &getOnIconPress(void) const noexcept;
247
255 Search &setOnIconPress(std::function<void(void)> handler);
256
261 const std::function<void(void)> &
262 getOnClearIconPress(void) const noexcept;
263
272 Search &setOnClearIconPress(std::function<void(void)> handler);
273
278 const std::function<void(void)> &
279 getOnTrailingIconPress(void) const noexcept;
280
288 Search &setOnTrailingIconPress(std::function<void(void)> handler);
289
295 ecs::Entity::Identifier getSearchIcon(void) const noexcept;
296
305
311 ecs::Entity::Identifier getTextEntity(void) const noexcept;
312
322
329
339
345 ecs::Entity::Identifier getClearIcon(void) const noexcept;
346
357
363 ecs::Entity::Identifier getLoadingIcon(void) const noexcept;
364
373
379 ecs::Entity::Identifier getTrailingIcon(void) const noexcept;
380
389
390 private:
399 template<typename Type> Search &assign(Type &field, const Type &value)
400 {
401 if (!(field == value)) {
402 field = value;
403 setHasChanged(true);
404 }
405 return *this;
406 }
407 };
408
409} // namespace guillaume::components
Search & setTextEntity(ecs::Entity::Identifier identifier)
Record the child the user types the query in.
Definition search.cpp:139
ecs::Entity::Identifier getLoadingIcon(void) const noexcept
Get the child that draws the loading glyph.
Definition search.cpp:164
const std::function< void(void)> & getOnIconPress(void) const noexcept
Get the callback run when the leading icon is pressed.
Definition search.cpp:89
Variant getVariant(void) const noexcept
Get the shape of the field.
Definition search.cpp:29
const std::string & getIconGlyph(void) const noexcept
Get the icon at the leading edge of the field.
Definition search.cpp:59
ecs::Entity::Identifier getClearIcon(void) const noexcept
Get the pressable child that draws the clear button.
Definition search.cpp:154
static constexpr float DefaultWidth
Width given to a search field that has no fixed width, in dp.
Definition search.hpp:72
~Search(void)=default
Default destructor.
Search & setLoadingIcon(ecs::Entity::Identifier identifier)
Record the child that draws the loading glyph.
Definition search.cpp:169
Search & setClearIcon(ecs::Entity::Identifier identifier)
Record the pressable child that draws the clear button.
Definition search.cpp:159
Variant
Shape of the search field.
Definition search.hpp:64
@ SearchBar
Pill-shaped bar, 56 dp high.
Definition search.hpp:65
@ View
Another name for FullScreen.
Definition search.hpp:68
const std::string & getText(void) const noexcept
Get the query.
Definition search.cpp:49
ecs::Entity::Identifier getSearchIcon(void) const noexcept
Get the pressable child that draws the leading icon.
Definition search.cpp:124
Search & setLoading(bool loading)
Show or hide the loading glyph.
Definition search.cpp:84
const std::function< void(void)> & getOnTrailingIconPress(void) const noexcept
Get the callback run when the trailing icon is pressed.
Definition search.cpp:113
static constexpr float IconSize
Size of the icons, in dp.
Definition search.hpp:81
Search & setText(const std::string &text)
Store the query.
Definition search.cpp:54
Search & setPlaceholderEntity(ecs::Entity::Identifier identifier)
Record the child that draws the placeholder.
Definition search.cpp:149
static constexpr const char * SearchGlyph
Glyph name of the default leading icon.
Definition search.hpp:84
Search & setTrailingIcon(ecs::Entity::Identifier identifier)
Record the pressable child that draws the trailing icon.
Definition search.cpp:179
const std::string & getPlaceholder(void) const noexcept
Get the hint shown while the query is empty.
Definition search.cpp:39
static constexpr const char * LoadingGlyph
Glyph name of the icon shown while loading.
Definition search.hpp:90
Search & setOnIconPress(std::function< void(void)> handler)
Set the callback run when the leading icon is pressed.
Definition search.cpp:94
static constexpr float ViewHeight
Height of the FullScreen variant, in dp.
Definition search.hpp:78
Search & setIconGlyph(const std::string &glyph)
Set the icon at the leading edge of the field, such as "arrow_back".
Definition search.cpp:64
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
const std::string & getTrailingGlyph(void) const noexcept
Get the icon at the trailing edge of the field.
Definition search.cpp:69
Search & setSearchIcon(ecs::Entity::Identifier identifier)
Record the pressable child that draws the leading icon.
Definition search.cpp:129
ecs::Entity::Identifier getTrailingIcon(void) const noexcept
Get the pressable child that draws the trailing icon.
Definition search.cpp:174
ecs::Entity::Identifier getPlaceholderEntity(void) const noexcept
Get the child that draws the placeholder.
Definition search.cpp:144
ecs::Entity::Identifier getTextEntity(void) const noexcept
Get the child the user types the query in.
Definition search.cpp:134
static constexpr float BarHeight
Height of the SearchBar variant, in dp.
Definition search.hpp:75
Search & setPlaceholder(const std::string &placeholder)
Set the hint shown while the query is empty, such as "Search".
Definition search.cpp:44
Search(void)=default
Create the configuration of an empty search bar with the "search" leading icon.
Search & setTrailingGlyph(const std::string &glyph)
Set the icon at the trailing edge of the field, such as "mic".
Definition search.cpp:74
Search & setVariant(Variant variant)
Set the shape of the field.
Definition search.cpp:34
bool isLoading(void) const noexcept
Tell whether the field shows its loading glyph.
Definition search.cpp:79
const std::function< void(void)> & getOnClearIconPress(void) const noexcept
Get the callback run when the clear button is pressed.
Definition search.cpp:101
Search & setOnClearIconPress(std::function< void(void)> handler)
Set the callback run when the clear button is pressed.
Definition search.cpp:106
Base class of every component: a piece of state attached to an entity, plus a "changed" flag.
Definition component.hpp:72
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
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
State. Every class here is plain data attached to an entity.