From 015b8db1c307fcfe735fa77455ce5affaac1645f Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 14 Feb 2023 15:14:15 +0200 Subject: [PATCH] Introduce reveal_path in Platform And implement it for MacPlatform, and instead of using an external process to run `open`, use the NSWorkspace selectFile instance method. --- crates/gpui/src/platform.rs | 1 + crates/gpui/src/platform/mac/platform.rs | 13 +++++++++++++ crates/gpui/src/platform/test.rs | 2 ++ crates/project_panel/src/project_panel.rs | 5 +++-- crates/util/src/lib.rs | 12 ------------ 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 57e8f89539033fb69a28ac8fcd34087553213d47..4753450110c7430c245be0ff4337c902cc130ffa 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -65,6 +65,7 @@ pub trait Platform: Send + Sync { fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; fn open_url(&self, url: &str); + fn reveal_path(&self, path: &Path); fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>; fn read_credentials(&self, url: &str) -> Result)>>; diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index 5d132275854c8b92e317b8226cf4d869dd92f422..ec15af3bd8ec9c4e999a312806e40ac21db329bd 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -599,6 +599,19 @@ impl platform::Platform for MacPlatform { } } + fn reveal_path(&self, path: &Path) { + unsafe { + let full_path = ns_string(path.to_str().unwrap_or("")); + let root_full_path = ns_string(""); + let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; + msg_send![ + workspace, + selectFile: full_path + inFileViewerRootedAtPath: root_full_path + ] + } + } + fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { let url = CFString::from(url); let username = CFString::from(username); diff --git a/crates/gpui/src/platform/test.rs b/crates/gpui/src/platform/test.rs index aa73aebc90d67e7fffdaca4c967d2a7979872d6f..d0cabd8bc1e22ac2e6fd68a42d06802ee9582526 100644 --- a/crates/gpui/src/platform/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -173,6 +173,8 @@ impl super::Platform for Platform { fn open_url(&self, _: &str) {} + fn reveal_path(&self, _: &Path) {} + fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> { Ok(()) } diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index a5ca4077f7bb51f63b1e7006a7bd9afbbda8d53d..f2330dfd4fb65400ed9292d74bcabddde1f6973c 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -791,8 +791,9 @@ impl ProjectPanel { } fn reveal_in_finder(&mut self, _: &RevealInFinder, cx: &mut ViewContext) { - if let Some((_worktree, entry)) = self.selected_entry(cx) { - util::reveal_in_finder(&entry.path); + if let Some((worktree, entry)) = self.selected_entry(cx) { + cx.platform() + .reveal_path(&worktree.abs_path().join(&entry.path)); } } diff --git a/crates/util/src/lib.rs b/crates/util/src/lib.rs index c3376f2e786a97630fc2d0e6cf17657ca357a7ac..65af53f8c52a22fe8dddae41adfe254c1ddbce92 100644 --- a/crates/util/src/lib.rs +++ b/crates/util/src/lib.rs @@ -65,18 +65,6 @@ pub fn open>(path: P) { } } -pub fn reveal_in_finder>(path: P) { - let path_to_reveal = path.as_ref().to_string_lossy(); - #[cfg(target_os = "macos")] - { - std::process::Command::new("open") - .arg("-R") // To reveal in Finder instead of opening the file - .arg(path_to_reveal.as_ref()) - .spawn() - .log_err(); - } -} - pub fn post_inc + AddAssign + Copy>(value: &mut T) -> T { let prev = *value; *value += T::from(1);