mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
feat: Add deep link plugin for mobile (#504)
Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: Lucas Nogueira <lucas@tauri.app> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_DEEPLINK__=function(e){"use strict";var t=Object.defineProperty,n=(e,n)=>{for(var r in n)t(e,r,{get:n[r],enumerable:!0})},r=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)},i=(e,t,n)=>(r(e,t,"read from private field"),n?n.call(e):t.get(e)),a=(e,t,n,i)=>(r(e,t,"write to private field"),i?i.call(e,n):t.set(e,n),n);function o(e,t=!1){let n=window.crypto.getRandomValues(new Uint32Array(1))[0],r=`_${n}`;return Object.defineProperty(window,r,{value:n=>(t&&Reflect.deleteProperty(window,r),e?.(n)),writable:!1,configurable:!0}),n}n({},{Channel:()=>s,PluginListener:()=>c,addPluginListener:()=>u,convertFileSrc:()=>d,invoke:()=>_,transformCallback:()=>o});var l,s=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,t,n)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,n)})(this,l,(()=>{})),this.id=o((e=>{i(this,l).call(this,e)}))}set onmessage(e){a(this,l,e)}get onmessage(){return i(this,l)}toJSON(){return`__CHANNEL__:${this.id}`}};l=new WeakMap;var c=class{constructor(e,t,n){this.plugin=e,this.event=t,this.channelId=n}async unregister(){return _(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function u(e,t,n){let r=new s;return r.onmessage=n,_(`plugin:${e}|register_listener`,{event:t,handler:r}).then((()=>new c(e,t,r.id)))}async function _(e,t={},n){return new Promise(((r,i)=>{let a=o((e=>{r(e),Reflect.deleteProperty(window,`_${l}`)}),!0),l=o((e=>{i(e),Reflect.deleteProperty(window,`_${a}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:a,error:l,payload:t,options:n})}))}function d(e,t="asset"){return window.__TAURI__.convertFileSrc(e,t)}n({},{TauriEvent:()=>w,emit:()=>f,listen:()=>E,once:()=>p});var w=(e=>(e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_CREATED="tauri://window-created",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_FILE_DROP="tauri://file-drop",e.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",e.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",e.MENU="tauri://menu",e))(w||{});async function h(e,t){await _("plugin:event|unlisten",{event:e,eventId:t})}async function E(e,t,n){return _("plugin:event|listen",{event:e,windowLabel:n?.target,handler:o(t)}).then((t=>async()=>h(e,t)))}async function p(e,t,n){return E(e,(n=>{t(n),h(e,n.id).catch((()=>{}))}),n)}async function f(e,t,n){await _("plugin:event|emit",{event:e,windowLabel:n?.target,payload:t})}async function I(){return await _("plugin:deep-link|get_current")}return e.getCurrent=I,e.onOpenUrl=async function(e){const t=await I();return null!=t&&e(t),await E("deep-link://new-url",(t=>e(t.payload)))},e}({});Object.defineProperty(window.__TAURI__,"deepLink",{value:__TAURI_DEEPLINK__})}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri::{command, AppHandle, Runtime, State, Window};
|
||||
|
||||
use crate::{DeepLink, Result};
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn get_current<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
_window: Window<R>,
|
||||
deep_link: State<'_, DeepLink<R>>,
|
||||
) -> Result<Option<Vec<url::Url>>> {
|
||||
deep_link.get_current()
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// This module is also imported in build.rs!
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct AssociatedDomain {
|
||||
#[serde(deserialize_with = "deserialize_associated_host")]
|
||||
pub host: String,
|
||||
#[serde(default, alias = "path-prefix", rename = "pathPrefix")]
|
||||
pub path_prefix: Vec<String>,
|
||||
}
|
||||
|
||||
fn deserialize_associated_host<'de, D>(deserializer: D) -> Result<String, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let host = String::deserialize(deserializer)?;
|
||||
if let Some((scheme, _host)) = host.split_once("://") {
|
||||
Err(serde::de::Error::custom(format!(
|
||||
"host `{host}` cannot start with a scheme, please remove the `{scheme}://` prefix"
|
||||
)))
|
||||
} else {
|
||||
Ok(host)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub domains: Vec<AssociatedDomain>,
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[cfg(mobile)]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{
|
||||
plugin::{Builder, PluginApi, TauriPlugin},
|
||||
AppHandle, Manager, Runtime,
|
||||
};
|
||||
|
||||
mod commands;
|
||||
mod config;
|
||||
mod error;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.deep_link";
|
||||
|
||||
fn init_deep_link<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
_api: PluginApi<R, C>,
|
||||
) -> crate::Result<DeepLink<R>> {
|
||||
#[cfg(target_os = "android")]
|
||||
{
|
||||
use tauri::ipc::{Channel, InvokeBody};
|
||||
|
||||
let handle = _api.register_android_plugin(PLUGIN_IDENTIFIER, "DeepLinkPlugin")?;
|
||||
|
||||
let app_handle = app.clone();
|
||||
handle.run_mobile_plugin::<()>(
|
||||
"setEventHandler",
|
||||
imp::EventHandler {
|
||||
handler: Channel::new(move |event| {
|
||||
println!("got channel event: {:?}", &event);
|
||||
|
||||
let url = match event {
|
||||
InvokeBody::Json(payload) => payload
|
||||
.get("url")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_owned()),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let payload = vec![url];
|
||||
app_handle.trigger_global(
|
||||
"deep-link://new-url",
|
||||
Some(serde_json::to_string(&payload).unwrap()),
|
||||
);
|
||||
let _ = app_handle.emit_all("deep-link://new-url", payload);
|
||||
Ok(())
|
||||
}),
|
||||
},
|
||||
)?;
|
||||
|
||||
return Ok(DeepLink(handle));
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
Ok(DeepLink {
|
||||
app: app.clone(),
|
||||
current: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
mod imp {
|
||||
use tauri::{plugin::PluginHandle, Runtime};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::ipc::Channel;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EventHandler {
|
||||
pub handler: Channel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct GetCurrentResponse {
|
||||
pub url: Option<url::Url>,
|
||||
}
|
||||
|
||||
/// Access to the deep-link APIs.
|
||||
pub struct DeepLink<R: Runtime>(pub(crate) PluginHandle<R>);
|
||||
|
||||
impl<R: Runtime> DeepLink<R> {
|
||||
/// Get the current URLs that triggered the deep link.
|
||||
pub fn get_current(&self) -> crate::Result<Option<Vec<url::Url>>> {
|
||||
self.0
|
||||
.run_mobile_plugin::<GetCurrentResponse>("getCurrent", ())
|
||||
.map(|v| v.url.map(|url| vec![url]))
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
mod imp {
|
||||
use std::sync::Mutex;
|
||||
use tauri::{AppHandle, Runtime};
|
||||
|
||||
/// Access to the deep-link APIs.
|
||||
pub struct DeepLink<R: Runtime> {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) app: AppHandle<R>,
|
||||
pub(crate) current: Mutex<Option<Vec<url::Url>>>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> DeepLink<R> {
|
||||
/// Get the current URLs that triggered the deep link.
|
||||
pub fn get_current(&self) -> crate::Result<Option<Vec<url::Url>>> {
|
||||
Ok(self.current.lock().unwrap().clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use imp::DeepLink;
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the deep-link APIs.
|
||||
pub trait DeepLinkExt<R: Runtime> {
|
||||
fn deep_link(&self) -> &DeepLink<R>;
|
||||
}
|
||||
|
||||
impl<R: Runtime, T: Manager<R>> crate::DeepLinkExt<R> for T {
|
||||
fn deep_link(&self) -> &DeepLink<R> {
|
||||
self.state::<DeepLink<R>>().inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
|
||||
Builder::new("deep-link")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![commands::get_current])
|
||||
.setup(|app, api| {
|
||||
app.manage(init_deep_link(app, api)?);
|
||||
Ok(())
|
||||
})
|
||||
.on_event(|_app, _event| {
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
if let tauri::RunEvent::Opened { urls } = _event {
|
||||
let _ = _app.emit_all("deep-link://new-url", urls);
|
||||
_app.state::<DeepLink<R>>()
|
||||
.current
|
||||
.lock()
|
||||
.unwrap()
|
||||
.replace(urls.clone());
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
Reference in New Issue
Block a user