guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
button_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
25
26#include <array>
27#include <cstddef>
28#include <optional>
29#include <stdexcept>
30
31#include "guillaume/theme.hpp"
32
34{
35 namespace
36 {
37 using Role = SchemeColorRole;
38
40 struct SizeMetrics {
41 float heightPadding;
42 float widthPadding;
43 float iconFontSize;
44 float iconLabelGap;
45 theme::ShapeRole pressedShape;
46 theme::ShapeRole squareShape;
47 };
48
50 constexpr std::array<SizeMetrics, 5> sizeTable { {
51 // ExtraSmall
52 { 6.0f, 12.0f, 15.0f, 4.0f, theme::ShapeRole::Small,
54 // Small
55 { 8.0f, 16.0f, 18.0f, 8.0f, theme::ShapeRole::Small,
57 // Medium
58 { 12.0f, 24.0f, 18.0f, 8.0f, theme::ShapeRole::Medium,
60 // Large
61 { 24.0f, 48.0f, 24.0f, 12.0f, theme::ShapeRole::Large,
63 // ExtraLarge
64 { 32.0f, 64.0f, 30.0f, 16.0f, theme::ShapeRole::Large,
66 } };
67
69 struct ModePalette {
70 std::optional<Role> container;
71 Role content;
72 };
73
75 struct Palette {
76 ModePalette plain;
77 ModePalette unselected;
78 ModePalette selected;
79 };
80
82 constexpr std::array<Palette, 6> paletteTable { {
83 // Elevated
84 { { Role::SurfaceContainerLow, Role::Primary },
85 { Role::SurfaceContainerLow, Role::Primary },
86 { Role::Primary, Role::OnPrimary } },
87 // Filled
88 { { Role::Primary, Role::OnPrimary },
89 { Role::SurfaceContainer, Role::OnSurfaceVariant },
90 { Role::Primary, Role::OnPrimary } },
91 // Tonal
92 { { Role::SecondaryContainer, Role::OnSecondaryContainer },
93 { Role::SecondaryContainer, Role::OnSecondaryContainer },
94 { Role::Secondary, Role::OnSecondary } },
95 // Outlined
96 { { std::nullopt, Role::Primary },
97 { std::nullopt, Role::Primary },
98 { Role::SecondaryContainer, Role::OnSecondaryContainer } },
99 // Text
100 { { std::nullopt, Role::Primary },
101 { std::nullopt, Role::Primary },
102 { std::nullopt, Role::Primary } },
103 // Segmented
104 { { std::nullopt, Role::OnSurfaceVariant },
105 { std::nullopt, Role::OnSurfaceVariant },
106 { Role::SecondaryContainer, Role::OnSecondaryContainer } },
107 } };
108
109 const SizeMetrics &metrics(components::CommonButton::Size size)
110 {
111 const auto index = static_cast<std::size_t>(size);
112 if (index >= sizeTable.size()) {
113 throw std::runtime_error("Invalid button size");
114 }
115 return sizeTable[index];
116 }
117
118 const ModePalette &palette(components::CommonButton::Color style,
119 bool isToggle, bool isSelected)
120 {
121 const auto index = static_cast<std::size_t>(style);
122 if (index >= paletteTable.size()) {
123 throw std::runtime_error("Invalid button color style");
124 }
125 const Palette &entry = paletteTable[index];
126 if (!isToggle) {
127 return entry.plain;
128 }
129 return isSelected ? entry.selected : entry.unselected;
130 }
131
132 utility::graphic::Color32Bit transparent(void)
133 {
134 return utility::graphic::Color32Bit(0, 0, 0, 0);
135 }
136
137 utility::graphic::Color32Bit
138 getBaseContainerColor(components::CommonButton::Color style,
139 bool isToggle, bool isSelected)
140 {
141 const auto role = palette(style, isToggle, isSelected).container;
142 return role ? getActiveColor(*role) : transparent();
143 }
144 } // namespace
145
147 {
148 return metrics(size).heightPadding;
149 }
150
152 {
153 // Paper compact buttons halve the horizontal label margin.
154 const float padding = metrics(size).widthPadding;
155 return compact ? padding / 2.0f : padding;
156 }
157
159 {
160 return metrics(size).iconFontSize;
161 }
162
164 {
165 return metrics(size).iconLabelGap;
166 }
167
170 bool isButtonPressed)
171 {
172 const SizeMetrics &entry = metrics(size);
173
174 if (isButtonPressed) {
175 return getShapeRadius(entry.pressedShape);
176 }
177 if (shape == components::CommonButton::Shape::Round) {
179 }
180 return getShapeRadius(entry.squareShape);
181 }
182
185 bool isToggle, bool isSelected, bool isMorph)
186 {
187 if (!isMorph || !isToggle || !isSelected) {
188 return baseShape;
189 }
190
191 return (baseShape == components::CommonButton::Shape::Round)
192 ? components::CommonButton::Shape::Square
193 : components::CommonButton::Shape::Round;
194 }
195
196 utility::graphic::Color32Bit
198 bool isSelected, bool isHovered, bool isButtonPressed)
199 {
200 const auto base = getBaseContainerColor(style, isToggle, isSelected);
201
202 if (!isButtonPressed && !isHovered) {
203 return base;
204 }
205 return ::guillaume::systems::style::stateLayer(
206 base, getContentColor(style, isToggle, isSelected),
209 }
210
211 utility::graphic::Color32Bit
213 bool isSelected)
214 {
215 return getActiveColor(palette(style, isToggle, isSelected).content);
216 }
217
218 utility::graphic::Color32Bit
220 bool isSelected)
221 {
222 // Segmented buttons keep their outline when selected (Paper
223 // SegmentedButtons); outlined toggles drop it.
224 const bool outlined =
225 style == components::CommonButton::Color::Segmented
226 || (style == components::CommonButton::Color::Outlined
227 && !(isToggle && isSelected));
228
229 return outlined ? getActiveColor(Role::Outline) : transparent();
230 }
231} // namespace guillaume::systems::button_style
Color
Which theme colors fill the container, the content and the outline.
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).
Maps a common button size, shape and color style to Material 3 metrics and scheme colors.
float getBorderRadius(components::CommonButton::Size size, components::CommonButton::Shape shape, bool isButtonPressed)
Corner radius of the container.
utility::graphic::Color32Bit getBorderColor(components::CommonButton::Color style, bool isToggle, bool isSelected)
Color of the outline of an enabled button.
float getWidthPadding(components::CommonButton::Size size, bool compact=false)
Space between the content and the left edge of the container (and, equally, the right edge).
float getHeightPadding(components::CommonButton::Size size)
Space between the content and the top edge of the container (and, equally, the bottom edge).
float getSpaceBetweenIconAndLabel(components::CommonButton::Size size)
Horizontal gap between the icon and the label.
utility::graphic::Color32Bit getContainerColor(components::CommonButton::Color style, bool isToggle, bool isSelected, bool isHovered, bool isButtonPressed)
Fill color of the container, hover or press tint included.
utility::graphic::Color32Bit getContentColor(components::CommonButton::Color style, bool isToggle, bool isSelected)
Color of the icon and of the label of an enabled button.
components::CommonButton::Shape getRestingShape(components::CommonButton::Shape baseShape, bool isToggle, bool isSelected, bool isMorph)
Shape a button shows while it is not pressed.
float getIconFontSize(components::CommonButton::Size size)
Font size of the leading icon of a button.
Color helpers shared by the widget systems.
@ Hovered
0.08 overlay of the content color.
Definition tokens.hpp:224
@ Pressed
0.12 overlay of the content color.
Definition tokens.hpp:226
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
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
std::uint8_t getStateLayerAlpha(theme::StateLayer layer)
Get the opacity the active theme gives to a state layer, as an alpha channel value.
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 common button: paddings, icon size, corner radius and colors for each size,...
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.