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: Handle null open and save panels (#66042)

Bennet Bo Fenner9f305aaf8837
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index 4b481881316..5d31ca46d73 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -863,11 +863,21 @@ impl Platform for MacPlatform {
&self,
options: PathPromptOptions,
) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>> {
- let marker = self.1;
let (done_tx, done_rx) = oneshot::channel();
self.foreground_executor()
.spawn(async move {
- let panel = NSOpenPanel::openPanel(marker);
+ // AppKit can return nil despite the nonnull annotation used by objc2's
+ // generated binding. This runs on the main thread via the foreground executor.
+ // TODO: Switch back to NSOpenPanel::openPanel when objc2-app-kit 0.4
+ // is published and adopted; it fixes the return type to be nullable:
+ // https://github.com/madsmtm/objc2/commit/b92857df57aa85dc41e9887534b1ccfa3fe902e4
+ let panel: Option<Retained<NSOpenPanel>> =
+ unsafe { msg_send![class!(NSOpenPanel), openPanel] };
+ let Some(panel) = panel else {
+ // The receiver may have been dropped while the panel was being created.
+ _ = done_tx.send(Err(anyhow!("macOS could not create the open dialog")));
+ return;
+ };
panel.setCanChooseDirectories(options.directories);
panel.setCanChooseFiles(options.files);
panel.setAllowsMultipleSelection(options.multiple);
@@ -913,10 +923,20 @@ impl Platform for MacPlatform {
let url = NSURL::from_directory_path(directory);
let suggested_name = suggested_name.map(NSString::from_str);
let (done_tx, done_rx) = oneshot::channel();
- let marker = self.1;
self.foreground_executor()
.spawn(async move {
- let panel = NSSavePanel::savePanel(marker);
+ // Like openPanel, savePanel may return nil despite its nonnull annotation.
+ // The foreground executor ensures this call runs on the main thread.
+ // TODO: Switch back to NSSavePanel::savePanel when objc2-app-kit 0.4
+ // is published and adopted; it fixes the return type to be nullable:
+ // https://github.com/madsmtm/objc2/commit/b92857df57aa85dc41e9887534b1ccfa3fe902e4
+ let panel: Option<Retained<NSSavePanel>> =
+ unsafe { msg_send![class!(NSSavePanel), savePanel] };
+ let Some(panel) = panel else {
+ // The receiver may have been dropped while the panel was being created.
+ _ = done_tx.send(Err(anyhow!("macOS could not create the save dialog")));
+ return;
+ };
panel.setDirectoryURL(url.as_deref());
if let Some(suggested_name) = suggested_name {