diff --git a/apps/readest-app/src-tauri/Cargo.lock b/apps/readest-app/src-tauri/Cargo.lock index ac82fb43..d6e0b19b 100644 --- a/apps/readest-app/src-tauri/Cargo.lock +++ b/apps/readest-app/src-tauri/Cargo.lock @@ -26,6 +26,7 @@ dependencies = [ "tauri-plugin-os", "tauri-plugin-process", "tauri-plugin-shell", + "tauri-plugin-single-instance", "tauri-plugin-updater", ] @@ -5239,6 +5240,21 @@ dependencies = [ "tokio", ] +[[package]] +name = "tauri-plugin-single-instance" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f36019ee9832dc99e4450bb55a21cfad8633b19c2c18bd17c7741939b070ede" +dependencies = [ + "serde", + "serde_json", + "tauri", + "thiserror 2.0.9", + "tracing", + "windows-sys 0.59.0", + "zbus 4.4.0", +] + [[package]] name = "tauri-plugin-updater" version = "2.3.0" diff --git a/apps/readest-app/src-tauri/Cargo.toml b/apps/readest-app/src-tauri/Cargo.toml index 1efe0330..b2bb0bce 100644 --- a/apps/readest-app/src-tauri/Cargo.toml +++ b/apps/readest-app/src-tauri/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Bilingify LLC"] license = "" repository = "" edition = "2021" -rust-version = "1.71" +rust-version = "1.77.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -45,4 +45,5 @@ rand = "0.8" [target.'cfg(any(target_os = "macos", windows, target_os = "linux"))'.dependencies] tauri-plugin-cli = "2" +tauri-plugin-single-instance = "2.2.0" tauri-plugin-updater = "2" diff --git a/apps/readest-app/src-tauri/src/lib.rs b/apps/readest-app/src-tauri/src/lib.rs index 3a5465d0..21e216bc 100644 --- a/apps/readest-app/src-tauri/src/lib.rs +++ b/apps/readest-app/src-tauri/src/lib.rs @@ -70,11 +70,16 @@ async fn start_server(window: Window) -> Result { .map_err(|err| err.to_string()) } +#[derive(Clone, serde::Serialize)] +struct Payload { + args: Vec, + cwd: String, +} + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { let builder = tauri::Builder::default() .plugin(tauri_plugin_process::init()) - .plugin(tauri_plugin_deep_link::init()) .plugin(tauri_plugin_oauth::init()) .invoke_handler(tauri::generate_handler![start_server]) .plugin(tauri_plugin_shell::init()) @@ -84,6 +89,17 @@ pub fn run() { .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_fs::init()); + #[cfg(desktop)] + let builder = builder.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| { + let _ = app + .get_webview_window("main") + .expect("no main window") + .set_focus(); + app.emit("single-instance", Payload { args: argv, cwd }).unwrap(); + })); + + let builder = builder.plugin(tauri_plugin_deep_link::init()); + #[cfg(desktop)] let builder = builder.plugin(tauri_plugin_updater::Builder::new().build()); @@ -136,6 +152,12 @@ pub fn run() { }); } + #[cfg(any(windows, target_os = "linux"))] + { + use tauri_plugin_deep_link::DeepLinkExt; + app.deep_link().register_all()?; + } + #[cfg(desktop)] if cfg!(debug_assertions) { app.handle().plugin( diff --git a/apps/readest-app/src/app/auth/page.tsx b/apps/readest-app/src/app/auth/page.tsx index 6372dd3c..16cfcaae 100644 --- a/apps/readest-app/src/app/auth/page.tsx +++ b/apps/readest-app/src/app/auth/page.tsx @@ -24,6 +24,11 @@ import { handleAuthCallback } from '@/helpers/auth'; type OAuthProvider = 'google' | 'apple' | 'azure' | 'github'; +interface SingleInstancePayload { + args: string[]; + cwd: string; +} + interface ProviderLoginProp { provider: OAuthProvider; handleSignIn: (provider: OAuthProvider) => void; @@ -97,6 +102,15 @@ export default function AuthPage() { const startTauriOAuth = async () => { try { if (process.env.NODE_ENV === 'production') { + const { getCurrentWindow } = await import('@tauri-apps/api/window'); + const currentWindow = getCurrentWindow(); + currentWindow.listen('single-instance', ({ event, payload }) => { + console.log('Received deep link:', event, payload); + const { args } = payload as SingleInstancePayload; + if (args?.[1]) { + handleOAuthUrl(args[1]); + } + }); await onOpenUrl((urls) => { urls.forEach((url) => { handleOAuthUrl(url); diff --git a/apps/readest-app/src/app/library/page.tsx b/apps/readest-app/src/app/library/page.tsx index de4476fd..9623290d 100644 --- a/apps/readest-app/src/app/library/page.tsx +++ b/apps/readest-app/src/app/library/page.tsx @@ -69,9 +69,14 @@ const LibraryPage = () => { async (appService: AppService, openWithFiles: string[], libraryBooks: Book[]) => { const bookIds: string[] = []; for (const file of openWithFiles) { - const book = await appService.importBook(file, libraryBooks); - if (book) { - bookIds.push(book.hash); + console.log('Open with book:', file); + try { + const book = await appService.importBook(file, libraryBooks); + if (book) { + bookIds.push(book.hash); + } + } catch (error) { + console.log('Failed to import book:', file, error); } } setLibrary(libraryBooks);