Skip to content

Commit

Permalink
Merge pull request #257 from JakeStanger/fix/missing-icon-crash
Browse files Browse the repository at this point in the history
feat(image resolver): add fallback image
  • Loading branch information
JakeStanger authored Jul 24, 2023
2 parents 4a8c823 + ef443e6 commit 00d5606
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/image/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> ImageProvider<'a> {
/// Note this checks that icons exist in theme, or files exist on disk
/// but no other check is performed.
pub fn parse(input: &str, theme: &'a IconTheme, size: i32) -> Option<Self> {
let location = Self::get_location(input, theme, size)?;
let location = Self::get_location(input, theme, size, 0)?;
Some(Self { location, size })
}

Expand All @@ -57,7 +57,14 @@ impl<'a> ImageProvider<'a> {
|| input.starts_with("https://")
}

fn get_location(input: &str, theme: &'a IconTheme, size: i32) -> Option<ImageLocation<'a>> {
fn get_location(
input: &str,
theme: &'a IconTheme,
size: i32,
recurse_depth: usize,
) -> Option<ImageLocation<'a>> {
const MAX_RECURSE_DEPTH: usize = 2;

let (input_type, input_name) = input
.split_once(':')
.map_or((None, input), |(t, n)| (Some(t), n));
Expand Down Expand Up @@ -97,14 +104,15 @@ impl<'a> ImageProvider<'a> {
None if PathBuf::from(input_name).is_file() => {
Some(ImageLocation::Local(PathBuf::from(input_name)))
}
None if recurse_depth == MAX_RECURSE_DEPTH => Some(Self::get_fallback_icon(theme)),
None => {
if let Some(location) = get_desktop_icon_name(input_name)
.map(|input| Self::get_location(&input, theme, size))
.map(|input| Self::get_location(&input, theme, size, recurse_depth + 1))
{
location
} else {
warn!("Failed to find image: {input}");
None
Some(Self::get_fallback_icon(theme))
}
}
}
Expand Down Expand Up @@ -248,4 +256,11 @@ impl<'a> ImageProvider<'a> {
)))
}
}

fn get_fallback_icon(theme: &'a IconTheme) -> ImageLocation<'a> {
ImageLocation::Icon {
name: "dialog-question-symbolic".to_string(),
theme,
}
}
}

0 comments on commit 00d5606

Please sign in to comment.