tray-item-rs icon indicating copy to clipboard operation
tray-item-rs copied to clipboard

MacOS: Unable to load icon

Open katopz opened this issue 3 years ago • 4 comments

    pub fn set_icon(&mut self, icon: &str) -> Result<(), TIError> {
        unsafe {
            let icon_name = NSString::alloc(nil).init_str(icon);
            self.icon = Some(NSImage::initWithContentsOfFile_(
                NSImage::alloc(nil),
                icon_name,
            ));
        }
        Ok(())
    }

I try to modify set_icon and use full path instead because I didn't know how imageNamed_ consume icon there. Anyway I didn't have much luck, even program didn't start properly when i changed.

Any hint is welcome.

katopz avatar Oct 25 '21 10:10 katopz

Found out that icon can be loaded using NSImage syntax, like tray.set_icon('NSImageNameAddTemplate')

But couldn't figure out a way to use custom PNGs

@Absolucy can you help with that?

0tabek16 avatar Feb 13 '22 20:02 0tabek16

I believe it needs to be a png file in the .app bundle the executable is located in.

example: Xenon.app/Contents/Resources/xenon-tray.png,

let mut tray = TrayItem::new("Xenon Client", "xenon-tray").unwrap();
let inner = tray.inner_mut();
inner
	.set_icon_template("xenon-tray")
	.expect("failed to set icon");

Absolucy avatar Feb 13 '22 20:02 Absolucy

Could also probably add a new wrapper in tray-item that uses -[NSImage initWithContentsOfFile:]

Absolucy avatar Feb 13 '22 20:02 Absolucy

Using initWithContentsOfFile_() with absolute path helped me :) Thanks

0tabek16 avatar Feb 14 '22 07:02 0tabek16

Can you elaborate on this? I am also being confused by this issue

LeeeSe avatar Dec 14 '22 12:12 LeeeSe

@0tabek16 can you provide a full example of how you've managed to use this with .set_icon()?

oren0e avatar Jul 20 '23 17:07 oren0e

@oren0e Maybe you can see my MessAuto

LeeeSe avatar Jul 20 '23 19:07 LeeeSe

Thanks! looks good. I also wanted to disable the icon from the taskbar (=dock), but it seems this is only possible in Linux.

oren0e avatar Jul 20 '23 19:07 oren0e

@oren0e sorry for late response, AFAIK I used absolute path

    pub fn set_icon(&mut self, icon: &str) -> Result<(), TIError> {
        unsafe {
            let icon_name = NSString::alloc(nil).init_str(icon);
            self.icon = Some(NSImage::initWithContentsOfFile_(
                NSImage::alloc(nil),
                icon_name, // <- absolute path here
            ));
        }
        Ok(())
    }```

0tabek16 avatar Jul 20 '23 20:07 0tabek16

I didn't have time to try it yet but in case @0tabek16 confirm this has been solve so I will close it.

katopz avatar Jul 21 '23 02:07 katopz