1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 18:51:04 +02:00

fix(image): using fallback in places it shouldn't

This commit is contained in:
Jake Stanger 2023-07-26 21:49:45 +01:00
parent 1c68a97d33
commit 2367faab04
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
7 changed files with 27 additions and 15 deletions

View file

@ -12,7 +12,7 @@ pub fn new_icon_button(input: &str, icon_theme: &IconTheme, size: i32) -> Button
image.add_class("image"); image.add_class("image");
image.add_class("icon"); image.add_class("icon");
match ImageProvider::parse(input, icon_theme, size) match ImageProvider::parse(input, icon_theme, false, size)
.map(|provider| provider.load_into_image(image.clone())) .map(|provider| provider.load_into_image(image.clone()))
{ {
Some(_) => { Some(_) => {
@ -41,7 +41,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
container.add(&image); container.add(&image);
ImageProvider::parse(input, icon_theme, size) ImageProvider::parse(input, icon_theme, false, size)
.map(|provider| provider.load_into_image(image)); .map(|provider| provider.load_into_image(image));
} else { } else {
let label = Label::new(Some(input)); let label = Label::new(Some(input));

View file

@ -41,8 +41,9 @@ impl<'a> ImageProvider<'a> {
/// ///
/// Note this checks that icons exist in theme, or files exist on disk /// Note this checks that icons exist in theme, or files exist on disk
/// but no other check is performed. /// but no other check is performed.
pub fn parse(input: &str, theme: &'a IconTheme, size: i32) -> Option<Self> { pub fn parse(input: &str, theme: &'a IconTheme, use_fallback: bool, size: i32) -> Option<Self> {
let location = Self::get_location(input, theme, size, 0)?; let location = Self::get_location(input, theme, size, use_fallback, 0)?;
Some(Self { location, size }) Some(Self { location, size })
} }
@ -61,8 +62,19 @@ impl<'a> ImageProvider<'a> {
input: &str, input: &str,
theme: &'a IconTheme, theme: &'a IconTheme,
size: i32, size: i32,
use_fallback: bool,
recurse_depth: usize, recurse_depth: usize,
) -> Option<ImageLocation<'a>> { ) -> Option<ImageLocation<'a>> {
macro_rules! fallback {
() => {
if use_fallback {
Some(Self::get_fallback_icon(theme))
} else {
None
}
};
}
const MAX_RECURSE_DEPTH: usize = 2; const MAX_RECURSE_DEPTH: usize = 2;
let (input_type, input_name) = input let (input_type, input_name) = input
@ -99,20 +111,20 @@ impl<'a> ImageProvider<'a> {
Report::msg(format!("Unsupported image type: {input_type}")) Report::msg(format!("Unsupported image type: {input_type}"))
.note("You may need to recompile with support if available") .note("You may need to recompile with support if available")
); );
None fallback!()
} }
None if PathBuf::from(input_name).is_file() => { None if PathBuf::from(input_name).is_file() => {
Some(ImageLocation::Local(PathBuf::from(input_name))) Some(ImageLocation::Local(PathBuf::from(input_name)))
} }
None if recurse_depth == MAX_RECURSE_DEPTH => Some(Self::get_fallback_icon(theme)), None if recurse_depth == MAX_RECURSE_DEPTH => fallback!(),
None => { None => {
if let Some(location) = get_desktop_icon_name(input_name) if let Some(location) = get_desktop_icon_name(input_name).map(|input| {
.map(|input| Self::get_location(&input, theme, size, recurse_depth + 1)) Self::get_location(&input, theme, size, use_fallback, recurse_depth + 1)
{ }) {
location location
} else { } else {
warn!("Failed to find image: {input}"); warn!("Failed to find image: {input}");
Some(Self::get_fallback_icon(theme)) fallback!()
} }
} }
} }

View file

@ -32,7 +32,7 @@ impl CustomWidget for ImageWidget {
let icon_theme = context.icon_theme.clone(); let icon_theme = context.icon_theme.clone();
dynamic_string(&self.src, move |src| { dynamic_string(&self.src, move |src| {
ImageProvider::parse(&src, &icon_theme, self.size) ImageProvider::parse(&src, &icon_theme, false, self.size)
.map(|image| image.load_into_image(gtk_image.clone())); .map(|image| image.load_into_image(gtk_image.clone()));
Continue(true) Continue(true)

View file

@ -128,7 +128,7 @@ impl Module<gtk::Box> for FocusedModule {
let icon_theme = icon_theme.clone(); let icon_theme = icon_theme.clone();
context.widget_rx.attach(None, move |(name, id)| { context.widget_rx.attach(None, move |(name, id)| {
if self.show_icon { if self.show_icon {
match ImageProvider::parse(&id, &icon_theme, self.icon_size) match ImageProvider::parse(&id, &icon_theme, true, self.icon_size)
.map(|image| image.load_into_image(icon.clone())) .map(|image| image.load_into_image(icon.clone()))
{ {
Some(Ok(_)) => icon.show(), Some(Ok(_)) => icon.show(),

View file

@ -191,7 +191,7 @@ impl ItemButton {
if appearance.show_icons { if appearance.show_icons {
let gtk_image = gtk::Image::new(); let gtk_image = gtk::Image::new();
let image = let image =
ImageProvider::parse(&item.app_id.clone(), icon_theme, appearance.icon_size); ImageProvider::parse(&item.app_id.clone(), icon_theme, true, appearance.icon_size);
if let Some(image) = image { if let Some(image) = image {
button.set_image(Some(&gtk_image)); button.set_image(Some(&gtk_image));
button.set_always_show_image(true); button.set_always_show_image(true);

View file

@ -408,7 +408,7 @@ impl Module<Button> for MusicModule {
if prev_cover != new_cover { if prev_cover != new_cover {
prev_cover = new_cover.clone(); prev_cover = new_cover.clone();
let res = if let Some(image) = new_cover.and_then(|cover_path| { let res = if let Some(image) = new_cover.and_then(|cover_path| {
ImageProvider::parse(&cover_path, &icon_theme, image_size) ImageProvider::parse(&cover_path, &icon_theme, false, image_size)
}) { }) {
image.load_into_image(album_image.clone()) image.load_into_image(album_image.clone())
} else { } else {

View file

@ -189,7 +189,7 @@ impl Module<gtk::Button> for UpowerModule {
.attach(None, move |properties: UpowerProperties| { .attach(None, move |properties: UpowerProperties| {
let format = format.replace("{percentage}", &properties.percentage.to_string()); let format = format.replace("{percentage}", &properties.percentage.to_string());
let icon_name = String::from("icon:") + &properties.icon_name; let icon_name = String::from("icon:") + &properties.icon_name;
ImageProvider::parse(&icon_name, &icon_theme, self.icon_size) ImageProvider::parse(&icon_name, &icon_theme, false, self.icon_size)
.map(|provider| provider.load_into_image(icon.clone())); .map(|provider| provider.load_into_image(icon.clone()));
label.set_markup(format.as_ref()); label.set_markup(format.as_ref());
Continue(true) Continue(true)