From 4bd4393aed94aa017d747615ab60313f85aa7dac Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 30 Apr 2024 19:52:18 +0200 Subject: [PATCH] Use objc2 This is likely slightly faster, as the message sending can be done without needing to put the view into an autorelease pool (an internal optimization). We could've used `objc2-app-kit` to avoid the manual `msg_send_id!` here, but the extra dependency seemed excessive. --- Cargo.toml | 2 +- src/lib.rs | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f265575a..4211bd8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ raw-window-handle-0-5 = { package = "raw-window-handle", version = "0.5.0"} raw-window-handle-0-6 = { package = "raw-window-handle", version = "0.6.0", optional = true } [target.'cfg(target_os = "macos")'.dependencies] -objc = "0.2" +objc2 = "0.5.1" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["libloaderapi"] } diff --git a/src/lib.rs b/src/lib.rs index 6f9780af..582380fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -229,9 +229,6 @@ extern crate log; extern crate bitflags; #[cfg(feature = "image")] extern crate image; -#[cfg(target_os = "macos")] -#[macro_use] -extern crate objc; #[cfg(feature = "raw-window-handle-v0-6")] extern crate raw_window_handle_0_6 as raw_window_handle; @@ -3646,15 +3643,17 @@ fn raw_window_handle(context: &C) -> RawWindowHandle { } #[cfg(target_os = "macos")] { - use std::ptr::NonNull; + use objc2::msg_send_id; + use objc2::rc::Id; + use objc2::runtime::NSObject; use raw_window_handle::AppKitWindowHandle; - let ns_view = unsafe { - let ns_window: *mut objc::runtime::Object = ffi::glfwGetCocoaWindow(context.window_ptr()) as *mut _; - let ns_view: *mut objc::runtime::Object = objc::msg_send![ns_window, contentView]; - assert_ne!(ns_view, std::ptr::null_mut()); - ns_view as *mut std::ffi::c_void - }; - let handle = AppKitWindowHandle::new(NonNull::new(ns_view).unwrap()); + use std::ptr::NonNull; + let ns_window: *mut NSObject = + unsafe { ffi::glfwGetCocoaWindow(context.window_ptr()) as *mut _ }; + let ns_view: Option> = unsafe { msg_send_id![ns_window, contentView] }; + let ns_view = ns_view.expect("failed to access contentView on GLFW NSWindow"); + let ns_view: NonNull = NonNull::from(&*ns_view); + let handle = AppKitWindowHandle::new(ns_view.cast()); RawWindowHandle::AppKit(handle) } #[cfg(target_os = "emscripten")]