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 `register_url_scheme` to `objc2` (#63945)

tidely907b55f7eabe
diff --git a/Cargo.toml b/Cargo.toml
index cfe8f56d63f..c1c4824cb52 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -702,6 +702,7 @@ objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"NSResponder",
"NSView",
"NSWindow",
+ "NSWorkspace",
"block2",
"objc2-core-foundation",
] }
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index 3a28477635d..88bf7541f46 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -727,6 +727,10 @@ impl Platform for MacPlatform {
}
fn register_url_scheme(&self, scheme: &str) -> Task<anyhow::Result<()>> {
+ use block2::RcBlock;
+ use objc2_app_kit::NSWorkspace;
+ 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();
@@ -736,43 +740,42 @@ impl Platform for MacPlatform {
)));
}
- let bundle_id = unsafe {
- let bundle: id = msg_send![class!(NSBundle), mainBundle];
- let bundle_id: id = msg_send![bundle, bundleIdentifier];
- if bundle_id == nil {
- return Task::ready(Err(anyhow!("Can only register URL scheme in bundled apps")));
- }
- bundle_id
+ let Some(bundle_id) = NSBundle::mainBundle().bundleIdentifier() else {
+ return Task::ready(Err(anyhow!("Can only register URL scheme in bundled apps")));
};
- unsafe {
- let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
- let scheme: id = ns_string(scheme);
- let app: id = msg_send![workspace, URLForApplicationWithBundleIdentifier: bundle_id];
- if app == nil {
- return Task::ready(Err(anyhow!(
- "Cannot register URL scheme until app is installed"
- )));
+ let workspace = NSWorkspace::sharedWorkspace();
+ let Some(app) = workspace.URLForApplicationWithBundleIdentifier(&bundle_id) else {
+ return Task::ready(Err(anyhow!(
+ "Cannot register URL scheme until app is installed"
+ )));
+ };
+
+ let scheme = NSString::from_str(scheme);
+
+ let done_tx = Cell::new(Some(done_tx));
+ let handler = RcBlock::new(move |error: *mut NSError| {
+ let result = if let Some(error) = unsafe { error.as_ref() } {
+ Err(anyhow!(
+ "Failed to register: {}",
+ error.localizedDescription()
+ ))
+ } else {
+ Ok(())
+ };
+
+ if let Some(done_tx) = done_tx.take() {
+ _ = done_tx.send(result);
}
- let done_tx = Cell::new(Some(done_tx));
- let block = ConcreteBlock::new(move |error: id| {
- let result = if error == nil {
- Ok(())
- } else {
- let msg: id = msg_send![error, localizedDescription];
- Err(anyhow!("Failed to register: {msg:?}"))
- };
-
- if let Some(done_tx) = done_tx.take() {
- let _ = done_tx.send(result);
- }
- });
- let block = block.copy();
- let _: () = msg_send![workspace, setDefaultApplicationAtURL: app toOpenURLsWithScheme: scheme completionHandler: block];
- }
+ });
+
+ workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler(
+ &app,
+ &scheme,
+ Some(&handler),
+ );
- self.background_executor()
- .spawn(async { done_rx.await.map_err(|e| anyhow!(e))? })
+ self.background_executor().spawn(async { done_rx.await? })
}
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {