Haptic feedback

Haptic feedback uses a brief physical sensation to acknowledge an interaction or report an outcome. It can reinforce a visible or audible response, such as a selection change or a completed action.

day-part-haptics provides impact, selection, success, warning, and error effects. Calls have no effect on devices without a supported haptic engine.

Add to your app

Add day-part-haptics to your app:

cargo add 'day-part-haptics@0.*'

The implementation is selected by the compilation target; no toolkit feature flags or dependency entry in Day.toml are needed.

Android and HarmonyOS vibration permissions are contributed by the crate during day build. is_supported() reports API availability, not the presence of physical haptic hardware.

Platforms / toolkitsAvailability
iOS, macOS, Android, HarmonyOSHaptic output on compatible hardware. macOS uses the Force Touch trackpad.
Windows, Linux (GTK / Qt), WebNo haptic implementation; play() has no effect.

Authoring

use day_part_haptics::Haptic;

if day_part_haptics::is_supported() {
    day_part_haptics::play(Haptic::Success);
}

play(Haptic) is fire-and-forget: it returns nothing, never blocks, never errors, and never panics. is_supported() -> bool reports whether the platform has a haptic engine wired here (it is API availability, not a live hardware probe; a Simulator still reports true). Where support is false, play is a no-op.

Haptic is modeled on iOS’s three feedback-generator families so the same call maps to a native pattern everywhere:

StyleMeaning
Light / Medium / Heavyphysical impact intensities
Success / Warning / Errornotification outcomes
Selectionthe light tick as a value scrolls past a detent

There are no cargo features; platform selection is purely #[cfg(target_os)], because a haptic engine is an OS concern rather than a toolkit one. crates/day-part-haptics/examples/haptics.rs is a plain main that uses it with no Day framework at all.

Platform implementations

Platform guidance: Apple: playing haptics; Android: haptics design principles.

OSAPIdependency
iOSUIImpactFeedbackGenerator / UINotificationFeedbackGenerator / UISelectionFeedbackGeneratorobjc2 + objc2-ui-kit
macOSNSHapticFeedbackManager.defaultPerformer (Force Touch trackpad)objc2-app-kit
AndroidVibrator / VibrationEffect via a Java shimday-toolkit-material + [package.metadata.day.android]
HarmonyOSvibrator.startVibration (Sensor Service Kit) via an ArkTS armday-bridge + [package.metadata.day.ohos]
Windows · desktop Linux (GTK/Qt)— (no haptic engine wired)none (no-op, is_supported() == false)

How each platform realizes the styles

  • iOS: a direct one-to-one mapping. Light/Medium/Heavy pick the matching UIImpactFeedbackStyle; Success/Warning/Error pick the matching UINotificationFeedbackType; Selection calls selectionChanged(). The generators are MainThreadOnly and are prepare()d before firing to minimize latency; day runs on the main thread, so a call off it is a safe no-op. The Simulator has no Taptic engine, so the calls are silently ignored there.
  • macOS: NSHapticFeedbackManager offers only three patterns, so the seven styles fold onto them: Light/Selection → Alignment (the subtlest snap), Medium/Heavy → LevelChange (a firmer detent), Success/Warning/Error → Generic. A Mac without a Force Touch trackpad feels nothing; the call is harmless.
  • Android: a Java shim (DayHaptics.java) resolves the Vibrator (via VibratorManager on API 31+), and on API 29+ plays a predefined VibrationEffect: EFFECT_TICK (Light/Selection), EFFECT_CLICK (Medium), EFFECT_HEAVY_CLICK (Heavy/Warning), EFFECT_DOUBLE_CLICK (Success/Error). Older APIs fall back to a short one-shot buzz whose length stands in for intensity. Requires android.permission.VIBRATE, a normal install-time permission the crate contributes to the manifest itself (see below).
  • HarmonyOS: an ArkTS daybridge arm (docs/bridge) plays the system’s own haptic presets through the Sensor Service Kit’s vibrator: haptic.effect.soft (Light), haptic.clock.timer (Medium/Selection), haptic.effect.hard (Heavy/Warning/Error), haptic.effect.sharp (Success). A device without a preset gets a timed buzz whose length stands in for strength, like Android’s pre-API-29 fallback. The C vibrator API takes only durations, which is why the arm is ArkTS. Requires ohos.permission.VIBRATE, a system_grant permission the crate contributes through [package.metadata.day.ohos] permissions. The OpenHarmony emulator has no vibrator, so the call is accepted and nothing is felt.

Extension integration

Like day-part-device and day-part-connectivity, this is a headless external crate: it has no UI Piece and registers nothing into any toolkit’s RENDERERS slice. It exercises the manifest-permission overlay (docs/extending): [package.metadata.day.android] stages its own Java shim and contributes android.permission.VIBRATE, which day build merges into the app manifest with no edits to any core day crate. On Android the crate rides on the Day runtime (day-toolkit-material’s cached JVM + DayBridge.ctx); on every other platform it is fully day-independent.