From e095b0a70de22993873ad1611bf86620d15fc892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 27 Sep 2022 20:43:16 +0200 Subject: [PATCH] Fix crash when changing themes while Settings is open (#1365) * Make enum Copy * Clean up ThemeBaseConfig resolve * Clean up ThemeConfig resolve * Simplify config load a bit * Fix crash when changing theme while settings is open * Pacify clippy --- lapce-data/src/config.rs | 111 +++++++++++---------------------------- lapce-ui/src/settings.rs | 17 +++--- 2 files changed, 38 insertions(+), 90 deletions(-) diff --git a/lapce-data/src/config.rs b/lapce-data/src/config.rs index 22a7006e..cae54fc2 100644 --- a/lapce-data/src/config.rs +++ b/lapce-data/src/config.rs @@ -432,27 +432,19 @@ fn resolve_color( colors .iter() .map(|(name, hex)| { - if let Some(stripped) = hex.strip_prefix('$') { - if let Some(c) = base.get(stripped) { - return (name.to_string(), c.clone()); - } - if let Some(default) = default { - if let Some(c) = default.get(name) { - return (name.to_string(), c.clone()); - } - } - return (name.to_string(), Color::rgb8(0, 0, 0)); - } + let color = if let Some(stripped) = hex.strip_prefix('$') { + base.get(stripped).cloned() + } else { + Color::from_hex_str(hex).ok() + }; - if let Ok(c) = Color::from_hex_str(hex) { - return (name.to_string(), c); - } - if let Some(default) = default { - if let Some(c) = default.get(name) { - return (name.to_string(), c.clone()); - } - } - (name.to_string(), Color::rgb8(0, 0, 0)) + let color = color + .or_else(|| { + default.and_then(|default| default.get(name).cloned()) + }) + .unwrap_or(Color::rgb8(0, 0, 0)); + + (name.to_string(), color) }) .collect() } @@ -491,62 +483,19 @@ pub struct ThemeBaseConfig { impl ThemeBaseConfig { pub fn resolve(&self, default: Option<&ThemeBaseColor>) -> ThemeBaseColor { + let default = default.cloned().unwrap_or_default(); ThemeBaseColor { - white: Color::from_hex_str(&self.white).unwrap_or_else(|_| { - default - .map(|d| d.white.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - black: Color::from_hex_str(&self.black).unwrap_or_else(|_| { - default - .map(|d| d.black.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - grey: Color::from_hex_str(&self.grey).unwrap_or_else(|_| { - default - .map(|d| d.grey.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - blue: Color::from_hex_str(&self.blue).unwrap_or_else(|_| { - default - .map(|d| d.blue.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - red: Color::from_hex_str(&self.red).unwrap_or_else(|_| { - default - .map(|d| d.red.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - yellow: Color::from_hex_str(&self.yellow).unwrap_or_else(|_| { - default - .map(|d| d.yellow.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - orange: Color::from_hex_str(&self.orange).unwrap_or_else(|_| { - default - .map(|d| d.orange.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - green: Color::from_hex_str(&self.green).unwrap_or_else(|_| { - default - .map(|d| d.green.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - purple: Color::from_hex_str(&self.purple).unwrap_or_else(|_| { - default - .map(|d| d.purple.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - cyan: Color::from_hex_str(&self.cyan).unwrap_or_else(|_| { - default - .map(|d| d.cyan.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), - magenta: Color::from_hex_str(&self.magenta).unwrap_or_else(|_| { - default - .map(|d| d.magenta.clone()) - .unwrap_or_else(|| Color::rgb8(0, 0, 0)) - }), + white: Color::from_hex_str(&self.white).unwrap_or(default.white), + black: Color::from_hex_str(&self.black).unwrap_or(default.black), + grey: Color::from_hex_str(&self.grey).unwrap_or(default.grey), + blue: Color::from_hex_str(&self.blue).unwrap_or(default.blue), + red: Color::from_hex_str(&self.red).unwrap_or(default.red), + yellow: Color::from_hex_str(&self.yellow).unwrap_or(default.yellow), + orange: Color::from_hex_str(&self.orange).unwrap_or(default.orange), + green: Color::from_hex_str(&self.green).unwrap_or(default.green), + purple: Color::from_hex_str(&self.purple).unwrap_or(default.purple), + cyan: Color::from_hex_str(&self.cyan).unwrap_or(default.cyan), + magenta: Color::from_hex_str(&self.magenta).unwrap_or(default.magenta), } } @@ -698,13 +647,13 @@ pub fn load(workspace: &LapceWorkspace) -> Result { if let Some((_, theme)) = available_themes.get(&config.lapce.color_theme.to_lowercase()) { - if let Ok(theme_settings) = - default_settings.clone().with_merged(theme.clone()) + if let Ok(mut theme_config) = default_settings + .clone() + .with_merged(theme.clone()) + .and_then(|theme| theme.try_into::()) { - if let Ok(mut theme_config) = theme_settings.try_into::() { - theme_config.resolve_colors(Some(&default_config)); - default_config = theme_config; - } + theme_config.resolve_colors(Some(&default_config)); + default_config = theme_config; } config = Self::merge_settings( default_settings, diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index 588ae0d3..8b2a7cbf 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -986,7 +986,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { } } -#[derive(Clone)] +#[derive(Clone, Copy)] pub enum ThemeKind { Base, UI, @@ -1071,7 +1071,7 @@ fn update_inputs(&mut self, ctx: &mut EventCtx, data: &mut LapceTabData) { self.inputs.clear(); self.text_layouts = None; - let colors: Vec<&str> = match &self.kind { + let colors: Vec<&str> = match self.kind { ThemeKind::Base => { data.config.color.base.keys().into_iter().sorted().collect() } @@ -1108,7 +1108,7 @@ fn update_inputs(&mut self, ctx: &mut EventCtx, data: &mut LapceTabData) { data.proxy.clone(), ); doc.reload( - Rope::from(match &self.kind { + Rope::from(match self.kind { ThemeKind::Base => data.config.theme.base.get(color).unwrap(), ThemeKind::UI => data.config.theme.ui.get(color).unwrap(), ThemeKind::Syntax => { @@ -1303,8 +1303,7 @@ fn layout( .unwrap() .to_string(); ( - data.config.theme.base.get(&self.keys[i]).unwrap() - != &default, + data.config.theme.base.get(&self.keys[i]) != Some(&default), default, ) } @@ -1317,7 +1316,7 @@ fn layout( .unwrap() .to_string(); ( - data.config.theme.ui.get(&self.keys[i]).unwrap() != &default, + data.config.theme.ui.get(&self.keys[i]) != Some(&default), default, ) } @@ -1330,8 +1329,8 @@ fn layout( .cloned() .unwrap_or_else(|| "".to_string()); ( - data.config.theme.syntax.get(&self.keys[i]).unwrap() - != &default, + data.config.theme.syntax.get(&self.keys[i]) + != Some(&default), default, ) } @@ -1352,7 +1351,7 @@ fn layout( fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { let header_text = ctx .text() - .new_text_layout(match &self.kind { + .new_text_layout(match self.kind { ThemeKind::Base => "Base Colors", ThemeKind::UI => "UI Colors", ThemeKind::Syntax => "Syntax Colors",