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 `start_external_drag` to `objc2` (#64033)

tidely72c53bf0aae2
diff --git a/Cargo.toml b/Cargo.toml
index cb9bd7f4706..69d7469c3fa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -699,9 +699,12 @@ objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"NSApplication",
"NSButton",
"NSControl",
+ "NSDraggingItem",
"NSGraphics",
+ "NSImage",
"NSOpenPanel",
"NSPanel",
+ "NSPasteboard",
"NSResponder",
"NSSavePanel",
"NSScreen",
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index bbb60a1d640..7d06dcdabda 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -6,7 +6,6 @@ use crate::{
};
#[cfg(any(test, feature = "test-support"))]
use anyhow::Result;
-use block::ConcreteBlock;
use block2::RcBlock;
use cocoa::{
appkit::{
@@ -50,12 +49,17 @@ use objc::{
runtime::{BOOL, Class, NO, Object, Protocol, Sel, YES},
sel, sel_impl,
};
-use objc2::{MainThreadMarker, rc::Retained, runtime::AnyObject as Objc2Object};
+use objc2::{
+ AnyThread, MainThreadMarker,
+ rc::Retained,
+ runtime::{AnyObject as Objc2Object, ProtocolObject},
+};
use objc2_app_kit::{
- NSAlert, NSAlertStyle, NSBeep, NSButton as Objc2NSButton, NSView as Objc2NSView,
- NSWindow as Objc2NSWindow, NSWindowButton as Objc2NSWindowButton,
+ NSAlert, NSAlertStyle, NSBeep, NSButton as Objc2NSButton, NSDraggingImageComponent,
+ NSDraggingImageComponentIconKey, NSDraggingItem, NSPasteboardWriting, NSView as Objc2NSView,
+ NSWindow as Objc2NSWindow, NSWindowButton as Objc2NSWindowButton, NSWorkspace,
};
-use objc2_foundation::{NSPoint as Objc2NSPoint, NSRect as Objc2NSRect};
+use objc2_foundation::{NSPoint as Objc2NSPoint, NSRect as Objc2NSRect, NSURL};
use parking_lot::Mutex;
use raw_window_handle as rwh;
use smallvec::SmallVec;
@@ -125,12 +129,6 @@ pub enum UserTabbingPreference {
InFullScreen,
}
-#[link(name = "AppKit", kind = "framework")]
-unsafe extern "C" {
- // AppKit constant naming the icon component of an NSDraggingImageComponent.
- #[allow(non_upper_case_globals)]
- static NSDraggingImageComponentIconKey: id;
-}
#[ctor(unsafe)]
unsafe fn build_classes() {
unsafe {
@@ -2203,6 +2201,8 @@ impl PlatformWindow for MacWindow {
}
fn start_external_drag(&self, payload: &ExternalDragPayload) -> bool {
+ use objc2_foundation::{NSArray, NSPoint, NSRect, NSSize, NSString};
+
let ExternalDragPayload::Files(paths) = payload;
if paths.entries().is_empty() {
log::warn!("start_external_drag declined: no paths");
@@ -2225,7 +2225,7 @@ impl PlatformWindow for MacWindow {
// SAFETY: This method runs on the AppKit/foreground path during drag initiation. The
// native view/window are retained by MacWindowState, copied out under a short lock above,
- // and Objective-C results that may be nil are checked before use.
+ // and all pointers passed to Objective-C remain valid for their respective calls.
unsafe {
let event: id = Retained::as_ptr(&last_left_mouse_down_event)
.cast_mut()
@@ -2233,7 +2233,7 @@ impl PlatformWindow for MacWindow {
let dragging_items: id = msg_send![class!(NSMutableArray), array];
// AppKit keeps this frame's distance from the event's location as the drag image's
// offset from the cursor, so it has to stay anchored on `event`.
- let location: NSPoint = msg_send![event, locationInWindow];
+ let location: cocoa::foundation::NSPoint = msg_send![event, locationInWindow];
let frame = NSRect::new(
NSPoint::new(location.x - 16., location.y - 16.),
NSSize::new(32., 32.),
@@ -2246,24 +2246,18 @@ impl PlatformWindow for MacWindow {
continue;
};
- let url: id = msg_send![
- class!(NSURL),
- fileURLWithFileSystemRepresentation: path_bytes.as_ptr()
- isDirectory: is_directory.to_objc()
- relativeToURL: nil
- ];
-
- if url.is_null() {
- log::warn!("start_external_drag skipped path with nil NSURL");
- continue;
- }
+ let path_bytes = NonNull::new_unchecked(path_bytes.as_ptr().cast_mut());
+ let url = NSURL::fileURLWithFileSystemRepresentation_isDirectory_relativeToURL(
+ path_bytes,
+ *is_directory,
+ None,
+ );
- let item: id = msg_send![class!(NSDraggingItem), alloc];
- let item: id = msg_send![item, initWithPasteboardWriter: url];
- if item.is_null() {
- log::warn!("start_external_drag declined: NSDraggingItem allocation failed");
- continue;
- }
+ let pasteboard_writer = ProtocolObject::<dyn NSPasteboardWriting>::from_ref(&*url);
+ let item = NSDraggingItem::initWithPasteboardWriter(
+ NSDraggingItem::alloc(),
+ pasteboard_writer,
+ );
// Resolve drag images lazily via `imageComponentsProvider` (Apple's
// recommendation for large item counts), and by file *type* rather than
@@ -2279,26 +2273,24 @@ impl PlatformWindow for MacWindow {
.map(|extension| extension.to_string())
.unwrap_or_else(|| "public.data".to_string())
};
- let provider = ConcreteBlock::new(move || -> id {
- let component: id = msg_send![
- class!(NSDraggingImageComponent),
- draggingImageComponentWithKey: NSDraggingImageComponentIconKey
- ];
- let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
- let icon: id = msg_send![workspace, iconForFileType: ns_string(&file_type)];
- let _: () = msg_send![component, setContents: icon];
+ let provider = RcBlock::new(move || {
+ let component = NSDraggingImageComponent::draggingImageComponentWithKey(
+ NSDraggingImageComponentIconKey,
+ );
+ let workspace = NSWorkspace::sharedWorkspace();
+ let file_type = NSString::from_str(&file_type);
+ // TODO: Replace with `iconForContentType` once Zed no longer supports MacOS 10.15
+ #[expect(deprecated, reason = "Support for MacOS 10.15")]
+ let icon = workspace.iconForFileType(&file_type);
+ component.setContents(Some(&icon));
// Component frames are relative to the item's dragging frame.
- let _: () = msg_send![
- component,
- setFrame: NSRect::new(NSPoint::new(0., 0.), NSSize::new(32., 32.))
- ];
- msg_send![class!(NSArray), arrayWithObject: component]
+ component.setFrame(NSRect::new(NSPoint::new(0., 0.), NSSize::new(32., 32.)));
+ let components = NSArray::from_slice(&[&*component]);
+ NonNull::new_unchecked(Retained::autorelease_return(components))
});
- let provider = provider.copy();
- let _: () = msg_send![item, setDraggingFrame: frame];
- let _: () = msg_send![item, setImageComponentsProvider: provider];
- let _: () = msg_send![dragging_items, addObject: item];
- let _: () = msg_send![item, release];
+ item.setDraggingFrame(frame);
+ item.setImageComponentsProvider(Some(&provider));
+ let _: () = msg_send![dragging_items, addObject: Retained::as_ptr(&item)];
}
let count: NSUInteger = msg_send![dragging_items, count];