From dfa747854c210f485d9f8fd33b729d49d739a9f6 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 12 Sep 2024 09:52:38 -0300 Subject: [PATCH] fix(deep-link): emit new-url event on app load --- .changes/deep-link-event.md | 6 ++ .../apple/deep-link-example_iOS/Info.plist | 2 +- .../deep-link-example_iOS.entitlements | 1 - .../examples/app/src-tauri/src/lib.rs | 13 +++- plugins/deep-link/src/lib.rs | 64 ++++++++----------- 5 files changed, 42 insertions(+), 44 deletions(-) create mode 100644 .changes/deep-link-event.md diff --git a/.changes/deep-link-event.md b/.changes/deep-link-event.md new file mode 100644 index 000000000..15d45f049 --- /dev/null +++ b/.changes/deep-link-event.md @@ -0,0 +1,6 @@ +--- +"deep-link": patch +--- + +Emit the `deep-link://new-url` event on Linux and Windows when the app is executed with a deep link CLI argument, +matching the iOS and macOS behavior. diff --git a/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/Info.plist b/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/Info.plist index c658e0ff4..7ce866140 100644 --- a/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/Info.plist +++ b/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/Info.plist @@ -17,7 +17,7 @@ CFBundleShortVersionString 0.0.0 CFBundleVersion - 0.0.0 + 0.1.0 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/deep-link-example_iOS.entitlements b/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/deep-link-example_iOS.entitlements index 72f5d991b..3216c743b 100644 --- a/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/deep-link-example_iOS.entitlements +++ b/plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/deep-link-example_iOS.entitlements @@ -6,7 +6,6 @@ applinks:fabianlars.de applinks:tauri.app - applinks:91f4-177-23-156-161.ngrok-free.app \ No newline at end of file diff --git a/plugins/deep-link/examples/app/src-tauri/src/lib.rs b/plugins/deep-link/examples/app/src-tauri/src/lib.rs index f72f28f65..eba3f679a 100644 --- a/plugins/deep-link/examples/app/src-tauri/src/lib.rs +++ b/plugins/deep-link/examples/app/src-tauri/src/lib.rs @@ -12,10 +12,17 @@ fn greet(name: &str) -> String { #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { - tauri::Builder::default() - .plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| { + #[allow(unused_mut)] + let mut builder = tauri::Builder::default(); + + #[cfg(desktop)] + { + builder = builder.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| { println!("single instance triggered: {argv:?}"); - })) + })); + } + + builder .plugin(tauri_plugin_deep_link::init()) .plugin( tauri_plugin_log::Builder::default() diff --git a/plugins/deep-link/src/lib.rs b/plugins/deep-link/src/lib.rs index 7b0e827a4..1fff4e441 100644 --- a/plugins/deep-link/src/lib.rs +++ b/plugins/deep-link/src/lib.rs @@ -70,17 +70,14 @@ fn init_deep_link( #[cfg(desktop)] { let args = std::env::args(); - let current = if let Some(config) = api.config() { - imp::deep_link_from_args(config, args) - } else { - None - }; - - Ok(DeepLink { + let deep_link = DeepLink { app: app.clone(), - current: std::sync::Mutex::new(current.map(|url| vec![url])), + current: Default::default(), config: api.config().clone(), - }) + }; + deep_link.handle_cli_arguments(args); + + Ok(deep_link) } } @@ -179,31 +176,6 @@ mod imp { pub(crate) config: Option, } - pub(crate) fn deep_link_from_args, I: Iterator>( - config: &crate::config::Config, - mut args: I, - ) -> Option { - if cfg!(windows) || cfg!(target_os = "linux") { - args.next(); // bin name - let arg = args.next(); - - let maybe_deep_link = args.next().is_none(); // single argument - if !maybe_deep_link { - return None; - } - - if let Some(url) = arg.and_then(|arg| arg.as_ref().parse::().ok()) { - if config.desktop.contains_scheme(&url.scheme().to_string()) { - return Some(url); - } else if cfg!(debug_assertions) { - log::warn!("argument {url} does not match any configured deep link scheme; skipping it"); - } - } - } - - None - } - impl DeepLink { /// Checks if the provided list of arguments (which should match [`std::env::args`]) /// contains a deep link argument (for Linux and Windows). @@ -216,17 +188,31 @@ mod imp { /// /// This function updates the [`Self::get_current`] value and emits a `deep-link://new-url` event. #[cfg(desktop)] - pub fn handle_cli_arguments, I: Iterator>(&self, args: I) { + pub fn handle_cli_arguments, I: Iterator>(&self, mut args: I) { use tauri::Emitter; let Some(config) = &self.config else { return; }; - if let Some(url) = deep_link_from_args(config, args) { - let mut current = self.current.lock().unwrap(); - current.replace(vec![url.clone()]); - let _ = self.app.emit("deep-link://new-url", vec![url]); + if cfg!(windows) || cfg!(target_os = "linux") { + args.next(); // bin name + let arg = args.next(); + + let maybe_deep_link = args.next().is_none(); // single argument + if !maybe_deep_link { + return; + } + + if let Some(url) = arg.and_then(|arg| arg.as_ref().parse::().ok()) { + if config.desktop.contains_scheme(&url.scheme().to_string()) { + let mut current = self.current.lock().unwrap(); + current.replace(vec![url.clone()]); + let _ = self.app.emit("deep-link://new-url", vec![url]); + } else if cfg!(debug_assertions) { + log::warn!("argument {url} does not match any configured deep link scheme; skipping it"); + } + } } }