23#include "guillaume/entities/navigation_drawer_destination.hpp"
24#include "guillaume/entities/navigation_drawer.hpp"
30#include "guillaume/components/navigation_drawer_destination.hpp"
31#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/entities/content_helpers.hpp"
33#include "guillaume/entities/divider.hpp"
35#include "guillaume/entities/text.hpp"
36#include "guillaume/systems/navigation_drawer.hpp"
37#include "guillaume/theme.hpp"
50 std::vector<NavigationDrawer::Entry> toEntries(
51 const std::vector<std::pair<std::string, std::string>> &pairs)
53 std::vector<NavigationDrawer::Entry> entries;
54 entries.reserve(pairs.size());
55 for (
const auto &pair: pairs) {
57 entry.icon = pair.first;
58 entry.label = pair.second;
59 entries.push_back(entry);
73 std::shared_ptr<NavigationDrawer>
78 _variant, _entries, _onIndexChange);
85 _variant = Variant::Standard;
90 _config.mainAxisAlignment =
92 _config.crossAxisAlignment =
115 const std::string &iconGlyph,
const std::string &label,
116 const std::string &right,
bool disabled)
119 entry.
kind = EntryKind::Destination;
120 entry.
icon = iconGlyph;
124 _entries.push_back(entry);
132 entry.
kind = EntryKind::Section;
134 _entries.push_back(entry);
141 entry.
kind = EntryKind::Divider;
142 _entries.push_back(entry);
147 std::function<
void(std::size_t)> onIndexChange)
149 _onIndexChange = std::move(onIndexChange);
153 std::shared_ptr<NavigationDrawer>
155 std::shared_ptr<ecs::Entity> parent,
Variant variant,
156 const std::vector<std::pair<std::string, std::string>>
161 for (
const auto &destination: destinations) {
168 NavigationDrawer::NavigationDrawer(
171 const std::vector<std::pair<std::string, std::string>> &destinations)
172 : NavigationDrawer(registry, config, variant, toEntries(destinations),
177 NavigationDrawer::NavigationDrawer(
179 Variant variant,
const std::vector<Entry> &entries,
180 std::function<
void(std::size_t)> onIndexChange)
182 components::NavigationDrawer>(registry, config)
199 void NavigationDrawer::buildDestinations(
void)
202 if (weak_from_this().expired()) {
208 const std::vector<Entry> entries =
211 const bool collapsed =
214 == Variant::Collapsed;
215 const float itemWidth =
224 backdrop->setParent(shared_from_this());
227 .setBackdrop(backdrop->getIdentifier());
232 const auto previousEntries =
235 for (
const auto previous: previousEntries) {
240 std::vector<ecs::Entity::Identifier> entryIdentifiers;
241 std::vector<ecs::Entity::Identifier> destinationIdentifiers;
242 std::vector<ecs::Entity::Identifier> sectionIdentifiers;
243 std::vector<ecs::Entity::Identifier> dividerIdentifiers;
245 for (
const auto &entry: entries) {
246 std::shared_ptr<ecs::Entity> child;
248 switch (entry.kind) {
249 case EntryKind::Section: {
257 registry, *
this, shared_from_this(), entry.label,
260 if (child !=
nullptr) {
261 sectionIdentifiers.push_back(child->getIdentifier());
265 case EntryKind::Divider: {
266 Divider::Builder builder(registry, *
this);
267 child = builder.makeDivider(shared_from_this(), itemWidth,
268 Divider::Variant::FullWidth);
269 if (child !=
nullptr) {
270 dividerIdentifiers.push_back(child->getIdentifier());
274 case EntryKind::Destination:
279 collapsed, itemWidth);
280 child->setParent(shared_from_this());
282 destinationIdentifiers.push_back(child->getIdentifier());
287 if (child !=
nullptr) {
288 entryIdentifiers.push_back(child->getIdentifier());
289 children.push_back(std::move(child));
294 .setEntryEntities(entryIdentifiers)
295 .setDestinationEntities(destinationIdentifiers)
296 .setSectionEntities(sectionIdentifiers)
297 .setDividerEntities(dividerIdentifiers);
312 (variant == Variant::Collapsed) != (previous == Variant::Collapsed);
313 const std::size_t selected =
322 .getDestinationEntities()
362 std::function<
void(std::size_t)> onIndexChange)
416 const auto &destinations =
419 .getDestinationEntities();
421 return index < destinations.size() ? destinations[index]
444 .getSectionEntities()
451 const auto §ions =
454 .getSectionEntities();
456 return index < sections.size() ? sections[index]
464 .getDividerEntities()
475 void NavigationDrawer::build(
void)
480 .getDestinationEntities()
@ Start
At the top (row) or left (column).
@ Start
Packed at the left (row) or top (column).
@ Vertical
Column: children placed top to bottom.
State of one destination of a navigation drawer: an icon, a label and an optional text at the trailin...
State of a navigation drawer: the side sheet that lists the destinations of an app,...
static constexpr float Width
Width of a standard or modal drawer, in dp.
static constexpr float Height
Height of the drawer, in dp.
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.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
bool removeEntity(Entity::Identifier identifier)
Remove an entity, with its children and all their components.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
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.
static std::shared_ptr< DrawerDestination > create(Arguments &&...arguments)
Create a destination and its children, without registering it in an entity registry or setting its pa...
std::shared_ptr< NavigationDrawer > registerEntity(std::shared_ptr< ecs::Entity > parent)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a NavigationDrawer, creates it and registers it in an entity registry.
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the drawer is placed.
Builder & addDivider(void)
Append a horizontal divider after the entries already added.
Builder & withVariant(Variant variant)
Set the variant.
void reset(void) override
Restore the defaults.
Builder & addDestination(const std::string &iconGlyph, const std::string &label, const std::string &right="", bool disabled=false)
Append a destination after the entries already added.
std::shared_ptr< NavigationDrawer > buildEntity(void) override
Build a drawer and its children from the current configuration, without registering it or setting its...
Builder & addSection(const std::string &title)
Append a section title after the entries already added.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
std::shared_ptr< NavigationDrawer > makeNavigationDrawer(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::vector< std::pair< std::string, std::string > > &destinations)
Build a drawer, register it under parent and reset the builder.
Builder & withOnIndexChange(std::function< void(std::size_t)> onIndexChange)
Set the handler called when the user selects another destination.
A side sheet listing destinations, of which exactly one is selected, with optional section titles and...
NavigationDrawer & setDestinationDisabled(std::size_t index, bool disabled)
Enable or disable a destination. Restyles it at once.
NavigationDrawer & select(std::size_t index)
Select a destination and deselect the others.
components::NavigationDrawer::Variant Variant
Variant getVariant(void) const
Get the variant.
NavigationDrawer & close(void)
Close the drawer. Same as hide().
NavigationDrawer & open(void)
Open the drawer. Same as show().
static std::shared_ptr< NavigationDrawer > create(Arguments &&...arguments)
Create a drawer and its children, without registering it in an entity registry.
std::size_t getSelectedIndex(void) const
Get the index of the selected destination.
~NavigationDrawer(void) override
Default destructor.
bool isOpen(void) const
Whether the drawer is open. Same as isVisible().
NavigationDrawer & hide(void)
Hide the drawer and its backdrop.
ecs::Entity::Identifier getDestinationIdentifier(std::size_t index) const
Get the child entity of a destination: its pressable row.
NavigationDrawer & setVariant(Variant variant)
Change the variant.
std::size_t getDividerCount(void) const
Get the number of dividers.
components::NavigationDrawer::Entry Entry
NavigationDrawer & show(void)
Show the drawer, and its backdrop when the variant is Modal.
ecs::Entity::Identifier getBackdropIdentifier(void) const
Get the backdrop: the child that dims what is behind an open Modal drawer.
std::size_t getSectionCount(void) const
Get the number of section titles built.
bool isVisible(void) const
Whether the drawer is shown.
std::size_t getDestinationCount(void) const
Get the number of destinations.
NavigationDrawer & setOnIndexChange(std::function< void(std::size_t)> onIndexChange)
Set the handler called when the user selects another destination.
ecs::Entity::Identifier getRightIdentifier(std::size_t index) const
Get the trailing text entity of a destination.
ecs::Entity::Identifier getSectionIdentifier(std::size_t index) const
Get the text entity of a section title.
std::vector< std::shared_ptr< ecs::Entity > > Children
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
One destination of a NavigationDrawer: a pressable row with an icon, a label and an optional trailing...
static float widthOf(components::NavigationDrawer::Variant variant)
Get the drawer width of a variant, in dp.
static constexpr float Spacing
Gap between two stacked entries: 4 dp.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Shape and color the drawer, style its destinations, stack its entries and place its backdrop.
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select a destination from code.
static void hide(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Hide the drawer and its backdrop.
static void setDestinationDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, bool disabled)
Enable or disable one destination and restyle it.
static void show(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Show the drawer, and its backdrop when it is modal.
static void selectFirstEnabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Select the first destination that is not disabled, then restyle.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Connect a navigation drawer to its destinations and section headers.
static constexpr float Padding
Space between the edge of the drawer and its entries: 12 dp.
static std::size_t selectedIndex(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the index of the selected destination.
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.
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.
@ TitleSmall
14 / 20, medium weight.
std::string label
Destination label or section title.
EntryKind kind
What it is.
std::string icon
Icon glyph name of a destination.
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...