feat(shell): support opening URLs on mobile (#1319)

* feat(shell): support opening URLs on mobile

closes #595

* Update and rename StorePlugin.swift to ShellPlugin.swift

* unwrap

* fix func name (ios)

* use undeprecated func if avail

---------

Co-authored-by: fabianlars <fabianlars@fabianlars.de>
This commit is contained in:
Amr Bashir
2024-05-16 02:09:52 +03:00
committed by GitHub
parent 068b9a22f3
commit f0fb25a9b7
13 changed files with 231 additions and 0 deletions
+3
View File
@@ -8,6 +8,9 @@ use serde::{Serialize, Serializer};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[cfg(mobile)]
#[error(transparent)]
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("current executable path has no parent")]
+30
View File
@@ -35,11 +35,21 @@ mod scope_entry;
pub use error::Error;
type Result<T> = std::result::Result<T, Error>;
#[cfg(mobile)]
use tauri::plugin::PluginHandle;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.shell";
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_shell);
type ChildStore = Arc<Mutex<HashMap<u32, CommandChild>>>;
pub struct Shell<R: Runtime> {
#[allow(dead_code)]
app: AppHandle<R>,
#[cfg(mobile)]
mobile_plugin_handle: PluginHandle<R>,
open_scope: scope::OpenScope,
children: ChildStore,
}
@@ -61,9 +71,20 @@ impl<R: Runtime> Shell<R> {
/// Open a (url) path with a default or specific browser opening program.
///
/// See [`crate::open::open`] for how it handles security-related measures.
#[cfg(desktop)]
pub fn open(&self, path: impl Into<String>, with: Option<open::Program>) -> Result<()> {
open::open(&self.open_scope, path.into(), with).map_err(Into::into)
}
/// Open a (url) path with a default or specific browser opening program.
///
/// See [`crate::open::open`] for how it handles security-related measures.
#[cfg(mobile)]
pub fn open(&self, path: impl Into<String>, _with: Option<open::Program>) -> Result<()> {
self.mobile_plugin_handle
.run_mobile_plugin("open", path.into())
.map_err(Into::into)
}
}
pub trait ShellExt<R: Runtime> {
@@ -89,10 +110,19 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
.setup(|app, api| {
let default_config = config::Config::default();
let config = api.config().as_ref().unwrap_or(&default_config);
#[cfg(target_os = "android")]
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "ShellPlugin")?;
#[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_shell)?;
app.manage(Shell {
app: app.clone(),
children: Default::default(),
open_scope: open_scope(&config.open),
#[cfg(mobile)]
mobile_plugin_handle: handle,
});
Ok(())
})