From 0d48e332a9902a4ef807dc5bc90761849309c5bd Mon Sep 17 00:00:00 2001 From: squid Date: Sat, 7 Nov 2026 18:25:40 +0100 Subject: [PATCH] Added error handling for missing theme files. Added debug config file usage in a debug build --- src/main.rs | 118 ++++++++++++++++++++++++++++++++++++++++++--------- src/theme.rs | 10 ++++- 2 files changed, 107 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index bdcba7e..2777629 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1120,7 +1120,7 @@ fn builtin_toast_colors(preference: theme::Preference) -> [Color; 9] { notifications::ToastColors::default_for(&theme).as_array() } -/// Everything persisted in `config.toml`, grouped by settings tab. +/// Everything persisted in the config file, grouped by settings tab. #[derive(Debug, Clone, Serialize, Deserialize)] struct ConfigFile { #[serde(default)] @@ -1178,6 +1178,8 @@ struct AppearanceConfig { theme: String, #[serde(default, skip_serializing_if = "Option::is_none")] custom_theme: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + custom_theme_name: Option, #[serde(default = "default_true")] hide_tab_close: bool, #[serde(default)] @@ -1189,6 +1191,7 @@ impl Default for AppearanceConfig { Self { theme: default_theme(), custom_theme: None, + custom_theme_name: None, hide_tab_close: true, toast: ToastFile::default(), } @@ -1434,12 +1437,12 @@ impl SettingsSection { } } -/// Path of the settings file. Everything lives in the same `config.toml`. +/// Path of the settings file. Everything lives in the same file. fn config_path() -> Option { theme::config_path() } -/// Reads `config.toml`, falling back to the old split config files so an +/// Reads the config file, falling back to the old split config files so an /// upgrade keeps the user's existing settings. fn load_config() -> ConfigFile { if let Some(path) = config_path() @@ -1483,7 +1486,7 @@ fn load_config() -> ConfigFile { config } -/// Toast customization saved for built-in themes in `config.toml`. +/// Toast customization saved for built-in themes in the config file. fn builtin_toast_config(appearance: &AppearanceConfig) -> (bool, [Color; 9]) { let preference = theme::Preference::parse(&appearance.theme).unwrap_or_default(); @@ -1493,23 +1496,45 @@ fn builtin_toast_config(appearance: &AppearanceConfig) -> (bool, [Color; 9]) { ) } -/// Resolves the saved theme selection to its live theme and colors. +/// Resolves the saved theme selection to its live theme and colors. The final +/// flag reports that a selected custom theme could not be found, in which case +/// the app falls back to the built-in dark theme. fn resolve_theme( appearance: &AppearanceConfig, custom_themes: &[CustomTheme], -) -> (ThemeChoice, theme::Preference, [Color; 5], bool, [Color; 9]) { - if let Some(id) = &appearance.custom_theme - && let Some(theme) = custom_themes.iter().find(|theme| theme.id == *id) - { +) -> ( + ThemeChoice, + theme::Preference, + [Color; 5], + bool, + [Color; 9], + bool, +) { + if let Some(id) = &appearance.custom_theme { + if let Some(theme) = custom_themes.iter().find(|theme| theme.id == *id) { + return ( + ThemeChoice::Custom { + id: theme.id.clone(), + name: theme.name.clone(), + }, + theme.based_on, + theme.colors, + theme.custom_toasts, + theme.toast_colors, + false, + ); + } + + let preference = theme::Preference::Dark; + let (custom_toasts, toast_colors) = builtin_toast_config(appearance); + return ( - ThemeChoice::Custom { - id: theme.id.clone(), - name: theme.name.clone(), - }, - theme.based_on, - theme.colors, - theme.custom_toasts, - theme.toast_colors, + ThemeChoice::BuiltIn(preference), + preference, + builtin_colors(preference), + custom_toasts, + toast_colors, + true, ); } @@ -1522,9 +1547,24 @@ fn resolve_theme( builtin_colors(preference), custom_toasts, toast_colors, + false, ) } +/// Warning shown when the saved custom theme is no longer on disk. +fn missing_theme_notification(name: Option<&str>) -> notifications::Notification { + let message = match name { + Some(name) => { + format!("The theme \"{name}\" is missing. Switched to the Dark theme.") + } + None => "The selected custom theme is missing. Switched to the Dark theme.".to_owned(), + }; + + notifications::Notification::new(notifications::NotificationKind::Warning, message) + .title("Theme unavailable") + .persistent() +} + /// 0.5s total: 250ms fading out plus 250ms fading in. fn new_fade_animation() -> Animation { Animation::new(true).duration(Duration::from_millis(250)) @@ -1621,6 +1661,10 @@ fn config_file(state: &Moorly) -> ConfigFile { ThemeChoice::Custom { id, .. } => Some(id.clone()), ThemeChoice::BuiltIn(_) => None, }, + custom_theme_name: match &state.saved_theme_choice { + ThemeChoice::Custom { name, .. } => Some(name.clone()), + ThemeChoice::BuiltIn(_) => None, + }, hide_tab_close: state.hide_tab_close, toast: ToastFile::from_colors(state.builtin_custom_toasts, &state.builtin_toast_colors), }, @@ -1645,7 +1689,7 @@ impl Default for Moorly { fn default() -> Self { let config = load_config(); let custom_themes = load_custom_themes(); - let (theme_choice, theme, colors, custom_toasts, toast_colors) = + let (theme_choice, theme, colors, custom_toasts, toast_colors, theme_missing) = resolve_theme(&config.appearance, &custom_themes); let (builtin_custom_toasts, builtin_toast_palette) = builtin_toast_config(&config.appearance); @@ -1656,6 +1700,13 @@ impl Default for Moorly { settings: Shortcut::parse(&config.shortcuts.settings), quit: Shortcut::parse(&config.shortcuts.quit), }; + let mut notifications = notifications::NotificationManager::default(); + + if theme_missing { + notifications.add(missing_theme_notification( + config.appearance.custom_theme_name.as_deref(), + )); + } Self { windows: HashMap::new(), @@ -1702,7 +1753,7 @@ impl Default for Moorly { profile_dialog: None, dir_picker: None, next_blank_tab: 1, - notifications: notifications::NotificationManager::default(), + notifications, } } } @@ -2093,6 +2144,12 @@ fn update(state: &mut Moorly, message: Message) -> Task { state.saved_colors = theme.colors; } } else { + let missing_name = match &state.theme_choice { + ThemeChoice::Custom { name, .. } => Some(name.clone()), + ThemeChoice::BuiltIn(_) => None, + }; + + state.theme = theme::Preference::Dark; state.theme_choice = ThemeChoice::BuiltIn(state.theme); state.colors = builtin_colors(state.theme); state.custom_toasts = state.builtin_custom_toasts; @@ -2102,9 +2159,14 @@ fn update(state: &mut Moorly, message: Message) -> Task { &state.saved_theme_choice, ThemeChoice::Custom { id: saved, .. } if saved == &id ) { + state.saved_theme = state.theme; state.saved_theme_choice = state.theme_choice.clone(); state.saved_colors = state.colors; } + + state + .notifications + .add(missing_theme_notification(missing_name.as_deref())); } } } @@ -5384,6 +5446,22 @@ mod tests { }; let text = toml::to_string_pretty(&super::config_file(&state)).unwrap(); assert!(text.contains("custom_theme = \"abc\"")); + assert!(text.contains("custom_theme_name = \"Mine\"")); + } + + #[test] + fn missing_custom_theme_falls_back_to_dark() { + let appearance = super::AppearanceConfig { + theme: "light".to_owned(), + custom_theme: Some("gone".to_owned()), + ..super::AppearanceConfig::default() + }; + + let (choice, preference, _, _, _, missing) = super::resolve_theme(&appearance, &[]); + + assert!(missing); + assert_eq!(preference, theme::Preference::Dark); + assert_eq!(choice, super::ThemeChoice::BuiltIn(theme::Preference::Dark)); } #[test] @@ -5511,7 +5589,7 @@ mod tests { #[test] fn toaster_color_picker_updates_only_toast_colors() { let mut state = state(); - // A custom theme keeps edits out of config.toml and in the theme file. + // A custom theme keeps edits out of the config file and in the theme file. state.theme_choice = super::ThemeChoice::Custom { id: "test".to_owned(), name: "Test".to_owned(), diff --git a/src/theme.rs b/src/theme.rs index 84682c2..56f843c 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -80,7 +80,15 @@ pub(crate) fn config_path() -> Option { .or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".config"))) }?; - Some(base.join("moorly").join("config.toml")) + // Debug builds get their own file so manual testing never touches the + // settings used by a release build. + let file = if cfg!(debug_assertions) { + "config_debug.toml" + } else { + "config.toml" + }; + + Some(base.join("moorly").join(file)) } /// Style for the top menubar.