tauri_plugin_pldownloader/
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;
16mod fs;
17
18pub use error::{Error, Result};
19
20#[cfg(desktop)]
21use desktop::Pldownloader;
22#[cfg(mobile)]
23use mobile::Pldownloader;
24
25/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the pldownloader APIs.
26pub trait PldownloaderExt<R: Runtime> {
27  fn pldownloader(&self) -> &Pldownloader<R>;
28}
29
30impl<R: Runtime, T: Manager<R>> crate::PldownloaderExt<R> for T {
31  fn pldownloader(&self) -> &Pldownloader<R> {
32    self.state::<Pldownloader<R>>().inner()
33  }
34}
35
36/// Initializes the plugin.
37pub fn init<R: Runtime>() -> TauriPlugin<R> {
38  Builder::new("pldownloader")
39    .invoke_handler(tauri::generate_handler![
40      commands::ping,
41      commands::download_private,
42      commands::download_public,
43      commands::save_file_private_from_buffer,
44      commands::save_file_public_from_buffer,
45      fs::copy_file_path
46    ])
47    .setup(|app, api| {
48      #[cfg(mobile)]
49      let pldownloader = mobile::init(app, api)?;
50      #[cfg(desktop)]
51      let pldownloader = desktop::init(app, api)?;
52      app.manage(pldownloader);
53      Ok(())
54    })
55    .build()
56}