From 0675b917f2beeed3e6b626dad8fe34b8063d9c83 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 25 Feb 2024 17:13:43 +0000 Subject: [PATCH] fix(tray): icons ignoring scaling --- src/image/provider.rs | 13 ++++++++----- src/modules/tray/icon.rs | 13 ++++++++----- src/modules/tray/mod.rs | 3 +-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/image/provider.rs b/src/image/provider.rs index a23debf..1f3b646 100644 --- a/src/image/provider.rs +++ b/src/image/provider.rs @@ -171,7 +171,7 @@ impl<'a> ImageProvider<'a> { ); // Different error types makes this a bit awkward - match pixbuf.map(|pixbuf| Self::create_and_load_surface(&pixbuf, &image, scale)) + match pixbuf.map(|pixbuf| Self::create_and_load_surface(&pixbuf, &image)) { Ok(Err(err)) => error!("{err:?}"), Err(err) => error!("{err:?}"), @@ -202,7 +202,7 @@ impl<'a> ImageProvider<'a> { _ => unreachable!(), // handled above }?; - Self::create_and_load_surface(&pixbuf, image, scale) + Self::create_and_load_surface(&pixbuf, image) } /// Attempts to create a Cairo surface from the provided `Pixbuf`, @@ -210,10 +210,13 @@ impl<'a> ImageProvider<'a> { /// The surface is then loaded into the provided image. /// /// This is necessary for HiDPI since `Pixbuf`s are always treated as scale factor 1. - fn create_and_load_surface(pixbuf: &Pixbuf, image: >k::Image, scale: i32) -> Result<()> { + pub fn create_and_load_surface(pixbuf: &Pixbuf, image: >k::Image) -> Result<()> { let surface = unsafe { - let ptr = - gdk_cairo_surface_create_from_pixbuf(pixbuf.as_ptr(), scale, std::ptr::null_mut()); + let ptr = gdk_cairo_surface_create_from_pixbuf( + pixbuf.as_ptr(), + image.scale_factor(), + std::ptr::null_mut(), + ); Surface::from_raw_full(ptr) }?; diff --git a/src/modules/tray/icon.rs b/src/modules/tray/icon.rs index 3592941..7cf442e 100644 --- a/src/modules/tray/icon.rs +++ b/src/modules/tray/icon.rs @@ -39,8 +39,7 @@ fn get_icon_theme_search_paths(icon_theme: &IconTheme) -> HashSet { } pub fn get_image(item: &StatusNotifierItem, icon_theme: &IconTheme, size: u32) -> Result { - get_image_from_icon_name(item, icon_theme, size) - .or_else(|_| get_image_from_pixmap(item, size)) + get_image_from_icon_name(item, icon_theme, size).or_else(|_| get_image_from_pixmap(item, size)) } /// Attempts to get a GTK `Image` component @@ -77,10 +76,11 @@ fn get_image_from_pixmap(item: &StatusNotifierItem, size: u32) -> Result let pixmap = item .icon_pixmap .as_ref() - .and_then(|pixmap| pixmap.first())?; + .and_then(|pixmap| pixmap.first()) + .ok_or_else(|| Report::msg("Failed to get pixmap from tray icon"))?; let bytes = glib::Bytes::from(&pixmap.pixels); - let row_stride = pixmap.width * 4; // + let row_stride = pixmap.width * 4; let pixbuf = gdk_pixbuf::Pixbuf::from_bytes( &bytes, @@ -95,5 +95,8 @@ fn get_image_from_pixmap(item: &StatusNotifierItem, size: u32) -> Result let pixbuf = pixbuf .scale_simple(size as i32, size as i32, InterpType::Bilinear) .unwrap_or(pixbuf); - Some(Image::from_pixbuf(Some(&pixbuf))) + + let image = Image::new(); + ImageProvider::create_and_load_surface(&pixbuf, &image)?; + Ok(image) } diff --git a/src/modules/tray/mod.rs b/src/modules/tray/mod.rs index d64654a..d0f85e4 100644 --- a/src/modules/tray/mod.rs +++ b/src/modules/tray/mod.rs @@ -157,8 +157,7 @@ fn on_update( } if item.icon_name.as_ref() != menu_item.icon_name() { - match icon::get_image(&item, icon_theme, icon_size) - { + match icon::get_image(&item, icon_theme, icon_size) { Ok(image) => menu_item.set_image(&image), Err(_) => menu_item.set_label(label), };