Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui_macos: Migrate application and menu bindings to objc2 (#65932)

Kirill Bulatovd86aa6e6b9f5
diff --git a/Cargo.toml b/Cargo.toml
index 7038a790aed..4cd3f43ee03 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -740,19 +740,27 @@ objc2-ui-kit = { version = "0.3.2", default-features = false, features = [
] }
objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"NSAlert",
+ "NSAppearance",
"NSApplication",
"NSButton",
+ "NSCell",
"NSControl",
+ "NSCursor",
+ "NSDocumentController",
"NSDraggingItem",
"NSEvent",
"NSGraphics",
"NSImage",
+ "NSMenu",
+ "NSMenuItem",
"NSOpenPanel",
"NSPanel",
"NSPasteboard",
"NSResponder",
+ "NSRunningApplication",
"NSSavePanel",
"NSScreen",
+ "NSScroller",
"NSTrackingArea",
"NSView",
"NSWindow",
@@ -768,6 +776,7 @@ objc2-core-video = { version = "=0.3.2", default-features = false, features = ["
objc2-foundation = { version = "=0.3.2", default-features = false, features = [
"NSArray",
"NSAttributedString",
+ "NSBundle",
"NSCoder",
"NSData",
"NSDate",
diff --git a/crates/gpui_macos/src/gpui_macos.rs b/crates/gpui_macos/src/gpui_macos.rs
index 6314ca3da40..3d1a3efb0d9 100644
--- a/crates/gpui_macos/src/gpui_macos.rs
+++ b/crates/gpui_macos/src/gpui_macos.rs
@@ -33,7 +33,7 @@ use cocoa::{
};
use objc::runtime::{BOOL, NO, YES};
-use objc2::runtime::AnyObject;
+use objc2::{rc::Retained, runtime::AnyObject};
use objc2_foundation::NSString as Objc2NSString;
use std::{
ffi::{CStr, c_char},
@@ -144,6 +144,10 @@ unsafe impl objc::Encode for NSRange {
/// Allow NSString::alloc use here because it sets autorelease
#[allow(clippy::disallowed_methods)]
-unsafe fn ns_string(string: &str) -> id {
+fn legacy_ns_string(string: &str) -> id {
unsafe { NSString::alloc(nil).init_str(string).autorelease() }
}
+
+fn ns_string(string: &str) -> *mut AnyObject {
+ Retained::autorelease_ptr(Objc2NSString::from_str(string)).cast()
+}
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index 913c96fc2ea..8db0cb9002a 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -1,26 +1,11 @@
use crate::{
- AppleActivity, AppleDispatcher, BoolExt, MacDisplay, MacKeyboardLayout, MacKeyboardMapper,
- MacWindow, events::key_to_native, ns_string, pasteboard::Pasteboard, renderer,
+ AppleActivity, AppleDispatcher, MacDisplay, MacKeyboardLayout, MacKeyboardMapper, MacWindow,
+ events::key_to_native, ns_string, pasteboard::Pasteboard, renderer,
set_active_window_cursor_style,
};
use anyhow::{Context as _, anyhow};
use block2::RcBlock;
-use cocoa::{
- appkit::{
- NSAppearanceNameVibrantDark, NSAppearanceNameVibrantLight, NSApplication,
- NSApplicationActivationPolicy,
- NSApplicationActivationPolicy::{
- NSApplicationActivationPolicyAccessory, NSApplicationActivationPolicyRegular,
- },
- NSControl as _, NSEventModifierFlags, NSMenu, NSMenuItem, NSVisualEffectState,
- NSVisualEffectView, NSWindow,
- },
- base::{BOOL, NO, YES, id, nil, selector},
- foundation::{
- NSArray, NSAutoreleasePool, NSBundle, NSInteger, NSProcessInfo, NSString, NSUInteger, NSURL,
- },
-};
-use core_foundation::{base::CFRelease, runloop::CFRunLoopRun, string::CFStringRef};
+use core_foundation::{runloop::CFRunLoopRun, string::CFStringRef};
use ctor::ctor;
use dispatch2::DispatchQueue;
use futures::channel::oneshot;
@@ -38,21 +23,24 @@ use gpui_apple::{
};
use gpui_util::{ResultExt, new_std_command};
use itertools::Itertools;
-use objc::{
- class,
- declare::ClassDecl,
- msg_send,
- runtime::{Class, Object, Sel},
- sel, sel_impl,
+use objc2::{
+ Encode, MainThreadMarker, MainThreadOnly, class, msg_send,
+ rc::{Retained, autoreleasepool},
+ runtime::{AnyClass, AnyObject, Bool, ClassBuilder, Ivar, Sel},
+ sel,
};
-use objc2::{MainThreadMarker, runtime::AnyObject};
use objc2_app_kit::{
- NSModalResponse, NSModalResponseOK, NSOpenPanel, NSSavePanel, NSWorkspace,
- NSWorkspaceDidWakeNotification, NSWorkspaceWillSleepNotification,
+ NSAppearance, NSAppearanceNameAqua, NSAppearanceNameDarkAqua, NSAppearanceNameVibrantDark,
+ NSAppearanceNameVibrantLight, NSApplication, NSApplicationActivationPolicy,
+ NSControlStateValueOn, NSCursor, NSDocumentController, NSEventModifierFlags, NSMenu,
+ NSMenuItem, NSModalResponse, NSModalResponseOK, NSOpenPanel, NSSavePanel, NSScroller,
+ NSScrollerStyle, NSWorkspace, NSWorkspaceDidWakeNotification, NSWorkspaceWillSleepNotification,
+};
+use objc2_foundation::{
+ NSActivityOptions, NSArray, NSBundle, NSError, NSInteger, NSProcessInfo, NSString, NSURL,
+ NSUTF8StringEncoding,
};
-use objc2_foundation::NSActivityOptions;
use parking_lot::Mutex;
-use ptr::null_mut;
use semver::Version;
use std::{
cell::Cell,
@@ -68,106 +56,105 @@ use std::{
},
};
-#[allow(non_upper_case_globals)]
-const NSUTF8StringEncoding: NSUInteger = 4;
-
-const MAC_PLATFORM_IVAR: &str = "platform";
-static mut APP_CLASS: *const Class = ptr::null();
-static mut APP_DELEGATE_CLASS: *const Class = ptr::null();
+const MAC_PLATFORM_IVAR: &CStr = c"platform";
+static mut APP_CLASS: *const AnyClass = ptr::null();
+static mut APP_DELEGATE_CLASS: *const AnyClass = ptr::null();
#[ctor(unsafe)]
unsafe fn build_classes() {
unsafe {
APP_CLASS = {
- let mut decl = ClassDecl::new("GPUIApplication", class!(NSApplication)).unwrap();
+ let mut decl = ClassBuilder::new(c"GPUIApplication", class!(NSApplication))
+ .expect("failed to register GPUIApplication");
decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR);
decl.register()
}
};
unsafe {
APP_DELEGATE_CLASS = {
- let mut decl = ClassDecl::new("GPUIApplicationDelegate", class!(NSResponder)).unwrap();
+ let mut decl = ClassBuilder::new(c"GPUIApplicationDelegate", class!(NSResponder))
+ .expect("failed to register GPUIApplicationDelegate");
decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR);
decl.add_method(
sel!(applicationWillFinishLaunching:),
- will_finish_launching as extern "C" fn(&mut Object, Sel, id),
+ will_finish_launching as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(applicationDidFinishLaunching:),
- did_finish_launching as extern "C" fn(&mut Object, Sel, id),
+ did_finish_launching as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(applicationShouldHandleReopen:hasVisibleWindows:),
- should_handle_reopen as extern "C" fn(&mut Object, Sel, id, bool),
+ should_handle_reopen as extern "C-unwind" fn(_, Sel, *mut AnyObject, Bool) -> Bool,
);
decl.add_method(
sel!(applicationWillTerminate:),
- will_terminate as extern "C" fn(&mut Object, Sel, id),
+ will_terminate as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(handleGPUIMenuItem:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
// Add menu item handlers so that OS save panels have the correct key commands
decl.add_method(
sel!(cut:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(copy:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(paste:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(selectAll:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(undo:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(redo:),
- handle_menu_item as extern "C" fn(&mut Object, Sel, id),
+ handle_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(validateMenuItem:),
- validate_menu_item as extern "C" fn(&mut Object, Sel, id) -> bool,
+ validate_menu_item as extern "C-unwind" fn(_, Sel, *mut AnyObject) -> Bool,
);
decl.add_method(
sel!(menuWillOpen:),
- menu_will_open as extern "C" fn(&mut Object, Sel, id),
+ menu_will_open as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(applicationDockMenu:),
- handle_dock_menu as extern "C" fn(&mut Object, Sel, id) -> id,
+ handle_dock_menu as extern "C-unwind" fn(_, Sel, *mut AnyObject) -> *mut AnyObject,
);
decl.add_method(
sel!(application:openURLs:),
- open_urls as extern "C" fn(&mut Object, Sel, id, id),
+ open_urls as extern "C-unwind" fn(_, Sel, *mut AnyObject, *mut AnyObject),
);
decl.add_method(
sel!(onKeyboardLayoutChange:),
- on_keyboard_layout_change as extern "C" fn(&mut Object, Sel, id),
+ on_keyboard_layout_change as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(onScreenParametersChange:),
- on_screen_parameters_change as extern "C" fn(&mut Object, Sel, id),
+ on_screen_parameters_change as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(onSystemSleep:),
- on_system_sleep as extern "C" fn(&mut Object, Sel, id),
+ on_system_sleep as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.add_method(
sel!(onSystemWake:),
- on_system_wake as extern "C" fn(&mut Object, Sel, id),
+ on_system_wake as extern "C-unwind" fn(_, Sel, *mut AnyObject),
);
decl.register()
@@ -201,7 +188,7 @@ pub(crate) struct MacPlatformState {
menu_actions: Vec<Box<dyn Action>>,
open_urls: Option<Box<dyn FnMut(Vec<String>)>>,
finish_launching: Option<Box<dyn FnOnce()>>,
- dock_menu: Option<id>,
+ dock_menu: Option<Retained<NSMenu>>,
menus: Option<Vec<OwnedMenu>>,
keyboard_mapper: Rc<MacKeyboardMapper>,
/// Mirrors `[NSCursor setHiddenUntilMouseMoves:]` state, which AppKit doesn't expose.
@@ -220,8 +207,8 @@ fn activation_policy_for(request: &WindowingRequest) -> ActivationPolicy {
fn native_activation_policy(policy: ActivationPolicy) -> NSApplicationActivationPolicy {
match policy {
- ActivationPolicy::Regular => NSApplicationActivationPolicyRegular,
- ActivationPolicy::Accessory => NSApplicationActivationPolicyAccessory,
+ ActivationPolicy::Regular => NSApplicationActivationPolicy::Regular,
+ ActivationPolicy::Accessory => NSApplicationActivationPolicy::Accessory,
}
}
@@ -282,37 +269,32 @@ impl MacPlatform {
unsafe fn create_menu_bar(
&self,
menus: &Vec<Menu>,
- delegate: id,
+ delegate: *mut AnyObject,
actions: &mut Vec<Box<dyn Action>>,
keymap: &Keymap,
- ) -> id {
+ ) -> Retained<NSMenu> {
unsafe {
- let application_menu = NSMenu::new(nil).autorelease();
- application_menu.setDelegate_(delegate);
+ let application_menu = NSMenu::new(self.1);
+ let _: () = msg_send![&application_menu, setDelegate: delegate];
for menu_config in menus {
- let menu = NSMenu::new(nil).autorelease();
- let menu_title = ns_string(&menu_config.name);
- menu.setTitle_(menu_title);
- menu.setDelegate_(delegate);
+ let menu = NSMenu::new(self.1);
+ let menu_title = NSString::from_str(&menu_config.name);
+ menu.setTitle(&menu_title);
+ let _: () = msg_send![&menu, setDelegate: delegate];
for item_config in &menu_config.items {
- menu.addItem_(Self::create_menu_item(
- item_config,
- delegate,
- actions,
- keymap,
- ));
+ menu.addItem(&self.create_menu_item(item_config, delegate, actions, keymap));
}
- let menu_item = NSMenuItem::new(nil).autorelease();
- menu_item.setTitle_(menu_title);
- menu_item.setSubmenu_(menu);
- application_menu.addItem_(menu_item);
+ let menu_item = NSMenuItem::new(self.1);
+ menu_item.setTitle(&menu_title);
+ menu_item.setSubmenu(Some(&menu));
+ application_menu.addItem(&menu_item);
if menu_config.name == "Window" {
- let app: id = msg_send![APP_CLASS, sharedApplication];
- app.setWindowsMenu_(menu);
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
+ let _: () = msg_send![app, setWindowsMenu: &*menu];
}
}
@@ -323,20 +305,15 @@ impl MacPlatform {
unsafe fn create_dock_menu(
&self,
menu_items: Vec<MenuItem>,
- delegate: id,
+ delegate: *mut AnyObject,
actions: &mut Vec<Box<dyn Action>>,
keymap: &Keymap,
- ) -> id {
+ ) -> Retained<NSMenu> {
unsafe {
- let dock_menu = NSMenu::new(nil);
- dock_menu.setDelegate_(delegate);
+ let dock_menu = NSMenu::new(self.1);
+ let _: () = msg_send![&dock_menu, setDelegate: delegate];
for item_config in menu_items {
- dock_menu.addItem_(Self::create_menu_item(
- &item_config,
- delegate,
- actions,
- keymap,
- ));
+ dock_menu.addItem(&self.create_menu_item(&item_config, delegate, actions, keymap));
}
dock_menu
@@ -344,16 +321,17 @@ impl MacPlatform {
}
unsafe fn create_menu_item(
+ &self,
item: &MenuItem,
- delegate: id,
+ delegate: *mut AnyObject,
actions: &mut Vec<Box<dyn Action>>,
keymap: &Keymap,
- ) -> id {
+ ) -> Retained<NSMenuItem> {
static DEFAULT_CONTEXT: OnceLock<Vec<KeyContext>> = OnceLock::new();
unsafe {
match item {
- MenuItem::Separator => NSMenuItem::separatorItem(nil),
+ MenuItem::Separator => NSMenuItem::separatorItem(self.1),
MenuItem::Action {
name,
action,
@@ -385,15 +363,15 @@ impl MacPlatform {
.map(|binding| binding.keystrokes());
let selector = match os_action {
- Some(gpui::OsAction::Cut) => selector("cut:"),
- Some(gpui::OsAction::Copy) => selector("copy:"),
- Some(gpui::OsAction::Paste) => selector("paste:"),
- Some(gpui::OsAction::SelectAll) => selector("selectAll:"),
+ Some(gpui::OsAction::Cut) => sel!(cut:),
+ Some(gpui::OsAction::Copy) => sel!(copy:),
+ Some(gpui::OsAction::Paste) => sel!(paste:),
+ Some(gpui::OsAction::SelectAll) => sel!(selectAll:),
// "undo:" and "redo:" are always disabled in our case, as
// we don't have a NSTextView/NSTextField to enable them on.
- Some(gpui::OsAction::Undo) => selector("handleGPUIMenuItem:"),
- Some(gpui::OsAction::Redo) => selector("handleGPUIMenuItem:"),
- None => selector("handleGPUIMenuItem:"),
+ Some(gpui::OsAction::Undo) => sel!(handleGPUIMenuItem:),
+ Some(gpui::OsAction::Redo) => sel!(handleGPUIMenuItem:),
+ None => sel!(handleGPUIMenuItem:),
};
let item;
@@ -404,63 +382,51 @@ impl MacPlatform {
for (modifier, flag) in &[
(
keystroke.modifiers().platform,
- NSEventModifierFlags::NSCommandKeyMask,
- ),
- (
- keystroke.modifiers().control,
- NSEventModifierFlags::NSControlKeyMask,
- ),
- (
- keystroke.modifiers().alt,
- NSEventModifierFlags::NSAlternateKeyMask,
- ),
- (
- keystroke.modifiers().shift,
- NSEventModifierFlags::NSShiftKeyMask,
+ NSEventModifierFlags::Command,
),
+ (keystroke.modifiers().control, NSEventModifierFlags::Control),
+ (keystroke.modifiers().alt, NSEventModifierFlags::Option),
+ (keystroke.modifiers().shift, NSEventModifierFlags::Shift),
] {
if *modifier {
mask |= *flag;
}
}
- item = NSMenuItem::alloc(nil)
- .initWithTitle_action_keyEquivalent_(
- ns_string(name),
- selector,
- ns_string(key_to_native(keystroke.key()).as_ref()),
- )
- .autorelease();
+ item = NSMenuItem::initWithTitle_action_keyEquivalent(
+ NSMenuItem::alloc(self.1),
+ &NSString::from_str(name),
+ Some(selector),
+ &NSString::from_str(key_to_native(keystroke.key()).as_ref()),
+ );
if Self::os_version() >= Version::new(12, 0, 0) {
- let _: () = msg_send![item, setAllowsAutomaticKeyEquivalentLocalization: NO];
+ item.setAllowsAutomaticKeyEquivalentLocalization(false);
}
- item.setKeyEquivalentModifierMask_(mask);
+ item.setKeyEquivalentModifierMask(mask);
} else {
- item = NSMenuItem::alloc(nil)
- .initWithTitle_action_keyEquivalent_(
- ns_string(name),
- selector,
- ns_string(""),
- )
- .autorelease();
+ item = NSMenuItem::initWithTitle_action_keyEquivalent(
+ NSMenuItem::alloc(self.1),
+ &NSString::from_str(name),
+ Some(selector),
+ &NSString::from_str(""),
+ );
}
} else {
- item = NSMenuItem::alloc(nil)
- .initWithTitle_action_keyEquivalent_(
- ns_string(name),
- selector,
- ns_string(""),
- )
- .autorelease();
+ item = NSMenuItem::initWithTitle_action_keyEquivalent(
+ NSMenuItem::alloc(self.1),
+ &NSString::from_str(name),
+ Some(selector),
+ &NSString::from_str(""),
+ );
}
if *checked {
- item.setState_(NSVisualEffectState::Active);
+ item.setState(NSControlStateValueOn);
}
- item.setEnabled_(if *disabled { NO } else { YES });
+ item.setEnabled(!disabled);
let tag = actions.len() as NSInteger;
- let _: () = msg_send![item, setTag: tag];
+ item.setTag(tag);
actions.push(action.boxed_clone());
item
}
@@ -469,28 +435,28 @@ impl MacPlatform {
items,
disabled,
}) => {
- let item = NSMenuItem::new(nil).autorelease();
- let submenu = NSMenu::new(nil).autorelease();
- submenu.setDelegate_(delegate);
+ let item = NSMenuItem::new(self.1);
+ let submenu = NSMenu::new(self.1);
+ let _: () = msg_send![&submenu, setDelegate: delegate];
for item in items {
- submenu.addItem_(Self::create_menu_item(item, delegate, actions, keymap));
+ submenu.addItem(&self.create_menu_item(item, delegate, actions, keymap));
}
- item.setSubmenu_(submenu);
- item.setEnabled_(if *disabled { NO } else { YES });
- item.setTitle_(ns_string(name));
+ item.setSubmenu(Some(&submenu));
+ item.setEnabled(!disabled);
+ item.setTitle(&NSString::from_str(name));
item
}
MenuItem::SystemMenu(OsMenu { name, menu_type }) => {
- let item = NSMenuItem::new(nil).autorelease();
- let submenu = NSMenu::new(nil).autorelease();
- submenu.setDelegate_(delegate);
- item.setSubmenu_(submenu);
- item.setTitle_(ns_string(name));
+ let item = NSMenuItem::new(self.1);
+ let submenu = NSMenu::new(self.1);
+ let _: () = msg_send![&submenu, setDelegate: delegate];
+ item.setSubmenu(Some(&submenu));
+ item.setTitle(&NSString::from_str(name));
match menu_type {
SystemMenuType::Services => {
- let app: id = msg_send![APP_CLASS, sharedApplication];
- app.setServicesMenu_(item);
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
+ let _: () = msg_send![app, setServicesMenu: &*item];
}
}
@@ -501,14 +467,11 @@ impl MacPlatform {
}
fn os_version() -> Version {
- let version = unsafe {
- let process_info = NSProcessInfo::processInfo(nil);
- process_info.operatingSystemVersion()
- };
+ let version = NSProcessInfo::processInfo().operatingSystemVersion();
Version::new(
- version.majorVersion,
- version.minorVersion,
- version.patchVersion,
+ version.majorVersion as u64,
+ version.minorVersion as u64,
+ version.patchVersion as u64,
)
}
@@ -525,8 +488,8 @@ impl MacPlatform {
// SAFETY: APP_CLASS is registered during startup and `sharedApplication`
// returns a live NSApplication instance.
let delegate = unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
- (*(app as *const objc2_app_kit::NSApplication)).delegate()
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
+ (*(app as *const NSApplication)).delegate()
};
if let Some(delegate) = delegate {
register_system_power_observers(delegate.as_ref());
@@ -560,7 +523,7 @@ impl Platform for MacPlatform {
}
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
// An accessory app must not register in the Dock during launch, so its
// policy has to precede the run loop. `Regular` is applied in
// `did_finish_launching` instead: setting it this early leaves the menu
@@ -571,21 +534,30 @@ impl Platform for MacPlatform {
state.activation_policy
};
if policy == ActivationPolicy::Accessory {
- app.setActivationPolicy_(native_activation_policy(policy));
+ let _: Bool = msg_send![app, setActivationPolicy: native_activation_policy(policy)];
}
- let app_delegate: id = msg_send![APP_DELEGATE_CLASS, new];
- app.setDelegate_(app_delegate);
-
- let self_ptr = self as *const Self as *const c_void;
- (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr);
- (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr);
-
- let pool = NSAutoreleasePool::new(nil);
- app.run();
- pool.drain();
-
- (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
- (*NSWindow::delegate(app)).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
+ let app_delegate: *mut AnyObject = msg_send![APP_DELEGATE_CLASS, new];
+ let _: () = msg_send![app, setDelegate: app_delegate];
+
+ let self_ptr = self as *const Self as *mut c_void;
+ mac_platform_ivar(&*app)
+ .load_ptr::<*mut c_void>(&*app)
+ .write(self_ptr);
+ mac_platform_ivar(&*app_delegate)
+ .load_ptr::<*mut c_void>(&*app_delegate)
+ .write(self_ptr);
+
+ autoreleasepool(|_| {
+ let _: () = msg_send![app, run];
+ });
+
+ mac_platform_ivar(&*app)
+ .load_ptr::<*mut c_void>(&*app)
+ .write(ptr::null_mut());
+ let app_delegate: *mut AnyObject = msg_send![app, delegate];
+ mac_platform_ivar(&*app_delegate)
+ .load_ptr::<*mut c_void>(&*app_delegate)
+ .write(ptr::null_mut());
}
self.0.lock().application_created = false;
}
@@ -604,8 +576,8 @@ impl Platform for MacPlatform {
extern "C" fn quit(_: *mut c_void) {
unsafe {
- let app = NSApplication::sharedApplication(nil);
- let _: () = msg_send![app, terminate: nil];
+ let app: *mut AnyObject = msg_send![class!(NSApplication), sharedApplication];
+ let _: () = msg_send![app, terminate: ptr::null_mut::<AnyObject>()];
}
}
}
@@ -658,11 +630,10 @@ impl Platform for MacPlatform {
}
}
+ #[allow(deprecated)]
fn activate(&self, ignoring_other_apps: bool) {
- unsafe {
- let app = NSApplication::sharedApplication(nil);
- app.activateIgnoringOtherApps_(ignoring_other_apps.to_objc());
- }
+ let app = NSApplication::sharedApplication(self.1);
+ app.activateIgnoringOtherApps(ignoring_other_apps);
}
fn set_activation_policy(&self, policy: ActivationPolicy) {
@@ -672,8 +643,8 @@ impl Platform for MacPlatform {
drop(state);
if should_apply {
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
- app.setActivationPolicy_(native_activation_policy(policy));
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
+ let _: Bool = msg_send![app, setActivationPolicy: native_activation_policy(policy)];
}
}
}
@@ -708,24 +679,15 @@ impl Platform for MacPlatform {
}
fn hide(&self) {
- unsafe {
- let app = NSApplication::sharedApplication(nil);
- let _: () = msg_send![app, hide: nil];
- }
+ NSApplication::sharedApplication(self.1).hide(None);
}
fn hide_other_apps(&self) {
- unsafe {
- let app = NSApplication::sharedApplication(nil);
- let _: () = msg_send![app, hideOtherApplications: nil];
- }
+ NSApplication::sharedApplication(self.1).hideOtherApplications(None);
}
fn unhide_other_apps(&self) {
- unsafe {
- let app = NSApplication::sharedApplication(nil);
- let _: () = msg_send![app, unhideAllApplications: nil];
- }
+ NSApplication::sharedApplication(self.1).unhideAllApplications(None);
}
fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>> {
@@ -744,8 +706,12 @@ impl Platform for MacPlatform {
#[cfg(feature = "screen-capture")]
fn is_screen_capture_supported(&self) -> bool {
- let min_version = cocoa::foundation::NSOperatingSystemVersion::new(12, 3, 0);
- crate::is_macos_version_at_least(min_version)
+ let min_version = objc2_foundation::NSOperatingSystemVersion {
+ majorVersion: 12,
+ minorVersion: 3,
+ patchVersion: 0,
+ };
+ NSProcessInfo::processInfo().isOperatingSystemAtLeastVersion(min_version)
}
#[cfg(feature = "screen-capture")]
@@ -799,51 +765,39 @@ impl Platform for MacPlatform {
fn window_appearance(&self) -> WindowAppearance {
unsafe {
- let app = NSApplication::sharedApplication(nil);
- let appearance: id = msg_send![app, effectiveAppearance];
- crate::window_appearance::window_appearance_from_native(appearance)
+ let app = NSApplication::sharedApplication(self.1);
+ let appearance: *mut AnyObject = msg_send![&app, effectiveAppearance];
+ crate::window_appearance::window_appearance_from_native(appearance.cast())
}
}
fn set_window_appearance(&self, appearance: Option<WindowAppearance>) {
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
// `None` clears the override by setting a nil appearance, so the app
// falls back to tracking the system-wide light/dark setting.
- let ns_appearance: id = match appearance {
- None => nil,
- Some(appearance) => {
- let name: id = match appearance {
- WindowAppearance::Light => crate::window_appearance::NSAppearanceNameAqua,
- WindowAppearance::Dark => {
- crate::window_appearance::NSAppearanceNameDarkAqua
- }
- WindowAppearance::VibrantLight => NSAppearanceNameVibrantLight,
- WindowAppearance::VibrantDark => NSAppearanceNameVibrantDark,
- };
- msg_send![class!(NSAppearance), appearanceNamed: name]
- }
- };
- let _: () = msg_send![app, setAppearance: ns_appearance];
+ let ns_appearance = appearance.and_then(|appearance| {
+ let name = match appearance {
+ WindowAppearance::Light => NSAppearanceNameAqua,
+ WindowAppearance::Dark => NSAppearanceNameDarkAqua,
+ WindowAppearance::VibrantLight => NSAppearanceNameVibrantLight,
+ WindowAppearance::VibrantDark => NSAppearanceNameVibrantDark,
+ };
+ NSAppearance::appearanceNamed(name)
+ });
+ let _: () = msg_send![app, setAppearance: ns_appearance.as_deref()];
}
}
fn open_url(&self, url: &str) {
- unsafe {
- let ns_url = NSURL::alloc(nil).initWithString_(ns_string(url));
- if ns_url.is_null() {
- log::error!("Failed to create NSURL from string: {}", url);
- return;
- }
- let url = ns_url.autorelease();
- let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
- msg_send![workspace, openURL: url]
- }
+ let Some(url) = NSURL::URLWithString(&NSString::from_str(url)) else {
+ log::error!("Failed to create NSURL from string: {url}");
+ return;
+ };
+ NSWorkspace::sharedWorkspace().openURL(&url);
}
fn register_url_scheme(&self, scheme: &str) -> Task<anyhow::Result<()>> {
- use objc2_foundation::{NSBundle, NSError, NSString};
-
// API only available post Monterey
// https://developer.apple.com/documentation/appkit/nsworkspace/3753004-setdefaultapplicationaturl
let (done_tx, done_rx) = oneshot::channel();
@@ -899,8 +853,6 @@ impl Platform for MacPlatform {
&self,
options: PathPromptOptions,
) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>> {
- use objc2_foundation::NSString;
-
let marker = self.1;
let (done_tx, done_rx) = oneshot::channel();
self.foreground_executor()
@@ -948,8 +900,6 @@ impl Platform for MacPlatform {
directory: &Path,
suggested_name: Option<&str>,
) -> oneshot::Receiver<Result<Option<PathBuf>>> {
- use objc2_foundation::{NSString, NSURL};
-
let url = NSURL::from_directory_path(directory);
let suggested_name = suggested_name.map(NSString::from_str);
let (done_tx, done_rx) = oneshot::channel();
@@ -1021,23 +971,17 @@ impl Platform for MacPlatform {
}
fn reveal_path(&self, path: &Path) {
- unsafe {
- let path = path.to_path_buf();
- self.0
- .lock()
- .background_executor
- .spawn(async move {
- let full_path = ns_string(path.to_str().unwrap_or(""));
- let root_full_path = ns_string("");
- let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
- let _: BOOL = msg_send![
- workspace,
- selectFile: full_path
- inFileViewerRootedAtPath: root_full_path
- ];
- })
- .detach();
- }
+ let path = path.to_path_buf();
+ self.0
+ .lock()
+ .background_executor
+ .spawn(async move {
+ let full_path = NSString::from_str(path.to_str().unwrap_or(""));
+ let root_full_path = NSString::from_str("");
+ NSWorkspace::sharedWorkspace()
+ .selectFile_inFileViewerRootedAtPath(Some(&full_path), &root_full_path);
+ })
+ .detach();
}
fn open_with_system(&self, path: &Path) {
@@ -1141,7 +1085,7 @@ impl Platform for MacPlatform {
fn app_path(&self) -> Result<PathBuf> {
unsafe {
- let bundle: id = NSBundle::mainBundle();
+ let bundle: *mut AnyObject = msg_send![class!(NSBundle), mainBundle];
anyhow::ensure!(!bundle.is_null(), "app is not running inside a bundle");
Ok(path_from_objc(msg_send![bundle, bundlePath]))
}
@@ -1149,12 +1093,13 @@ impl Platform for MacPlatform {
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap) {
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
let mut state = self.0.lock();
let actions = &mut state.menu_actions;
- let menu = self.create_menu_bar(&menus, NSWindow::delegate(app), actions, keymap);
+ let delegate: *mut AnyObject = msg_send![app, delegate];
+ let menu = self.create_menu_bar(&menus, delegate, actions, keymap);
drop(state);
- app.setMainMenu_(menu);
+ let _: () = msg_send![app, setMainMenu: &*menu];
}
self.0.lock().menus = Some(menus.into_iter().map(|menu| menu.owned()).collect());
}
@@ -1165,33 +1110,28 @@ impl Platform for MacPlatform {
fn set_dock_menu(&self, menu: Vec<MenuItem>, keymap: &Keymap) {
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
let mut state = self.0.lock();
let actions = &mut state.menu_actions;
- let new = self.create_dock_menu(menu, NSWindow::delegate(app), actions, keymap);
- if let Some(old) = state.dock_menu.replace(new) {
- CFRelease(old as _)
- }
+ let delegate: *mut AnyObject = msg_send![app, delegate];
+ state.dock_menu = Some(self.create_dock_menu(menu, delegate, actions, keymap));
}
}
fn add_recent_document(&self, path: &Path) {
if let Some(path_str) = path.to_str() {
- unsafe {
- let document_controller: id =
- msg_send![class!(NSDocumentController), sharedDocumentController];
- let url: id = NSURL::fileURLWithPath_(nil, ns_string(path_str));
- let _: () = msg_send![document_controller, noteNewRecentDocumentURL:url];
- }
+ let document_controller = NSDocumentController::sharedDocumentController(self.1);
+ let url = NSURL::fileURLWithPath(&NSString::from_str(path_str));
+ document_controller.noteNewRecentDocumentURL(&url);
}
}
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
unsafe {
- let bundle: id = NSBundle::mainBundle();
+ let bundle: *mut AnyObject = msg_send![class!(NSBundle), mainBundle];
anyhow::ensure!(!bundle.is_null(), "app is not running inside a bundle");
let name = ns_string(name);
- let url: id = msg_send![bundle, URLForAuxiliaryExecutable: name];
+ let url: *mut AnyObject = msg_send![bundle, URLForAuxiliaryExecutable: name];
anyhow::ensure!(!url.is_null(), "resource not found");
ns_url_to_path(url)
}
@@ -1210,9 +1150,7 @@ impl Platform for MacPlatform {
if !cursor_visible.swap(false, Ordering::Relaxed) {
return;
}
- unsafe {
- let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves: YES];
- }
+ NSCursor::setHiddenUntilMouseMoves(true);
}
fn is_cursor_visible(&self) -> bool {
@@ -1220,13 +1158,7 @@ impl Platform for MacPlatform {
}
fn should_auto_hide_scrollbars(&self) -> bool {
- #[allow(non_upper_case_globals)]
- const NSScrollerStyleOverlay: NSInteger = 1;
-
- unsafe {
- let style: NSInteger = msg_send![class!(NSScroller), preferredScrollerStyle];
- style == NSScrollerStyleOverlay
- }
+ NSScroller::preferredScrollerStyle(self.1) == NSScrollerStyle::Overlay
}
fn read_from_clipboard(&self) -> Option<ClipboardItem> {
@@ -1270,70 +1202,85 @@ impl Platform for MacPlatform {
}
}
-unsafe fn path_from_objc(path: id) -> PathBuf {
- let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
- let bytes = unsafe { path.UTF8String() as *const u8 };
+unsafe fn path_from_objc(path: *mut AnyObject) -> PathBuf {
+ let path = unsafe { &*path.cast::<NSString>() };
+ let len = path.lengthOfBytesUsingEncoding(NSUTF8StringEncoding);
+ let bytes = path.UTF8String().cast::<u8>();
let path = str::from_utf8(unsafe { slice::from_raw_parts(bytes, len) }).unwrap();
PathBuf::from(path)
}
-unsafe fn get_mac_platform(object: &mut Object) -> &MacPlatform {
+unsafe fn get_mac_platform(object: &AnyObject) -> &MacPlatform {
unsafe {
- let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR);
+ let platform_ptr = *mac_platform_ivar(object).load::<*mut c_void>(object);
assert!(!platform_ptr.is_null());
&*(platform_ptr as *const MacPlatform)
}
}
-extern "C" fn will_finish_launching(_this: &mut Object, _: Sel, _: id) {
+fn mac_platform_ivar(object: &AnyObject) -> &Ivar {
+ let ivar = object
+ .class()
+ .instance_variable(MAC_PLATFORM_IVAR)
+ .expect("platform ivar is missing");
+ assert!(
+ <*mut c_void>::ENCODING.equivalent_to_str(
+ ivar.type_encoding()
+ .to_str()
+ .expect("platform ivar encoding is not UTF-8")
+ ),
+ "platform ivar has an unexpected type encoding"
+ );
+ ivar
+}
+
+extern "C-unwind" fn will_finish_launching(_this: &AnyObject, _: Sel, _: *mut AnyObject) {
unsafe {
- let user_defaults: id = msg_send![class!(NSUserDefaults), standardUserDefaults];
+ let user_defaults: *mut AnyObject = msg_send![class!(NSUserDefaults), standardUserDefaults];
// The autofill heuristic controller causes slowdown and high CPU usage.
// We don't know exactly why. This disables the full heuristic controller.
//
// Adapted from: https://github.com/ghostty-org/ghostty/pull/8625
let name = ns_string("NSAutoFillHeuristicControllerEnabled");
- let existing_value: id = msg_send![user_defaults, objectForKey: name];
- if existing_value == nil {
- let false_value: id = msg_send![class!(NSNumber), numberWithBool:false];
- let _: () = msg_send![user_defaults, setObject: false_value forKey: name];
+ let existing_value: *mut AnyObject = msg_send![user_defaults, objectForKey: name];
+ if existing_value.is_null() {
+ let false_value: *mut AnyObject = msg_send![class!(NSNumber), numberWithBool: Bool::NO];
+ let _: () = msg_send![user_defaults, setObject: false_value, forKey: name];
}
}
}
-extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn did_finish_launching(this: &AnyObject, _: Sel, _: *mut AnyObject) {
unsafe {
- let app: id = msg_send![APP_CLASS, sharedApplication];
+ let app: *mut AnyObject = msg_send![APP_CLASS, sharedApplication];
let policy = get_mac_platform(this).0.lock().activation_policy;
- app.setActivationPolicy_(native_activation_policy(policy));
+ let _: Bool = msg_send![app, setActivationPolicy: native_activation_policy(policy)];
- let notification_center: *mut Object =
+ let notification_center: *mut AnyObject =
msg_send![class!(NSNotificationCenter), defaultCenter];
let name = ns_string("NSTextInputContextKeyboardSelectionDidChangeNotification");
- let _: () = msg_send![notification_center, addObserver: this as id
- selector: sel!(onKeyboardLayoutChange:)
- name: name
- object: nil
+ let _: () = msg_send![notification_center, addObserver: this,
+ selector: sel!(onKeyboardLayoutChange:),
+ name: name,
+ object: ptr::null_mut::<AnyObject>()
];
// Posted when a display is connected, disconnected, rearranged, or
// changes mode, which includes its refresh rate.
- let _: () = msg_send![notification_center, addObserver: this as id
- selector: sel!(onScreenParametersChange:)
- name: ns_string("NSApplicationDidChangeScreenParametersNotification")
- object: nil
+ let _: () = msg_send![notification_center, addObserver: this,
+ selector: sel!(onScreenParametersChange:),
+ name: ns_string("NSApplicationDidChangeScreenParametersNotification"),
+ object: ptr::null_mut::<AnyObject>()
];
- // SAFETY: `this` is a live Objective-C object; only the pointer's type changes.
- let observer = &*(this as *mut Object as *const AnyObject);
let platform = get_mac_platform(this);
let callback = {
let mut state = platform.0.lock();
if (state.on_system_sleep.is_some() || state.on_system_wake.is_some())
&& !state.system_power_observers_registered
{
- register_system_power_observers(observer);
+ register_system_power_observers(this);
state.system_power_observers_registered = true;
}
state.finish_launching.take()
@@ -1366,8 +1313,13 @@ fn register_system_power_observers(observer: &AnyObject) {
}
}
-extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) {
- if !has_open_windows {
+extern "C-unwind" fn should_handle_reopen(
+ this: &AnyObject,
+ _: Sel,
+ _: *mut AnyObject,
+ has_open_windows: Bool,
+) -> Bool {
+ if !has_open_windows.as_bool() {
let platform = unsafe { get_mac_platform(this) };
let mut lock = platform.0.lock();
if let Some(mut callback) = lock.reopen.take() {
@@ -1376,9 +1328,10 @@ extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_wi
platform.0.lock().reopen.get_or_insert(callback);
}
}
+ Bool::YES
}
-extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn will_terminate(this: &AnyObject, _: Sel, _: *mut AnyObject) {
let platform = unsafe { get_mac_platform(this) };
let mut lock = platform.0.lock();
if let Some(mut callback) = lock.quit.take() {
@@ -1388,7 +1341,7 @@ extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) {
}
}
-extern "C" fn on_keyboard_layout_change(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn on_keyboard_layout_change(this: &AnyObject, _: Sel, _: *mut AnyObject) {
let platform = unsafe { get_mac_platform(this) };
let mut lock = platform.0.lock();
let keyboard_layout = MacKeyboardLayout::new();
@@ -1404,7 +1357,7 @@ extern "C" fn on_keyboard_layout_change(this: &mut Object, _: Sel, _: id) {
}
}
-extern "C" fn on_screen_parameters_change(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn on_screen_parameters_change(this: &AnyObject, _: Sel, _: *mut AnyObject) {
// Defer to the next run loop iteration to avoid re-entrant borrows of the App RefCell,
// as NSNotificationCenter delivers this notification synchronously and it may fire while
// the App is already borrowed (same pattern as quit() above).
@@ -1431,7 +1384,7 @@ extern "C" fn on_screen_parameters_change(this: &mut Object, _: Sel, _: id) {
// Deferred to the main queue like the wake and thermal notifications. The
// machine keeps running the main run loop for well over a frame after the
// observers return, so the block runs before it suspends.
-extern "C" fn on_system_sleep(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn on_system_sleep(this: &AnyObject, _: Sel, _: *mut AnyObject) {
// SAFETY: this is the registered app delegate carrying MAC_PLATFORM_IVAR.
let platform = unsafe { get_mac_platform(this) };
let platform_ptr = platform as *const MacPlatform as *mut c_void;
@@ -1452,7 +1405,7 @@ extern "C" fn on_system_sleep(this: &mut Object, _: Sel, _: id) {
}
}
-extern "C" fn on_system_wake(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn on_system_wake(this: &AnyObject, _: Sel, _: *mut AnyObject) {
// SAFETY: this is the registered app delegate carrying MAC_PLATFORM_IVAR.
let platform = unsafe { get_mac_platform(this) };
let platform_ptr = platform as *const MacPlatform as *mut c_void;
@@ -1473,12 +1426,15 @@ extern "C" fn on_system_wake(this: &mut Object, _: Sel, _: id) {
}
}
-extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) {
+extern "C-unwind" fn open_urls(this: &AnyObject, _: Sel, _: *mut AnyObject, urls: *mut AnyObject) {
let urls = unsafe {
- (0..urls.count())
- .filter_map(|i| {
- let url = urls.objectAtIndex(i);
- match CStr::from_ptr(url.absoluteString().UTF8String() as *mut c_char).to_str() {
+ urls.cast::<NSArray<NSURL>>()
+ .as_ref()
+ .into_iter()
+ .flat_map(|urls| urls.iter())
+ .filter_map(|url| {
+ let string = url.absoluteString()?;
+ match CStr::from_ptr(string.UTF8String()).to_str() {
Ok(string) => Some(string.to_string()),
Err(err) => {
log::error!("error converting path to string: {}", err);
@@ -1497,12 +1453,16 @@ extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) {
}
}
-extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
+extern "C-unwind" fn handle_menu_item(this: &AnyObject, _: Sel, item: *mut AnyObject) {
unsafe {
let platform = get_mac_platform(this);
let mut lock = platform.0.lock();
if let Some(mut callback) = lock.menu_command.take() {
- let tag: NSInteger = msg_send![item, tag];
+ let tag: NSInteger = if item.is_null() {
+ 0
+ } else {
+ msg_send![item, tag]
+ };
let index = tag as usize;
if let Some(action) = lock.menu_actions.get(index) {
let action = action.boxed_clone();
@@ -1514,13 +1474,17 @@ extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
}
}
-extern "C" fn validate_menu_item(this: &mut Object, _: Sel, item: id) -> bool {
+extern "C-unwind" fn validate_menu_item(this: &AnyObject, _: Sel, item: *mut AnyObject) -> Bool {
unsafe {
let mut result = false;
let platform = get_mac_platform(this);
let mut lock = platform.0.lock();
if let Some(mut callback) = lock.validate_menu_command.take() {
- let tag: NSInteger = msg_send![item, tag];
+ let tag: NSInteger = if item.is_null() {
+ 0
+ } else {
+ msg_send![item, tag]
+ };
let index = tag as usize;
if let Some(action) = lock.menu_actions.get(index) {
let action = action.boxed_clone();
@@ -1533,11 +1497,11 @@ extern "C" fn validate_menu_item(this: &mut Object, _: Sel, item: id) -> bool {
.validate_menu_command
.get_or_insert(callback);
}
- result
+ Bool::new(result)
}
}
-extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) {
+extern "C-unwind" fn menu_will_open(this: &AnyObject, _: Sel, _: *mut AnyObject) {
unsafe {
let platform = get_mac_platform(this);
let mut lock = platform.0.lock();
@@ -1549,22 +1513,31 @@ extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) {
}
}
-extern "C" fn handle_dock_menu(this: &mut Object, _: Sel, _: id) -> id {
+extern "C-unwind" fn handle_dock_menu(
+ this: &AnyObject,
+ _: Sel,
+ _: *mut AnyObject,
+) -> *mut AnyObject {
unsafe {
let platform = get_mac_platform(this);
let state = platform.0.lock();
- if let Some(id) = state.dock_menu {
- id
- } else {
- nil
- }
+ state.dock_menu.as_ref().map_or(ptr::null_mut(), |menu| {
+ Retained::as_ptr(menu).cast_mut().cast::<AnyObject>()
+ })
}
}
-unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> {
- let path: *mut c_char = msg_send![url, fileSystemRepresentation];
+unsafe fn ns_url_to_path(url: *mut AnyObject) -> Result<PathBuf> {
+ let path: *const c_char = unsafe { msg_send![url, fileSystemRepresentation] };
anyhow::ensure!(!path.is_null(), "url is not a file path: {}", unsafe {
- CStr::from_ptr(url.absoluteString().UTF8String()).to_string_lossy()
+ let string: *mut NSString = msg_send![url, absoluteString];
+ let string = string.as_ref().map_or(c"".as_ptr(), NSString::UTF8String);
+ let string = if string.is_null() {
+ c""
+ } else {
+ CStr::from_ptr(string)
+ };
+ string.to_string_lossy()
});
Ok(PathBuf::from(OsStr::from_bytes(unsafe {
CStr::from_ptr(path).to_bytes()
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index 2e6fe9a953b..654ae09940b 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -2,7 +2,7 @@ use crate::{
BoolExt, MacDisplay, NSRange, NSStringExt, TISCopyCurrentKeyboardInputSource,
TISGetInputSourceProperty, WindowFrameSource, events::platform_input_from_native,
kTISPropertyInputSourceIsASCIICapable, kTISPropertyInputSourceType, kTISTypeKeyboardInputMode,
- ns_string, renderer,
+ legacy_ns_string as ns_string, renderer,
};
#[cfg(any(test, feature = "test-support"))]
use anyhow::Result;