From 1963815acee643265c01c2c5e1caebe4bc8384df Mon Sep 17 00:00:00 2001 From: "prompt.ac/@jeffrey" Date: Thu, 7 May 2026 22:43:29 -0400 Subject: [PATCH] slab/menuband: hide unsupported haptics controls --- slab/menuband/Package.swift | 1 + .../Sources/MenuBand/MenuBandHaptics.swift | 54 +++++++++++++++++++ .../ExpandedPianoWaveformView.swift | 10 ++++ .../PianoKeyboardView.swift | 3 ++ 4 files changed, 68 insertions(+) create mode 100644 slab/menuband/Sources/MenuBand/MenuBandHaptics.swift diff --git a/slab/menuband/Package.swift b/slab/menuband/Package.swift index a2c9939bab..40f7b9cda0 100644 --- a/slab/menuband/Package.swift +++ b/slab/menuband/Package.swift @@ -20,6 +20,7 @@ let package = Package( .process("WaveformShaders.metal"), ], linkerSettings: [ + .linkedFramework("IOKit"), // Embed the package-root Info.plist into the dev // binary so macOS TCC has an // NSMicrophoneUsageDescription to show when diff --git a/slab/menuband/Sources/MenuBand/MenuBandHaptics.swift b/slab/menuband/Sources/MenuBand/MenuBandHaptics.swift new file mode 100644 index 0000000000..579d457516 --- /dev/null +++ b/slab/menuband/Sources/MenuBand/MenuBandHaptics.swift @@ -0,0 +1,54 @@ +import Foundation +import IOKit + +enum MenuBandHaptics { + static let isAvailable: Bool = detectAvailability() + + private static func detectAvailability() -> Bool { + var iterator: io_iterator_t = 0 + let result = IOServiceGetMatchingServices( + kIOMasterPortDefault, + IOServiceMatching("AppleMultitouchTrackpadHIDEventDriver"), + &iterator + ) + guard result == KERN_SUCCESS else { return false } + defer { IOObjectRelease(iterator) } + + var service = IOIteratorNext(iterator) + while service != 0 { + defer { IOObjectRelease(service) } + if boolProperty("ForceSupported", on: service) == true || + boolProperty("ActuationSupported", on: service) == true || + multitouchDefaultProperty("ForceSupported", on: service) == true || + multitouchDefaultProperty("ActuationSupported", on: service) == true { + return true + } + service = IOIteratorNext(iterator) + } + return false + } + + private static func boolProperty(_ key: String, on service: io_object_t) -> Bool? { + guard let value = IORegistryEntryCreateCFProperty( + service, + key as CFString, + kCFAllocatorDefault, + 0 + )?.takeRetainedValue() else { + return nil + } + return value as? Bool + } + + private static func multitouchDefaultProperty(_ key: String, on service: io_object_t) -> Bool? { + guard let value = IORegistryEntryCreateCFProperty( + service, + "DefaultMultitouchProperties" as CFString, + kCFAllocatorDefault, + 0 + )?.takeRetainedValue() as? [String: Any] else { + return nil + } + return value[key] as? Bool + } +} diff --git a/slab/menuband/Sources/MenuBand/PianoWaveformWindow/ExpandedPianoWaveformView.swift b/slab/menuband/Sources/MenuBand/PianoWaveformWindow/ExpandedPianoWaveformView.swift index 408030ad14..1c33bb7a35 100644 --- a/slab/menuband/Sources/MenuBand/PianoWaveformWindow/ExpandedPianoWaveformView.swift +++ b/slab/menuband/Sources/MenuBand/PianoWaveformWindow/ExpandedPianoWaveformView.swift @@ -72,6 +72,7 @@ final class ExpandedPianoWaveformView: NSView { private let chordCandidatesRowHorizontalInset: CGFloat = 6 private var widthConstraint: NSLayoutConstraint? private var waveformHeightConstraint: NSLayoutConstraint? + private var hapticsWidthConstraint: NSLayoutConstraint? private var isPresented = false private var trackingArea: NSTrackingArea? private static let panelCornerRadius: CGFloat = 18 @@ -162,6 +163,7 @@ final class ExpandedPianoWaveformView: NSView { hapticsControls.translatesAutoresizingMaskIntoConstraints = false hapticsControls.setContentHuggingPriority(.required, for: .horizontal) hapticsControls.setContentCompressionResistancePriority(.required, for: .horizontal) + hapticsWidthConstraint = hapticsControls.widthAnchor.constraint(equalToConstant: 0) hapticsLabel.font = NSFont.systemFont(ofSize: 10, weight: .semibold) hapticsLabel.textColor = .secondaryLabelColor hapticsLabel.lineBreakMode = .byClipping @@ -495,6 +497,12 @@ final class ExpandedPianoWaveformView: NSView { } private func updateHapticsControl() { + let available = MenuBandHaptics.isAvailable + hapticsControls.isHidden = !available + hapticsWidthConstraint?.isActive = !available + hapticsSwitch.isEnabled = available + hapticsInfoButton.isEnabled = available + guard available else { return } hapticsSwitch.state = (menuBand?.hapticsEnabled ?? true) ? .on : .off } @@ -742,10 +750,12 @@ final class ExpandedPianoWaveformView: NSView { } @objc private func hapticsSwitchChanged(_ sender: NSSwitch) { + guard MenuBandHaptics.isAvailable else { return } menuBand?.hapticsEnabled = (sender.state == .on) } @objc private func showHapticsInfo(_ sender: NSButton) { + guard MenuBandHaptics.isAvailable else { return } guard let window else { return } let alert = NSAlert() alert.alertStyle = .informational diff --git a/slab/menuband/Sources/MenuBand/PianoWaveformWindow/PianoKeyboardView.swift b/slab/menuband/Sources/MenuBand/PianoWaveformWindow/PianoKeyboardView.swift index dba510db10..0c810d4b58 100644 --- a/slab/menuband/Sources/MenuBand/PianoWaveformWindow/PianoKeyboardView.swift +++ b/slab/menuband/Sources/MenuBand/PianoWaveformWindow/PianoKeyboardView.swift @@ -254,6 +254,7 @@ final class PianoKeyboardView: NSView { private func performKeyTapHaptic(isInitialTap: Bool) { guard menuBand?.hapticsEnabled != false else { return } + guard MenuBandHaptics.isAvailable else { return } let now = ProcessInfo.processInfo.systemUptime guard now - lastHapticTime >= Self.hapticCooldown else { return } lastHapticTime = now @@ -275,6 +276,7 @@ final class PianoKeyboardView: NSView { private func presentHapticsSetupHintIfNeeded() { guard menuBand?.hapticsEnabled != false else { return } + guard MenuBandHaptics.isAvailable else { return } guard UserDefaults.standard.bool(forKey: Self.hapticHintDefaultsKey) == false, let window else { return } @@ -292,6 +294,7 @@ final class PianoKeyboardView: NSView { private func performHoverHaptic(for note: UInt8) { guard menuBand?.hapticsEnabled != false else { return } + guard MenuBandHaptics.isAvailable else { return } let now = ProcessInfo.processInfo.systemUptime guard now - lastHoverHapticTime >= Self.hoverHapticCooldown else { return } lastHoverHapticTime = now -- knotmirror 0.0.1