From 4f0c9a3e317a9a51981a9fc1c6da7a40409f6a90 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 16 Sep 2021 14:43:19 -0600 Subject: [PATCH] Build workspace editor TextStyle from font fields in settings We'll specify values in the theme but we'll only end up using the color for these editors. --- gpui/src/font_cache.rs | 8 ++++---- gpui/src/fonts.rs | 7 +++++-- zed/src/editor.rs | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/gpui/src/font_cache.rs b/gpui/src/font_cache.rs index 3c11b9659cb26441ab7746bac6a6f306fdbcef42..ddb7c9c28801ee490e03c8bd43c17189742dcf40 100644 --- a/gpui/src/font_cache.rs +++ b/gpui/src/font_cache.rs @@ -17,7 +17,7 @@ use std::{ pub struct FamilyId(usize); struct Family { - name: String, + name: Arc, font_ids: Vec, } @@ -49,7 +49,7 @@ impl FontCache { })) } - pub fn family_name(&self, family_id: FamilyId) -> Result { + pub fn family_name(&self, family_id: FamilyId) -> Result> { self.0 .read() .families @@ -62,7 +62,7 @@ impl FontCache { for name in names { let state = self.0.upgradable_read(); - if let Some(ix) = state.families.iter().position(|f| f.name == *name) { + if let Some(ix) = state.families.iter().position(|f| f.name.as_ref() == *name) { return Ok(FamilyId(ix)); } @@ -81,7 +81,7 @@ impl FontCache { } state.families.push(Family { - name: String::from(*name), + name: Arc::from(*name), font_ids, }); return Ok(family_id); diff --git a/gpui/src/fonts.rs b/gpui/src/fonts.rs index 3d3ff1efb6897d6a544ace1f78b11c8985b775da..9df36464f6c065206d4538129c3dbe1192281282 100644 --- a/gpui/src/fonts.rs +++ b/gpui/src/fonts.rs @@ -1,5 +1,6 @@ use crate::{ color::Color, + font_cache::FamilyId, json::{json, ToJson}, text_layout::RunStyle, FontCache, @@ -22,6 +23,7 @@ pub type GlyphId = u32; pub struct TextStyle { pub color: Color, pub font_family_name: Arc, + pub font_family_id: FamilyId, pub font_id: FontId, pub font_size: f32, pub font_properties: Properties, @@ -85,11 +87,12 @@ impl TextStyle { font_cache: &FontCache, ) -> anyhow::Result { let font_family_name = font_family_name.into(); - let family_id = font_cache.load_family(&[&font_family_name])?; - let font_id = font_cache.select_font(family_id, &font_properties)?; + let font_family_id = font_cache.load_family(&[&font_family_name])?; + let font_id = font_cache.select_font(font_family_id, &font_properties)?; Ok(Self { color, font_family_name, + font_family_id, font_id, font_size, font_properties, diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 8be376e987c632b5f6941c23939e3d761d7e2101..3bdb200226f0d9222891c6e7220ca9e6a0cf440e 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -2597,15 +2597,18 @@ impl Snapshot { } impl EditorStyle { - #[cfg(any(test, feature ="test-support"))] + #[cfg(any(test, feature = "test-support"))] pub fn test(font_cache: &FontCache) -> Self { - let font_family_name = "Monaco"; + let font_family_name = Arc::from("Monaco"); let font_properties = Default::default(); - let family_id = font_cache.load_family(&[font_family_name]).unwrap(); - let font_id = font_cache.select_font(family_id, &font_properties).unwrap(); + let font_family_id = font_cache.load_family(&[&font_family_name]).unwrap(); + let font_id = font_cache + .select_font(font_family_id, &font_properties) + .unwrap(); Self { text: TextStyle { - font_family_name: font_family_name.into(), + font_family_name, + font_family_id, font_id, font_size: 14., color: Color::from_u32(0xff0000ff), @@ -2718,7 +2721,29 @@ impl workspace::Item for Buffer { Editor::for_buffer( handle, settings.clone(), - move |_| settings.borrow().theme.editor.clone(), + move |cx| { + let settings = settings.borrow(); + let font_cache = cx.font_cache(); + let font_family_id = settings.buffer_font_family; + let font_family_name = cx.font_cache().family_name(font_family_id).unwrap(); + let font_properties = Default::default(); + let font_id = font_cache + .select_font(font_family_id, &font_properties) + .unwrap(); + let font_size = settings.buffer_font_size; + + let mut theme = settings.theme.editor.clone(); + theme.text = TextStyle { + color: theme.text.color, + font_family_name, + font_family_id, + font_id, + font_size, + font_properties, + underline: false, + }; + theme + }, cx, ) }