Get open-with files from command line
This commit is contained in:
parent
2d355a13b9
commit
15fe7d1283
File diff suppressed because it is too large
Load Diff
|
|
@ -1,10 +1,10 @@
|
|||
use std::sync::{Mutex};
|
||||
use std::{path::PathBuf, sync::Mutex};
|
||||
|
||||
use tauri_plugin_log::{Target, TargetKind};
|
||||
use log::{trace, info, debug, warn, error};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use chrono::Local;
|
||||
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};
|
||||
use tauri::{AppHandle, Emitter, Manager, Url, WebviewUrl, WebviewWindowBuilder};
|
||||
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
@ -56,10 +56,89 @@ async fn create_window(app: tauri::AppHandle) {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {
|
||||
// -- Scope handling start --
|
||||
|
||||
// You can remove this block if you only want to know about the paths, but not actually "use" them in the frontend.
|
||||
|
||||
// This requires the `fs` tauri plugin and is required to make the plugin's frontend work:
|
||||
use tauri_plugin_fs::FsExt;
|
||||
let fs_scope = app.fs_scope();
|
||||
|
||||
// This is for the `asset:` protocol to work:
|
||||
// let asset_protocol_scope = app.asset_protocol_scope();
|
||||
|
||||
for file in &files {
|
||||
// This requires the `fs` plugin:
|
||||
let _ = fs_scope.allow_file(file);
|
||||
|
||||
// This is for the `asset:` protocol:
|
||||
// let _ = asset_protocol_scope.allow_file(file);
|
||||
}
|
||||
|
||||
// -- Scope handling end --
|
||||
|
||||
let files = files
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
let file = f.to_string_lossy().replace('\\', "\\\\"); // escape backslash
|
||||
format!("\"{file}\"",) // wrap in quotes for JS array
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
warn!("{}",files);
|
||||
|
||||
// tauri::WebviewWindowBuilder::new(&app, "main", Default::default())
|
||||
// .initialization_script(&format!("window.openedFiles = [{files}]"))
|
||||
// .build()
|
||||
// .unwrap();
|
||||
let window = app.get_webview_window("main").unwrap();
|
||||
window.eval(&format!("window.openedFiles = [{files}]")).unwrap();
|
||||
// window.emit("openedFiles", &format!("[{files}]")).unwrap();
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let pkg_name = env!("CARGO_PKG_NAME").to_string();
|
||||
tauri::Builder::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`
|
||||
for maybe_file in std::env::args().skip(1) {
|
||||
// skip flags like -f or --flag
|
||||
if maybe_file.starts_with('-') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// handle `file://` path urls and skip other urls
|
||||
if let Ok(url) = Url::parse(&maybe_file) {
|
||||
// if let Ok(url) = url::Url::parse(&maybe_file) {
|
||||
if let Ok(path) = url.to_file_path() {
|
||||
files.push(path);
|
||||
}
|
||||
} else {
|
||||
files.push(PathBuf::from(maybe_file))
|
||||
}
|
||||
}
|
||||
|
||||
handle_file_associations(app.handle().clone(), files);
|
||||
}
|
||||
#[cfg(debug_assertions)] // only include this code on debug builds
|
||||
{
|
||||
let window = app.get_webview_window("main").unwrap();
|
||||
window.open_devtools();
|
||||
window.close_devtools();
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.plugin(
|
||||
tauri_plugin_log::Builder::new()
|
||||
.timezone_strategy(tauri_plugin_log::TimezoneStrategy::UseLocal)
|
||||
|
|
@ -81,26 +160,28 @@ pub fn run() {
|
|||
Target::new(TargetKind::LogDir { file_name: Some("logs".to_string()) }),
|
||||
Target::new(TargetKind::Webview),
|
||||
])
|
||||
.build(),
|
||||
.build()
|
||||
)
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.invoke_handler(tauri::generate_handler![greet, trace, debug, info, warn, error, create_window])
|
||||
// .manage(window_counter)
|
||||
.setup(|app| {
|
||||
#[cfg(debug_assertions)] // only include this code on debug builds
|
||||
{
|
||||
let window = app.get_webview_window("main").unwrap();
|
||||
window.open_devtools();
|
||||
window.close_devtools();
|
||||
}
|
||||
{
|
||||
app.manage(Mutex::new(AppState::default()));
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
.run(
|
||||
#[allow(unused_variables)]
|
||||
|app, event| {
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
if let tauri::RunEvent::Opened { urls } = event {
|
||||
let files = urls
|
||||
.into_iter()
|
||||
.filter_map(|url| url.to_file_path().ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
handle_file_associations(app.clone(), files);
|
||||
}
|
||||
},
|
||||
);
|
||||
tracing_subscriber::fmt().with_env_filter(EnvFilter::new(format!("{}=trace", pkg_name))).init();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,12 @@
|
|||
"files": {},
|
||||
"release": "1"
|
||||
}
|
||||
}
|
||||
},
|
||||
"fileAssociations": [
|
||||
{
|
||||
"ext": ["beam"],
|
||||
"mimeType": "text/plain"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue