tray-item-rs
tray-item-rs copied to clipboard
MacOS: Unable to load icon
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.
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?
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");
Could also probably add a new wrapper in tray-item that uses -[NSImage initWithContentsOfFile:]
Using initWithContentsOfFile_()
with absolute path helped me :) Thanks
Can you elaborate on this? I am also being confused by this issue
@0tabek16 can you provide a full example of how you've managed to use this with .set_icon()
?
@oren0e Maybe you can see my MessAuto
Thanks! looks good. I also wanted to disable the icon from the taskbar (=dock), but it seems this is only possible in Linux.
@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(())
}```
I didn't have time to try it yet but in case @0tabek16 confirm this has been solve so I will close it.