feat(settings): global setting for icon theme

This commit is contained in:
Michael Aaron Murphy 2023-01-05 14:16:27 +01:00 committed by Michael Murphy
parent 043485c68d
commit 7d018a2139
8 changed files with 103 additions and 48 deletions

View file

@ -16,21 +16,12 @@ pub mod font;
pub mod theme;
pub mod widget;
pub mod settings;
pub use settings::settings;
mod ext;
pub use ext::ElementExt;
pub use theme::Theme;
pub type Renderer = iced::Renderer<Theme>;
pub type Element<'a, Message> = iced::Element<'a, Message, Renderer>;
#[must_use]
pub fn settings<Flags: Default>() -> iced::Settings<Flags> {
iced::Settings {
default_font: match font::FONT {
iced::Font::Default => None,
iced::Font::External { bytes, .. } => Some(bytes),
},
default_text_size: 18,
..iced::Settings::default()
}
}

31
src/settings.rs Normal file
View file

@ -0,0 +1,31 @@
use crate::font;
use std::cell::RefCell;
thread_local! {
/// The fallback icon theme to search if no icon theme was specified.
pub(crate) static DEFAULT_ICON_THEME: RefCell<String> = RefCell::new(String::from("Pop"));
}
/// The fallback icon theme to search if no icon theme was specified.
#[must_use]
pub fn default_icon_theme() -> String {
DEFAULT_ICON_THEME.with(|f| f.borrow().clone())
}
/// Set the fallback icon theme to search when loading system icons.
pub fn set_default_icon_theme(name: impl Into<String>) {
DEFAULT_ICON_THEME.with(|f| *f.borrow_mut() = name.into());
}
/// Default iced settings for COSMIC applications.
#[must_use]
pub fn settings<Flags: Default>() -> iced::Settings<Flags> {
iced::Settings {
default_font: match font::FONT {
iced::Font::Default => None,
iced::Font::External { bytes, .. } => Some(bytes),
},
default_text_size: 18,
..iced::Settings::default()
}
}

View file

@ -10,9 +10,9 @@ use iced::{
ContentFit, Length,
};
use std::{
borrow::Cow, collections::hash_map::DefaultHasher, ffi::OsStr, hash::Hasher, path::Path,
borrow::Cow, collections::hash_map::DefaultHasher, ffi::OsStr, hash::Hash, hash::Hasher,
path::Path, path::PathBuf,
};
use std::{hash::Hash, path::PathBuf};
#[derive(Debug, Hash)]
pub enum IconSource<'a> {
@ -112,18 +112,25 @@ impl<'a> Icon<'a> {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
iced_lazy::lazy(hasher.finish(), move || -> Element<Message> {
if self.theme.is_none() {
crate::settings::DEFAULT_ICON_THEME.with(|f| f.borrow().hash(&mut hasher));
}
let hash = hasher.finish();
iced_lazy::lazy(hash, move || -> Element<Message> {
let name_path_buffer: Option<PathBuf>;
let icon: Option<&Path> = match &self.name {
IconSource::Path(path) => Some(path),
IconSource::Name(name) => {
let mut builder = freedesktop_icons::lookup(name).with_size(self.size);
if let Some(theme) = self.theme.as_deref() {
builder = builder.with_theme(theme);
}
let icon = builder.with_cache().find();
let icon = crate::settings::DEFAULT_ICON_THEME.with(|default_theme| {
let default_theme: &str = &default_theme.borrow();
freedesktop_icons::lookup(name)
.with_size(self.size)
.with_theme(self.theme.as_deref().unwrap_or(default_theme))
.with_cache()
.find()
});
name_path_buffer = if icon.is_none() {
freedesktop_icons::lookup(name)

View file

@ -45,7 +45,7 @@ impl<T: 'static + Copy + Hash + ToString, Message: 'static> SpinButton<T, Messag
let Self { on_change, value } = self;
Element::from(iced_lazy::lazy(
value,
(value, crate::settings::default_icon_theme()),
move || -> Element<'static, SpinMessage> {
container(
row![