Load embedded fonts when initializing settings

Antonio Scandurra created

Change summary

gpui/src/platform.rs            |  2 +-
gpui/src/platform/mac/fonts.rs  | 11 +++++++----
zed/src/editor.rs               |  2 +-
zed/src/main.rs                 |  4 ++--
zed/src/settings.rs             | 12 +++++++++++-
zed/src/test.rs                 |  3 ++-
zed/src/theme/theme_registry.rs |  8 ++++----
7 files changed, 28 insertions(+), 14 deletions(-)

Detailed changes

gpui/src/platform.rs 🔗

@@ -128,7 +128,7 @@ pub enum CursorStyle {
 }
 
 pub trait FontSystem: Send + Sync {
-    fn add_fonts(&self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()>;
+    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
     fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
     fn select_font(
         &self,

gpui/src/platform/mac/fonts.rs 🔗

@@ -50,7 +50,7 @@ impl FontSystem {
 }
 
 impl platform::FontSystem for FontSystem {
-    fn add_fonts(&self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()> {
+    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
         self.0.write().add_fonts(fonts)
     }
 
@@ -102,9 +102,12 @@ impl platform::FontSystem for FontSystem {
 }
 
 impl FontSystemState {
-    fn add_fonts(&mut self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()> {
-        self.memory_source
-            .add_fonts(fonts.into_iter().map(|bytes| Handle::from_memory(bytes, 0)))?;
+    fn add_fonts(&mut self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
+        self.memory_source.add_fonts(
+            fonts
+                .iter()
+                .map(|bytes| Handle::from_memory(bytes.clone(), 0)),
+        )?;
         Ok(())
     }
 

zed/src/editor.rs 🔗

@@ -3478,7 +3478,7 @@ mod tests {
         });
 
         view.update(cx, |view, cx| {
-            view.set_wrap_width(140., cx);
+            view.set_wrap_width(130., cx);
             assert_eq!(
                 view.display_text(cx),
                 "use one::{\n    two::three::\n    four::five\n};"

zed/src/main.rs 🔗

@@ -27,8 +27,8 @@ fn main() {
         .list("fonts")
         .into_iter()
         .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
-        .collect();
-    app.platform().fonts().add_fonts(embedded_fonts).unwrap();
+        .collect::<Vec<_>>();
+    app.platform().fonts().add_fonts(&embedded_fonts).unwrap();
 
     let themes = settings::ThemeRegistry::new(Assets, app.font_cache());
     let (settings_tx, settings) = settings::channel(&app.font_cache(), &themes).unwrap();

zed/src/settings.rs 🔗

@@ -17,15 +17,25 @@ pub struct Settings {
 impl Settings {
     #[cfg(any(test, feature = "test-support"))]
     pub fn test(cx: &gpui::AppContext) -> Self {
+        use crate::assets::Assets;
+        use gpui::AssetSource;
+
         lazy_static::lazy_static! {
             static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
+            static ref FONTS: Vec<Arc<Vec<u8>>> = Assets
+                .list("fonts")
+                .into_iter()
+                .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
+                .collect();
         }
 
+        cx.platform().fonts().add_fonts(&FONTS).unwrap();
+
         let mut theme_guard = DEFAULT_THEME.lock();
         let theme = if let Some(theme) = theme_guard.as_ref() {
             theme.clone()
         } else {
-            let theme = ThemeRegistry::new(crate::assets::Assets, cx.font_cache().clone())
+            let theme = ThemeRegistry::new(Assets, cx.font_cache().clone())
                 .get(DEFAULT_THEME_NAME)
                 .expect("failed to load default theme in tests");
             *theme_guard = Some(theme.clone());

zed/src/test.rs 🔗

@@ -1,4 +1,5 @@
 use crate::{
+    assets::Assets,
     channel::ChannelList,
     fs::RealFs,
     language::LanguageRegistry,
@@ -158,7 +159,7 @@ fn write_tree(path: &Path, tree: serde_json::Value) {
 pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
     let (settings_tx, settings) = settings::test(cx);
     let languages = Arc::new(LanguageRegistry::new());
-    let themes = ThemeRegistry::new((), cx.font_cache().clone());
+    let themes = ThemeRegistry::new(Assets, cx.font_cache().clone());
     let rpc = rpc::Client::new();
     let user_store = Arc::new(UserStore::new(rpc.clone()));
     Arc::new(AppState {

zed/src/theme/theme_registry.rs 🔗

@@ -515,16 +515,16 @@ fn value_at<'a>(object: &'a mut Map<String, Value>, key_path: &KeyPath) -> Optio
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::{assets::Assets, theme::DEFAULT_THEME_NAME};
+    use crate::{test::test_app_state, theme::DEFAULT_THEME_NAME};
     use gpui::MutableAppContext;
     use rand::{prelude::StdRng, Rng};
 
     #[gpui::test]
     fn test_bundled_themes(cx: &mut MutableAppContext) {
-        let registry = ThemeRegistry::new(Assets, cx.font_cache().clone());
+        let app_state = test_app_state(cx);
         let mut has_default_theme = false;
-        for theme_name in registry.list() {
-            let theme = registry.get(&theme_name).unwrap();
+        for theme_name in app_state.themes.list() {
+            let theme = app_state.themes.get(&theme_name).unwrap();
             if theme.name == DEFAULT_THEME_NAME {
                 has_default_theme = true;
             }