23#include "guillaume/entities/navigation_bar_destination.hpp"
24#include "guillaume/entities/navigation_bar.hpp"
30#include "guillaume/components/navigation_bar_destination.hpp"
31#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/systems/navigation_bar.hpp"
33#include "guillaume/theme.hpp"
47 std::vector<NavigationBar::Destination> toDestinations(
48 const std::vector<std::pair<std::string, std::string>> &pairs)
50 std::vector<NavigationBar::Destination> destinations;
51 destinations.reserve(pairs.size());
52 for (
const auto &pair: pairs) {
54 pair.first, pair.second,
"",
"" });
68 const ecs::ComponentRegistry ®istry,
71 components::NavigationBarDestination::*getter)(
void)
74 const auto &destinations =
75 registry.getComponent<components::NavigationBar>(identifier)
76 .getDestinationEntities();
78 if (index >= destinations.size()
79 || !registry.hasComponent<components::NavigationBarDestination>(
80 destinations[index])) {
83 return (registry.getComponent<components::NavigationBarDestination>(
100 _variant, _specs, _onIndexChange);
107 _variant = Variant::WithLabels;
108 _destinations.clear();
113 _config.mainAxisAlignment =
115 _config.crossAxisAlignment =
118 _config.spacing = 0.0f;
119 _config.setFixedWidth(360.0f);
137 const std::string &iconGlyph,
const std::string &label,
138 const std::string &focusedIconGlyph,
const std::string &badge)
140 _destinations.emplace_back(iconGlyph, label);
142 Destination { iconGlyph, label, focusedIconGlyph, badge });
147 std::function<
void(std::size_t)> onIndexChange)
149 _onIndexChange = std::move(onIndexChange);
154 std::shared_ptr<ecs::Entity> parent,
Variant variant,
155 const std::vector<std::pair<std::string, std::string>> &destinations)
159 for (
const auto &destination: destinations) {
166 NavigationBar::NavigationBar(
169 const std::vector<std::pair<std::string, std::string>> &destinations)
170 : NavigationBar(registry, config, variant, toDestinations(destinations),
176 const SurfaceConfig &config,
Variant variant,
177 const std::vector<Destination> &destinations,
178 std::function<
void(std::size_t)> onIndexChange)
185 .setDestinations(destinations)
196 void NavigationBar::buildDestinations(
void)
199 if (weak_from_this().expired()) {
205 const std::vector<Destination> destinations =
208 std::vector<ecs::Entity::Identifier> identifiers =
210 .getDestinationEntities();
214 for (std::size_t index = identifiers.size();
215 index < destinations.size(); ++index) {
216 const auto destination =
218 destination->setParent(shared_from_this());
220 identifiers.push_back(destination->getIdentifier());
224 .setDestinationEntities(identifiers);
228 for (
const auto identifier: identifiers) {
230 children.push_back(std::move(destination));
255 const std::string &label)
274 const std::string &badge)
280 if (index >= bar.getDestinations().size()) {
287 const auto destination = index < destinations.size()
290 if (destination !=
nullptr) {
291 destination->setBadge(badge);
299 std::function<
void(std::size_t)> onIndexChange)
308 std::function<
void(std::size_t)> onTabPress)
340 const auto &destinations =
343 .getDestinationEntities();
345 return index < destinations.size() ? destinations[index]
352 return destinationPart(
360 return destinationPart(
368 return destinationPart(
376 return destinationPart(
381 void NavigationBar::build(
void)
@ Horizontal
Row: children placed left to right.
ecs::Entity::Identifier getIndicator(void) const noexcept
Get the pill drawn behind the icon of the selected destination.
ecs::Entity::Identifier getIconEntity(void) const noexcept
Get the child that draws the icon.
ecs::Entity::Identifier getBadgeEntity(void) const noexcept
Get the child that draws the badge.
ecs::Entity::Identifier getLabelEntity(void) const noexcept
Get the child that draws the label.
State of a navigation bar: the bottom strip of three to five destinations that switches between the t...
static constexpr float Height
Height of the bar, in dp.
const std::vector< ecs::Entity::Identifier > & getDestinationEntities(void) const noexcept
Get the destination children.
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.
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< BarDestination > create(Arguments &&...arguments)
Create a destination and its children, without registering it in an entity registry or setting its pa...
std::shared_ptr< NavigationBar > registerEntity(std::shared_ptr< ecs::Entity > parent)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures a NavigationBar, creates it and registers it in an entity registry.
std::shared_ptr< NavigationBar > buildEntity(void) override
Build a navigation bar and its destinations from the current configuration, without registering it or...
Builder & withOnIndexChange(std::function< void(std::size_t)> onIndexChange)
Set the handler called when the user selects another destination (Paper onIndexChange).
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the bar is placed.
std::shared_ptr< NavigationBar > makeNavigationBar(std::shared_ptr< ecs::Entity > parent, Variant variant, const std::vector< std::pair< std::string, std::string > > &destinations)
Build a navigation bar, register it under parent and reset the builder.
Builder & addDestination(const std::string &iconGlyph, const std::string &label, const std::string &focusedIconGlyph="", const std::string &badge="")
Append a destination after the ones already added.
Builder & withVariant(Variant variant)
Set the variant.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Create a builder set to the defaults of reset().
void reset(void) override
Restore the defaults.
NavigationBar & setOnTabPress(std::function< void(std::size_t)> onTabPress)
Set the handler called on every destination press, even on the selected one (Paper onTabPress).
std::size_t getSelectedIndex(void) const
Get the index of the selected destination.
components::NavigationBar::Destination Destination
ecs::Entity::Identifier getBadgeIdentifier(std::size_t index) const
Get the badge entity of a destination.
NavigationBar & setOnIndexChange(std::function< void(std::size_t)> onIndexChange)
Set the handler called when the user selects another destination (Paper onIndexChange).
components::NavigationBar::Variant Variant
NavigationBar & addDestination(const std::string &iconGlyph, const std::string &label)
Append a destination without focused icon or badge.
ecs::Entity::Identifier getLabelIdentifier(std::size_t index) const
Get the label entity of a destination.
NavigationBar & setVariant(Variant variant)
Change the variant. Restyles at once.
NavigationBar & setBadge(std::size_t index, const std::string &badge)
Change the badge label of a destination.
Variant getVariant(void) const
Get the variant.
std::size_t getDestinationCount(void) const
Get the number of destinations.
ecs::Entity::Identifier getIconIdentifier(std::size_t index) const
Get the icon entity of a destination.
NavigationBar & select(std::size_t index)
Select a destination and deselect the others.
ecs::Entity::Identifier getIndicatorIdentifier(std::size_t index) const
Get the active-indicator pill of a destination: the rounded shape behind the icon.
static std::shared_ptr< NavigationBar > create(Arguments &&...arguments)
Create a navigation bar and its destination children, without registering it in an entity registry.
ecs::Entity::Identifier getDestinationIdentifier(std::size_t index) const
Get the child entity of a destination: the pressable area holding its indicator, icon and label.
~NavigationBar(void) override
Default destructor.
std::vector< std::shared_ptr< ecs::Entity > > Children
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
One destination of a NavigationBar: a pressable column with an active-indicator pill around the icon,...
static void setBadge(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index, const std::string &badge)
Set the badge of a destination and restyle that destination.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Connect a navigation bar to its destinations.
static std::size_t selectedIndex(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the index of the selected destination.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Color the bar, size and style its destinations and place them in a row.
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select a destination from code.
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.
Widgets: the classes used to create entities with the right components and children.
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...