fix opening files on macOS

This commit is contained in:
Skyler Lehmkuhl 2025-01-18 03:13:15 -05:00
parent 420443dc59
commit 7b6dbf21c2
2 changed files with 90 additions and 78 deletions

View File

@ -126,17 +126,15 @@ fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {
pub fn run() {
let pkg_name = env!("CARGO_PKG_NAME").to_string();
tauri::Builder::default()
.manage(Mutex::new(AppState::default()))
.setup(|app| {
{
app.manage(Mutex::new(AppState::default()));
}
#[cfg(any(windows, target_os = "linux"))] // Windows/Linux needs different handling from macOS
{
let mut files = Vec::new();
// NOTICE: `args` may include URL protocol (`your-app-protocol://`)
// or arguments (`--`) if your app supports them.
// files may aslo be passed as `file://path/to/file`
// files may also be passed as `file://path/to/file`
for maybe_file in std::env::args().skip(1) {
// skip flags like -f or --flag
if maybe_file.starts_with('-') {
@ -199,12 +197,21 @@ pub fn run() {
|app, event| {
#[cfg(any(target_os = "macos", target_os = "ios"))]
if let tauri::RunEvent::Opened { urls } = event {
let app = app.clone();
let files = urls
.into_iter()
.filter_map(|url| url.to_file_path().ok())
.map(|f| {
let file = f.to_string_lossy().replace('\\', "\\\\"); // escape backslash
format!("\"{file}\"",) // wrap in quotes for JS array
})
.collect::<Vec<_>>();
handle_file_associations(app.clone(), files);
tauri::async_runtime::spawn(async move {
for path in files {
create_window(app.clone(), Some(path)).await;
}
});
}
},
);

View File

@ -7896,6 +7896,11 @@ async function renderMenu() {
enabled: false,
action: () => {},
},
{
text: "Close Window",
enabled: true,
action: quit,
},
{
text: "Quit Lightningbeam",
enabled: true,