From 01f3d38e2ac0518113597a361718aa78ecc39b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 28 Mar 2022 22:08:19 +0200 Subject: [PATCH] Add parent to get_theme --- lapce-data/src/config.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lapce-data/src/config.rs b/lapce-data/src/config.rs index 4f159393..c1828db1 100644 --- a/lapce-data/src/config.rs +++ b/lapce-data/src/config.rs @@ -194,11 +194,11 @@ fn default() -> Self { let mut themes = HashMap::new(); themes.insert( "Lapce Light".to_string(), - get_theme(DEFAULT_LIGHT_THEME).unwrap(), + get_theme(DEFAULT_LIGHT_THEME, &Theme::default()).unwrap(), ); themes.insert( "Lapce Dark".to_string(), - get_theme(DEFAULT_DARK_THEME).unwrap(), + get_theme(DEFAULT_DARK_THEME, &Theme::default()).unwrap(), ); let default_theme = themes["Lapce Light"].clone(); @@ -266,8 +266,7 @@ fn load_theme(&mut self, theme_name: &str) -> Result<()> { let theme_content = std::fs::read_to_string(theme_path).map_err(LoadThemeError::Read)?; - let mut theme = get_theme(&theme_content)?; - theme.merge_from(&self.default_theme); + let theme = get_theme(&theme_content, &self.default_theme)?; // Insert it into the themes hashmap // Most users won't have an absurd amount of themes, so that we don't clean this @@ -627,14 +626,14 @@ pub fn recent_workspaces_file() -> Option { } } -fn get_theme(content: &str) -> Result { +/// Creates a new theme based on `parent` and the theme parsed from `content`. +fn get_theme(content: &str, parent: &Theme) -> Result { let theme_colors: std::collections::HashMap = toml::from_str(content)?; let mut theme = HashMap::new(); for (k, v) in theme_colors.iter() { if let Some(stripped) = v.strip_prefix('$') { - let var_name = stripped; - if let Some(hex) = theme_colors.get(var_name) { + if let Some(hex) = theme_colors.get(stripped) { if let Ok(color) = hex_to_color(hex) { theme.insert(k.clone(), color); } @@ -643,5 +642,9 @@ fn get_theme(content: &str) -> Result { theme.insert(k.clone(), color); } } - Ok(Theme::from(theme)) + + let mut theme = Theme::from(theme); + Theme::merge_from(&mut theme, parent); + + Ok(theme) }