Skip to main content

gpui_phosphor/
weight.rs

1/// A visual weight from the Phosphor icon family.
2#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
3pub enum Weight {
4    /// The standard outline weight.
5    #[default]
6    Regular,
7    /// The thinnest outline weight.
8    Thin,
9    /// A light outline weight.
10    Light,
11    /// A heavy outline weight.
12    Bold,
13    /// A solid filled weight.
14    Fill,
15    /// An outline with a translucent secondary layer.
16    Duotone,
17}
18
19impl Weight {
20    /// All six Phosphor weights, in the order used by the crate’s feature list.
21    pub const ALL: [Self; 6] = [
22        Self::Regular,
23        Self::Thin,
24        Self::Light,
25        Self::Bold,
26        Self::Fill,
27        Self::Duotone,
28    ];
29
30    /// Return the lowercase name used in asset paths and Cargo features.
31    #[must_use]
32    pub const fn as_str(self) -> &'static str {
33        match self {
34            Self::Regular => "regular",
35            Self::Thin => "thin",
36            Self::Light => "light",
37            Self::Bold => "bold",
38            Self::Fill => "fill",
39            Self::Duotone => "duotone",
40        }
41    }
42
43    /// Find a weight by its lowercase name.
44    #[must_use]
45    pub fn from_name(name: &str) -> Option<Self> {
46        Self::ALL.into_iter().find(|weight| weight.as_str() == name)
47    }
48
49    /// Whether this weight’s Cargo feature is enabled in the current build.
50    #[must_use]
51    pub const fn is_enabled(self) -> bool {
52        match self {
53            Self::Regular => cfg!(feature = "regular"),
54            Self::Thin => cfg!(feature = "thin"),
55            Self::Light => cfg!(feature = "light"),
56            Self::Bold => cfg!(feature = "bold"),
57            Self::Fill => cfg!(feature = "fill"),
58            Self::Duotone => cfg!(feature = "duotone"),
59        }
60    }
61
62    /// Return the first enabled weight for use by a default component.
63    #[must_use]
64    pub const fn default_enabled() -> Self {
65        if cfg!(feature = "regular") {
66            Self::Regular
67        } else if cfg!(feature = "thin") {
68            Self::Thin
69        } else if cfg!(feature = "light") {
70            Self::Light
71        } else if cfg!(feature = "bold") {
72            Self::Bold
73        } else if cfg!(feature = "fill") {
74            Self::Fill
75        } else if cfg!(feature = "duotone") {
76            Self::Duotone
77        } else {
78            // Cargo permits --no-default-features with no replacement weight.
79            // The component will report the missing feature through Icon::path.
80            Self::Regular
81        }
82    }
83}
84
85impl std::fmt::Display for Weight {
86    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        formatter.write_str(self.as_str())
88    }
89}