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}