Skip to main content

tauri_plugin_haptics/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Trigger haptic feedback on supported devices.
6//!
7//! ## Cargo features
8//!
9//! - **specta**: Add support for [`specta::specta`](https://docs.rs/specta/2.0.0-rc.25/specta/attr.specta.html) on structs like [`ImpactFeedbackStyle`], [`NotificationFeedbackType`].
10
11use tauri::{
12    Manager, Runtime,
13    plugin::{Builder, TauriPlugin},
14};
15
16pub use models::*;
17
18#[cfg(desktop)]
19mod desktop;
20#[cfg(mobile)]
21mod mobile;
22
23mod commands;
24mod error;
25mod models;
26
27pub use error::{Error, Result};
28
29#[cfg(desktop)]
30pub use desktop::Haptics;
31#[cfg(mobile)]
32pub use mobile::Haptics;
33
34/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the haptics APIs.
35pub trait HapticsExt<R: Runtime> {
36    /// Returns a handle to the [`Haptics`] APIs.
37    fn haptics(&self) -> &Haptics<R>;
38}
39
40impl<R: Runtime, T: Manager<R>> crate::HapticsExt<R> for T {
41    fn haptics(&self) -> &Haptics<R> {
42        self.state::<Haptics<R>>().inner()
43    }
44}
45
46/// Initializes the plugin.
47pub fn init<R: Runtime>() -> TauriPlugin<R> {
48    Builder::new("haptics")
49        .invoke_handler(tauri::generate_handler![
50            commands::vibrate,
51            commands::impact_feedback,
52            commands::notification_feedback,
53            commands::selection_feedback
54        ])
55        .setup(|app, api| {
56            #[cfg(mobile)]
57            let haptics = mobile::init(app, api)?;
58            #[cfg(desktop)]
59            let haptics = desktop::init(app, api)?;
60            app.manage(haptics);
61            Ok(())
62        })
63        .build()
64}