guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
fab_style.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
24
25#include <array>
26#include <optional>
27#include <stdexcept>
28
29#include "guillaume/theme.hpp"
30
32{
33 namespace
34 {
35 using Role = SchemeColorRole;
36
38 struct SizeMetrics {
39 float containerSize;
40 float iconFontSize;
41 float labelFontSize;
42 theme::ShapeRole shape;
43 };
44
46 constexpr std::array<SizeMetrics, 3> sizeTable { {
47 { 40.0f, 24.0f, 18.0f, theme::ShapeRole::Medium },
48 { 56.0f, 24.0f, 24.0f, theme::ShapeRole::Large },
49 { 96.0f, 36.0f, 36.0f, theme::ShapeRole::ExtraLarge },
50 } };
51
54 struct Palette {
55 std::optional<Role> container;
56 Role content;
57 };
58
60 constexpr std::array<Palette, 4> paletteTable { {
61 { std::nullopt, Role::Primary },
62 { Role::PrimaryContainer, Role::OnPrimaryContainer },
63 { Role::SecondaryContainer, Role::OnSecondaryContainer },
64 { Role::TertiaryContainer, Role::OnTertiaryContainer },
65 } };
66
67 const SizeMetrics &metrics(std::size_t size)
68 {
69 if (size >= sizeTable.size()) {
70 throw std::runtime_error("Invalid FAB size");
71 }
72 return sizeTable[size];
73 }
74
75 const Palette &palette(std::size_t variant)
76 {
77 if (variant >= paletteTable.size()) {
78 throw std::runtime_error("Invalid FAB variant");
79 }
80 return paletteTable[variant];
81 }
82 } // namespace
83
84 float containerSize(std::size_t size)
85 {
86 return metrics(size).containerSize;
87 }
88
89 float iconFontSize(std::size_t size)
90 {
91 return metrics(size).iconFontSize;
92 }
93
94 float labelFontSize(std::size_t size)
95 {
96 return metrics(size).labelFontSize;
97 }
98
99 float borderRadius(std::size_t size)
100 {
101 return getShapeRadius(metrics(size).shape);
102 }
103
104 utility::graphic::Color32Bit containerColor(std::size_t variant,
105 bool lowered)
106 {
107 const auto role = palette(variant).container;
108 if (role) {
109 return getActiveColor(*role);
110 }
111 // Paper's surface FAB uses the tonal elevation.level3 color (level1
112 // when lowered).
115 }
116
117 utility::graphic::Color32Bit contentColor(std::size_t variant)
118 {
119 return getActiveColor(palette(variant).content);
120 }
121} // namespace guillaume::systems::fab_style::detail
The table lookups behind the templates, taking the enumerator as an index. Call the templated functio...
Definition fab_style.hpp:70
float borderRadius(std::size_t size)
Corner radius of the container.
Definition fab_style.cpp:99
float labelFontSize(std::size_t size)
Font size of the label and icon of an extended FAB.
Definition fab_style.cpp:94
utility::graphic::Color32Bit contentColor(std::size_t variant)
Color of the icon and of the label.
utility::graphic::Color32Bit containerColor(std::size_t variant, bool lowered)
Fill color of the container.
float iconFontSize(std::size_t size)
Font size of the icon of a regular FAB.
Definition fab_style.cpp:89
float containerSize(std::size_t size)
Width and height of the container at its default size.
Definition fab_style.cpp:84
@ Level1
Lowest raised level.
Definition tokens.hpp:132
@ Level3
Third raised level.
Definition tokens.hpp:134
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
@ Large
16 dp (FABs, drawers).
Definition tokens.hpp:47
@ Medium
12 dp (cards).
Definition tokens.hpp:46
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
SchemeColorRole
The named colors of a scheme: what a color is used for, not what it looks like.
Definition theme.hpp:58
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
utility::graphic::Color32Bit getActiveColor(SchemeColorRole role)
Get the color the active scheme gives to a role.
Style tables of the floating action buttons (FAB): container size, font sizes, corner radius and colo...