26#include "guillaume/entities/dialog.hpp"
28#include "guillaume/entities/builder_base.hpp"
29#include "guillaume/entities/content_helpers.hpp"
31#include "guillaume/systems/dialog.hpp"
32#include "guillaume/theme.hpp"
48 constexpr float HeroIconSize = 24.0f;
55 std::vector<Dialog::Action>
56 toActions(
const std::vector<std::string> &labels)
58 std::vector<Dialog::Action> actions;
59 actions.reserve(labels.size());
60 for (
const auto &label: labels) {
78 _title, _message, _actions, _icon, _onDismiss);
85 _variant = Variant::Alert;
93 _config.mainAxisAlignment =
95 _config.crossAxisAlignment =
97 _config.padding = DialogPadding;
98 _config.spacing = DialogSpacing;
100 _config.setFixedWidth(DialogWidth);
130 std::function<
void(
void)> onPress)
132 _actions.push_back(
Action { label, std::move(onPress) });
145 _onDismiss = std::move(onDismiss);
149 std::shared_ptr<Dialog>
151 Variant variant,
const std::string &title,
152 const std::string &message,
153 const std::vector<std::string> &actions)
155 withVariant(variant).withTitle(title).withMessage(message);
157 for (
const auto &action: actions) {
166 const std::string &title,
const std::string &message,
167 const std::vector<std::string> &actions)
168 :
Dialog(registry, config, variant, title, message, toActions(actions),
175 const std::string &title,
const std::string &message,
176 const std::vector<Action> &actions,
const std::string &icon,
177 std::function<
void(
void)> onDismiss)
191 .setOnDismissHandler(std::move(onDismiss));
278 .setOnDismissHandler(std::move(onDismiss));
292 const auto &actions =
295 .getActionEntities();
297 return index < actions.size() ? actions[index]
322 void Dialog::build(
void)
328 void Dialog::buildContent(
void)
331 if (weak_from_this().expired()) {
341 backdrop->setParent(shared_from_this());
343 dialog.setBackdrop(backdrop->getIdentifier());
346 const std::string glyph = dialog.getEffectiveIcon();
350 registry, *
this, shared_from_this(), glyph, HeroIconSize,
352 if (icon !=
nullptr) {
353 dialog.setIconEntity(icon->getIdentifier());
358 && !dialog.getTitle().empty()) {
360 buildText(registry, *
this, shared_from_this(),
363 if (title !=
nullptr) {
364 dialog.setTitleEntity(title->getIdentifier());
370 && !dialog.getMessage().empty()) {
372 buildText(registry, *
this, shared_from_this(),
375 if (message !=
nullptr) {
376 dialog.setMessageEntity(message->getIdentifier());
381 && !dialog.getActions().empty()) {
388 children.push_back(std::move(icon));
391 children.push_back(std::move(title));
394 children.push_back(std::move(message));
397 children.push_back(std::move(row));
402 void Dialog::buildActionRow(
void)
408 View::Builder rowBuilder(registry, *
this);
409 const auto row = rowBuilder.makeDefaultView(
410 shared_from_this(), utility::graphic::PoseF(), {});
411 if (row ==
nullptr) {
417 row->setCrossAxisAlignment(
420 row->setPadding(0.0f);
421 row->setFixedWidth(DialogWidth - 2.0f * DialogPadding);
425 ecs::ComponentRegistry *registryPointer = ®istry;
427 const auto &actions = dialog.getActions();
428 std::vector<std::shared_ptr<ecs::Entity>> buttons;
429 std::vector<ecs::Entity::Identifier> identifiers;
431 for (std::size_t index = 0; index < actions.size(); ++index) {
432 if (actions[index].label.empty()) {
435 Button::Builder buttonBuilder(registry, *row);
437 buttonBuilder.withLabel(actions[index].label)
438 .withColorStyle(Button::Color::Text)
439 .withShape(Button::Shape::Round)
440 .withSize(Button::Size::Small)
441 .withOnClick([registryPointer, identifier, index]() {
445 .registerEntity(row);
446 if (button !=
nullptr) {
447 buttons.push_back(button);
448 identifiers.push_back(button->getIdentifier());
451 row->setEntities(buttons);
453 dialog.setActionEntities(identifiers)
454 .setActionRow(row->getIdentifier());
State of a dialog: a modal panel with an optional icon, a title, a message and a row of text buttons.
static constexpr float Width
Width of the dialog panel, in dp.
@ Start
At the top (row) or left (column).
@ Center
Packed in the middle.
@ End
Packed at the right (row) or bottom (column).
@ Vertical
Column: children placed top to bottom.
@ Horizontal
Row: children placed left to right.
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
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.
Owns entities and gives the systems the list of entities to visit.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
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)
static std::shared_ptr< Backdrop > create(Arguments &&...arguments)
Build a hidden backdrop without registering it in an entity registry.
Configures a Dialog, then builds it and registers it in the entity registry given at construction.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
Builder & withTitle(const std::string &title)
Set the title, shown above the message.
Builder & withPose(const utility::graphic::PoseF &pose)
Set the pose the dialog is created with.
Builder & addAction(const std::string &label, std::function< void(void)> onPress={})
Append an action, shown as a text button at the end of the action row.
void reset(void) override
Restore the defaults: Alert variant, no title, no message, no action, no icon, no dismiss handler,...
std::shared_ptr< Dialog > makeDialog(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::string &title, const std::string &message, const std::vector< std::string > &actions={})
Build a dialog, register it under parent and reset the builder.
Builder & withVariant(Variant variant)
Set the dialog variant.
Builder & withMessage(const std::string &message)
Set the message, shown under the title.
std::shared_ptr< Dialog > buildEntity(void) override
Build a dialog and its children from the current configuration, without registering it or setting its...
Builder & withIcon(const std::string &iconGlyph)
Set the icon shown above the title.
Builder & withOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the dialog is dismissed (see Dialog::setOnDismiss()).
Dialog & close(void)
Hide the dialog; same as hide().
Dialog & show(void)
Show the dialog and, except for the FullScreen variant, its backdrop.
bool isOpen(void) const
Check whether the dialog is shown; same as isVisible().
ecs::Entity::Identifier getIconIdentifier(void) const
Get the identifier of the icon shown above the title.
Dialog(ecs::ComponentRegistry ®istry, const SurfaceConfig &config, Variant variant, const std::string &title, const std::string &message, const std::vector< Action > &actions, const std::string &icon, std::function< void(void)> onDismiss={})
Construct a dialog with pressable actions and an icon.
Dialog & open(void)
Show the dialog; same as show().
bool isVisible(void) const
Check whether the dialog is shown.
static std::shared_ptr< Dialog > create(Arguments &&...arguments)
Create a dialog and its children, without registering it in an entity registry.
Dialog & setVariant(Variant variant)
Change the dialog variant.
Variant getVariant(void) const
Get the dialog variant.
Dialog & dismiss(void)
Dismiss the dialog as the user would: hide it, then call the dismiss handler.
ecs::Entity::Identifier getBackdropIdentifier(void) const
Get the identifier of the backdrop drawn behind the panel.
components::Dialog::Action Action
ecs::Entity::Identifier getActionRowIdentifier(void) const
Get the identifier of the row that holds the action buttons.
Dialog & setMessage(const std::string &message)
Change the message text.
ecs::Entity::Identifier getActionIdentifier(std::size_t index) const
Get the identifier of an action button.
Dialog & setOnDismiss(std::function< void(void)> onDismiss)
Set the handler called when the dialog is dismissed (Paper onDismiss).
Dialog & setTitle(const std::string &title)
Change the title text.
Dialog & hide(void)
Hide the dialog and its backdrop.
std::size_t getActionCount(void) const
Get the number of action buttons.
components::Dialog::Variant Variant
Look of the dialog: Alert, Simple, Confirmation or FullScreen.
std::shared_ptr< Dialog > registerEntity(std::shared_ptr< ecs::Entity > parent)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
std::vector< std::shared_ptr< ecs::Entity > > Children
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one dialog, arrange its children and place its backdrop.
static void dismiss(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Close the dialog as the user would with Escape or a click outside.
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the dialog and its backdrop without telling anyone.
static constexpr float Padding
Space between the edge of the panel and its content: 24 dp.
static void pressAction(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Activate an action as a press on its button would.
static void installTitle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Expose the title child to assistive technology as a header.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Give a dialog panel its accessibility semantics.
static constexpr float Spacing
Gap between two stacked sections of the dialog: 16 dp.
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the dialog and its backdrop.
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.
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.
@ HeadlineSmall
24 / 32, regular.
@ BodyMedium
14 / 20, 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...