tauri_plugin_sharekit/
lib.rs

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