Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe: Use objc2 and its framework crates #4395

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 65 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions crates/eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,19 @@ wgpu = { workspace = true, optional = true, features = [

# mac:
[target.'cfg(any(target_os = "macos"))'.dependencies]
cocoa = "0.25.0"
objc = "0.2.7"
objc2 = "0.5.1"
objc2-foundation = { version = "0.2.0", features = [
"block2",
"NSData",
"NSString",
] }
objc2-app-kit = { version = "0.2.0", features = [
"NSApplication",
"NSImage",
"NSMenu",
"NSMenuItem",
"NSResponder",
] }

# windows:
[target.'cfg(any(target_os = "windows"))'.dependencies]
Expand Down
43 changes: 19 additions & 24 deletions crates/eframe/src/native/app_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,9 @@ fn set_title_and_icon_mac(title: &str, icon_data: Option<&IconData>) -> AppIconS
use crate::icon_data::IconDataExt as _;
crate::profile_function!();

use cocoa::{
appkit::{NSApp, NSApplication, NSImage, NSMenu, NSWindow},
base::{id, nil},
foundation::{NSData, NSString},
};
use objc::{msg_send, sel, sel_impl};
use objc2::ClassType;
use objc2_app_kit::{NSApplication, NSImage};
use objc2_foundation::{NSData, NSString};

let png_bytes = if let Some(icon_data) = icon_data {
match icon_data.to_png_bytes() {
Expand All @@ -222,38 +219,36 @@ fn set_title_and_icon_mac(title: &str, icon_data: Option<&IconData>) -> AppIconS
None
};

// SAFETY: Accessing raw data from icon in a read-only manner. Icon data is static!
// TODO: Move this into `objc2-app-kit`
extern "C" {
static NSApp: Option<&'static NSApplication>;
}

unsafe {
let app = NSApp();
if app.is_null() {
let app = if let Some(app) = NSApp {
app
} else {
log::debug!("NSApp is null");
return AppIconStatus::NotSetIgnored;
}
};

if let Some(png_bytes) = png_bytes {
let data = NSData::dataWithBytes_length_(
nil,
png_bytes.as_ptr().cast::<std::ffi::c_void>(),
png_bytes.len() as u64,
);
let data = NSData::from_vec(png_bytes);

log::trace!("NSImage::initWithData…");
let app_icon = NSImage::initWithData_(NSImage::alloc(nil), data);
let app_icon = NSImage::initWithData(NSImage::alloc(), &data);

crate::profile_scope!("setApplicationIconImage_");
log::trace!("setApplicationIconImage…");
app.setApplicationIconImage_(app_icon);
app.setApplicationIconImage(app_icon.as_deref());
}

// Change the title in the top bar - for python processes this would be again "python" otherwise.
let main_menu = app.mainMenu();
if !main_menu.is_null() {
let item = main_menu.itemAtIndex_(0);
if !item.is_null() {
let app_menu: id = msg_send![item, submenu];
if !app_menu.is_null() {
if let Some(main_menu) = app.mainMenu() {
if let Some(item) = main_menu.itemAtIndex(0) {
if let Some(app_menu) = item.submenu() {
crate::profile_scope!("setTitle_");
app_menu.setTitle_(NSString::alloc(nil).init_str(title));
app_menu.setTitle(&NSString::from_str(title));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ skip = [
skip-tree = [
{ name = "criterion" }, # dev-dependency
{ name = "fastrand" }, # old version via accesskit_unix
{ name = "foreign-types" }, # small crate. Old version via cocoa and core-graphics (winit).
{ name = "foreign-types" }, # small crate. Old version via core-graphics (winit).
{ name = "objc2" }, # old version via accesskit_macos
{ name = "polling" }, # old version via accesskit_unix
{ name = "rfd" }, # example dependency
Expand Down
Loading