File size: 2,112 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use serde::{de::DeserializeOwned, Serialize};
use tauri::{
    plugin::{PluginApi, PluginHandle},
    AppHandle, Runtime,
};

use crate::models::*;

#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.haptics";

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_haptics);

// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<Haptics<R>> {
    #[cfg(target_os = "android")]
    let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "HapticsPlugin")?;
    #[cfg(target_os = "ios")]
    let handle = api.register_ios_plugin(init_plugin_haptics)?;
    Ok(Haptics(handle))
}

/// Access to the haptics APIs.
pub struct Haptics<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> Haptics<R> {
    pub fn vibrate(&self, duration: u32) -> crate::Result<()> {
        self.0
            .run_mobile_plugin("vibrate", VibratePayload { duration })
            .map_err(Into::into)
    }

    pub fn impact_feedback(&self, style: ImpactFeedbackStyle) -> crate::Result<()> {
        self.0
            .run_mobile_plugin("impactFeedback", ImpactFeedbackPayload { style })
            .map_err(Into::into)
    }

    pub fn notification_feedback(&self, r#type: NotificationFeedbackType) -> crate::Result<()> {
        self.0
            .run_mobile_plugin(
                "notificationFeedback",
                NotificationFeedbackPayload { r#type },
            )
            .map_err(Into::into)
    }

    pub fn selection_feedback(&self) -> crate::Result<()> {
        self.0
            .run_mobile_plugin("selectionFeedback", ())
            .map_err(Into::into)
    }
}

#[derive(Serialize)]
struct VibratePayload {
    duration: u32,
}

#[derive(Serialize)]
struct ImpactFeedbackPayload {
    style: ImpactFeedbackStyle,
}

#[derive(Serialize)]
struct NotificationFeedbackPayload {
    r#type: NotificationFeedbackType,
}