content_into_gpui.rs

  1use gpui::{
  2    FontFeatures, FontStyle, FontWeight, Modifiers, Pixels, SharedString,
  3    WindowBackgroundAppearance, px,
  4};
  5use settings_content::{
  6    FontFamilyName, FontFeaturesContent, FontSize, FontStyleContent, FontWeightContent,
  7    ModifiersContent, WindowBackgroundContent,
  8};
  9use std::sync::Arc;
 10
 11/// A trait for converting settings content types into their GPUI equivalents.
 12pub trait IntoGpui {
 13    type Output;
 14    fn into_gpui(self) -> Self::Output;
 15}
 16
 17impl IntoGpui for FontStyleContent {
 18    type Output = FontStyle;
 19
 20    fn into_gpui(self) -> Self::Output {
 21        match self {
 22            FontStyleContent::Normal => FontStyle::Normal,
 23            FontStyleContent::Italic => FontStyle::Italic,
 24            FontStyleContent::Oblique => FontStyle::Oblique,
 25        }
 26    }
 27}
 28
 29impl IntoGpui for FontWeightContent {
 30    type Output = FontWeight;
 31
 32    fn into_gpui(self) -> Self::Output {
 33        FontWeight(self.0.clamp(100., 950.))
 34    }
 35}
 36
 37impl IntoGpui for FontFeaturesContent {
 38    type Output = FontFeatures;
 39
 40    fn into_gpui(self) -> Self::Output {
 41        FontFeatures(Arc::new(self.0.into_iter().collect()))
 42    }
 43}
 44
 45impl IntoGpui for WindowBackgroundContent {
 46    type Output = WindowBackgroundAppearance;
 47
 48    fn into_gpui(self) -> Self::Output {
 49        match self {
 50            WindowBackgroundContent::Opaque => WindowBackgroundAppearance::Opaque,
 51            WindowBackgroundContent::Transparent => WindowBackgroundAppearance::Transparent,
 52            WindowBackgroundContent::Blurred => WindowBackgroundAppearance::Blurred,
 53        }
 54    }
 55}
 56
 57impl IntoGpui for ModifiersContent {
 58    type Output = Modifiers;
 59
 60    fn into_gpui(self) -> Self::Output {
 61        Modifiers {
 62            control: self.control,
 63            alt: self.alt,
 64            shift: self.shift,
 65            platform: self.platform,
 66            function: self.function,
 67        }
 68    }
 69}
 70
 71impl IntoGpui for FontSize {
 72    type Output = Pixels;
 73
 74    fn into_gpui(self) -> Self::Output {
 75        px(self.0)
 76    }
 77}
 78
 79impl IntoGpui for FontFamilyName {
 80    type Output = SharedString;
 81
 82    fn into_gpui(self) -> Self::Output {
 83        SharedString::from(self.0)
 84    }
 85}
 86
 87#[cfg(test)]
 88mod tests {
 89    use gpui::FontWeight;
 90    use settings_content::FontWeightContent;
 91
 92    #[test]
 93    fn test_font_weight_content_constants_match_gpui() {
 94        assert_eq!(FontWeightContent::THIN.0, FontWeight::THIN.0);
 95        assert_eq!(FontWeightContent::EXTRA_LIGHT.0, FontWeight::EXTRA_LIGHT.0);
 96        assert_eq!(FontWeightContent::LIGHT.0, FontWeight::LIGHT.0);
 97        assert_eq!(FontWeightContent::NORMAL.0, FontWeight::NORMAL.0);
 98        assert_eq!(FontWeightContent::MEDIUM.0, FontWeight::MEDIUM.0);
 99        assert_eq!(FontWeightContent::SEMIBOLD.0, FontWeight::SEMIBOLD.0);
100        assert_eq!(FontWeightContent::BOLD.0, FontWeight::BOLD.0);
101        assert_eq!(FontWeightContent::EXTRA_BOLD.0, FontWeight::EXTRA_BOLD.0);
102        assert_eq!(FontWeightContent::BLACK.0, FontWeight::BLACK.0);
103    }
104}