Skip to content

Commit

Permalink
feat(macos): support to change tray icon aspect ratio, close #564 (#565)
Browse files Browse the repository at this point in the history
* feat(macos): support to change tray icon aspect ratio, close #564

* chore: add changes file

* fix(macos): use icon aspect ratio

* chore: changes file
  • Loading branch information
zzzgydi committed Sep 25, 2022
1 parent 9aa7e52 commit dbbfd97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-tray-icon-aspect-ratio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Scale the tray icon according to its aspect ratio on macOS.
4 changes: 4 additions & 0 deletions src/platform_impl/macos/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
let mut png = Vec::new();

Expand Down
9 changes: 6 additions & 3 deletions src/platform_impl/macos/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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];
Expand Down

0 comments on commit dbbfd97

Please sign in to comment.