Skip to main content

tauri_plugin_user_input/
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;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::UserInput;
21#[cfg(mobile)]
22use mobile::UserInput;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the user-input APIs.
25pub trait UserInputExt<R: Runtime> {
26    fn user_input(&self) -> &UserInput<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::UserInputExt<R> for T {
30    fn user_input(&self) -> &UserInput<R> {
31        self.state::<UserInput<R>>().inner()
32    }
33}
34
35/// Initializes the plugin.
36pub fn init<R: Runtime>() -> TauriPlugin<R> {
37    Builder::new("user-input")
38        .invoke_handler(tauri::generate_handler![
39            commands::start_listening,
40            commands::stop_listening,
41            commands::key,
42            commands::text,
43            commands::button,
44            commands::move_mouse,
45            commands::scroll,
46            commands::is_listening,
47            commands::set_window_labels,
48            commands::set_event_types,
49        ])
50        .setup(|app, api| {
51            #[cfg(mobile)]
52            let user_input = mobile::init(app, api)?;
53            #[cfg(desktop)]
54            let user_input = desktop::init(app, api)?;
55            app.manage(user_input);
56            Ok(())
57        })
58        .build()
59}