guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
surface.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/theme.hpp"
24#include "guillaume/entities/surface.hpp"
25#include "guillaume/systems/surface_tone.hpp"
26
27namespace guillaume::entities
28{
29 SurfaceConfig &SurfaceConfig::setPose(const utility::graphic::PoseF &value)
30 {
31 pose = value;
32 return *this;
33 }
34
36 SurfaceConfig::setColor(const utility::graphic::Color32Bit &value)
37 {
38 color = value;
39 return *this;
40 }
41
43 {
44 borderRadius = value;
45 return *this;
46 }
47
49 {
50 axis = value;
51 return *this;
52 }
53
60
67
69 {
70 spacing = value;
71 return *this;
72 }
73
75 {
76 padding = value;
77 return *this;
78 }
79
81 {
82 hasFixedWidth = true;
83 fixedWidth = value;
84 return *this;
85 }
86
88 {
89 hasFixedHeight = true;
90 fixedHeight = value;
91 return *this;
92 }
93
96 float spacing)
97 {
98 SurfaceConfig config;
99
100 config.axis = axis;
101 config.mainAxisAlignment =
103 config.crossAxisAlignment =
105 config.spacing = spacing;
106 config.padding = 24.0f;
108 config.hasFixedWidth = true;
109 config.fixedWidth = width;
110
111 return config;
112 }
113
114} // namespace guillaume::entities
115
116namespace guillaume::entities
117{
119 ecs::EntityRegistry &entityRegistry)
120 : EntityBuilderBase<Surface>(componentRegistry, entityRegistry)
121 {
122 reset();
123 }
124
125 std::shared_ptr<Surface> Surface::Builder::buildEntity(void)
126 {
127 return Surface::create(this->getComponentRegistry(), _config,
128 _elevation, _mode);
129 }
130
132 {
133 _config = SurfaceConfig();
135 _mode = Mode::Elevated;
136 _config.borderRadius = getShapeRadius(theme::ShapeRole::None);
138 }
139
141 Surface::Builder::withPose(const utility::graphic::PoseF &pose)
142 {
143 _config.pose = pose;
144 return *this;
145 }
146
149 {
150 _elevation = elevation;
151 return *this;
152 }
153
155 {
156 _mode = mode;
157 return *this;
158 }
159
161 {
162 _config.borderRadius = getShapeRadius(role);
163 return *this;
164 }
165
167 {
168 const auto pose = _config.pose;
169 _config = config;
170 _config.pose = pose;
171 return *this;
172 }
173
174 std::shared_ptr<Surface>
175 Surface::Builder::makeSurface(std::shared_ptr<ecs::Entity> parent,
176 theme::ElevationLevel elevation,
177 Mode mode)
178 {
179 return withElevation(elevation).withMode(mode).registerEntity(parent);
180 }
181
182 Surface::Surface(ecs::ComponentRegistry &registry,
183 const SurfaceConfig &config,
184 theme::ElevationLevel elevation, Mode mode)
185 : SurfaceBase<components::Elevation, components::SurfaceTone>(registry,
186 config)
187 {
190 .setElevation(elevation)
191 .setShadow(mode == Mode::Elevated);
193 }
194
203
210
219
221 {
222 return getComponentRegistry()
224 .hasShadow()
226 : Mode::Flat;
227 }
228
229} // namespace guillaume::entities
CrossAxisAlignment
Where each child goes along the cross axis, inside the padding. In a container that wraps its childre...
Definition layout.hpp:99
MainAxisAlignment
Where the children go along the main axis when the container is larger than them.
Definition layout.hpp:86
Axis
Direction along which children are arranged.
Definition layout.hpp:74
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
State of a tonal surface: how high it sits above the background, and whether it casts a shadow.
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.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
SurfaceBase(ecs::ComponentRegistry &registry, const SurfaceConfig &config)
Configures and registers Surface widgets.
Definition surface.hpp:403
Builder & withMode(Mode mode)
Choose whether the surface casts a shadow.
Definition surface.cpp:154
std::shared_ptr< Surface > makeSurface(std::shared_ptr< ecs::Entity > parent, theme::ElevationLevel elevation, Mode mode=Mode::Elevated)
Build a surface with the given elevation and register it under a parent.
Definition surface.cpp:175
Builder & withShape(theme::ShapeRole role)
Set the corner radius from a shape role of the theme.
Definition surface.cpp:160
Builder & withElevation(theme::ElevationLevel elevation)
Set the elevation level, which tints the background and sizes the shadow.
Definition surface.cpp:148
std::shared_ptr< Surface > buildEntity(void) override
Create a surface from the current configuration, without attaching nor registering it.
Definition surface.cpp:125
Builder & withConfig(const SurfaceConfig &config)
Replace the layout configuration: axis, alignment, spacing, padding, fixed size and corner radius.
Definition surface.cpp:166
void reset(void) override
Restore the defaults: elevation level 1, a shadow (Mode::Elevated), square corners,...
Definition surface.cpp:131
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the surface is.
Definition surface.cpp:141
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder set to the defaults of reset().
Definition surface.cpp:118
Mode getMode(void) const
Tell whether the surface casts a shadow.
Definition surface.cpp:220
theme::ElevationLevel getElevation(void) const
Get the elevation level.
Definition surface.cpp:204
Surface & setMode(Mode mode)
Turn the shadow on or off, and restyle at once.
Definition surface.cpp:211
static std::shared_ptr< Surface > create(Arguments &&...arguments)
Create a surface without registering it.
Definition surface.hpp:516
Surface & setElevation(theme::ElevationLevel elevation)
Change the elevation level.
Definition surface.cpp:195
Mode
Whether the surface casts a shadow.
Definition surface.hpp:394
@ Elevated
Tinted background and the shadow of the level.
Definition surface.hpp:396
@ Flat
Tinted background, no shadow.
Definition surface.hpp:395
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Write the color and the shadow of one surface from its components::SurfaceTone.
State. Every class here is plain data attached to an entity.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
ElevationLevel
How far a surface is raised above the background, in six steps.
Definition tokens.hpp:130
@ Level1
Lowest raised level.
Definition tokens.hpp:132
ShapeRole
The corner radii a widget can ask for, by name.
Definition tokens.hpp:42
@ ExtraLarge
28 dp (dialogs, search bars, sheets).
Definition tokens.hpp:48
@ None
0 dp: square corners.
Definition tokens.hpp:43
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...
Definition surface.hpp:71
SurfaceConfig & setMainAxisAlignment(components::Layout::MainAxisAlignment value)
Set where the children go along the axis.
Definition surface.cpp:54
float padding
Gap between the edges and the children, in dp.
Definition surface.hpp:88
SurfaceConfig & setFixedHeight(float value)
Give the surface a height that does not depend on its children.
Definition surface.cpp:87
SurfaceConfig & setBorderRadius(float value)
Set the radius of the four corners.
Definition surface.cpp:42
utility::graphic::PoseF pose
Position and orientation of the surface.
Definition surface.hpp:73
SurfaceConfig & setAxis(components::Layout::Axis value)
Choose between a row and a column.
Definition surface.cpp:48
SurfaceConfig & setCrossAxisAlignment(components::Layout::CrossAxisAlignment value)
Set where the children go across the axis.
Definition surface.cpp:61
float spacing
Gap between two children, in dp.
Definition surface.hpp:87
SurfaceConfig & setPose(const utility::graphic::PoseF &value)
Set where the surface is.
Definition surface.cpp:29
bool hasFixedWidth
True when fixedWidth replaces the content width.
Definition surface.hpp:91
float fixedWidth
Width in dp, used only when hasFixedWidth.
Definition surface.hpp:94
SurfaceConfig & setPadding(float value)
Set the gap between the edges of the surface and its children.
Definition surface.cpp:74
components::Layout::MainAxisAlignment mainAxisAlignment
Where the children go along the axis.
Definition surface.hpp:81
utility::graphic::Color32Bit color
Fill color (transparent by default).
Definition surface.hpp:74
SurfaceConfig & setColor(const utility::graphic::Color32Bit &value)
Set the fill color.
Definition surface.cpp:36
components::Layout::Axis axis
Direction the children are arranged in.
Definition surface.hpp:78
static SurfaceConfig panel(float width, components::Layout::Axis axis, float spacing)
Get the configuration shared by dialog and picker panels.
Definition surface.cpp:94
bool hasFixedHeight
True when fixedHeight replaces the content height.
Definition surface.hpp:97
float fixedHeight
Height in dp, used only when hasFixedHeight.
Definition surface.hpp:100
components::Layout::CrossAxisAlignment crossAxisAlignment
Where the children go across the axis.
Definition surface.hpp:84
SurfaceConfig & setFixedWidth(float value)
Give the surface a width that does not depend on its children.
Definition surface.cpp:80
SurfaceConfig & setSpacing(float value)
Set the gap between two consecutive children.
Definition surface.cpp:68
float borderRadius
Radius of the four corners, in dp.
Definition surface.hpp:77