snapshot of https://github.com/tauri-apps/plugins-workspace.git (v2, 2026-09-23; blobs > 131072B omitted)
Some checks failed
integration tests / run-integration-tests (windows-latest) (push) Has been cancelled
Test Rust / test (push) Has been cancelled
test plugins e2e (mobile) / ios (push) Has been cancelled
test plugins e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test plugins e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
integration tests / run-integration-tests (macos-latest) (push) Has been cancelled
version or publish / version-or-publish (push) Failing after 0s
integration tests / run-integration-tests (ubuntu-22.04) (push) Failing after 0s
Lint JavaScript / eslint (push) Failing after 0s
Sync / sync-to-mirrors (push) Failing after 0s
test plugins e2e (mobile) / android (push) Failing after 0s
test plugins e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
Test Rust / changes (push) Failing after 0s
Lint Rust / changes (push) Failing after 0s
Lint Rust / clippy (push) Has been skipped
Audit Rust / audit-rust (push) Failing after 1s
Audit JavaScript / audit-js (push) Failing after 1s
Some checks failed
integration tests / run-integration-tests (windows-latest) (push) Has been cancelled
Test Rust / test (push) Has been cancelled
test plugins e2e (mobile) / ios (push) Has been cancelled
test plugins e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test plugins e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
integration tests / run-integration-tests (macos-latest) (push) Has been cancelled
version or publish / version-or-publish (push) Failing after 0s
integration tests / run-integration-tests (ubuntu-22.04) (push) Failing after 0s
Lint JavaScript / eslint (push) Failing after 0s
Sync / sync-to-mirrors (push) Failing after 0s
test plugins e2e (mobile) / android (push) Failing after 0s
test plugins e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
Test Rust / changes (push) Failing after 0s
Lint Rust / changes (push) Failing after 0s
Lint Rust / clippy (push) Has been skipped
Audit Rust / audit-rust (push) Failing after 1s
Audit JavaScript / audit-js (push) Failing after 1s
This commit is contained in:
33
plugins/haptics/src/commands.rs
Normal file
33
plugins/haptics/src/commands.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri::{command, AppHandle, Runtime};
|
||||
|
||||
use crate::{HapticsExt, ImpactFeedbackStyle, NotificationFeedbackType, Result};
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn vibrate<R: Runtime>(app: AppHandle<R>, duration: u32) -> Result<()> {
|
||||
app.haptics().vibrate(duration)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn impact_feedback<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
style: ImpactFeedbackStyle,
|
||||
) -> Result<()> {
|
||||
app.haptics().impact_feedback(style)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn notification_feedback<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
r#type: NotificationFeedbackType,
|
||||
) -> Result<()> {
|
||||
app.haptics().notification_feedback(r#type)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn selection_feedback<R: Runtime>(app: AppHandle<R>) -> Result<()> {
|
||||
app.haptics().selection_feedback()
|
||||
}
|
||||
44
plugins/haptics/src/desktop.rs
Normal file
44
plugins/haptics/src/desktop.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{plugin::PluginApi, AppHandle, Runtime};
|
||||
|
||||
use crate::models::*;
|
||||
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
_api: PluginApi<R, C>,
|
||||
) -> crate::Result<Haptics<R>> {
|
||||
Ok(Haptics(app.clone()))
|
||||
}
|
||||
|
||||
/// Access to the haptics APIs.
|
||||
pub struct Haptics<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> Haptics<R> {
|
||||
/// No-op on desktop; haptic feedback is not supported on Linux, macOS or Windows. Always
|
||||
/// returns `Ok`.
|
||||
pub fn vibrate(&self, _duration: u32) -> crate::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// No-op on desktop; haptic feedback is not supported on Linux, macOS or Windows. Always
|
||||
/// returns `Ok`.
|
||||
pub fn impact_feedback(&self, _style: ImpactFeedbackStyle) -> crate::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// No-op on desktop; haptic feedback is not supported on Linux, macOS or Windows. Always
|
||||
/// returns `Ok`.
|
||||
pub fn notification_feedback(&self, _type: NotificationFeedbackType) -> crate::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// No-op on desktop; haptic feedback is not supported on Linux, macOS or Windows. Always
|
||||
/// returns `Ok`.
|
||||
pub fn selection_feedback(&self) -> crate::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
35
plugins/haptics/src/error.rs
Normal file
35
plugins/haptics/src/error.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
/// Alias for a [`std::result::Result`] with the error type [`Error`].
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// TODO: Improve Error handling (different typed errors instead of one (stringified) PluginInvokeError for all mobile errors)
|
||||
|
||||
/// Errors returned by the haptics APIs.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[cfg_attr(feature = "specta", derive(specta::Type))]
|
||||
pub enum Error {
|
||||
/// The call to the Android or iOS plugin implementation failed, either because the
|
||||
/// arguments could not be serialized, the response could not be deserialized, or the
|
||||
/// native side rejected the call. Only produced on Android and iOS.
|
||||
#[cfg(mobile)]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(
|
||||
#[cfg_attr(feature = "specta", specta(skip))]
|
||||
#[from]
|
||||
tauri::plugin::mobile::PluginInvokeError,
|
||||
),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
64
plugins/haptics/src/lib.rs
Normal file
64
plugins/haptics/src/lib.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//! Trigger haptic feedback on supported devices.
|
||||
//!
|
||||
//! ## Cargo features
|
||||
//!
|
||||
//! - **specta**: Add support for [`specta::specta`](https://docs.rs/specta/2.0.0-rc.25/specta/attr.specta.html) on structs like [`ImpactFeedbackStyle`], [`NotificationFeedbackType`].
|
||||
|
||||
use tauri::{
|
||||
plugin::{Builder, TauriPlugin},
|
||||
Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
|
||||
#[cfg(desktop)]
|
||||
mod desktop;
|
||||
#[cfg(mobile)]
|
||||
mod mobile;
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(desktop)]
|
||||
pub use desktop::Haptics;
|
||||
#[cfg(mobile)]
|
||||
pub use mobile::Haptics;
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the haptics APIs.
|
||||
pub trait HapticsExt<R: Runtime> {
|
||||
/// Returns a handle to the [`Haptics`] APIs.
|
||||
fn haptics(&self) -> &Haptics<R>;
|
||||
}
|
||||
|
||||
impl<R: Runtime, T: Manager<R>> crate::HapticsExt<R> for T {
|
||||
fn haptics(&self) -> &Haptics<R> {
|
||||
self.state::<Haptics<R>>().inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("haptics")
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::vibrate,
|
||||
commands::impact_feedback,
|
||||
commands::notification_feedback,
|
||||
commands::selection_feedback
|
||||
])
|
||||
.setup(|app, api| {
|
||||
#[cfg(mobile)]
|
||||
let haptics = mobile::init(app, api)?;
|
||||
#[cfg(desktop)]
|
||||
let haptics = desktop::init(app, api)?;
|
||||
app.manage(haptics);
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
105
plugins/haptics/src/mobile.rs
Normal file
105
plugins/haptics/src/mobile.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
// 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> {
|
||||
/// Triggers a vibration for `duration` milliseconds.
|
||||
///
|
||||
/// On iOS this plays a continuous [Core Haptics](https://developer.apple.com/documentation/corehaptics)
|
||||
/// pattern when the device supports it, falling back to the system alert vibration
|
||||
/// otherwise. On Android it uses [`Vibrator`](https://developer.android.com/reference/android/os/Vibrator).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::PluginInvoke`](crate::Error::PluginInvoke) if the underlying Android or
|
||||
/// iOS plugin invocation fails.
|
||||
pub fn vibrate(&self, duration: u32) -> crate::Result<()> {
|
||||
self.0
|
||||
.run_mobile_plugin("vibrate", VibratePayload { duration })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Triggers an impact-feedback haptic with the given [`ImpactFeedbackStyle`].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::PluginInvoke`](crate::Error::PluginInvoke) if the underlying Android or
|
||||
/// iOS plugin invocation fails.
|
||||
pub fn impact_feedback(&self, style: ImpactFeedbackStyle) -> crate::Result<()> {
|
||||
self.0
|
||||
.run_mobile_plugin("impactFeedback", ImpactFeedbackPayload { style })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Triggers a notification-feedback haptic for the given [`NotificationFeedbackType`].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::PluginInvoke`](crate::Error::PluginInvoke) if the underlying Android or
|
||||
/// iOS plugin invocation fails.
|
||||
pub fn notification_feedback(&self, r#type: NotificationFeedbackType) -> crate::Result<()> {
|
||||
self.0
|
||||
.run_mobile_plugin(
|
||||
"notificationFeedback",
|
||||
NotificationFeedbackPayload { r#type },
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Triggers a haptic indicating that a selection changed, e.g. when the value of a picker
|
||||
/// control changes.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::PluginInvoke`](crate::Error::PluginInvoke) if the underlying Android or
|
||||
/// iOS plugin invocation fails.
|
||||
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,
|
||||
}
|
||||
54
plugins/haptics/src/models.rs
Normal file
54
plugins/haptics/src/models.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
/*
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "specta", derive(specta::Type))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HapticsOptions {
|
||||
// TODO: support array to match web api
|
||||
pub duration: u32,
|
||||
}
|
||||
*/
|
||||
|
||||
/// The style of an impact-feedback haptic.
|
||||
///
|
||||
/// On iOS this maps directly to a `UIImpactFeedbackGenerator.FeedbackStyle` case. On Android,
|
||||
/// which has no equivalent system API, each style instead plays a distinct vibration waveform of
|
||||
/// increasing intensity. Has no effect on desktop platforms. Defaults to `Medium`.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "specta", derive(specta::Type))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum ImpactFeedbackStyle {
|
||||
/// A collision between small, light user interface elements.
|
||||
Light,
|
||||
/// A collision between moderately sized user interface elements.
|
||||
#[default]
|
||||
Medium,
|
||||
/// A collision between large, heavy user interface elements.
|
||||
Heavy,
|
||||
/// A soft, muted impact between user interface elements.
|
||||
Soft,
|
||||
/// A sharp, rigid impact between user interface elements.
|
||||
Rigid,
|
||||
}
|
||||
|
||||
/// The type of notification feedback, indicating the outcome of a task or action.
|
||||
///
|
||||
/// On iOS this maps directly to a `UINotificationFeedbackGenerator.FeedbackType` case. On
|
||||
/// Android, which has no equivalent system API, each type instead plays a distinct vibration
|
||||
/// waveform. Has no effect on desktop platforms. Defaults to `Success`.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "specta", derive(specta::Type))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum NotificationFeedbackType {
|
||||
/// A task or action has completed successfully.
|
||||
#[default]
|
||||
Success,
|
||||
/// A task or action has produced a warning.
|
||||
Warning,
|
||||
/// A task or action has failed.
|
||||
Error,
|
||||
}
|
||||
Reference in New Issue
Block a user