Skip to main content

tauri_plugin_vnidrop_share/
lib.rs

1//! # tauri-plugin-vnidrop-share
2//!
3//! A Tauri plugin to share content using the native sharing dialog on Windows, macOS, and mobile platforms.
4//!
5//! On desktop, the plugin handles sharing text, URLs, and files. For files, it manages their lifecycle by creating
6//! temporary files from Base64 content and cleaning them up once the share dialog is closed or the application exits.
7//!
8//! ## Installation
9//!
10//! ```sh
11//! # Cargo.toml
12//! [dependencies]
13//! tauri-plugin-vnidrop-share = { git = "[https://github.com/vnidrop/plugin-share](https://github.com/vnidrop/plugin-share)" }
14//! ```
15//!
16//! ## Usage
17//!
18//! ### Rust
19//!
20//! You need to initialize the plugin in your `main.rs` or `lib.rs` to register the commands and set up state management.
21//!
22//! ```rust
23//! // src/main.rs
24//! fn main() {
25//!     tauri::Builder::default()
26//!         .plugin(tauri_plugin_vnidrop_share::init())
27//!         .run(tauri::generate_context!())
28//!         .expect("error while running tauri application");
29//! }
30//! ```
31//!
32//! ### Frontend (JavaScript/TypeScript)
33//!
34//! The plugin provides a JavaScript API to call the commands.
35//!
36//! ```js
37//! import { share, canShare } from '@vnidrop/tauri-plugin-share';
38//!
39//! // Check if sharing is available
40//! const canShareResult = await canShare();
41//! console.log(`Can share on this platform: ${canShareResult}`);
42//!
43//! // Share text and a URL
44//! if (canShareResult) {
45//!   await share({
46//!     title: 'Check this out!',
47//!     text: 'I found this cool project built with Tauri.',
48//!     url: '[https://tauri.app](https://tauri.app)',
49//!   });
50//! }
51//!
52//! // Share a file from Base64 content
53//! // The file will be created as a temporary file and cleaned up automatically.
54//! const fileContent = '...'; // Your Base64-encoded file data
55//! const blob = new Blob([fileContent], { type: 'text/plain' });
56//! await share({
57//!   files: [new File([blob], 'document.txt')],
58//! });
59//! ```
60//!
61
62use tauri::{
63    plugin::{Builder, TauriPlugin},
64    Manager, Runtime,
65};
66
67pub use models::*;
68
69#[cfg(desktop)]
70mod desktop;
71#[cfg(mobile)]
72mod mobile;
73
74mod commands;
75mod error;
76mod models;
77mod platform;
78mod state;
79
80pub use error::{Error, Result};
81
82#[cfg(desktop)]
83use desktop::Share;
84#[cfg(mobile)]
85use mobile::Share;
86
87/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the share APIs.
88pub trait ShareExt<R: Runtime> {
89    fn share(&self) -> &Share<R>;
90}
91
92impl<R: Runtime, T: Manager<R>> crate::ShareExt<R> for T {
93    fn share(&self) -> &Share<R> {
94        self.state::<Share<R>>().inner()
95    }
96}
97
98/// Initializes the plugin.
99///
100/// This function sets up the plugin, registers its commands, and configures the
101/// state management for temporary files. The cleanup of these files is
102/// automatically handled when the application exits.
103pub fn init<R: Runtime>() -> TauriPlugin<R> {
104    Builder::new("vnidrop-share")
105        .invoke_handler(tauri::generate_handler![
106            commands::share,
107            commands::can_share,
108            commands::cleanup,
109        ])
110        .setup(|app, api| {
111            #[cfg(mobile)]
112            let share = mobile::init(app, api)?;
113            #[cfg(desktop)]
114            let share = desktop::init(app, api)?;
115            app.manage(share);
116            app.manage(state::PluginTempFileManager::new());
117            Ok(())
118        })
119        .on_drop(|app| {
120            app.state::<state::PluginTempFileManager>()
121                .cleanup_all_managed_files();
122        })
123        .build()
124}