27#include "guillaume/entities/search.hpp"
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"
47 _variant, _placeholder, _text);
54 _variant = Variant::SearchBar;
59 _config.mainAxisAlignment =
61 _config.crossAxisAlignment =
86 _placeholder = placeholder;
97 std::shared_ptr<ecs::Entity> parent,
Variant variant,
98 const std::string &placeholder,
const std::string &text)
101 .withPlaceholder(placeholder)
103 .registerEntity(parent);
108 const std::string &placeholder,
const std::string &text)
174 .setIconGlyph(iconGlyph);
198 std::function<
void(
void)> onPress)
202 .setTrailingGlyph(iconGlyph)
229 .setElevationLevel(level);
252 .getPlaceholderEntity();
295 void Search::build(
void)
301 void Search::buildChildren(
void)
304 if (weak_from_this().expired()) {
310 const auto self = shared_from_this();
311 const auto iconColor =
schemeColor(SchemeColorRole::OnSurfaceVariant);
321 if (
const auto icon =
322 buildIcon(registry, *
this, self, config.getIconGlyph(),
324 search().setSearchIcon(icon->getIdentifier());
332 Text::Builder builder(registry, *
this);
333 const auto text = builder.makeText(
336 text->setContent(config.getText());
337 registry.
addComponent<components::Focus>(text->getIdentifier());
338 search().setTextEntity(text->getIdentifier());
343 if (
const auto placeholder =
344 buildText(registry, *
this, self, config.getPlaceholder(),
346 search().setPlaceholderEntity(placeholder->getIdentifier());
355 search().setClearIcon(icon->getIdentifier());
366 search().setLoadingIcon(icon->getIdentifier());
372 if (
const auto icon =
373 buildIcon(registry, *
this, self, config.getTrailingGlyph(),
375 search().setTrailingIcon(icon->getIdentifier());
387 const auto ¤t = search();
390 for (
const auto child:
391 { current.getSearchIcon(), current.getTextEntity(),
392 current.getPlaceholderEntity(), current.getClearIcon(),
393 current.getLoadingIcon(), current.getTrailingIcon() }) {
395 children.push_back(entity);
Soft shadow drawn around an elevated surface.
@ Start
Packed at the left (row) or top (column).
@ Horizontal
Row: children placed left to right.
State of a search field: a leading icon, the query the user types (or a placeholder),...
static constexpr float DefaultWidth
Width given to a search field that has no fixed width, in dp.
static constexpr float IconSize
Size of the icons, in dp.
static constexpr const char * LoadingGlyph
Glyph name of the icon shown while loading.
Search & setOnTrailingIconPress(std::function< void(void)> handler)
Set the callback run when the trailing icon is pressed.
static constexpr const char * ClearGlyph
Glyph name of the clear button.
static constexpr float BarHeight
Height of the SearchBar variant, in dp.
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.
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
ComponentRegistry & getComponentRegistry(void)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a Search, creates it and registers it in an entity registry.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
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.
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the search field is placed.
std::shared_ptr< Search > buildEntity(void) override
Build a search field and its children from the current configuration, without registering it or setti...
Builder & withText(const std::string &text)
Set the initial query.
void reset(void) override
Restore the defaults.
Builder & withVariant(Variant variant)
Set the variant.
Builder & withPlaceholder(const std::string &placeholder)
Set the placeholder: the hint shown while the query is empty.
A search field: a leading icon, an editable query with a placeholder, a clear button and an optional ...
Search & setLoading(bool loading)
Show the loading glyph in place of the clear button, or the reverse. Restyles at once.
Search & setOnIconPress(std::function< void(void)> onIconPress)
Set the handler called when the user presses the leading icon (Paper onIconPress).
Search & setPlaceholder(const std::string &placeholder)
Change the placeholder: the hint shown while the query is empty.
components::Search::Variant Variant
Search variant.
static std::shared_ptr< Search > create(Arguments &&...arguments)
Create a search field and its children, without registering it in an entity registry.
Search & hide(void)
Hide the search field: set its components::Overlay not visible.
Search & setElevation(theme::ElevationLevel level)
Set the elevation level (Paper elevation). Restyles at once.
Search & setIcon(const std::string &iconGlyph)
Change the glyph of the leading icon (Paper icon). Restyles at once.
~Search(void) override
Default destructor.
ecs::Entity::Identifier getTextIdentifier(void) const
Get the query text entity: the text the user edits.
ecs::Entity::Identifier getPlaceholderIdentifier(void) const
Get the placeholder text entity.
ecs::Entity::Identifier getIconIdentifier(void) const
Get the leading icon: a pressable icon entity.
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...
Search & open(void)
Open the search field. Same as show().
bool isVisible(void) const
Whether the search field is shown.
bool isLoading(void) const
Whether the loading glyph replaces the clear button.
ecs::Entity::Identifier getClearIdentifier(void) const
Get the clear button: a pressable icon entity.
Search & clear(void)
Empty the query, as the clear button does, then call the clear handler.
Search & show(void)
Show the search field: set its components::Overlay visible.
Search & close(void)
Close the search field. Same as hide().
Search & setText(const std::string &text)
Replace the query. Restyles at once.
Search & setTrailingIcon(const std::string &iconGlyph, std::function< void(void)> onPress={})
Set or hide the trailing icon (Paper right and traileringIcon).
Search & setVariant(Variant variant)
Change the variant. Restyles at once.
bool isOpen(void) const
Whether the search field is open. Same as isVisible().
Variant getVariant(void) const
Get the variant.
std::string getText(void) const
Get the query, including what the user typed.
std::vector< std::shared_ptr< ecs::Entity > > Children
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
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.
static bool isVisible(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the search field is shown.
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the search field.
static void installClearIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the clear icon a button that calls clear().
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one search field and lay out its children.
static void installIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the leading icon a button that calls pressIcon().
static void setText(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const std::string &text)
Replace the query and restyle the field.
static std::string text(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the query the field holds.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a search field its accessibility role and make its icons pressable.
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the search field.
static void installTrailingIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the trailing icon a button that calls pressTrailingIcon().
static constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
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.
std::shared_ptr< Text > buildText(ecs::ComponentRegistry ®istry, 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 ®istry, 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.
@ BodyLarge
16 / 24, regular.
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
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...