Add ability to open files with system default application (#17231)
This commit is contained in:
parent
06142f975b
commit
ae3880e71a
10 changed files with 63 additions and 1 deletions
|
@ -657,6 +657,11 @@ impl AppContext {
|
|||
self.platform.reveal_path(path)
|
||||
}
|
||||
|
||||
/// Opens the specified path with the system's default application.
|
||||
pub fn open_with_system(&self, path: &Path) {
|
||||
self.platform.open_with_system(path)
|
||||
}
|
||||
|
||||
/// Returns whether the user has configured scrollbars to auto-hide at the platform level.
|
||||
pub fn should_auto_hide_scrollbars(&self) -> bool {
|
||||
self.platform.should_auto_hide_scrollbars()
|
||||
|
|
|
@ -149,6 +149,7 @@ pub(crate) trait Platform: 'static {
|
|||
) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>>;
|
||||
fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Result<Option<PathBuf>>>;
|
||||
fn reveal_path(&self, path: &Path);
|
||||
fn open_with_system(&self, path: &Path);
|
||||
|
||||
fn on_quit(&self, callback: Box<dyn FnMut()>);
|
||||
fn on_reopen(&self, callback: Box<dyn FnMut()>);
|
||||
|
|
|
@ -351,6 +351,19 @@ impl<P: LinuxClient + 'static> Platform for P {
|
|||
self.reveal_path(path.to_owned());
|
||||
}
|
||||
|
||||
fn open_with_system(&self, path: &Path) {
|
||||
let executor = self.background_executor().clone();
|
||||
let path = path.to_owned();
|
||||
executor
|
||||
.spawn(async move {
|
||||
let _ = std::process::Command::new("xdg-open")
|
||||
.arg(path)
|
||||
.spawn()
|
||||
.expect("Failed to open file with xdg-open");
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn on_quit(&self, callback: Box<dyn FnMut()>) {
|
||||
self.with_common(|common| {
|
||||
common.callbacks.quit = Some(callback);
|
||||
|
|
|
@ -718,6 +718,20 @@ impl Platform for MacPlatform {
|
|||
}
|
||||
}
|
||||
|
||||
fn open_with_system(&self, path: &Path) {
|
||||
let path = path.to_path_buf();
|
||||
self.0
|
||||
.lock()
|
||||
.background_executor
|
||||
.spawn(async move {
|
||||
std::process::Command::new("open")
|
||||
.arg(path)
|
||||
.spawn()
|
||||
.expect("Failed to open file");
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn on_quit(&self, callback: Box<dyn FnMut()>) {
|
||||
self.0.lock().quit = Some(callback);
|
||||
}
|
||||
|
|
|
@ -318,6 +318,10 @@ impl Platform for TestPlatform {
|
|||
fn register_url_scheme(&self, _: &str) -> Task<anyhow::Result<()>> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn open_with_system(&self, _path: &Path) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
|
|
|
@ -400,6 +400,19 @@ impl Platform for WindowsPlatform {
|
|||
.detach();
|
||||
}
|
||||
|
||||
fn open_with_system(&self, path: &Path) {
|
||||
let executor = self.background_executor().clone();
|
||||
let path = path.to_owned();
|
||||
executor
|
||||
.spawn(async move {
|
||||
let _ = std::process::Command::new("cmd")
|
||||
.args(&["/c", "start", "", path.to_str().expect("path to string")])
|
||||
.spawn()
|
||||
.expect("Failed to open file");
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn on_quit(&self, callback: Box<dyn FnMut()>) {
|
||||
self.state.borrow_mut().callbacks.quit = Some(callback);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue