guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
app_bar.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/systems/app_bar.hpp"
24
25#include <optional>
26#include <vector>
27
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/button.hpp"
30#include "guillaume/components/color.hpp"
31#include "guillaume/components/elevation.hpp"
32#include "guillaume/components/layout.hpp"
33#include "guillaume/components/text.hpp"
34#include "guillaume/components/typography.hpp"
36#include "guillaume/systems/button.hpp"
37#include "guillaume/systems/layout.hpp"
38#include "guillaume/systems/typography.hpp"
39#include "guillaume/theme.hpp"
40
41namespace guillaume::systems
42{
43 namespace
44 {
45 using Variant = components::AppBar::Variant;
47
54 void styleTitle(ecs::ComponentRegistry &registry,
55 const components::AppBar &appBar)
56 {
57 const auto title = appBar.getTitleEntity();
58
59 if (registry.hasComponent<components::Text>(title)) {
60 registry.getComponent<components::Text>(title).setContent(
61 appBar.getTitle());
62 }
63 if (registry.hasComponent<components::Typography>(title)
64 && registry.hasComponent<components::Text>(title)) {
65 auto &typography =
66 registry.getComponent<components::Typography>(title);
67 const auto role = AppBar::titleRole(appBar.getVariant());
68
69 // Resolving the typescale again would mark the title changed.
70 if (typography.getRole() != role
71 || typography.getFontSize().has_value()) {
72 typography.setRole(role).setFontSize(std::nullopt);
73 Typography::apply(registry, title);
74 }
75 }
76 if (registry.hasComponent<components::Accessibility>(title)) {
77 registry.getComponent<components::Accessibility>(title)
78 .setLabel(appBar.getTitle());
79 }
80 }
81
89 void styleNavigation(ecs::ComponentRegistry &registry,
90 ecs::Entity::Identifier navigation)
91 {
92 if (!registry.hasComponent<components::Button>(navigation)) {
93 return;
94 }
95
96 auto &button =
97 registry.getComponent<components::Button>(navigation);
99 schemeColor(SchemeColorRole::OnSurface);
100
101 if (button.getContentColorOverride() != color) {
102 button.setContentColorOverride(color);
103 Button::restyle(registry, navigation);
104 }
105 }
106 } // namespace
107
110 {
111 }
112
114 const ecs::Entity::Identifier &entityIdentifier)
115 {
116 const auto &appBar = componentRegistry.getComponent<components::AppBar>(
117 entityIdentifier);
118 const auto title = appBar.getTitleEntity();
119
120 if (title == ecs::Entity::InvalidIdentifier) {
121 return;
122 }
123 if (!componentRegistry.hasComponent<components::Accessibility>(title)) {
124 componentRegistry.addComponent<components::Accessibility>(title);
125 }
126 componentRegistry.getComponent<components::Accessibility>(title)
128 .setLabel(appBar.getTitle())
129 .setFocusable(false);
130 }
131
133 {
134 // Paper Appbar/utils.ts: 64 small/center-aligned, 112 medium,
135 // 152 large; the bottom app bar is 80.
136 switch (variant) {
137 case Variant::Medium:
138 return 112.0f;
139 case Variant::Large:
140 return 152.0f;
141 case Variant::Bottom:
142 return 80.0f;
143 case Variant::Small:
144 case Variant::CenterAligned:
145 default:
146 return 64.0f;
147 }
148 }
149
151 {
152 switch (variant) {
153 case Variant::Medium:
155 case Variant::Large:
157 case Variant::Small:
158 case Variant::CenterAligned:
159 case Variant::Bottom:
160 default:
162 }
163 }
164
166 const ecs::ComponentRegistry &componentRegistry,
167 const ecs::Entity::Identifier &entityIdentifier)
168 {
169 const auto &appBar = componentRegistry.getComponent<components::AppBar>(
170 entityIdentifier);
171
173 && !appBar.getNavigation().icon.empty();
174 }
175
176 bool AppBar::needsTitle(const ecs::ComponentRegistry &componentRegistry,
177 const ecs::Entity::Identifier &entityIdentifier)
178 {
179 const auto &appBar = componentRegistry.getComponent<components::AppBar>(
180 entityIdentifier);
181
183 && !appBar.getTitle().empty();
184 }
185
187 const ecs::ComponentRegistry &componentRegistry,
188 const ecs::Entity::Identifier &entityIdentifier)
189 {
190 const auto &appBar = componentRegistry.getComponent<components::AppBar>(
191 entityIdentifier);
192
193 return appBar.getActionButtons().size() != appBar.getActions().size();
194 }
195
196 void AppBar::apply(ecs::ComponentRegistry &componentRegistry,
197 const ecs::Entity::Identifier &entityIdentifier)
198 {
199 const auto &appBar = componentRegistry.getComponent<components::AppBar>(
200 entityIdentifier);
201 auto &layout = componentRegistry.getComponent<components::Layout>(
202 entityIdentifier);
203 const auto variant = appBar.getVariant();
204 const auto invalid = ecs::Entity::InvalidIdentifier;
205 const auto navigation = appBar.getNavigationButton();
206 const auto title = appBar.getTitleEntity();
207
208 // A center-aligned bar spreads its children, or centers a lone title.
209 auto mainAxisAlignment = components::Layout::MainAxisAlignment::Start;
210 if (variant == Variant::CenterAligned) {
211 mainAxisAlignment = (appBar.getNavigation().icon.empty()
212 && appBar.getActions().empty())
215 }
216
217 std::vector<ecs::Entity::Identifier> children;
218 if (navigation != invalid) {
219 children.push_back(navigation);
220 }
221 if (title != invalid) {
222 children.push_back(title);
223 }
224 for (const auto action: appBar.getActionButtons()) {
225 if (action != invalid) {
226 children.push_back(action);
227 }
228 }
229
231 .setCrossAxisAlignment(
233 .setMainAxisAlignment(mainAxisAlignment)
236 .setFixedHeight(height(variant))
237 .setChildren(children);
238
239 // Paper: elevated bars (and the bottom bar) use elevation.level2.
240 const bool tinted = appBar.isElevated() || variant == Variant::Bottom;
241 componentRegistry.getComponent<components::Color>(entityIdentifier)
242 .setColor(
244 : schemeColor(SchemeColorRole::Surface));
245 componentRegistry.getComponent<components::Elevation>(entityIdentifier)
246 .setElevationLevel(appBar.isElevated()
249
250 styleNavigation(componentRegistry, navigation);
251 styleTitle(componentRegistry, appBar);
252
253 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
254 Layout::layerOf(componentRegistry, entityIdentifier));
255 }
256
257 void AppBar::update(const ecs::Entity::Identifier &entityIdentifier)
258 {
259 if (!requireComponent<components::AppBar>(entityIdentifier)) {
260 return;
261 }
262
263 apply(getComponentRegistry(), entityIdentifier);
264 }
265} // namespace guillaume::systems
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setFocusable(bool focusable)
Include the entity in the Tab traversal or leave it out.
Accessibility & setLabel(const std::string &label)
Set the name announced for the entity, such as "Close".
State of an app bar: the strip that carries a screen's title, its navigation icon and its action icon...
Definition app_bar.hpp:54
ecs::Entity::Identifier getNavigationButton(void) const noexcept
Get the icon button child built for the navigation action.
Definition app_bar.cpp:97
bool isElevated(void) const noexcept
Tell whether the bar is elevated.
Definition app_bar.cpp:83
ecs::Entity::Identifier getTitleEntity(void) const noexcept
Get the text child that displays the title.
Definition app_bar.cpp:111
const Identifiers & getActionButtons(void) const noexcept
Get the icon button children built for the trailing actions.
Definition app_bar.cpp:125
Variant
Shape of the bar. It sets the height and the title size.
Definition app_bar.hpp:64
static constexpr float HorizontalPadding
Padding on each side of the bar and gap between its children, in dp.
Definition app_bar.hpp:85
std::optional< utility::graphic::Color32Bit > OptionalColor
A color that replaces a themed one, or nothing to keep the theme.
Definition button.hpp:86
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Soft shadow drawn around an elevated surface.
Definition elevation.hpp:63
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
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.
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...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
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...
static bool needsNavigationButton(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the navigation button child still has to be created.
Definition app_bar.cpp:165
static theme::TypeRole titleRole(components::AppBar::Variant variant)
Get the typescale role the title of a variant is drawn with.
Definition app_bar.cpp:150
static bool needsActionButtons(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the action button children have to be created again.
Definition app_bar.cpp:186
static void installTitle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Expose the title child to assistive technology as a header.
Definition app_bar.cpp:113
static bool needsTitle(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the title child still has to be created.
Definition app_bar.cpp:176
static float height(components::AppBar::Variant variant)
Get the fixed height of a variant.
Definition app_bar.cpp:132
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one app bar and arrange its children.
Definition app_bar.cpp:196
AppBar(void)
Create the system in the Layout phase.
Definition app_bar.cpp:108
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one app bar; called by the frame loop.
Definition app_bar.cpp:257
static void restyle(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Recompute the colors and geometry of the button at once.
Definition button.cpp:228
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static void apply(ecs::ComponentRegistry &registry, 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.
Definition layout.cpp:126
static std::uint32_t layerOf(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Definition layout.cpp:90
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.
@ Header
Heading (app bar title, list subheader).
The entity-component-system core, independent of any widget.
Definition component.hpp:28
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 schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Level2
Second raised level.
Definition tokens.hpp:133
@ Level0
Flat: no shadow, no tint.
Definition tokens.hpp:131
TypeRole
The named text styles of Material Design 3: what a piece of text is, not how big it is.
Definition typescale.hpp:47
@ TitleLarge
22 / 28, regular.
Definition typescale.hpp:54
@ HeadlineMedium
28 / 36, regular.
Definition typescale.hpp:52
@ HeadlineSmall
24 / 32, regular.
Definition typescale.hpp:53
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.