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    Radii([f32; 4]),
7    Full,
8}
9
10impl CornerShape {
11    /// Returns physical radii in top-left, top-right, bottom-right, bottom-left
12    /// order. Scales all corners together when adjacent arcs would overlap.
13    pub fn radii(self, width: f32, height: f32, direction: Direction) -> [f32; 4] {
14        let width = nonnegative(width);
15        let height = nonnegative(height);
16        let [tl, tr, br, bl] = match self {
17            Self::Full => return [width.min(height) * 0.5; 4],
18            Self::Radii(radii) => radii.map(nonnegative),
19        };
20        let mut factor = 1.0_f32;
21        for (edge, a, b) in [
22            (width, tl, tr),
23            (width, bl, br),
24            (height, tl, bl),
25            (height, tr, br),
26        ] {
27            // Use a wider sum so even two finite maximum radii can be scaled.
28            let sum = f64::from(a) + f64::from(b);
29            if sum > f64::from(edge) {
30                factor = factor.min((f64::from(edge) / sum) as f32);
31            }
32        }
33        let physical = match direction {
34            Direction::Ltr => [tl, tr, br, bl],
35            Direction::Rtl => [tr, tl, bl, br],
36        };
37        physical.map(|r| r * factor)
38    }
39}
40
41fn nonnegative(value: f32) -> f32 {
42    if value.is_finite() {
43        value.max(0.0)
44    } else {
45        0.0
46    }
47}
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum CornerRole {
51    None,
52    ExtraSmall,
53    ExtraSmallTop,
54    Small,
55    Medium,
56    Large,
57    LargeStart,
58    LargeEnd,
59    LargeTop,
60    LargeIncreased,
61    ExtraLarge,
62    ExtraLargeTop,
63    ExtraLargeIncreased,
64    ExtraExtraLarge,
65    Full,
66}
67
68impl CornerRole {
69    pub const fn shape(self) -> CornerShape {
70        match self {
71            Self::None => MD_SYS_SHAPE_CORNER_NONE,
72            Self::ExtraSmall => MD_SYS_SHAPE_CORNER_EXTRA_SMALL,
73            Self::ExtraSmallTop => MD_SYS_SHAPE_CORNER_EXTRA_SMALL_TOP,
74            Self::Small => MD_SYS_SHAPE_CORNER_SMALL,
75            Self::Medium => MD_SYS_SHAPE_CORNER_MEDIUM,
76            Self::Large => MD_SYS_SHAPE_CORNER_LARGE,
77            Self::LargeStart => MD_SYS_SHAPE_CORNER_LARGE_START,
78            Self::LargeEnd => MD_SYS_SHAPE_CORNER_LARGE_END,
79            Self::LargeTop => MD_SYS_SHAPE_CORNER_LARGE_TOP,
80            Self::LargeIncreased => MD_SYS_SHAPE_CORNER_LARGE_INCREASED,
81            Self::ExtraLarge => MD_SYS_SHAPE_CORNER_EXTRA_LARGE,
82            Self::ExtraLargeTop => MD_SYS_SHAPE_CORNER_EXTRA_LARGE_TOP,
83            Self::ExtraLargeIncreased => MD_SYS_SHAPE_CORNER_EXTRA_LARGE_INCREASED,
84            Self::ExtraExtraLarge => MD_SYS_SHAPE_CORNER_EXTRA_EXTRA_LARGE,
85            Self::Full => MD_SYS_SHAPE_CORNER_FULL,
86        }
87    }
88}
89
90/// Inputs for the finite rounded polygons used as morph endpoints. These
91/// are source coordinates before path generation and normalization.
92#[derive(Clone, Copy, Debug, PartialEq)]
93pub enum PolygonSpec {
94    /// The final circle radius, as in AndroidX RoundedPolygon.circle.
95    Circle { vertices: u8, radius: f32 },
96    Rectangle {
97        width: f32,
98        height: f32,
99        rounding_radius: f32,
100        smoothing: f32,
101    },
102    /// The circumradius of the unrounded regular polygon.
103    Regular {
104        vertices: u8,
105        radius: f32,
106        rounding_radius: f32,
107        smoothing: f32,
108        rotation_degrees: f32,
109    },
110}
111
112#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub enum PolygonRole {
114    Circle,
115    Square,
116    Triangle,
117}
118
119impl PolygonRole {
120    pub const fn spec(self) -> PolygonSpec {
121        match self {
122            Self::Circle => MD_SYS_SHAPE_POLYGON_CIRCLE,
123            Self::Square => MD_SYS_SHAPE_POLYGON_SQUARE,
124            Self::Triangle => MD_SYS_SHAPE_POLYGON_TRIANGLE,
125        }
126    }
127}
128
129/// Named Material 3 Expressive shapes matching pinned Compose `MaterialShapes`.
130#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
131#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
132pub enum MaterialShape {
133    Circle,
134    Square,
135    Slanted,
136    Arch,
137    Fan,
138    Arrow,
139    SemiCircle,
140    Oval,
141    Pill,
142    Triangle,
143    Diamond,
144    ClamShell,
145    Pentagon,
146    Gem,
147    Sunny,
148    VerySunny,
149    Cookie4Sided,
150    Cookie6Sided,
151    Cookie7Sided,
152    Cookie9Sided,
153    Cookie12Sided,
154    Ghostish,
155    Clover4Leaf,
156    Clover8Leaf,
157    Burst,
158    SoftBurst,
159    Boom,
160    SoftBoom,
161    Flower,
162    Puffy,
163    PuffyDiamond,
164    PixelCircle,
165    PixelTriangle,
166    Bun,
167    Heart,
168}
169
170impl MaterialShape {
171    /// All 35 Material 3 Expressive shapes in Compose `MaterialShapes` order.
172    pub const ALL: [Self; 35] = [
173        Self::Circle,
174        Self::Square,
175        Self::Slanted,
176        Self::Arch,
177        Self::Fan,
178        Self::Arrow,
179        Self::SemiCircle,
180        Self::Oval,
181        Self::Pill,
182        Self::Triangle,
183        Self::Diamond,
184        Self::ClamShell,
185        Self::Pentagon,
186        Self::Gem,
187        Self::Sunny,
188        Self::VerySunny,
189        Self::Cookie4Sided,
190        Self::Cookie6Sided,
191        Self::Cookie7Sided,
192        Self::Cookie9Sided,
193        Self::Cookie12Sided,
194        Self::Ghostish,
195        Self::Clover4Leaf,
196        Self::Clover8Leaf,
197        Self::Burst,
198        Self::SoftBurst,
199        Self::Boom,
200        Self::SoftBoom,
201        Self::Flower,
202        Self::Puffy,
203        Self::PuffyDiamond,
204        Self::PixelCircle,
205        Self::PixelTriangle,
206        Self::Bun,
207        Self::Heart,
208    ];
209
210    /// Stable lowercase identifier for this shape.
211    pub const fn name(self) -> &'static str {
212        match self {
213            Self::Circle => "circle",
214            Self::Square => "square",
215            Self::Slanted => "slanted",
216            Self::Arch => "arch",
217            Self::Fan => "fan",
218            Self::Arrow => "arrow",
219            Self::SemiCircle => "semi-circle",
220            Self::Oval => "oval",
221            Self::Pill => "pill",
222            Self::Triangle => "triangle",
223            Self::Diamond => "diamond",
224            Self::ClamShell => "clam-shell",
225            Self::Pentagon => "pentagon",
226            Self::Gem => "gem",
227            Self::Sunny => "sunny",
228            Self::VerySunny => "very-sunny",
229            Self::Cookie4Sided => "cookie-4-sided",
230            Self::Cookie6Sided => "cookie-6-sided",
231            Self::Cookie7Sided => "cookie-7-sided",
232            Self::Cookie9Sided => "cookie-9-sided",
233            Self::Cookie12Sided => "cookie-12-sided",
234            Self::Ghostish => "ghostish",
235            Self::Clover4Leaf => "clover-4-leaf",
236            Self::Clover8Leaf => "clover-8-leaf",
237            Self::Burst => "burst",
238            Self::SoftBurst => "soft-burst",
239            Self::Boom => "boom",
240            Self::SoftBoom => "soft-boom",
241            Self::Flower => "flower",
242            Self::Puffy => "puffy",
243            Self::PuffyDiamond => "puffy-diamond",
244            Self::PixelCircle => "pixel-circle",
245            Self::PixelTriangle => "pixel-triangle",
246            Self::Bun => "bun",
247            Self::Heart => "heart",
248        }
249    }
250}
251
252impl From<PolygonRole> for MaterialShape {
253    fn from(role: PolygonRole) -> Self {
254        match role {
255            PolygonRole::Circle => Self::Circle,
256            PolygonRole::Square => Self::Square,
257            PolygonRole::Triangle => Self::Triangle,
258        }
259    }
260}