olback / tray-item-rs

Multi-platform Tray Indicator
https://crates.io/crates/tray-item
MIT License
282 stars 39 forks source link

Linux: unable to load icon via GResource #6

Open njust opened 3 years ago

njust commented 3 years ago

Hi,

I saw in your comment here that icons on linux can be loaded via gresources. I tried but unfortunately it shows only a black box: image

I tried to load the icon resource via gio::resources_lookup_data, which is successful. So it seems the resource itself is correctly packed and loaded.

use tray_item::TrayItem;
use gtk;
use gio::ResourceLookupFlags;

fn main() {
    gtk::init().unwrap();

    let res_bytes = include_bytes!("../resources.gresource");
    let data = glib::Bytes::from(&res_bytes[..]);
    let resources = gio::Resource::from_data(&data).unwrap();
    gio::resources_register(&resources);

    // let svg = gio::resources_lookup_data("/org/gtk/test/assets/test.svg", ResourceLookupFlags::all()).expect("Failed to load svg");
    // println!("Svg size: {}", svg.len());
    let png = gio::resources_lookup_data("/org/gtk/test/assets/test.png", ResourceLookupFlags::all()).expect("Failed to load png");
    println!("Png size: {}", png.len());

    let mut tray = TrayItem::new("Tray Example", "/org/gtk/test/assets/test.png").unwrap();
    tray.set_icon("/org/gtk/test/assets/test.png");
    // let mut tray = TrayItem::new("Tray Example", "/org/gtk/test/assets/test.png").unwrap();
    // let mut tray = TrayItem::new("Tray Example", "accessories-calculator").unwrap();

    tray.add_label("Tray Label").unwrap();

    tray.add_menu_item("Hello", || {
        println!("Hello!");
    }).unwrap();

    tray.add_menu_item("Quit", || {
        gtk::main_quit();
    }).unwrap();

    gtk::main();

}

Any idea what's missing? Full example is here: https://github.com/njust/tray_test.

olback commented 3 years ago

I've been trying to figure this out for a few hours now and I can't get it to work. After some searching around I found this Reddit thread which suggests that AppIndicator is not loading icons from resources.

I'm planning to move away from AppIndicator anyway in favor of ksni (#2).

njust commented 3 years ago

Thanks for looking into this. Not sure how to load icons from a gresource file via ksni, but I got a working example where I can load a tray icon which I included into the binary in the example here. Would be nice if the TrayItem::new and TrayItem::set_icon would allow to handle different sources for tray icons like this:

enum IconSource {
    Resource(String),
    Path(String),
    Data(Vec<u8>),
}
pub fn new(title: &str, icon: IconSource) -> Result<Self, TIError>{
..
}
pub fn set_icon(&mut self, icon: IconSource) -> Result<(), TIError> {
..
}