mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
fix(deep-link): emit new-url event on app load (#1770)
This commit is contained in:
@@ -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.
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>0.0.0</string>
|
<string>0.0.0</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>0.0.0</string>
|
<string>0.1.0</string>
|
||||||
<key>LSRequiresIPhoneOS</key>
|
<key>LSRequiresIPhoneOS</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>UILaunchStoryboardName</key>
|
<key>UILaunchStoryboardName</key>
|
||||||
|
|||||||
-1
@@ -6,7 +6,6 @@
|
|||||||
<array>
|
<array>
|
||||||
<string>applinks:fabianlars.de</string>
|
<string>applinks:fabianlars.de</string>
|
||||||
<string>applinks:tauri.app</string>
|
<string>applinks:tauri.app</string>
|
||||||
<string>applinks:91f4-177-23-156-161.ngrok-free.app</string>
|
|
||||||
</array>
|
</array>
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
@@ -12,10 +12,17 @@ fn greet(name: &str) -> String {
|
|||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
tauri::Builder::default()
|
#[allow(unused_mut)]
|
||||||
.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
|
let mut builder = tauri::Builder::default();
|
||||||
|
|
||||||
|
#[cfg(desktop)]
|
||||||
|
{
|
||||||
|
builder = builder.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
|
||||||
println!("single instance triggered: {argv:?}");
|
println!("single instance triggered: {argv:?}");
|
||||||
}))
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder
|
||||||
.plugin(tauri_plugin_deep_link::init())
|
.plugin(tauri_plugin_deep_link::init())
|
||||||
.plugin(
|
.plugin(
|
||||||
tauri_plugin_log::Builder::default()
|
tauri_plugin_log::Builder::default()
|
||||||
|
|||||||
@@ -70,17 +70,14 @@ fn init_deep_link<R: Runtime>(
|
|||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
{
|
{
|
||||||
let args = std::env::args();
|
let args = std::env::args();
|
||||||
let current = if let Some(config) = api.config() {
|
let deep_link = DeepLink {
|
||||||
imp::deep_link_from_args(config, args)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(DeepLink {
|
|
||||||
app: app.clone(),
|
app: app.clone(),
|
||||||
current: std::sync::Mutex::new(current.map(|url| vec![url])),
|
current: Default::default(),
|
||||||
config: api.config().clone(),
|
config: api.config().clone(),
|
||||||
})
|
};
|
||||||
|
deep_link.handle_cli_arguments(args);
|
||||||
|
|
||||||
|
Ok(deep_link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,31 +176,6 @@ mod imp {
|
|||||||
pub(crate) config: Option<crate::config::Config>,
|
pub(crate) config: Option<crate::config::Config>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn deep_link_from_args<S: AsRef<str>, I: Iterator<Item = S>>(
|
|
||||||
config: &crate::config::Config,
|
|
||||||
mut args: I,
|
|
||||||
) -> Option<url::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 None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(url) = arg.and_then(|arg| arg.as_ref().parse::<url::Url>().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<R: Runtime> DeepLink<R> {
|
impl<R: Runtime> DeepLink<R> {
|
||||||
/// Checks if the provided list of arguments (which should match [`std::env::args`])
|
/// Checks if the provided list of arguments (which should match [`std::env::args`])
|
||||||
/// contains a deep link argument (for Linux and Windows).
|
/// 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.
|
/// This function updates the [`Self::get_current`] value and emits a `deep-link://new-url` event.
|
||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
pub fn handle_cli_arguments<S: AsRef<str>, I: Iterator<Item = S>>(&self, args: I) {
|
pub fn handle_cli_arguments<S: AsRef<str>, I: Iterator<Item = S>>(&self, mut args: I) {
|
||||||
use tauri::Emitter;
|
use tauri::Emitter;
|
||||||
|
|
||||||
let Some(config) = &self.config else {
|
let Some(config) = &self.config else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(url) = deep_link_from_args(config, args) {
|
if cfg!(windows) || cfg!(target_os = "linux") {
|
||||||
let mut current = self.current.lock().unwrap();
|
args.next(); // bin name
|
||||||
current.replace(vec![url.clone()]);
|
let arg = args.next();
|
||||||
let _ = self.app.emit("deep-link://new-url", vec![url]);
|
|
||||||
|
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::<url::Url>().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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user