Skip to content

Commit 984110a

Browse files
authored
fix(deep-link): emit new-url event on app load (#1770)
1 parent 2b898f0 commit 984110a

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

.changes/deep-link-event.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"deep-link": patch
3+
---
4+
5+
Emit the `deep-link://new-url` event on Linux and Windows when the app is executed with a deep link CLI argument,
6+
matching the iOS and macOS behavior.

plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>0.0.0</string>
1919
<key>CFBundleVersion</key>
20-
<string>0.0.0</string>
20+
<string>0.1.0</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>UILaunchStoryboardName</key>

plugins/deep-link/examples/app/src-tauri/gen/apple/deep-link-example_iOS/deep-link-example_iOS.entitlements

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<array>
77
<string>applinks:fabianlars.de</string>
88
<string>applinks:tauri.app</string>
9-
<string>applinks:91f4-177-23-156-161.ngrok-free.app</string>
109
</array>
1110
</dict>
1211
</plist>

plugins/deep-link/examples/app/src-tauri/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ fn greet(name: &str) -> String {
1212

1313
#[cfg_attr(mobile, tauri::mobile_entry_point)]
1414
pub fn run() {
15-
tauri::Builder::default()
16-
.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
15+
#[allow(unused_mut)]
16+
let mut builder = tauri::Builder::default();
17+
18+
#[cfg(desktop)]
19+
{
20+
builder = builder.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
1721
println!("single instance triggered: {argv:?}");
18-
}))
22+
}));
23+
}
24+
25+
builder
1926
.plugin(tauri_plugin_deep_link::init())
2027
.plugin(
2128
tauri_plugin_log::Builder::default()

plugins/deep-link/src/lib.rs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,14 @@ fn init_deep_link<R: Runtime>(
7070
#[cfg(desktop)]
7171
{
7272
let args = std::env::args();
73-
let current = if let Some(config) = api.config() {
74-
imp::deep_link_from_args(config, args)
75-
} else {
76-
None
77-
};
78-
79-
Ok(DeepLink {
73+
let deep_link = DeepLink {
8074
app: app.clone(),
81-
current: std::sync::Mutex::new(current.map(|url| vec![url])),
75+
current: Default::default(),
8276
config: api.config().clone(),
83-
})
77+
};
78+
deep_link.handle_cli_arguments(args);
79+
80+
Ok(deep_link)
8481
}
8582
}
8683

@@ -179,31 +176,6 @@ mod imp {
179176
pub(crate) config: Option<crate::config::Config>,
180177
}
181178

182-
pub(crate) fn deep_link_from_args<S: AsRef<str>, I: Iterator<Item = S>>(
183-
config: &crate::config::Config,
184-
mut args: I,
185-
) -> Option<url::Url> {
186-
if cfg!(windows) || cfg!(target_os = "linux") {
187-
args.next(); // bin name
188-
let arg = args.next();
189-
190-
let maybe_deep_link = args.next().is_none(); // single argument
191-
if !maybe_deep_link {
192-
return None;
193-
}
194-
195-
if let Some(url) = arg.and_then(|arg| arg.as_ref().parse::<url::Url>().ok()) {
196-
if config.desktop.contains_scheme(&url.scheme().to_string()) {
197-
return Some(url);
198-
} else if cfg!(debug_assertions) {
199-
log::warn!("argument {url} does not match any configured deep link scheme; skipping it");
200-
}
201-
}
202-
}
203-
204-
None
205-
}
206-
207179
impl<R: Runtime> DeepLink<R> {
208180
/// Checks if the provided list of arguments (which should match [`std::env::args`])
209181
/// contains a deep link argument (for Linux and Windows).
@@ -216,17 +188,31 @@ mod imp {
216188
///
217189
/// This function updates the [`Self::get_current`] value and emits a `deep-link://new-url` event.
218190
#[cfg(desktop)]
219-
pub fn handle_cli_arguments<S: AsRef<str>, I: Iterator<Item = S>>(&self, args: I) {
191+
pub fn handle_cli_arguments<S: AsRef<str>, I: Iterator<Item = S>>(&self, mut args: I) {
220192
use tauri::Emitter;
221193

222194
let Some(config) = &self.config else {
223195
return;
224196
};
225197

226-
if let Some(url) = deep_link_from_args(config, args) {
227-
let mut current = self.current.lock().unwrap();
228-
current.replace(vec![url.clone()]);
229-
let _ = self.app.emit("deep-link://new-url", vec![url]);
198+
if cfg!(windows) || cfg!(target_os = "linux") {
199+
args.next(); // bin name
200+
let arg = args.next();
201+
202+
let maybe_deep_link = args.next().is_none(); // single argument
203+
if !maybe_deep_link {
204+
return;
205+
}
206+
207+
if let Some(url) = arg.and_then(|arg| arg.as_ref().parse::<url::Url>().ok()) {
208+
if config.desktop.contains_scheme(&url.scheme().to_string()) {
209+
let mut current = self.current.lock().unwrap();
210+
current.replace(vec![url.clone()]);
211+
let _ = self.app.emit("deep-link://new-url", vec![url]);
212+
} else if cfg!(debug_assertions) {
213+
log::warn!("argument {url} does not match any configured deep link scheme; skipping it");
214+
}
215+
}
230216
}
231217
}
232218

0 commit comments

Comments
 (0)