Skip to main content

tauri_plugin_video/
lib.rs

1use tauri::{
2    plugin::{Builder as TauriBuilder, TauriPlugin},
3    Manager, Runtime,
4};
5
6pub use models::*;
7
8mod commands;
9#[cfg(desktop)]
10mod desktop;
11mod error;
12#[cfg(mobile)]
13mod mobile;
14mod models;
15
16pub use error::{Error, Result};
17
18/// Runtime state shared by the Rust commands on every supported platform.
19pub struct Video<R: Runtime> {
20    _app: tauri::AppHandle<R>,
21    #[cfg(desktop)]
22    desktop: desktop::DesktopVideo<R>,
23    #[cfg(mobile)]
24    mobile: mobile::MobileVideo<R>,
25}
26
27impl<R: Runtime> Video<R> {
28    fn new(
29        app: tauri::AppHandle<R>,
30        #[cfg(desktop)] desktop: desktop::DesktopVideo<R>,
31        #[cfg(mobile)] mobile: mobile::MobileVideo<R>,
32    ) -> Self {
33        Self {
34            _app: app,
35            #[cfg(desktop)]
36            desktop,
37            #[cfg(mobile)]
38            mobile,
39        }
40    }
41
42    #[cfg(desktop)]
43    pub(crate) fn desktop(&self) -> &desktop::DesktopVideo<R> {
44        &self.desktop
45    }
46
47    #[cfg(mobile)]
48    pub(crate) fn mobile(&self) -> &mobile::MobileVideo<R> {
49        &self.mobile
50    }
51}
52
53/// Extensions to Tauri managers for accessing the video plugin state from Rust.
54pub trait VideoExt<R: Runtime> {
55    fn video(&self) -> tauri::State<'_, Video<R>>;
56}
57
58impl<R: Runtime, T: Manager<R>> VideoExt<R> for T {
59    fn video(&self) -> tauri::State<'_, Video<R>> {
60        self.state::<Video<R>>()
61    }
62}
63
64/// Plugin builder.
65pub struct Builder;
66
67impl Default for Builder {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73impl Builder {
74    pub fn new() -> Self {
75        Self
76    }
77
78    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
79        TauriBuilder::new("video")
80            .invoke_handler(tauri::generate_handler![
81                commands::native_open,
82                commands::native_control,
83                commands::native_layout,
84                commands::native_stats,
85                commands::native_close,
86            ])
87            .setup(move |app, api| {
88                #[cfg(mobile)]
89                let mobile = mobile::init(app, api)?;
90                #[cfg(desktop)]
91                let desktop = desktop::init(app, api)?;
92
93                let video = Video::new(
94                    app.clone(),
95                    #[cfg(desktop)]
96                    desktop,
97                    #[cfg(mobile)]
98                    mobile,
99                );
100                app.manage(video);
101                Ok(())
102            })
103            .build()
104    }
105}
106
107/// Initializes the plugin with production defaults.
108pub fn init<R: Runtime>() -> TauriPlugin<R> {
109    Builder::new().build()
110}