From dbbfd97c615ba0eec582b484e06608b87f34ef7a Mon Sep 17 00:00:00 2001 From: GyDi Date: Sun, 25 Sep 2022 20:16:45 +0800 Subject: [PATCH] feat(macos): support to change tray icon aspect ratio, close #564 (#565) * feat(macos): support to change tray icon aspect ratio, close #564 * chore: add changes file * fix(macos): use icon aspect ratio * chore: changes file --- .changes/macos-tray-icon-aspect-ratio.md | 5 +++++ src/platform_impl/macos/icon.rs | 4 ++++ src/platform_impl/macos/system_tray.rs | 9 ++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 .changes/macos-tray-icon-aspect-ratio.md diff --git a/.changes/macos-tray-icon-aspect-ratio.md b/.changes/macos-tray-icon-aspect-ratio.md new file mode 100644 index 000000000..3e3ac8cb1 --- /dev/null +++ b/.changes/macos-tray-icon-aspect-ratio.md @@ -0,0 +1,5 @@ +--- +"tao": "patch" +--- + +Scale the tray icon according to its aspect ratio on macOS. diff --git a/src/platform_impl/macos/icon.rs b/src/platform_impl/macos/icon.rs index 5febfc6e3..8cd31f3d5 100644 --- a/src/platform_impl/macos/icon.rs +++ b/src/platform_impl/macos/icon.rs @@ -13,6 +13,10 @@ impl PlatformIcon { Ok(PlatformIcon(RgbaIcon::from_rgba(rgba, width, height)?)) } + pub fn get_size(&self) -> (u32, u32) { + (self.0.width, self.0.height) + } + pub fn to_png(&self) -> Vec { let mut png = Vec::new(); diff --git a/src/platform_impl/macos/system_tray.rs b/src/platform_impl/macos/system_tray.rs index c51763e0a..1bfb4239e 100644 --- a/src/platform_impl/macos/system_tray.rs +++ b/src/platform_impl/macos/system_tray.rs @@ -166,13 +166,16 @@ impl SystemTray { } fn create_button_with_icon(&self) { - const ICON_WIDTH: f64 = 18.0; - const ICON_HEIGHT: f64 = 18.0; // The image is to the right of the title https://developer.apple.com/documentation/appkit/nscellimageposition/nsimageleft const NSIMAGE_LEFT: i32 = 2; let icon = self.icon.inner.to_png(); + let (width, height) = self.icon.inner.get_size(); + + let icon_height: f64 = 18.0; + let icon_width: f64 = (width as f64) / (height as f64 / icon_height); + unsafe { let status_item = self.ns_status_bar; let button = status_item.button(); @@ -185,7 +188,7 @@ impl SystemTray { ); let nsimage = NSImage::initWithData_(NSImage::alloc(nil), nsdata); - let new_size = NSSize::new(ICON_WIDTH, ICON_HEIGHT); + let new_size = NSSize::new(icon_width, icon_height); button.setImage_(nsimage); let _: () = msg_send![nsimage, setSize: new_size];