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