Skip to main content

gpui_material_theme/
shape.rs

1use crate::{Direction, generated::*};
2
3/// Logical rounded corners in top-start, top-end, bottom-end, bottom-start order.
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub enum CornerShape {
6    /// Explicit logical corner radii `[top_start, top_end, bottom_end, bottom_start]` in `dp`.
7    Radii([f32; 4]),
8    /// Pill / stadium rounding using half of the shorter side.
9    Full,
10}
11
12impl CornerShape {
13    /// Creates a rounded rectangle with the same logical radius on all four corners.
14    pub const fn uniform(radius: f32) -> Self {
15        Self::Radii([radius; 4])
16    }
17
18    /// Rounds the top-start and top-end logical corners.
19    pub const fn top(radius: f32) -> Self {
20        Self::Radii([radius, radius, 0.0, 0.0])
21    }
22
23    /// Rounds the bottom-start and bottom-end logical corners.
24    pub const fn bottom(radius: f32) -> Self {
25        Self::Radii([0.0, 0.0, radius, radius])
26    }
27
28    /// Rounds the top-start and bottom-start logical corners.
29    pub const fn start(radius: f32) -> Self {
30        Self::Radii([radius, 0.0, 0.0, radius])
31    }
32
33    /// Rounds the top-end and bottom-end logical corners.
34    pub const fn end(radius: f32) -> Self {
35        Self::Radii([0.0, radius, radius, 0.0])
36    }
37
38    /// Returns physical radii in top-left, top-right, bottom-right, bottom-left
39    /// order. Scales all corners together when adjacent arcs would overlap.
40    pub fn radii(self, width: f32, height: f32, direction: Direction) -> [f32; 4] {
41        let width = nonnegative(width);
42        let height = nonnegative(height);
43        let [tl, tr, br, bl] = match self {
44            Self::Full => return [width.min(height) * 0.5; 4],
45            Self::Radii(radii) => radii.map(nonnegative),
46        };
47        let mut factor = 1.0_f32;
48        for (edge, a, b) in [
49            (width, tl, tr),
50            (width, bl, br),
51            (height, tl, bl),
52            (height, tr, br),
53        ] {
54            // Use a wider sum so even two finite maximum radii can be scaled.
55            let sum = f64::from(a) + f64::from(b);
56            if sum > f64::from(edge) {
57                factor = factor.min((f64::from(edge) / sum) as f32);
58            }
59        }
60        let physical = match direction {
61            Direction::Ltr => [tl, tr, br, bl],
62            Direction::Rtl => [tr, tl, bl, br],
63        };
64        physical.map(|r| r * factor)
65    }
66}
67
68fn nonnegative(value: f32) -> f32 {
69    if value.is_finite() {
70        value.max(0.0)
71    } else {
72        0.0
73    }
74}
75
76/// Semantic Material 3 corner-radius scale tokens.
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum CornerRole {
79    /// Sharp corners (`0 dp`).
80    None,
81    /// Extra-small uniform rounding (`4 dp`).
82    ExtraSmall,
83    /// Extra-small rounding on the top corners only (`4 dp` top, `0 dp` bottom).
84    ExtraSmallTop,
85    /// Small uniform rounding (`8 dp`).
86    Small,
87    /// Medium uniform rounding (`12 dp`).
88    Medium,
89    /// Large uniform rounding (`16 dp`).
90    Large,
91    /// Large rounding on the logical start corners only.
92    LargeStart,
93    /// Large rounding on the logical end corners only.
94    LargeEnd,
95    /// Large rounding on the top corners only.
96    LargeTop,
97    /// Increased large uniform rounding (`20 dp`).
98    LargeIncreased,
99    /// Extra-large uniform rounding (`28 dp`).
100    ExtraLarge,
101    /// Extra-large rounding on the top corners only.
102    ExtraLargeTop,
103    /// Increased extra-large uniform rounding (`32 dp`).
104    ExtraLargeIncreased,
105    /// Extra-extra-large uniform rounding (`48 dp`).
106    ExtraExtraLarge,
107    /// Full pill/circle rounding.
108    Full,
109}
110
111impl CornerRole {
112    /// Resolves this corner role to its Material [`CornerShape`] token value.
113    pub const fn shape(self) -> CornerShape {
114        match self {
115            Self::None => MD_SYS_SHAPE_CORNER_NONE,
116            Self::ExtraSmall => MD_SYS_SHAPE_CORNER_EXTRA_SMALL,
117            Self::ExtraSmallTop => MD_SYS_SHAPE_CORNER_EXTRA_SMALL_TOP,
118            Self::Small => MD_SYS_SHAPE_CORNER_SMALL,
119            Self::Medium => MD_SYS_SHAPE_CORNER_MEDIUM,
120            Self::Large => MD_SYS_SHAPE_CORNER_LARGE,
121            Self::LargeStart => MD_SYS_SHAPE_CORNER_LARGE_START,
122            Self::LargeEnd => MD_SYS_SHAPE_CORNER_LARGE_END,
123            Self::LargeTop => MD_SYS_SHAPE_CORNER_LARGE_TOP,
124            Self::LargeIncreased => MD_SYS_SHAPE_CORNER_LARGE_INCREASED,
125            Self::ExtraLarge => MD_SYS_SHAPE_CORNER_EXTRA_LARGE,
126            Self::ExtraLargeTop => MD_SYS_SHAPE_CORNER_EXTRA_LARGE_TOP,
127            Self::ExtraLargeIncreased => MD_SYS_SHAPE_CORNER_EXTRA_LARGE_INCREASED,
128            Self::ExtraExtraLarge => MD_SYS_SHAPE_CORNER_EXTRA_EXTRA_LARGE,
129            Self::Full => MD_SYS_SHAPE_CORNER_FULL,
130        }
131    }
132}
133
134/// Inputs for the finite rounded polygons used as morph endpoints. These
135/// are source coordinates before path generation and normalization.
136#[derive(Clone, Copy, Debug, PartialEq)]
137pub enum PolygonSpec {
138    /// The final circle radius, as in AndroidX `RoundedPolygon.circle`.
139    Circle {
140        /// Number of polygon vertices approximating the circle (`>= 3`).
141        vertices: u8,
142        /// Target circle radius in source units.
143        radius: f32,
144    },
145    /// A rounded rectangle in source coordinates.
146    Rectangle {
147        /// Rectangle width in source units.
148        width: f32,
149        /// Rectangle height in source units.
150        height: f32,
151        /// Corner rounding radius in source units.
152        rounding_radius: f32,
153        /// Corner smoothing factor in `0.0..=1.0`.
154        smoothing: f32,
155    },
156    /// The circumradius of the unrounded regular polygon.
157    Regular {
158        /// Number of polygon vertices (`>= 3`).
159        vertices: u8,
160        /// Circumradius of the unrounded polygon.
161        radius: f32,
162        /// Corner rounding radius in source units.
163        rounding_radius: f32,
164        /// Corner smoothing factor in `0.0..=1.0`.
165        smoothing: f32,
166        /// Rotation angle in degrees.
167        rotation_degrees: f32,
168    },
169}
170
171/// Basic semantic polygon tokens bundled in `tokens/material.json`.
172#[derive(Clone, Copy, Debug, PartialEq, Eq)]
173pub enum PolygonRole {
174    /// Circular polygon token.
175    Circle,
176    /// Rounded square polygon token.
177    Square,
178    /// Rounded triangle polygon token.
179    Triangle,
180}
181
182impl PolygonRole {
183    /// Resolves this polygon role to its source [`PolygonSpec`].
184    pub const fn spec(self) -> PolygonSpec {
185        match self {
186            Self::Circle => MD_SYS_SHAPE_POLYGON_CIRCLE,
187            Self::Square => MD_SYS_SHAPE_POLYGON_SQUARE,
188            Self::Triangle => MD_SYS_SHAPE_POLYGON_TRIANGLE,
189        }
190    }
191}
192
193/// Named Material 3 Expressive shapes matching pinned Compose `MaterialShapes`.
194#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
195#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
196pub enum MaterialShape {
197    /// Circle shape (`circle`).
198    Circle,
199    /// Rounded square shape (`square`).
200    Square,
201    /// Slanted rounded square shape (`slanted`).
202    Slanted,
203    /// Arch shape (`arch`).
204    Arch,
205    /// Quarter-circle fan shape (`fan`).
206    Fan,
207    /// Rounded arrow / chevron shape (`arrow`).
208    Arrow,
209    /// Semi-circle shape (`semi-circle`).
210    SemiCircle,
211    /// Oval / ellipse shape (`oval`).
212    Oval,
213    /// Stadium / pill shape (`pill`).
214    Pill,
215    /// Rounded triangle shape (`triangle`).
216    Triangle,
217    /// Rounded diamond shape (`diamond`).
218    Diamond,
219    /// Clam-shell shape (`clam-shell`).
220    ClamShell,
221    /// Rounded pentagon shape (`pentagon`).
222    Pentagon,
223    /// Six-sided gem shape (`gem`).
224    Gem,
225    /// Eight-pointed sunny shape (`sunny`).
226    Sunny,
227    /// Twelve-pointed very-sunny shape (`very-sunny`).
228    VerySunny,
229    /// Four-sided scalloped cookie shape (`cookie-4-sided`).
230    Cookie4Sided,
231    /// Six-sided scalloped cookie shape (`cookie-6-sided`).
232    Cookie6Sided,
233    /// Seven-sided scalloped cookie shape (`cookie-7-sided`).
234    Cookie7Sided,
235    /// Nine-sided scalloped cookie shape (`cookie-9-sided`).
236    Cookie9Sided,
237    /// Twelve-sided scalloped cookie shape (`cookie-12-sided`).
238    Cookie12Sided,
239    /// Ghost-like organic shape (`ghostish`).
240    Ghostish,
241    /// Four-leaf clover shape (`clover-4-leaf`).
242    Clover4Leaf,
243    /// Eight-leaf clover shape (`clover-8-leaf`).
244    Clover8Leaf,
245    /// Sharp starburst shape (`burst`).
246    Burst,
247    /// Softened starburst shape (`soft-burst`).
248    SoftBurst,
249    /// Comic-style boom shape (`boom`).
250    Boom,
251    /// Softened boom shape (`soft-boom`).
252    SoftBoom,
253    /// Scalloped flower shape (`flower`).
254    Flower,
255    /// Puffy cloud-like shape (`puffy`).
256    Puffy,
257    /// Puffy diamond shape (`puffy-diamond`).
258    PuffyDiamond,
259    /// Stepped pixel-art circle shape (`pixel-circle`).
260    PixelCircle,
261    /// Stepped pixel-art triangle shape (`pixel-triangle`).
262    PixelTriangle,
263    /// Stacked bun shape (`bun`).
264    Bun,
265    /// Rounded heart shape (`heart`).
266    Heart,
267}
268
269impl MaterialShape {
270    /// All 35 Material 3 Expressive shapes in Compose `MaterialShapes` order.
271    pub const ALL: [Self; 35] = [
272        Self::Circle,
273        Self::Square,
274        Self::Slanted,
275        Self::Arch,
276        Self::Fan,
277        Self::Arrow,
278        Self::SemiCircle,
279        Self::Oval,
280        Self::Pill,
281        Self::Triangle,
282        Self::Diamond,
283        Self::ClamShell,
284        Self::Pentagon,
285        Self::Gem,
286        Self::Sunny,
287        Self::VerySunny,
288        Self::Cookie4Sided,
289        Self::Cookie6Sided,
290        Self::Cookie7Sided,
291        Self::Cookie9Sided,
292        Self::Cookie12Sided,
293        Self::Ghostish,
294        Self::Clover4Leaf,
295        Self::Clover8Leaf,
296        Self::Burst,
297        Self::SoftBurst,
298        Self::Boom,
299        Self::SoftBoom,
300        Self::Flower,
301        Self::Puffy,
302        Self::PuffyDiamond,
303        Self::PixelCircle,
304        Self::PixelTriangle,
305        Self::Bun,
306        Self::Heart,
307    ];
308
309    /// Stable lowercase identifier for this shape.
310    pub const fn name(self) -> &'static str {
311        match self {
312            Self::Circle => "circle",
313            Self::Square => "square",
314            Self::Slanted => "slanted",
315            Self::Arch => "arch",
316            Self::Fan => "fan",
317            Self::Arrow => "arrow",
318            Self::SemiCircle => "semi-circle",
319            Self::Oval => "oval",
320            Self::Pill => "pill",
321            Self::Triangle => "triangle",
322            Self::Diamond => "diamond",
323            Self::ClamShell => "clam-shell",
324            Self::Pentagon => "pentagon",
325            Self::Gem => "gem",
326            Self::Sunny => "sunny",
327            Self::VerySunny => "very-sunny",
328            Self::Cookie4Sided => "cookie-4-sided",
329            Self::Cookie6Sided => "cookie-6-sided",
330            Self::Cookie7Sided => "cookie-7-sided",
331            Self::Cookie9Sided => "cookie-9-sided",
332            Self::Cookie12Sided => "cookie-12-sided",
333            Self::Ghostish => "ghostish",
334            Self::Clover4Leaf => "clover-4-leaf",
335            Self::Clover8Leaf => "clover-8-leaf",
336            Self::Burst => "burst",
337            Self::SoftBurst => "soft-burst",
338            Self::Boom => "boom",
339            Self::SoftBoom => "soft-boom",
340            Self::Flower => "flower",
341            Self::Puffy => "puffy",
342            Self::PuffyDiamond => "puffy-diamond",
343            Self::PixelCircle => "pixel-circle",
344            Self::PixelTriangle => "pixel-triangle",
345            Self::Bun => "bun",
346            Self::Heart => "heart",
347        }
348    }
349}
350
351impl From<PolygonRole> for MaterialShape {
352    fn from(role: PolygonRole) -> Self {
353        match role {
354            PolygonRole::Circle => Self::Circle,
355            PolygonRole::Square => Self::Square,
356            PolygonRole::Triangle => Self::Triangle,
357        }
358    }
359}