Skip to content

Commit

Permalink
Merge pull request #782 from JakeStanger/fix/tray-fixes2
Browse files Browse the repository at this point in the history
More tray fixes
  • Loading branch information
JakeStanger authored Nov 17, 2024
2 parents 42e25f5 + f364bb6 commit bdf6b3b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 107 deletions.
66 changes: 1 addition & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ notifications = ["zbus"]

sys_info = ["sysinfo", "regex"]

tray = ["system-tray", "png"]
tray = ["system-tray"]

upower = ["upower_dbus", "zbus", "futures-lite"]

Expand Down Expand Up @@ -149,7 +149,6 @@ sysinfo = { version = "0.29.11", optional = true }

# tray
system-tray = { version = "0.4.0", features = ["dbusmenu-gtk3"], optional = true }
png = { version = "0.17.14", optional = true }

# upower
upower_dbus = { version = "0.3.2", optional = true }
Expand Down
34 changes: 0 additions & 34 deletions src/modules/tray/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use gtk::ffi::gtk_icon_theme_get_search_path;
use gtk::gdk_pixbuf::{Colorspace, InterpType, Pixbuf};
use gtk::prelude::IconThemeExt;
use gtk::{IconLookupFlags, IconTheme, Image};
use png::ColorType;
use std::collections::HashSet;
use std::ffi::CStr;
use std::os::raw::{c_char, c_int};
Expand Down Expand Up @@ -126,36 +125,3 @@ fn get_image_from_pixmap(item: &TrayMenu, size: u32) -> Result<Image> {
ImageProvider::create_and_load_surface(&pixbuf, &image)?;
Ok(image)
}

pub struct PngData<'a>(pub &'a [u8]);
impl TryFrom<PngData<'_>> for Image {
type Error = Report;

fn try_from(value: PngData) -> std::result::Result<Self, Self::Error> {
let data = value.0;

let decoder = png::Decoder::new(data);
let mut reader = decoder.read_info()?;
let mut buf = vec![0; reader.output_buffer_size()];

let info = reader.next_frame(&mut buf)?;
let bytes = glib::Bytes::from(&buf[..info.buffer_size()]);

let has_alpha = matches!(info.color_type, ColorType::Rgba | ColorType::GrayscaleAlpha);
let row_stride_multiplier = if has_alpha { 4 } else { 3 };

let pixbuf = Pixbuf::from_bytes(
&bytes,
Colorspace::Rgb,
has_alpha,
info.bit_depth as i32,
info.width as i32,
info.height as i32,
(info.width * row_stride_multiplier) as i32,
);

let image = Image::new();
ImageProvider::create_and_load_surface(&pixbuf, &image)?;
Ok(image)
}
}
13 changes: 10 additions & 3 deletions src/modules/tray/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use system_tray::item::{IconPixmap, StatusNotifierItem, Tooltip};
/// Main tray icon to show on the bar
pub(crate) struct TrayMenu {
pub event_box: EventBox,
pub widget: MenuItem,
widget: MenuItem,
image_widget: Option<Image>,
label_widget: Option<Label>,

Expand All @@ -18,7 +18,7 @@ pub(crate) struct TrayMenu {
}

impl TrayMenu {
pub fn new(item: StatusNotifierItem) -> Self {
pub fn new(address: &str, item: StatusNotifierItem) -> Self {
let event_box = EventBox::new();

let widget = MenuItem::new();
Expand All @@ -27,7 +27,7 @@ impl TrayMenu {

event_box.show_all();

Self {
let mut slf = Self {
event_box,
widget,
image_widget: None,
Expand All @@ -36,7 +36,14 @@ impl TrayMenu {
icon_name: item.icon_name,
icon_theme_path: item.icon_theme_path,
icon_pixmap: item.icon_pixmap,
};

if let Some(menu) = item.menu {
let menu = system_tray::gtk_menu::Menu::new(address, &menu);
slf.set_menu_widget(menu);
}

slf
}

/// Updates the label text, and shows it in favour of the image.
Expand Down
6 changes: 3 additions & 3 deletions src/modules/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn on_update(
Event::Add(address, item) => {
debug!("Received new tray item at '{address}': {item:?}");

let mut menu_item = TrayMenu::new(*item);
let mut menu_item = TrayMenu::new(&address, *item);
container.pack_start(&menu_item.event_box, true, true, 0);

if let Ok(image) = icon::get_image(&menu_item, icon_theme, icon_size, prefer_icons) {
Expand All @@ -160,7 +160,7 @@ fn on_update(
menu_item.set_label(&label);
};

menu_item.widget.show();
menu_item.event_box.show();
menus.insert(address.into(), menu_item);
}
Event::Update(address, update) => {
Expand Down Expand Up @@ -210,7 +210,7 @@ fn on_update(
debug!("Removing tray item at '{address}'");

if let Some(menu) = menus.get(address.as_str()) {
container.remove(&menu.widget);
container.remove(&menu.event_box);
}
}
};
Expand Down

0 comments on commit bdf6b3b

Please sign in to comment.