tauri-macos-haptics-api
v3.2.0
Published
Modern macOS haptics (Taptic Engine) plugin for Tauri v2 apps with objc2 bindings.
Maintainers
Readme
Tauri macOS Haptics
Modern macOS haptics (Taptic Engine™️) plugin for Tauri v2 apps.
Modernized and actively maintained by Entro314 Labs. Based on original implementation by ItsEeleeya.
Features
- 🎯 Type-Safe: Modern
objc2framework with full type safety - ⚡ Latest Tauri: Built for Tauri 2.9+ with all modern features
- 📱 Complete API: All 3 NSHapticFeedbackPattern types supported
- 🦀 Modern Rust: Uses Rust 1.77+ with 2021 edition
- 📝 Well Documented: Comprehensive inline documentation and examples
- 🔒 Robust Error Handling: Proper error types and handling throughout
Get the plugin
Using the command line from crates.io:
cargo add tauri-macos-hapticsOr add it manually to Cargo.toml:
[target.'cfg(target_os = "macos")'.dependencies]
tauri-macos-haptics = "2.0"Or get the latest from git:
[target.'cfg(target_os = "macos")'.dependencies]
tauri-macos-haptics = { git = "https://github.com/entro314-labs/tauri-macos-haptics" }Get the frontend bindings
pnpm add tauri-macos-haptics-api
# or
bun add tauri-macos-haptics-api
# or
npm install tauri-macos-haptics-api
# or
yarn add tauri-macos-haptics-apiUsage
1. Initialize the plugin
This is required to use the plugin from the frontend.
fn main() {
let mut builder = tauri::Builder::default();
#[cfg(target_os = "macos")]
{
// Initialize the haptics plugin
builder = builder.plugin(tauri_macos_haptics::init());
}
builder
.run(tauri::generate_context!())
.expect("error while running tauri application");
}2. Permissions
Add "tauri-macos-haptics:default" to your permissions.
Usually found under src-tauri/capabilities/*.json
Read more about the specific permissions.
3. Perform haptic feedback
From Rust:
use tauri_macos_haptics::haptics::*;
fn provide_feedback() {
#[cfg(target_os = "macos")]
{
// Performs a generic haptic feedback immediately
HapticFeedbackManager::default_performer()
.perform(HapticPattern::Generic, None)
.expect("Failed to perform haptic feedback");
// Or with specific timing
HapticFeedbackManager::default_performer()
.perform(
HapticPattern::Alignment,
Some(PerformanceTime::DrawCompleted)
)
.expect("Failed to perform haptic feedback");
}
}From the frontend (TypeScript):
import {
isSupported,
perform,
HapticFeedbackPattern,
PerformanceTime,
HapticError,
} from "tauri-macos-haptics-api";
// Check support first
if (await isSupported()) {
// Basic usage
await perform();
// With specific pattern and timing
await perform(HapticFeedbackPattern.Alignment, PerformanceTime.Now);
// With error handling
try {
await perform(HapticFeedbackPattern.LevelChange, PerformanceTime.DrawCompleted);
} catch (error) {
if (error instanceof HapticError) {
console.error('Haptic feedback failed:', error.message);
}
}
}Haptic Feedback Patterns
Alignment
Use when the user is dragging an object into alignment with another object:
- Aligning shapes in a drawing application
- Snapping to grid positions
- Reaching minimum/maximum bounds
- Positioning objects at preferred locations
LevelChange
Use when transitioning between discrete levels or states:
- Adjusting volume or brightness in steps
- Switching between preset values
- Moving through a multi-level accelerator
- Designed for multilevel accelerator buttons
Generic
General-purpose haptic feedback when no other pattern applies:
- General confirmation of user actions
- Default feedback for most interactions
Performance Timing
Default
The system chooses the most appropriate time for feedback based on current conditions.
Now
Provide immediate haptic feedback for instant response to user actions.
DrawCompleted
Provide feedback after the next screen update completes, synchronizing haptic feedback with visual changes.
Important Usage Guidelines
⚠️ Please note that you should trigger feedback ONLY in response to user-initiated actions.
Ideally, visual feedback, such as a highlight or appearance of an alignment guide, should accompany the haptic feedback.
In some cases, the system may override a call to this method. For example, a Force Touch trackpad won't provide haptic feedback if the user isn't touching the trackpad.
Haptic feedback is intended to be provided in response to a user action, such as aligning one object to another. Do not use it to provide feedback for events that are not user initiated. Excessive or unnecessary haptic feedback could be interpreted by the user as a malfunction and could encourage the user to disable haptic feedback entirely.
Learn more about NSHapticFeedbackManager (Apple's documentation)
Example App (Tauri)
The example app lives in the example/ folder and is not published to npm. It’s the fastest way to see the plugin in action with a native-looking UI.
pnpm install
pnpm run-exampleSystem Requirements
- macOS: 10.11 (OS X El Capitan) or later
- Hardware: Force Touch trackpad or compatible haptic hardware
- Rust: 1.77 or later (Rust 2021 edition)
- Tauri: 2.9 or later
What's New in 2.0
Breaking Changes
- Migrated from legacy
objc/cocoato modernobjc2framework - Updated minimum Rust version to 1.77
- Renamed internal types (with backward-compatible aliases)
Improvements
- ✨ Updated to Tauri 2.9 and latest dependencies
- 🔒 Enhanced type safety with
objc2 - 📚 Comprehensive inline documentation
- 🎯 Better error handling with
HapticErrortype - ⚡ Improved performance and memory safety
- 🧪 Better TypeScript types and JSDoc comments
Development
Building the plugin
cargo buildBuilding the TypeScript bindings
pnpm install
pnpm buildRunning the example
pnpm run-exampleContributing
Any contributions are welcomed! Feel free to submit issues or pull requests.
License
MIT License - see LICENSE for details.
