tauri_plugin_sharekit/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(desktop)]
9mod desktop;
10#[cfg(mobile)]
11mod mobile;
12
13mod commands;
14mod error;
15mod models;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::ShareKit;
21#[cfg(mobile)]
22use mobile::ShareKit;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the share APIs.
25pub trait ShareExt<R: Runtime> {
26    fn share(&self) -> &ShareKit<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::ShareExt<R> for T {
30    fn share(&self) -> &ShareKit<R> {
31        self.state::<ShareKit<R>>().inner()
32    }
33}
34
35/// Initializes the plugin.
36pub fn init<R: Runtime>() -> TauriPlugin<R> {
37    Builder::new("sharekit")
38        .invoke_handler(tauri::generate_handler![
39            commands::share_text,
40            commands::share_file
41        ])
42        .setup(|app, api| {
43            #[cfg(mobile)]
44            let share = mobile::init(app, api)?;
45            #[cfg(desktop)]
46            let share = desktop::init(app, api)?;
47            app.manage(share);
48            Ok(())
49        })
50        .build()
51}