Skip to content

Commit

Permalink
Merge pull request #53 from windoze/master
Browse files Browse the repository at this point in the history
Set icon image with PNG file on macOS
  • Loading branch information
olback authored Apr 7, 2024
2 parents 4f8708a + e250697 commit 606f614
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gtk = { version = "0.18", optional = true }
padlock = "0.2"

[target.'cfg(target_os="windows")'.dependencies.windows-sys]
version = "0.48.0"
version = "0.52.0"
features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
Expand Down
70 changes: 49 additions & 21 deletions src/api/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use cocoa::{
base::id,
foundation::{NSData, NSSize},
};

use {
crate::TIError,
crate::IconSource,
crate::TIError,
callback::*,
cocoa::{
appkit::{
Expand All @@ -16,6 +21,42 @@ use {

mod callback;

fn get_icon_image(icon: IconSource) -> Option<id> {
unsafe {
match icon {
IconSource::Resource(_) => {
let icon = icon.as_str();
let icon = Some(icon).filter(|icon| !icon.is_empty());
icon.map(|icon_name| {
let icon_name = NSString::alloc(nil).init_str(icon_name);
NSImage::imageNamed_(NSImage::alloc(nil), icon_name)
})
}
IconSource::Data {
height,
width,
data,
} => {
let data = NSData::dataWithBytes_length_(
nil,
data.as_ptr() as *const std::os::raw::c_void,
data.len() as u64,
);
let image = NSImage::initWithData_(NSImage::alloc(nil), data);
let new_size = if width != 0 && height != 0 {
let icon_height: f64 = 18.0;
let icon_width: f64 = (width as f64) / (height as f64 / icon_height);
NSSize::new(icon_width, icon_height)
} else {
NSSize::new(18.0, 18.0)
};
let _: () = msg_send![image, setSize: new_size];
Some(image)
}
}
}
}

pub struct TrayItemMacOS {
name: String,
menu: *mut objc::runtime::Object,
Expand All @@ -26,35 +67,22 @@ pub struct TrayItemMacOS {

impl TrayItemMacOS {
pub fn new(title: &str, icon: IconSource) -> Result<Self, TIError> {
unsafe {
let t = unsafe {
let pool = NSAutoreleasePool::new(nil);

let icon = icon.as_str();
let icon = Some(icon).filter(|icon| !icon.is_empty());
let icon = icon.map(|icon_name| {
let icon_name = NSString::alloc(nil).init_str(icon_name);
NSImage::imageNamed_(NSImage::alloc(nil), icon_name)
});

let t = TrayItemMacOS {
TrayItemMacOS {
name: title.to_string(),
_pool: pool,
icon,
icon: get_icon_image(icon),
menu: NSMenu::new(nil).autorelease(),
main_thread: None,
};

// t.display();

Ok(t)
}
}
};
Ok(t)
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct TrayItem(api::TrayItemImpl);
#[derive(Clone)]
pub enum IconSource {
Resource(&'static str),
#[cfg(all(target_os = "linux", feature = "ksni"))]
#[cfg(any(target_os = "macos", all(target_os = "linux", feature = "ksni")))]
Data {
height: i32,
width: i32,
Expand Down

0 comments on commit 606f614

Please sign in to comment.