23#include "guillaume/systems/search.hpp"
29#include "guillaume/components/accessibility.hpp"
30#include "guillaume/components/borders.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/elevation.hpp"
33#include "guillaume/components/glyph.hpp"
34#include "guillaume/components/layout.hpp"
35#include "guillaume/components/line.hpp"
36#include "guillaume/components/overlay.hpp"
37#include "guillaume/components/text.hpp"
38#include "guillaume/components/text_field.hpp"
41#include "guillaume/systems/layout.hpp"
42#include "guillaume/systems/text_field.hpp"
43#include "guillaume/theme.hpp"
69 void colorChild(ecs::ComponentRegistry ®istry,
71 const utility::graphic::Color32Bit &color)
74 && registry.hasComponent<components::Color>(child)) {
75 registry.getComponent<components::Color>(child).setColor(color);
85 void glyphChild(ecs::ComponentRegistry ®istry,
89 && registry.hasComponent<components::Glyph>(child)) {
90 registry.getComponent<components::Glyph>(child).setName(glyph);
101 void makeIconButton(ecs::ComponentRegistry ®istry,
103 const std::string &label,
104 std::function<
void(
void)> onPress)
106 if (!isValid(icon)) {
112 registry.getComponent<components::Accessibility>(icon).setLabel(
122 void applyVariant(ecs::ComponentRegistry ®istry,
126 const bool fullScreen = search.getVariant() == Variant::FullScreen;
129 registry.getComponent<components::Layout>(identifier);
131 .setMainAxisAlignment(
133 .setCrossAxisAlignment(
139 if (!layout.hasFixedWidth() || layout.getFixedWidth() <= 0.0f) {
143 auto &line = registry.getComponent<components::Line>(identifier);
145 line.setThickness(1.0f)
147 .setColor(
schemeColor(SchemeColorRole::OutlineVariant));
149 line.setThickness(0.0f).clearColor();
152 registry.getComponent<components::Borders>(identifier)
158 registry.getComponent<components::Elevation>(identifier)
159 .getElevationLevel();
160 registry.getComponent<components::Color>(identifier)
163 ?
schemeColor(SchemeColorRole::SurfaceContainerHigh)
199 "search", [registry, identifier]() {
200 pressIcon(*registry, identifier);
215 "clear search", [registry, identifier]() {
216 clear(*registry, identifier);
231 makeIconButton(componentRegistry, search.getTrailingIcon(), label,
232 [registry, identifier]() {
233 pressTrailingIcon(*registry, identifier);
244 if (isValid(textEntity)
249 return search.getText();
254 const std::string &
text)
260 search.setText(
text);
261 if (isValid(textEntity)
266 apply(componentRegistry, entityIdentifier);
272 setText(componentRegistry, entityIdentifier,
"");
277 .getOnClearIconPress();
302 .getOnTrailingIconPress();
325 return componentRegistry
338 search.
setText(
text(componentRegistry, entityIdentifier));
340 const bool hasQuery = !search.
getText().empty();
341 const bool loading = search.isLoading();
342 const bool showClear = hasQuery && !loading;
343 const bool showTrailing = !search.getTrailingGlyph().empty();
344 const auto iconColor =
schemeColor(SchemeColorRole::OnSurfaceVariant);
346 applyVariant(componentRegistry, entityIdentifier, search);
362 .setLabel(search.getPlaceholder());
364 const auto searchIcon = search.getSearchIcon();
365 const auto textEntity = search.getTextEntity();
366 const auto placeholder = search.getPlaceholderEntity();
367 const auto clearIcon = search.getClearIcon();
368 const auto loadingIcon = search.getLoadingIcon();
369 const auto trailing = search.getTrailingIcon();
371 glyphChild(componentRegistry, searchIcon, search.getIconGlyph());
372 colorChild(componentRegistry, searchIcon, iconColor);
374 if (isValid(placeholder)
377 .setContent(search.getPlaceholder());
379 colorChild(componentRegistry, placeholder,
382 colorChild(componentRegistry, clearIcon,
384 if (isValid(clearIcon)) {
388 colorChild(componentRegistry, loadingIcon,
393 glyphChild(componentRegistry, trailing, search.getTrailingGlyph());
395 colorChild(componentRegistry, trailing,
397 if (isValid(trailing)) {
404 std::vector<ecs::Entity::Identifier> children;
406 if (isValid(child)) {
407 children.push_back(child);
412 push(hasQuery || !isValid(placeholder) ? textEntity : placeholder);
413 push(loading ? loadingIcon : clearIcon);
419 .setChildren(children);
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Soft shadow drawn around an elevated surface.
How a container arranges its children: in a row or a column, with which alignment,...
@ Start
Packed at the left (row) or top (column).
@ Horizontal
Row: children placed left to right.
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
Overlay & setDismissOnOutsideClick(bool dismiss)
Choose whether a click outside the overlay dismisses it.
Overlay & setDismissOnEscape(bool dismiss)
Choose whether the Escape key dismisses the overlay.
State of a search field: a leading icon, the query the user types (or a placeholder),...
ecs::Entity::Identifier getClearIcon(void) const noexcept
Get the pressable child that draws the clear button.
static constexpr float DefaultWidth
Width given to a search field that has no fixed width, in dp.
Variant
Shape of the search field.
const std::string & getText(void) const noexcept
Get the query.
ecs::Entity::Identifier getSearchIcon(void) const noexcept
Get the pressable child that draws the leading icon.
Search & setText(const std::string &text)
Store the query.
static constexpr float ViewHeight
Height of the FullScreen variant, in dp.
const std::string & getTrailingGlyph(void) const noexcept
Get the icon at the trailing edge of the field.
ecs::Entity::Identifier getTextEntity(void) const noexcept
Get the child the user types the query in.
static constexpr float BarHeight
Height of the SearchBar variant, in dp.
The texts around the input of a text field, and its error state: placeholder, label,...
TextField & setPlaceholder(const std::string &placeholder)
Set the placeholder: the text that names the field, such as "Email".
TextField & setLabel(const std::string &label)
Store the label.
A run of text: its content, its font metrics and the position of the editing cursor.
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...
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
SystemFiller(Phase phase)
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
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...
Arranges the children of a container in a row or a column and sizes the container around them.
static void apply(ecs::ComponentRegistry ®istry, 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.
static std::uint32_t layerOf(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
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 pressIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the leading icon as a press on it would.
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().
Search(void)
Create the system in the Layout phase.
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().
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one search field; called by the frame loop.
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 pressTrailingIcon(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the trailing icon as a press on it would.
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 bool reconcile(components::TextField &textField, bool hasContent)
Derive the label-floating flag of a field that has no configuration, and store it.
static constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
State. Every class here is plain data attached to an entity.
@ Button
Pressable button.
The entity-component-system core, independent of any widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
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 transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Level0
Flat: no shadow, no tint.
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
@ None
0 dp: square corners.
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
void makeInteractive(ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
void setEnabled(ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, bool enabled)
Enable or disable an entity.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.