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
This commit is contained in:
Dániel Buga 2022-09-27 20:43:16 +02:00 committed by GitHub
parent 241341b3c0
commit e095b0a70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 90 deletions

View File

@ -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<Self> {
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::<Config>())
{
if let Ok(mut theme_config) = theme_settings.try_into::<Config>() {
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,

View File

@ -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",