23#include "guillaume/systems/list.hpp"
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/borders.hpp"
29#include "guillaume/components/bound.hpp"
30#include "guillaume/components/color.hpp"
31#include "guillaume/components/glyph.hpp"
32#include "guillaume/components/layout.hpp"
33#include "guillaume/components/transform.hpp"
36#include "guillaume/systems/layout.hpp"
37#include "guillaume/theme.hpp"
44 using Row = components::List::Row;
46 using Ids = std::vector<ecs::Entity::Identifier>;
59 constexpr float AccordionInset = 40.0f;
70 children.push_back(identifier);
80 void colorChild(ecs::ComponentRegistry ®istry,
82 const utility::graphic::Color32Bit &color)
85 && registry.hasComponent<components::Color>(child)) {
86 registry.getComponent<components::Color>(child).setColor(color);
96 bool isContainer(
const ecs::ComponentRegistry ®istry,
100 && registry.hasComponent<components::Layout>(identifier)
101 && registry.hasComponent<components::Transform>(identifier)
102 && registry.hasComponent<components::Bound>(identifier);
110 void arrange(ecs::ComponentRegistry ®istry,
113 if (!isContainer(registry, identifier)) {
117 registry.getComponent<components::Layout>(identifier)
130 void configureGroup(ecs::ComponentRegistry ®istry,
135 if (!isContainer(registry, identifier)) {
138 registry.getComponent<components::Layout>(identifier)
140 .setMainAxisAlignment(
142 .setCrossAxisAlignment(
148 .setChildren(children);
160 bool isHidden(
const components::List &list,
163 if (path.size() <= 1) {
166 return path.size() > 2 || path[0] >= list.getItems().size()
167 || !list.getItems()[path[0]].expanded;
177 void paintRow(ecs::ComponentRegistry ®istry,
const Row &row,
178 bool hidden,
bool showSupporting)
185 auto headlineColor = enabled
188 auto supportingColor = enabled
191 auto iconColor = supportingColor;
202 if (!showSupporting) {
206 colorChild(registry, row.view, rowColor);
207 colorChild(registry, row.headline, headlineColor);
208 colorChild(registry, row.supporting, supportingColor);
209 colorChild(registry, row.leadingIcon, iconColor);
210 colorChild(registry, row.trailingIcon, iconColor);
224 void configureRow(ecs::ComponentRegistry ®istry,
const Row &row,
225 const Item *item,
float width, std::size_t depth,
226 bool hidden,
bool showSupporting)
228 const bool accordion = item !=
nullptr && !item->children.empty();
230 if (isContainer(registry, row.view)) {
232 pushValid(rowChildren, row.lead);
233 pushValid(rowChildren, row.trailingIcon);
236 pushValid(leadChildren, row.spacer);
237 pushValid(leadChildren, row.leadingImage);
238 pushValid(leadChildren, row.leadingIcon);
239 pushValid(leadChildren, row.content);
242 pushValid(contentChildren, row.headline);
243 if (showSupporting) {
244 pushValid(contentChildren, row.supporting);
247 registry.getComponent<components::Layout>(row.view)
249 .setMainAxisAlignment(
254 .setCrossAxisAlignment(
256 .setPadding(RowPadding)
257 .setSpacing(RowSpacing)
258 .setFixedWidth(width)
259 .setChildren(rowChildren);
260 configureGroup(registry, row.lead,
263 configureGroup(registry, row.content,
266 if (isContainer(registry, row.spacer)) {
267 registry.getComponent<components::Layout>(row.spacer)
268 .setFixedWidth(
static_cast<float>(depth)
270 .setFixedHeight(1.0f);
275 if (registry.hasComponent<components::Accessibility>(
277 auto &accessibility =
278 registry.getComponent<components::Accessibility>(
280 accessibility.setLabel(item->headline)
281 .setHint(item->supporting);
283 accessibility.setExpanded(item->expanded);
287 !hidden && !item->disabled);
290 && registry.hasComponent<components::Glyph>(
292 registry.getComponent<components::Glyph>(row.trailingIcon)
293 .setName(item->expanded
299 paintRow(registry, row, hidden, showSupporting);
302 const bool childrenHidden =
303 hidden || depth > 0 || item ==
nullptr || !item->expanded;
304 for (std::size_t index = 0; index < row.children.size(); ++index) {
306 item !=
nullptr && index < item->children.size()
307 ? &item->children[index]
309 configureRow(registry, row.children[index], child, width,
310 depth + 1, childrenHidden, showSupporting);
319 void measureRow(ecs::ComponentRegistry ®istry,
const Row &row)
321 arrange(registry, row.spacer);
322 arrange(registry, row.content);
323 arrange(registry, row.lead);
324 arrange(registry, row.view);
333 void placeRow(ecs::ComponentRegistry ®istry,
const Row &row)
335 arrange(registry, row.view);
336 arrange(registry, row.lead);
337 arrange(registry, row.content);
347 void installRows(ecs::ComponentRegistry ®istry,
352 for (std::size_t index = 0; index < row.children.size(); ++index) {
353 auto childPath = path;
354 childPath.push_back(index);
355 installRows(registry, identifier, row.children[index],
375 for (std::size_t index = 0; index < rows.size(); ++index) {
376 installRows(componentRegistry, entityIdentifier, rows[index],
396 componentRegistry, row->view,
398 [registry, identifier, path]() {
399 pressItem(*registry, identifier, path);
401 [registry, identifier, path]() {
402 styleRow(*registry, identifier, path);
412 const auto *row = list.
findRow(path);
414 if (row ==
nullptr) {
417 paintRow(componentRegistry, *row, isHidden(list, path),
427 const auto *item = list.
findItem(path);
429 if (item ==
nullptr || item->disabled || isHidden(list, path)) {
434 const auto onPress = item->onPress;
436 if (!item->children.empty() && path.size() == 1) {
437 setExpanded(componentRegistry, entityIdentifier, path[0],
447 std::size_t index,
bool disabled)
451 apply(componentRegistry, entityIdentifier);
456 std::size_t index,
bool expanded)
459 .setItemExpanded(index, expanded);
460 apply(componentRegistry, entityIdentifier);
471 return index < items.size() && items[index].expanded;
479 const auto &rows = list.
getRows();
480 const auto &items = list.getItems();
481 const bool showSupporting = list.getVariant() != Variant::SingleLine;
491 if (!layout.hasFixedWidth()) {
503 std::vector<const Row *> shown;
506 for (std::size_t index = 0; index < rows.size(); ++index) {
507 const auto &row = rows[index];
508 const Item *item = index < items.size() ? &items[index] :
nullptr;
510 configureRow(componentRegistry, row, item, width, 0,
false,
514 shown.push_back(&row);
515 children.push_back(row.entity);
517 if (item ==
nullptr || !item->expanded) {
520 for (
const auto &child: row.children) {
522 shown.push_back(&child);
523 children.push_back(child.entity);
528 layout.setChildren(children);
532 for (
const auto *row: shown) {
533 measureRow(componentRegistry, *row);
537 for (
const auto *row: shown) {
538 placeRow(componentRegistry, *row);
Corner radii and outline stroke of a rectangular entity.
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
How a container arranges its children: in a row or a column, with which alignment,...
Layout & setAxis(Axis axis)
Choose between a row and a column.
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
float getFixedWidth(void) const
Get the forced container width.
@ Start
At the top (row) or left (column).
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
MainAxisAlignment
Where the children go along the main axis when the container is larger than them.
@ Start
Packed at the left (row) or top (column).
Axis
Direction along which children are arranged.
@ Vertical
Column: children placed top to bottom.
@ Horizontal
Row: children placed left to right.
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
State of a list: a column of rows, each with a headline and optional supporting text,...
const Item * findItem(const Path &path) const noexcept
Find an entry from its position in the tree.
static constexpr float DefaultWidth
Default width of a list, in dp.
Variant getVariant(void) const noexcept
Get the number of text lines a row is meant for.
static constexpr const char * CollapsedGlyph
Glyph name of the trailing icon of a collapsed accordion.
Variant
Number of text lines a row is meant for.
static constexpr const char * ExpandedGlyph
Glyph name of the trailing icon of an expanded accordion.
const std::vector< Row > & getRows(void) const noexcept
Get the entities built for the entries.
std::vector< std::size_t > Path
const Row * findRow(const Path &path) const noexcept
Find the entities built for an entry from its position in the tree.
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.
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.
components::List::Path Path
Index path of an entry: top-level index, then child indices.
static void setItemDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, bool disabled)
Enable or disable a top-level entry and restyle the list.
static void pressItem(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const Path &path)
Activate an entry as a press on its row would.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one list; called by the frame loop.
static bool isExpanded(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Whether a top-level entry is expanded.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make every row of a list pressable.
List(void)
Create the system in the Layout phase.
static void installRow(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const Path &path)
Make one row pressable.
static void setExpanded(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, bool expanded)
Expand or collapse a top-level accordion and arrange the list again.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one list, then size and place its rows.
static void styleRow(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, const Path &path)
Recolor one row without arranging the list again.
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.
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 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.
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 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.
@ None
0 dp: square corners.
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,...
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.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &content)
Container color of a widget in its current input state.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.