Skip to content

Commit

Permalink
fix(macos): scale menu item icon height to 18, closes #584 (#590)
Browse files Browse the repository at this point in the history
* fix(macos): scale menu item icon height to 18, closes #584

* fix imports and get_size order

* changefile

Co-authored-by: amrbashir <amr.bashir2015@gmail.com>
  • Loading branch information
grant0417 and amrbashir committed Oct 15, 2022
1 parent 7c7ce8a commit 5e3d344
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-menu-item-dpi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

On macOS, scale menu item icons height to 18.
12 changes: 9 additions & 3 deletions src/platform_impl/macos/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use cocoa::{
appkit::{NSApp, NSApplication, NSButton, NSEventModifierFlags, NSImage, NSMenu, NSMenuItem},
base::{id, nil, selector},
foundation::{NSAutoreleasePool, NSData, NSString},
foundation::{NSAutoreleasePool, NSData, NSSize, NSString},
};
use objc::{
declare::ClassDecl,
Expand Down Expand Up @@ -85,16 +85,22 @@ impl MenuItemAttributes {
}

pub fn set_icon(&mut self, icon: Icon) {
unsafe {
let icon = icon.inner.to_png();
let (width, height) = icon.inner.get_size();
let icon = icon.inner.to_png();

let icon_height: f64 = 18.0;
let icon_width: f64 = (width as f64) / (height as f64 / icon_height);

unsafe {
let nsdata = NSData::dataWithBytes_length_(
nil,
icon.as_ptr() as *const std::os::raw::c_void,
icon.len() as u64,
);

let nsimage = NSImage::initWithData_(NSImage::alloc(nil), nsdata);
let new_size = NSSize::new(icon_width, icon_height);
let _: () = msg_send![nsimage, setSize: new_size];
let _: () = msg_send![self.1, setImage: nsimage];
}
}
Expand Down

0 comments on commit 5e3d344

Please sign in to comment.