Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux: unable to load icon via GResource #6

Open
njust opened this issue May 20, 2021 · 2 comments
Open

Linux: unable to load icon via GResource #6

njust opened this issue May 20, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@njust
Copy link
Contributor

njust commented May 20, 2021

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 olback added the bug Something isn't working label May 27, 2021
@olback olback self-assigned this May 27, 2021
@olback
Copy link
Owner

olback commented May 27, 2021

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
Copy link
Contributor Author

njust commented Jun 5, 2021

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> {
..
}

@njust njust mentioned this issue Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants