tauri_plugin_polygon/
lib.rs

1use tauri::AppHandle;
2#[cfg(desktop)]
3use tauri::{
4    plugin::{Builder, TauriPlugin},
5    Manager, Runtime,
6};
7
8mod commands;
9mod desktop;
10mod error;
11mod grab;
12mod models;
13mod polygon;
14mod statics;
15mod utils;
16mod view;
17
18pub use desktop::Polygon;
19pub use error::{Error, Result};
20pub use grab::Event;
21
22/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the polygon APIs.
23pub trait PolygonExt<R: Runtime> {
24    fn polygon(&self) -> &Polygon<R>;
25}
26
27impl<R: Runtime, T: Manager<R>> crate::PolygonExt<R> for T {
28    fn polygon(&self) -> &Polygon<R> {
29        self.state::<Polygon<R>>().inner()
30    }
31}
32
33/// Initializes the plugin.
34pub fn init<R: Runtime, F: FnMut(&AppHandle<R>, crate::Event) + Send + Sync + 'static>(
35    f: F,
36) -> TauriPlugin<R> {
37    Builder::new("polygon")
38        .invoke_handler(tauri::generate_handler![
39            commands::register,
40            commands::register_all,
41            commands::remove,
42            commands::show,
43            commands::hide,
44            commands::update,
45            commands::clear
46        ])
47        .setup(|app, api| {
48            let polygon = desktop::init(app, api, Box::new(f))?;
49            app.manage(polygon);
50            Ok(())
51        })
52        .on_window_ready(move |win| {
53            if win.label() == "main" {
54                statics::init(win.clone());
55                grab::init(win.clone());
56            }
57        })
58        .on_drop(|app| {
59            // Clear all polygon in cache
60            let _ = app.state::<Polygon<R>>().inner().clear();
61            // drop callback
62            let _ = app.state::<Polygon<R>>().inner().destroy();
63        })
64        .build()
65}