27#include "guillaume/entities/text_field.hpp"
29#include "guillaume/entities/builder_base.hpp"
30#include "guillaume/entities/content_helpers.hpp"
32#include "guillaume/systems/text_field.hpp"
33#include "guillaume/theme.hpp"
48 _leadingIcon, _trailingIcon, _supportingText, _error, _text);
55 _variant = Variant::Filled;
58 _trailingIcon.clear();
59 _supportingText.clear();
64 _config.mainAxisAlignment =
66 _config.crossAxisAlignment =
90 _placeholder = placeholder;
97 _leadingIcon = iconGlyph;
104 _trailingIcon = iconGlyph;
109 const std::string &supportingText)
111 _supportingText = supportingText;
128 std::shared_ptr<ecs::Entity> parent,
Variant variant,
129 const std::string &placeholder,
const std::string &leadingIcon,
130 const std::string &trailingIcon,
const std::string &supportingText,
131 bool error,
const std::string &text)
134 .withPlaceholder(placeholder)
135 .withLeadingIcon(leadingIcon)
136 .withTrailingIcon(trailingIcon)
137 .withSupportingText(supportingText)
140 .registerEntity(parent);
145 const std::string &placeholder,
146 const std::string &leadingIcon,
147 const std::string &trailingIcon,
148 const std::string &supportingText,
bool error,
149 const std::string &text)
151 components::TextFieldConfiguration>(registry, config)
155 .setLeadingIcon(leadingIcon)
156 .setTrailingIcon(trailingIcon)
159 .setPlaceholder(placeholder)
160 .setSupportingText(supportingText)
177 TextField::configuration(
void)
const
189 void TextField::refresh(
void)
222 return configuration().getVariant();
303 return configuration().isEditable();
315 return configuration().isDense();
327 return configuration().isMultiline();
339 std::function<
void(
const std::string &)> onChange)
347 configuration().
setOnFocus(std::move(onFocus));
353 configuration().
setOnBlur(std::move(onBlur));
359 return configuration().getLabelEntity();
364 return configuration().getInputEntity();
369 return configuration().getSupportingEntity();
374 return configuration().getPrefixEntity();
379 return configuration().getSuffixEntity();
384 return configuration().getErrorIconEntity();
387 void TextField::build(
void)
392 void TextField::buildContent(
void)
395 if (weak_from_this().expired()) {
400 auto &config = configuration();
401 const auto &state = field();
402 const auto self = shared_from_this();
406 const auto content =
schemeColor(SchemeColorRole::OnSurface);
407 const auto variantColor =
409 bool rowChanged =
false;
411 if (config.getLeadingIconEntity() == invalid) {
413 buildIcon(registry, *
this, self, config.getLeadingIcon(),
414 iconSize, variantColor);
415 if (icon !=
nullptr) {
416 config.setLeadingIconEntity(icon->getIdentifier());
421 if (config.getTrailingIconEntity() == invalid) {
423 buildIcon(registry, *
this, self, config.getTrailingIcon(),
424 iconSize, variantColor);
425 if (icon !=
nullptr) {
426 config.setTrailingIconEntity(icon->getIdentifier());
431 if (config.getErrorIconEntity() == invalid) {
433 buildIcon(registry, *
this, self,
"error", iconSize,
435 if (icon !=
nullptr) {
436 config.setErrorIconEntity(icon->getIdentifier());
442 if (column ==
nullptr) {
443 View::Builder builder(registry, *
this);
445 builder.makeDefaultView(self, utility::graphic::PoseF(), {});
448 column->setMainAxisAlignment(
450 column->setCrossAxisAlignment(
452 column->setPadding(0.0f);
453 column->setSpacing(0.0f);
454 config.setInputColumn(column->getIdentifier());
458 if (config.getInputEntity() == invalid) {
460 Text::Builder builder(registry, *column);
461 const auto input = builder.makeText(
463 input->setContent(config.getText());
464 config.setInputEntity(input->getIdentifier());
467 if (config.getLabelEntity() == invalid
468 && !state.getPlaceholder().empty()) {
469 Text::Builder builder(registry, *column);
471 builder.makeText(column, state.getPlaceholder(),
473 label->setContent(state.getPlaceholder());
474 config.setLabelEntity(label->getIdentifier());
477 if (config.getPrefixEntity() == invalid) {
479 buildText(registry, *
this, self, state.getPrefix(),
481 if (prefix !=
nullptr) {
482 config.setPrefixEntity(prefix->getIdentifier());
487 if (config.getSuffixEntity() == invalid) {
489 buildText(registry, *
this, self, state.getSuffix(),
491 if (suffix !=
nullptr) {
492 config.setSuffixEntity(suffix->getIdentifier());
499 if (config.getSupportingEntity() == invalid) {
501 registry, *
this, self,
504 if (supporting !=
nullptr) {
505 config.setSupportingEntity(supporting->getIdentifier());
515 std::vector<std::shared_ptr<ecs::Entity>> children;
517 children.push_back(std::move(icon));
520 children.push_back(std::move(prefix));
522 children.push_back(column);
524 children.push_back(std::move(suffix));
527 children.push_back(std::move(icon));
530 children.push_back(std::move(icon));
@ Start
At the top (row) or left (column).
@ Center
Packed in the middle.
@ Start
Packed at the left (row) or top (column).
@ Vertical
Column: children placed top to bottom.
@ Horizontal
Row: children placed left to right.
State of a text field widget that is not text around the input: its variant, icons,...
The texts around the input of a text field, and its error state: placeholder, label,...
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.
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)
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
Configures a TextField, then builds it and registers it in the entity registry given at construction.
Builder & withText(const std::string &text)
Set the text the field holds when created.
Builder & withLeadingIcon(const std::string &iconGlyph)
Set the icon shown at the start of the box.
Builder & withError(bool error)
Choose whether the field starts in the error state.
void reset(void) override
Restore the defaults: Filled variant, no label, no icon, no supporting text, no error,...
Builder & withTrailingIcon(const std::string &iconGlyph)
Set the icon shown at the end of the box.
Builder & withPose(const utility::graphic::PoseF &pose)
Set the pose the field is created with.
std::shared_ptr< TextField > makeTextField(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &placeholder, const std::string &leadingIcon, const std::string &trailingIcon, const std::string &supportingText, bool error, const std::string &text="")
Build a text field, register it under parent and reset the builder.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Builder & withPlaceholder(const std::string &placeholder)
Set the label.
Builder & withVariant(Variant variant)
Set the field variant.
std::shared_ptr< TextField > buildEntity(void) override
Build a text field and its children from the current configuration, without registering it or setting...
Builder & withSupportingText(const std::string &supportingText)
Set the helper text shown under the box.
static std::shared_ptr< TextField > create(Arguments &&...arguments)
Create a text field and its children, without registering it in an entity registry.
ecs::Entity::Identifier getSupportingIdentifier(void) const
Get the identifier of the child that shows the supporting or error text under the box.
TextField & setSupportingText(const std::string &supportingText)
Set the helper text shown under the box.
TextField & setSuffix(const std::string &suffix)
Set the text shown just after the typed text, such as a unit.
TextField & setErrorText(const std::string &errorText)
Set the text shown under the box in the error state.
~TextField(void) override
Default destructor.
bool isError(void) const
Check whether the field is in the error state.
TextField & setText(const std::string &text)
Replace the typed text. Restyles at once.
TextField & setDisabled(bool disabled)
Enable or disable the field.
bool isMultiline(void) const
Check whether the height follows the content.
TextField & setOnFocus(std::function< void(void)> onFocus)
Set the handler called when the field gains the focus.
bool isFocused(void) const
Check whether the field has the keyboard focus.
bool isEditable(void) const
Check whether the user may edit the text.
TextField & setOnChange(std::function< void(const std::string &)> onChange)
Set the handler called when the user edits the text (Paper onChangeText).
ecs::Entity::Identifier getPrefixIdentifier(void) const
Get the identifier of the prefix child.
ecs::Entity::Identifier getInputIdentifier(void) const
Get the identifier of the child that holds the typed text.
TextField & setError(bool error)
Put the field in or out of the error state.
std::string getText(void) const
Get the typed text.
TextField & setOnBlur(std::function< void(void)> onBlur)
Set the handler called when the field loses the focus.
bool isDisabled(void) const
Check whether the field is disabled.
Variant getVariant(void) const
Get the field variant.
static constexpr float MinWidth
Smallest width of the box, in dp (100).
TextField & setDense(bool dense)
Use the smaller, dense height (Paper dense).
TextField & setVariant(Variant variant)
Change the field variant. Restyles at once.
components::TextFieldConfiguration::Variant Variant
Look of the field: Filled or Outlined.
TextField & setPlaceholder(const std::string &placeholder)
Change the label text.
TextField & setWidth(float width)
Fix the width of the box instead of letting it follow the content. Restyles at once.
ecs::Entity::Identifier getErrorIconIdentifier(void) const
Get the identifier of the "error" icon shown in the error state.
TextField & setPrefix(const std::string &prefix)
Set the text shown just before the typed text, such as a currency sign.
bool isLabelFloating(void) const
Check whether the label floats above the typed text.
ecs::Entity::Identifier getLabelIdentifier(void) const
Get the identifier of the label child.
ecs::Entity::Identifier getSuffixIdentifier(void) const
Get the identifier of the suffix child.
TextField & setEditable(bool editable)
Allow or forbid typing in the field (Paper editable).
TextField & setMultiline(bool multiline)
Let the height of the box follow its content instead of being fixed (Paper multiline)....
bool isDense(void) const
Check whether the field uses the dense height.
static constexpr float Height
Height of a single-line field that is not dense, in dp.
static bool isFocused(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the field has the input focus.
static constexpr float Padding
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity focusable and hoverable as a text field.
static std::string text(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the text the field holds.
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the field and restyle it.
static constexpr float IconSize
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the field ignores input.
static void setText(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const std::string &text)
Replace the text from code and restyle the field.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Report an edit, then style one text field and arrange its children.
static constexpr float Spacing
Gap between the icons, the affixes and the input: 12 dp.
static bool isLabelFloating(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the label is in its floating position.
static std::string supportingText(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the text shown below the field.
Makes the color helpers callable from guillaume::entities without qualification.
State. Every class here is plain data attached to an entity.
@ TextField
Editable text.
Widgets: the classes used to create entities with the right components and children.
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 ®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.
@ BodyLarge
16 / 24, regular.
@ BodySmall
12 / 16, regular.
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...