Load embedded fonts when initializing settings

This commit is contained in:
Antonio Scandurra
2021-09-06 14:08:42 +02:00
parent 6e71c43d29
commit 94959d18c4
7 changed files with 28 additions and 14 deletions
+1 -1
View File
@@ -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,
+7 -4
View File
@@ -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(())
}
+1 -1
View File
@@ -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};"
+2 -2
View File
@@ -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();
+11 -1
View File
@@ -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());
+2 -1
View File
@@ -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 {
+4 -4
View File
@@ -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;
}