feat: Add IconSource::load method to get icon handles
This commit is contained in:
parent
5224c9b75c
commit
197d5a1c14
1 changed files with 85 additions and 60 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
use crate::{Element, Renderer};
|
use crate::{Element, Renderer};
|
||||||
use derive_setters::Setters;
|
use derive_setters::Setters;
|
||||||
use iced::{
|
use iced::{
|
||||||
widget::{svg, Image},
|
widget::{image, svg, Image},
|
||||||
ContentFit, Length,
|
ContentFit, Length,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
|
|
@ -14,6 +14,11 @@ use std::{
|
||||||
path::Path, path::PathBuf,
|
path::Path, path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub enum Handle {
|
||||||
|
Image(image::Handle),
|
||||||
|
Svg(svg::Handle),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
pub enum IconSource<'a> {
|
pub enum IconSource<'a> {
|
||||||
Path(Cow<'a, Path>),
|
Path(Cow<'a, Path>),
|
||||||
|
|
@ -21,6 +26,60 @@ pub enum IconSource<'a> {
|
||||||
Embedded(Image),
|
Embedded(Image),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> IconSource<'a> {
|
||||||
|
/// Loads the icon as either an image or svg [`Handle`].
|
||||||
|
#[must_use]
|
||||||
|
pub fn load(&self, size: u16, theme: Option<&str>, svg: bool) -> Handle {
|
||||||
|
let name_path_buffer: Option<PathBuf>;
|
||||||
|
let icon: Option<&Path> = match self {
|
||||||
|
IconSource::Path(path) => Some(path),
|
||||||
|
IconSource::Name(name) => {
|
||||||
|
let icon = crate::settings::DEFAULT_ICON_THEME.with(|default_theme| {
|
||||||
|
let default_theme: &str = &default_theme.borrow();
|
||||||
|
freedesktop_icons::lookup(name)
|
||||||
|
.with_size(size)
|
||||||
|
.with_theme(theme.unwrap_or(default_theme))
|
||||||
|
.with_cache()
|
||||||
|
.find()
|
||||||
|
});
|
||||||
|
|
||||||
|
name_path_buffer = if icon.is_none() {
|
||||||
|
freedesktop_icons::lookup(name)
|
||||||
|
.with_size(size)
|
||||||
|
.with_cache()
|
||||||
|
.find()
|
||||||
|
} else {
|
||||||
|
icon
|
||||||
|
};
|
||||||
|
|
||||||
|
name_path_buffer.as_deref()
|
||||||
|
}
|
||||||
|
IconSource::Embedded(_) => unimplemented!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let is_svg = svg
|
||||||
|
|| icon
|
||||||
|
.as_ref()
|
||||||
|
.map_or(true, |path| path.extension() == Some(OsStr::new("svg")));
|
||||||
|
|
||||||
|
if is_svg {
|
||||||
|
let handle = if let Some(path) = icon {
|
||||||
|
svg::Handle::from_path(path)
|
||||||
|
} else {
|
||||||
|
eprintln!("svg icon '{:?}' size {} not found", self, size);
|
||||||
|
svg::Handle::from_memory(Vec::new())
|
||||||
|
};
|
||||||
|
|
||||||
|
Handle::Svg(handle)
|
||||||
|
} else if let Some(icon) = icon {
|
||||||
|
Handle::Image(icon.into())
|
||||||
|
} else {
|
||||||
|
eprintln!("icon '{:?}' size {} not found", self, size);
|
||||||
|
Handle::Image(image::Handle::from_memory(Vec::new()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> From<Cow<'a, Path>> for IconSource<'a> {
|
impl<'a> From<Cow<'a, Path>> for IconSource<'a> {
|
||||||
fn from(value: Cow<'a, Path>) -> Self {
|
fn from(value: Cow<'a, Path>) -> Self {
|
||||||
Self::Path(value)
|
Self::Path(value)
|
||||||
|
|
@ -67,7 +126,7 @@ impl<'a> From<Image> for IconSource<'a> {
|
||||||
#[derive(Hash, Setters)]
|
#[derive(Hash, Setters)]
|
||||||
pub struct Icon<'a> {
|
pub struct Icon<'a> {
|
||||||
#[setters(skip)]
|
#[setters(skip)]
|
||||||
name: IconSource<'a>,
|
source: IconSource<'a>,
|
||||||
#[setters(strip_option, into)]
|
#[setters(strip_option, into)]
|
||||||
theme: Option<Cow<'a, str>>,
|
theme: Option<Cow<'a, str>>,
|
||||||
style: crate::theme::Svg,
|
style: crate::theme::Svg,
|
||||||
|
|
@ -83,11 +142,11 @@ pub struct Icon<'a> {
|
||||||
|
|
||||||
/// A lazily-generated icon.
|
/// A lazily-generated icon.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn icon<'a>(name: impl Into<IconSource<'a>>, size: u16) -> Icon<'a> {
|
pub fn icon<'a>(source: impl Into<IconSource<'a>>, size: u16) -> Icon<'a> {
|
||||||
Icon {
|
Icon {
|
||||||
content_fit: None,
|
content_fit: None,
|
||||||
height: None,
|
height: None,
|
||||||
name: name.into(),
|
source: source.into(),
|
||||||
size,
|
size,
|
||||||
style: crate::theme::Svg::default(),
|
style: crate::theme::Svg::default(),
|
||||||
theme: None,
|
theme: None,
|
||||||
|
|
@ -99,7 +158,7 @@ pub fn icon<'a>(name: impl Into<IconSource<'a>>, size: u16) -> Icon<'a> {
|
||||||
impl<'a> Icon<'a> {
|
impl<'a> Icon<'a> {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn into_element<Message: 'static>(self) -> Element<'a, Message> {
|
fn into_element<Message: 'static>(self) -> Element<'a, Message> {
|
||||||
if let IconSource::Embedded(mut image) = self.name {
|
if let IconSource::Embedded(mut image) = self.source {
|
||||||
image = image
|
image = image
|
||||||
.width(self.width.unwrap_or(Length::Units(self.size)))
|
.width(self.width.unwrap_or(Length::Units(self.size)))
|
||||||
.height(self.height.unwrap_or(Length::Units(self.size)));
|
.height(self.height.unwrap_or(Length::Units(self.size)));
|
||||||
|
|
@ -119,65 +178,31 @@ impl<'a> Icon<'a> {
|
||||||
let hash = hasher.finish();
|
let hash = hasher.finish();
|
||||||
|
|
||||||
iced_lazy::lazy(hash, move || -> Element<Message> {
|
iced_lazy::lazy(hash, move || -> Element<Message> {
|
||||||
let name_path_buffer: Option<PathBuf>;
|
match self
|
||||||
let icon: Option<&Path> = match &self.name {
|
.source
|
||||||
IconSource::Path(path) => Some(path),
|
.load(self.size, self.theme.as_deref(), self.force_svg)
|
||||||
IconSource::Name(name) => {
|
{
|
||||||
let icon = crate::settings::DEFAULT_ICON_THEME.with(|default_theme| {
|
Handle::Svg(handle) => {
|
||||||
let default_theme: &str = &default_theme.borrow();
|
let mut widget = svg::Svg::<Renderer>::new(handle)
|
||||||
freedesktop_icons::lookup(name)
|
.style(self.style)
|
||||||
.with_size(self.size)
|
.width(self.width.unwrap_or(Length::Units(self.size)))
|
||||||
.with_theme(self.theme.as_deref().unwrap_or(default_theme))
|
.height(self.height.unwrap_or(Length::Units(self.size)));
|
||||||
.with_cache()
|
|
||||||
.find()
|
|
||||||
});
|
|
||||||
|
|
||||||
name_path_buffer = if icon.is_none() {
|
if let Some(content_fit) = self.content_fit {
|
||||||
freedesktop_icons::lookup(name)
|
widget = widget.content_fit(content_fit);
|
||||||
.with_size(self.size)
|
}
|
||||||
.with_cache()
|
|
||||||
.find()
|
|
||||||
} else {
|
|
||||||
icon
|
|
||||||
};
|
|
||||||
|
|
||||||
name_path_buffer.as_deref()
|
widget.into()
|
||||||
}
|
}
|
||||||
IconSource::Embedded(_) => unimplemented!(),
|
Handle::Image(handle) => {
|
||||||
};
|
let mut image = Image::new(handle)
|
||||||
|
.width(self.width.unwrap_or(Length::Units(self.size)))
|
||||||
let is_svg = self.force_svg
|
.height(self.height.unwrap_or(Length::Units(self.size)));
|
||||||
|| icon
|
if let Some(content_fit) = self.content_fit {
|
||||||
.as_ref()
|
image = image.content_fit(content_fit);
|
||||||
.map_or(true, |path| path.extension() == Some(OsStr::new("svg")));
|
}
|
||||||
|
image.into()
|
||||||
if is_svg {
|
|
||||||
let handle = if let Some(path) = icon {
|
|
||||||
svg::Handle::from_path(path)
|
|
||||||
} else {
|
|
||||||
eprintln!("icon '{:?}' size {} not found", &self.name, self.size);
|
|
||||||
svg::Handle::from_memory(Vec::new())
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut widget = svg::Svg::<Renderer>::new(handle)
|
|
||||||
.style(self.style)
|
|
||||||
.width(self.width.unwrap_or(Length::Units(self.size)))
|
|
||||||
.height(self.height.unwrap_or(Length::Units(self.size)));
|
|
||||||
|
|
||||||
if let Some(content_fit) = self.content_fit {
|
|
||||||
widget = widget.content_fit(content_fit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.into()
|
|
||||||
} else {
|
|
||||||
let icon_path = icon.unwrap();
|
|
||||||
let mut image = Image::new(icon_path)
|
|
||||||
.width(self.width.unwrap_or(Length::Units(self.size)))
|
|
||||||
.height(self.height.unwrap_or(Length::Units(self.size)));
|
|
||||||
if let Some(content_fit) = self.content_fit {
|
|
||||||
image = image.content_fit(content_fit);
|
|
||||||
}
|
|
||||||
image.into()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.into()
|
.into()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue