guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
button.hpp
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#pragma once
24
25#include <utility>
26#include <functional>
27#include <memory>
28#include <string>
29
30#include "guillaume/ecs/component_registry.hpp"
31#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/ecs/entity_builder.hpp"
33
34#include "guillaume/components/common_button.hpp"
35#include "guillaume/entities/button_base.hpp"
36
37namespace guillaume::entities
38{
39
75 class Button: public ButtonBase
76 {
77 public:
81
85
89
96 class Builder: public EntityBuilderBase<Button>
97 {
98 private:
99 std::string
100 _iconGlyphName;
101 components::Glyph::Style _iconStyle;
102 std::string _labelContent;
103 std::function<void(void)>
104 _onClick;
105 bool _isToggle;
106 Color _colorStyle;
107 Shape _shape;
108 Size _size;
109 bool _isMorph;
110 bool _isDisabled;
111 bool _isTrailingIcon;
113 std::string
114 _accessibilityLabel;
115
116 public:
125 Builder(ecs::ComponentRegistry &componentRegistry,
126 ecs::EntityRegistry &entityRegistry);
127
136 std::shared_ptr<Button> buildEntity(void) override;
137
146 void reset(void) override;
147
154 Builder &withIcon(const std::string &iconGlyphName);
155
163
170 Builder &withLabel(const std::string &labelContent);
171
178 Builder &withOnClick(std::function<void(void)> onClick);
179
186 Builder &withToggle(const bool &isToggle);
187
193 Builder &withColorStyle(const Color &colorStyle);
194
200 Builder &withShape(const Shape &shape);
201
207 Builder &withSize(const Size &size);
208
216 Builder &withMorph(const bool &isMorph);
217
224 Builder &withDisabled(const bool &isDisabled);
225
232 Builder &withTrailingIcon(const bool &isTrailingIcon);
233
240 Builder &
241 withAccessibilityLabel(const std::string &accessibilityLabel);
242
260 std::shared_ptr<Button>
261 makeButton(std::shared_ptr<Entity> parent,
262 const std::string &labelContent,
263 std::function<void(void)> onClick, Color colorStyle,
264 Shape shape, Size size, bool isMorph);
265
286 std::shared_ptr<Button> makeIconButton(
287 std::shared_ptr<Entity> parent, const std::string &labelContent,
288 const std::string &iconGlyphName,
289 const components::Glyph::Style &iconStyle,
290 std::function<void(void)> onClick, Color colorStyle,
291 Shape shape, Size size, bool isMorph);
292 };
293
294 protected:
299 float getIconFontSize(void) const override;
300
306 bool usesLabel(void) const override
307 {
308 return true;
309 }
310
315 utility::graphic::Color32Bit
316 getInitialContentColor(void) const override;
317
318 public:
333 template<typename... Arguments>
334 static std::shared_ptr<Button> create(Arguments &&...arguments)
335 {
336 std::shared_ptr<Button> entity(
337 new Button(std::forward<Arguments>(arguments)...));
338 entity->build();
339 return entity;
340 }
341
345 ~Button(void) override = default;
346
354 Button &setCompact(bool compact);
355
367 Button &setMinWidth(float minWidth);
368
374 bool isCompact(void) const;
375
385 Button &setUppercase(bool uppercase);
386
391 bool isUppercase(void) const;
392
397
403
409 Button &setColorStyle(const Color &colorStyle);
410
416 Button &setShape(const Shape &shape);
417
424 Button &setSize(const Size &size);
425
430 Color getColorStyle(void) const;
431
437 Shape getShape(void) const;
438
443 Size getSize(void) const;
444
445 private:
467 Button(ecs::ComponentRegistry &registry,
468 const std::string &iconGlyphName,
469 const components::Glyph::Style &iconStyle,
470 const std::string &labelContent, bool isToggle, Color colorStyle,
471 Shape shape, Size size, bool isMorph, bool isDisabled,
472 bool isTrailingIcon, const std::string &accessibilityLabel,
473 std::function<void(void)> onClick);
474 };
475
476} // namespace guillaume::entities
Color
Which theme colors fill the container, the content and the outline.
static constexpr float MinWidth
Minimum width of the container, in dp.
Size
Size step. It sets the padding, the icon size, the gap between icon and label and the corner radius; ...
Shape
Corner shape at rest: fully rounded (Round) or the rounded rectangle of the size (Square).
Style
Family of the Material Symbols font the icon belongs to.
Definition glyph.hpp:66
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Owns entities and gives the systems the list of entities to visit.
bool isDisabled(void) const
Tell whether the button is disabled.
ButtonBase(ecs::ComponentRegistry &registry, const std::string &iconGlyphName, const components::Glyph::Style &iconStyle, const std::string &labelContent, bool isToggle, bool isMorph, bool isDisabled, bool isTrailingIcon, const std::string &accessibilityLabel, std::function< void(void)> onClick)
Create the entity with its components and store the initial state. The children are created later,...
Builder & withToggle(const bool &isToggle)
Make the button a toggle: each click flips its selected state.
Definition button.cpp:95
Builder & withColorStyle(const Color &colorStyle)
Set the color style.
Definition button.cpp:101
Builder & withDisabled(const bool &isDisabled)
Choose whether the button starts disabled.
Definition button.cpp:125
std::shared_ptr< Button > buildEntity(void) override
Build a Button, with its icon and label children, from the current configuration, without registering...
Definition button.cpp:44
Builder & withLabel(const std::string &labelContent)
Set the label text.
Definition button.cpp:82
Builder & withTrailingIcon(const bool &isTrailingIcon)
Choose the side of the icon.
Definition button.cpp:132
Builder & withIcon(const std::string &iconGlyphName)
Set the icon shown next to the label.
Definition button.cpp:69
Builder & withOnClick(std::function< void(void)> onClick)
Set the click handler.
Definition button.cpp:89
std::shared_ptr< Button > makeIconButton(std::shared_ptr< Entity > parent, const std::string &labelContent, const std::string &iconGlyphName, const components::Glyph::Style &iconStyle, std::function< void(void)> onClick, Color colorStyle, Shape shape, Size size, bool isMorph)
Build a button with an icon and a label, register it under parent and reset the builder.
Definition button.cpp:159
Builder & withSize(const Size &size)
Set the size.
Definition button.cpp:113
void reset(void) override
Restore the default configuration.
Definition button.cpp:53
Builder & withShape(const Shape &shape)
Set the corner shape.
Definition button.cpp:107
Builder & withIconStyle(const components::Glyph::Style &iconStyle)
Set the glyph style of the icon.
Definition button.cpp:75
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder with the default configuration (see reset()).
Definition button.cpp:37
Builder & withAccessibilityLabel(const std::string &accessibilityLabel)
Set the label read by assistive technology.
Definition button.cpp:138
std::shared_ptr< Button > makeButton(std::shared_ptr< Entity > parent, const std::string &labelContent, std::function< void(void)> onClick, Color colorStyle, Shape shape, Size size, bool isMorph)
Build a button with a label and no icon, register it under parent and reset the builder.
Definition button.cpp:145
Builder & withMorph(const bool &isMorph)
Choose whether a selected toggle button swaps its shape (Round becomes Square and Square becomes Roun...
Definition button.cpp:119
components::CommonButton::Color Color
Definition button.hpp:80
Button & setShape(const Shape &shape)
Set the corner shape. Restyles at once.
Definition button.cpp:247
Button & setMinWidth(float minWidth)
Raise the minimum container width above MinWidth. Restyles at once.
Definition button.cpp:211
Shape getShape(void) const
Get the corner shape.
Definition button.cpp:286
Button & setUppercase(bool uppercase)
Render the label in upper case (Paper uppercase). Restyles at once.
Definition button.cpp:229
bool isCompact(void) const
Check whether the button uses the compact horizontal padding.
Definition button.cpp:265
Button & setCompact(bool compact)
Use the compact horizontal padding (Paper compact). Restyles at once.
Definition button.cpp:220
bool usesLabel(void) const override
Tell ButtonBase::build() that this variant has a label child.
Definition button.hpp:306
Button & setSize(const Size &size)
Set the size, which decides the paddings, the icon size and the corner radius. Restyles at once.
Definition button.cpp:256
static std::shared_ptr< Button > create(Arguments &&...arguments)
Create a button and its icon and label children, without registering it in an entity registry.
Definition button.hpp:334
bool isUppercase(void) const
Check whether the label is displayed in upper case.
Definition button.cpp:272
Size getSize(void) const
Get the size.
Definition button.cpp:293
Button & setColorStyle(const Color &colorStyle)
Set the color style. Restyles at once.
Definition button.cpp:238
~Button(void) override=default
Default destructor.
components::CommonButton::Size Size
Definition button.hpp:88
components::CommonButton::Shape Shape
Definition button.hpp:84
static constexpr float MinHeight
Minimum container height, in dp (M3: 40 dp). It does not apply to Size::ExtraSmall.
Definition button.hpp:402
float getIconFontSize(void) const override
Get the font size of the icon child for the current size.
Definition button.cpp:199
Color getColorStyle(void) const
Get the color style.
Definition button.cpp:279
utility::graphic::Color32Bit getInitialContentColor(void) const override
Get the color the icon and label children are created with.
Definition button.cpp:205
static constexpr float MinWidth
Minimum container width, in dp (M3: 64 dp).
Definition button.hpp:396
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46