23#include "guillaume/systems/text_field.hpp"
29#include "guillaume/components/accessibility.hpp"
30#include "guillaume/components/borders.hpp"
31#include "guillaume/components/bound.hpp"
32#include "guillaume/components/color.hpp"
33#include "guillaume/components/focus.hpp"
34#include "guillaume/components/layout.hpp"
35#include "guillaume/components/line.hpp"
36#include "guillaume/components/style_state.hpp"
37#include "guillaume/components/transform.hpp"
38#include "guillaume/components/typography.hpp"
42#include "guillaume/systems/layout.hpp"
43#include "guillaume/systems/typography.hpp"
44#include "guillaume/theme.hpp"
50 using Configuration = components::TextFieldConfiguration;
51 using Variant = Configuration::Variant;
58 constexpr std::uint8_t DisabledFilledAlpha = 10U;
67 utility::graphic::Color32Bit activeColor(
bool disabled,
bool error,
79 return schemeColor(SchemeColorRole::OnSurfaceVariant);
88 void colorChild(ecs::ComponentRegistry ®istry,
90 const utility::graphic::Color32Bit &color)
92 if (registry.hasComponent<components::Color>(child)) {
93 registry.getComponent<components::Color>(child).setColor(color);
103 void writeChild(ecs::ComponentRegistry ®istry,
105 const std::string &content)
118 void arrange(ecs::ComponentRegistry ®istry,
121 if (!registry.hasComponent<components::Layout>(container)
122 || !registry.hasComponent<components::Transform>(container)
123 || !registry.hasComponent<components::Bound>(container)) {
127 registry.getComponent<components::Layout>(container)
138 void reportChange(ecs::ComponentRegistry ®istry,
141 auto &configuration =
142 registry.getComponent<Configuration>(identifier);
145 if (current == configuration.getText()) {
148 configuration.setText(current);
151 const auto onChange = configuration.getOnChange();
163 void styleChildren(ecs::ComponentRegistry ®istry,
166 const auto &configuration =
167 registry.getComponent<Configuration>(identifier);
171 const bool floating =
173 const bool error = field.isError();
174 const auto content = disabled
177 const auto variantColor = disabled
180 const auto errorColor = (error && !disabled)
183 const auto label = configuration.getLabelEntity();
185 writeChild(registry, label, field.getPlaceholder());
186 writeChild(registry, configuration.getPrefixEntity(),
188 writeChild(registry, configuration.getSuffixEntity(),
190 writeChild(registry, configuration.getSupportingEntity(),
194 if (registry.hasComponent<components::Typography>(label)
196 registry.getComponent<components::Typography>(label)
199 .setFontSize(std::nullopt);
203 colorChild(registry, configuration.getInputEntity(), content);
204 colorChild(registry, label,
205 floating ? activeColor(disabled, error,
209 colorChild(registry, configuration.getLeadingIconEntity(),
211 colorChild(registry, configuration.getTrailingIconEntity(),
213 colorChild(registry, configuration.getErrorIconEntity(),
215 colorChild(registry, configuration.getPrefixEntity(),
217 colorChild(registry, configuration.getSuffixEntity(),
219 colorChild(registry, configuration.getSupportingEntity(),
220 (error && !disabled) ? errorColor : variantColor);
229 void arrangeChildren(ecs::ComponentRegistry ®istry,
233 const auto &configuration =
234 registry.getComponent<Configuration>(identifier);
237 const bool floating =
239 const auto column = configuration.getInputColumn();
240 const auto label = configuration.getLabelEntity();
241 const auto input = configuration.getInputEntity();
245 if (registry.hasComponent<components::Layout>(column)) {
246 std::vector<ecs::Entity::Identifier> columnChildren;
249 || configuration.getVariant() == Variant::Filled)) {
250 columnChildren.push_back(label);
252 if (input != invalid && (floating || label == invalid)) {
253 columnChildren.push_back(input);
255 registry.getComponent<components::Layout>(column).setChildren(
258 arrange(registry, column);
261 std::vector<ecs::Entity::Identifier> children;
262 if (configuration.getLeadingIconEntity() != invalid) {
263 children.push_back(configuration.getLeadingIconEntity());
265 if (configuration.getPrefixEntity() != invalid && floating) {
266 children.push_back(configuration.getPrefixEntity());
268 if (column != invalid) {
269 children.push_back(column);
271 if (configuration.getSuffixEntity() != invalid && floating) {
272 children.push_back(configuration.getSuffixEntity());
274 if (field.isError()) {
275 if (configuration.getErrorIconEntity() != invalid) {
276 children.push_back(configuration.getErrorIconEntity());
278 }
else if (configuration.getTrailingIconEntity() != invalid) {
279 children.push_back(configuration.getTrailingIconEntity());
284 for (
const auto child: children) {
285 if (registry.hasComponent<components::Bound>(child)) {
286 width += registry.getComponent<components::Bound>(child)
290 if (children.size() > 1) {
292 *
static_cast<float>(children.size() - 1);
296 registry.getComponent<components::Layout>(identifier);
299 std::max(Configuration::MinWidth,
300 configuration.getWidth().value_or(width)))
301 .setChildren(children);
302 arrange(registry, identifier);
304 arrange(registry, column);
313 void placeManualChildren(ecs::ComponentRegistry ®istry,
317 const auto &configuration =
318 registry.getComponent<Configuration>(identifier);
321 registry.getComponent<components::Bound>(identifier)
323 const auto supporting = configuration.getSupportingEntity();
324 const auto label = configuration.getLabelEntity();
326 if (supporting != invalid) {
328 utility::graphic::PositionF(
336 && configuration.getVariant() == Variant::Outlined
339 if (configuration.getLeadingIconEntity() != invalid) {
342 const float lineHeight =
345 registry, identifier, label,
346 utility::graphic::PositionF(x, -lineHeight / 2.0f, 0.0f),
365 {}, [registry, identifier]() {
374 registry->
getComponent<Configuration>(identifier).getOnFocus();
379 focus.setOnFocusLostHandler([registry, identifier]() {
382 registry->
getComponent<Configuration>(identifier).getOnBlur();
415 return isFocused(componentRegistry, entityIdentifier)
416 || !
text(componentRegistry, entityIdentifier).empty();
422 const auto &configuration =
423 componentRegistry.
getComponent<Configuration>(entityIdentifier);
424 const auto input = configuration.getInputEntity();
427 return configuration.getText();
435 const std::string &
text)
437 auto &configuration =
438 componentRegistry.
getComponent<Configuration>(entityIdentifier);
441 configuration.setText(
text);
442 writeChild(componentRegistry, configuration.getInputEntity(),
text);
443 apply(componentRegistry, entityIdentifier);
451 apply(componentRegistry, entityIdentifier);
462 return (field.isError() && !field.getErrorText().empty())
464 : field.getSupportingText();
471 const auto &configuration =
472 componentRegistry.
getComponent<Configuration>(entityIdentifier);
485 const bool disabled =
isDisabled(componentRegistry, entityIdentifier);
487 isFocused(componentRegistry, entityIdentifier) && !disabled;
488 const bool error = field.isError();
489 const float stroke = focused ? 2.0f : 1.0f;
493 .setCrossAxisAlignment(
497 if (configuration.isMultiline()) {
498 layout.clearFixedHeight();
499 }
else if (!configuration.isDense()) {
500 layout.setFixedHeight(
Height);
502 layout.setFixedHeight(field.getPlaceholder().empty()
507 if (configuration.getVariant() == Variant::Outlined) {
508 auto outline =
schemeColor(SchemeColorRole::Outline);
513 }
else if (focused) {
521 line.setThickness(0.0f).clearColor();
527 :
schemeColor(SchemeColorRole::SurfaceContainerHighest));
531 line.setThickness(stroke)
533 .
setColor(activeColor(disabled, error, focused));
541 const auto &configuration =
542 componentRegistry.
getComponent<Configuration>(entityIdentifier);
543 const auto input = configuration.getInputEntity();
549 const bool editing = configuration.isEditable()
550 && !
isDisabled(componentRegistry, entityIdentifier)
551 &&
isFocused(componentRegistry, entityIdentifier);
552 const bool hasFocus =
555 if (editing && !hasFocus) {
557 }
else if (!editing && hasFocus) {
566 reportChange(componentRegistry, entityIdentifier);
572 field.
setLabel(field.getPlaceholder())
579 .setLabel(field.getPlaceholder())
580 .
setHint(field.getSupportingText());
583 styleChildren(componentRegistry, entityIdentifier);
584 arrangeChildren(componentRegistry, entityIdentifier);
585 placeManualChildren(componentRegistry, entityIdentifier);
597 if (registry.hasComponent<Configuration>(entityIdentifier)) {
598 apply(registry, entityIdentifier);
614 const bool floating = hasContent || textField.
isError();
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setHint(const std::string &hint)
Set the sentence describing what activating the entity does, such as "Closes the dialog".
Corner radii and outline stroke of a rectangular entity.
Borders & setColor(const utility::graphic::Color32Bit &color)
Set the color of the stroke.
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Color & setColor(const utility::graphic::Color32Bit &color)
Set the color.
Makes an entity able to own the input focus, and holds whether it owns it and what to call when it ga...
bool isFocused(void) const
Check whether the entity owns the input focus.
void setOnFocusGainedHandler(const Handler &handler)
Store the callback run when the entity gains the focus.
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.
A straight stroke across the width of the entity: a divider, an indicator, the underline of a text fi...
The interaction state of an entity in one place: enabled or disabled, and whether it is hovered,...
The texts around the input of a text field, and its error state: placeholder, label,...
bool isError(void) const
Tell whether the field is in error.
const std::string & getErrorText(void) const
Get the message shown under the field while it is in error.
TextField & setLabel(const std::string &label)
Store the label.
TextField & setLabelFloating(bool floating)
Store whether the label floats above the input.
A run of text: its content, its font metrics and the position of the editing cursor.
const std::string & getContent(void) const
Get the text.
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.
void removeComponent(const Entity::Identifier &entityIdentifier)
Delete one component of an entity. Does nothing when the entity does not have it.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
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.
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
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 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 constexpr float DenseHeight
Height of a dense single-line field without a label, in dp.
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 constexpr float DenseLabelHeight
Height of a dense single-line field that has a label, in dp.
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the field and restyle it.
static void applyEditing(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Let the input child receive typed text only while the field is being edited.
static constexpr float IconSize
static void applyContainer(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style the container only, leaving the children alone.
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 constexpr float SupportingGap
static bool reconcile(components::TextField &textField, bool hasContent)
Derive the label-floating flag of a field that has no configuration, and store it.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Update one entity that has a components::TextField; called by the frame loop.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Report an edit, then style one text field and arrange its children.
TextField(void)
Create the system in the Layout phase.
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.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Resolve the metrics of one text and write them into its components::Text.
State. Every class here is plain data attached to an entity.
@ TextField
Editable text.
The entity-component-system core, independent of any widget.
void placeChild(ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, const utility::graphic::PositionF &offset, std::uint32_t parentLayer)
Place a child at an offset from the position of its parent.
utility::graphic::Color32Bit disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit withAlpha(const utility::graphic::Color32Bit &color, std::uint8_t alpha)
Copy a color with another alpha channel.
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 disabledContainerColor(void)
Fill color of the container of a disabled widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ BodyLarge
16 / 24, regular.
@ BodySmall
12 / 16, regular.
@ None
0 dp: square corners.
@ ExtraSmall
4 dp (text fields, menus, snackbars).
const theme::TypeToken & getTypeToken(theme::TypeRole role)
Get the text style the active theme gives to a role: font family, weight, size, line height and lette...
SchemeColorRole
The named colors of a scheme: what a color is used for, not what it looks like.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
float lineHeight
Distance between two baselines, in dp.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isEnabled(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
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.
Placement helpers for the children systems::Layout does not arrange.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.