From 61427b25b0ba21a7978a414ffd889d0bf13540ed Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 19 Mar 2024 16:09:52 -0400 Subject: [PATCH] fix: fallback to Cosmic icon theme --- examples/cosmic/src/window/demo.rs | 2 +- src/icon_theme.rs | 2 ++ src/widget/icon/named.rs | 47 +++++++++++++++++------------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/examples/cosmic/src/window/demo.rs b/examples/cosmic/src/window/demo.rs index 347809b4..20025441 100644 --- a/examples/cosmic/src/window/demo.rs +++ b/examples/cosmic/src/window/demo.rs @@ -8,7 +8,7 @@ use cosmic::{ iced_core::id, theme::ThemeType, widget::{ - button, color_picker::ColorPickerUpdate, cosmic_container::container, dropdown, icon, + button, color_picker::ColorPickerUpdate, dropdown, icon, layer_container as container, segmented_button, segmented_control, settings, spin_button, tab_bar, toggler, ColorPickerModel, }, diff --git a/src/icon_theme.rs b/src/icon_theme.rs index 6d8dfc89..c78f2cc1 100644 --- a/src/icon_theme.rs +++ b/src/icon_theme.rs @@ -6,6 +6,8 @@ use std::borrow::Cow; use std::cell::RefCell; +pub const COSMIC: &str = "Cosmic"; + thread_local! { /// The fallback icon theme to search if no icon theme was specified. pub(crate) static DEFAULT: RefCell> = RefCell::new("Cosmic".into()); diff --git a/src/widget/icon/named.rs b/src/widget/icon/named.rs index 736027e1..99a00467 100644 --- a/src/widget/icon/named.rs +++ b/src/widget/icon/named.rs @@ -56,43 +56,48 @@ impl Named { pub fn path(self) -> Option { let name = &*self.name; let fallback = &self.fallback; + let locate = |theme: &str, name| { + let mut lookup = freedesktop_icons::lookup(name) + .with_theme(theme.as_ref()) + .with_cache(); + + if let Some(scale) = self.scale { + lookup = lookup.with_scale(scale); + } + + if let Some(size) = self.size { + lookup = lookup.with_size(size); + } + + if self.prefer_svg { + lookup = lookup.force_svg(); + } + lookup.find() + }; + crate::icon_theme::DEFAULT.with(|theme| { let theme = theme.borrow(); - let locate = |name| { - let mut lookup = freedesktop_icons::lookup(name) - .with_theme(theme.as_ref()) - .with_cache(); - - if let Some(scale) = self.scale { - lookup = lookup.with_scale(scale); - } - - if let Some(size) = self.size { - lookup = lookup.with_size(size); - } - - if self.prefer_svg { - lookup = lookup.force_svg(); - } - - lookup.find() + let themes = if theme.as_ref() == crate::icon_theme::COSMIC { + vec![theme.as_ref()] + } else { + vec![theme.as_ref(), crate::icon_theme::COSMIC] }; - let mut result = locate(name); + let mut result = themes.iter().find_map(|t| locate(t, name)); // On failure, attempt to locate fallback icon. if result.is_none() { if matches!(fallback, Some(IconFallback::Default)) { for new_name in name.rmatch_indices('-').map(|(pos, _)| &name[..pos]) { - result = locate(new_name); + result = themes.iter().find_map(|t| locate(t, new_name)); if result.is_some() { break; } } } else if let Some(IconFallback::Names(fallbacks)) = fallback { for fallback in fallbacks { - result = locate(fallback); + result = themes.iter().find_map(|t| locate(t, fallback)); if result.is_some() { break; }