Fix open with Readest on Windows
This commit is contained in:
@@ -15,19 +15,34 @@ mod tauri_traffic_light_positioner_plugin;
|
||||
use tauri::TitleBarStyle;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use tauri::{AppHandle, Emitter, Listener, Manager, Url};
|
||||
use tauri::{AppHandle, Emitter, Manager, Url};
|
||||
use tauri::{WebviewUrl, WebviewWindowBuilder};
|
||||
use tauri_plugin_dialog;
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {
|
||||
let asset_protocol_scope = app.asset_protocol_scope();
|
||||
let fs_scope = app.fs_scope();
|
||||
for file in &files {
|
||||
let _ = fs_scope.allow_file(file);
|
||||
let _ = asset_protocol_scope.allow_file(file);
|
||||
}
|
||||
#[cfg(desktop)]
|
||||
use tauri::Listener;
|
||||
|
||||
#[cfg(desktop)]
|
||||
fn allow_file_in_scopes(app: &AppHandle, files: Vec<PathBuf>) {
|
||||
let fs_scope = app.fs_scope();
|
||||
let asset_protocol_scope = app.asset_protocol_scope();
|
||||
for file in &files {
|
||||
if let Err(e) = fs_scope.allow_file(&file) {
|
||||
eprintln!("Failed to allow file in fs_scope: {}", e);
|
||||
} else {
|
||||
println!("Allowed file in fs_scope: {:?}", file);
|
||||
}
|
||||
if let Err(e) = asset_protocol_scope.allow_file(&file) {
|
||||
eprintln!("Failed to allow file in asset_protocol_scope: {}", e);
|
||||
} else {
|
||||
println!("Allowed file in asset_protocol_scope: {:?}", file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(desktop)]
|
||||
fn set_window_open_with_files(app: &AppHandle, files: Vec<PathBuf>) {
|
||||
let files = files
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
@@ -37,7 +52,7 @@ fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let window = app.get_webview_window("main").unwrap();
|
||||
let script = format!("window.TAURI_CLI_ARGS = [{}];", files);
|
||||
let script = format!("window.OPEN_WITH_FILES = [{}];", files);
|
||||
if let Err(e) = window.eval(&script) {
|
||||
eprintln!("Failed to set open files variable: {}", e);
|
||||
}
|
||||
@@ -73,16 +88,20 @@ pub fn run() {
|
||||
if let Ok(url) = Url::parse(&maybe_file) {
|
||||
if let Ok(path) = url.to_file_path() {
|
||||
files.push(path);
|
||||
} else {
|
||||
files.push(PathBuf::from(maybe_file))
|
||||
}
|
||||
} else {
|
||||
files.push(PathBuf::from(maybe_file))
|
||||
}
|
||||
}
|
||||
let asset_protocol_scope = app.asset_protocol_scope();
|
||||
let fs_scope = app.fs_scope();
|
||||
for file in &files {
|
||||
let _ = fs_scope.allow_file(file);
|
||||
let _ = asset_protocol_scope.allow_file(file);
|
||||
if !files.is_empty() {
|
||||
let app_handle = app.handle().clone();
|
||||
allow_file_in_scopes(&app_handle, files.clone());
|
||||
app.listen("window-ready", move |_| {
|
||||
println!("Window is ready, proceeding to handle files.");
|
||||
set_window_open_with_files(&app_handle, files.clone());
|
||||
});
|
||||
}
|
||||
}
|
||||
#[cfg(desktop)]
|
||||
@@ -122,19 +141,23 @@ pub fn run() {
|
||||
})
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
.run(|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<_>>();
|
||||
.run(
|
||||
#[allow(unused_variables)]
|
||||
|app_handle, 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<_>>();
|
||||
|
||||
let app_handle = app.clone();
|
||||
app.listen("window-ready", move |_| {
|
||||
println!("Window is ready, proceeding to handle files.");
|
||||
handle_file_associations(app_handle.clone(), files.clone());
|
||||
});
|
||||
}
|
||||
});
|
||||
let app_handler_clone = app_handle.clone();
|
||||
allow_file_in_scopes(&app_handle, files.clone());
|
||||
app_handle.listen("window-ready", move |_| {
|
||||
println!("Window is ready, proceeding to handle files.");
|
||||
set_window_open_with_files(&app_handler_clone, files.clone());
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -79,13 +79,15 @@ const LibraryPage = () => {
|
||||
if (openWithFiles.length > 0) {
|
||||
await processOpenWithFiles(appService, openWithFiles, libraryBooks);
|
||||
} else {
|
||||
clearOpenWithBooks();
|
||||
setLibrary(libraryBooks);
|
||||
}
|
||||
|
||||
clearOpenWithBooks();
|
||||
};
|
||||
|
||||
initLibrary();
|
||||
return () => {
|
||||
clearOpenWithBooks();
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
@@ -137,6 +139,16 @@ const LibraryPage = () => {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (checkOpenWithBooks) {
|
||||
return (
|
||||
loading && (
|
||||
<div className='fixed inset-0 z-50 flex items-center justify-center'>
|
||||
<Spinner loading />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='library-page rounded-window bg-base-200/50 text-base-content flex h-full min-h-screen select-none flex-col overflow-hidden'>
|
||||
<div className='fixed top-0 z-40 w-full'>
|
||||
|
||||
@@ -2,7 +2,7 @@ import { getMatches } from '@tauri-apps/plugin-cli';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
TAURI_CLI_ARGS?: string[];
|
||||
OPEN_WITH_FILES?: string[];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ interface CliArgument {
|
||||
occurrences: number;
|
||||
}
|
||||
|
||||
const parseGlobalArgs = () => {
|
||||
return window.TAURI_CLI_ARGS;
|
||||
const parseWindowOpenWithFiles = () => {
|
||||
return window.OPEN_WITH_FILES;
|
||||
};
|
||||
|
||||
const parseCLIArgs = async () => {
|
||||
const parseCLIOpenWithFiles = async () => {
|
||||
const matches = await getMatches();
|
||||
const args = matches?.args;
|
||||
const files: string[] = [];
|
||||
@@ -32,9 +32,9 @@ const parseCLIArgs = async () => {
|
||||
};
|
||||
|
||||
export const parseOpenWithFiles = async () => {
|
||||
let files = parseGlobalArgs();
|
||||
let files = parseWindowOpenWithFiles();
|
||||
if (!files) {
|
||||
files = await parseCLIArgs();
|
||||
files = await parseCLIOpenWithFiles();
|
||||
}
|
||||
return files;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user