From 58e41606495e1ea75b247bf5405c02765a218475 Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Sat, 27 Jul 2019 13:05:02 +0200 Subject: [PATCH 1/2] HapticFeedback: fix broken haptic for Release builds + refactoring --- MTMR/CustomButtonTouchBarItem.swift | 11 ++--- MTMR/HapticFeedback.swift | 48 +++++++++++++--------- MTMR/Widgets/AppScrubberTouchBarItem.swift | 10 ++--- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index d83d851..a61a508 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -12,7 +12,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat var tapClosure: (() -> Void)? var longTapClosure: (() -> Void)? - private let hf: HapticFeedback = HapticFeedback() private var button: NSButton! private var singleClick: HapticClickGestureRecognizer! private var longClick: NSPressGestureRecognizer! @@ -118,10 +117,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat switch gr.state { case .began: if let closure = self.longTapClosure { - hf.tap(strong: 2) + HapticFeedback.shared.tap(strong: 2) closure() } else if let closure = self.tapClosure { - hf.tap(strong: 6) + HapticFeedback.shared.tap(strong: 6) closure() print("long click") } @@ -171,15 +170,13 @@ class CustomButtonCell: NSButtonCell { } class HapticClickGestureRecognizer: NSClickGestureRecognizer { - let hf: HapticFeedback = HapticFeedback() - override func touchesBegan(with event: NSEvent) { - hf.tap(strong: 2) + HapticFeedback.shared.tap(strong: 2) super.touchesBegan(with: event) } override func touchesEnded(with event: NSEvent) { - hf.tap(strong: 1) + HapticFeedback.shared.tap(strong: 1) super.touchesEnded(with: event) } } diff --git a/MTMR/HapticFeedback.swift b/MTMR/HapticFeedback.swift index cd4c0c1..7bd0b20 100644 --- a/MTMR/HapticFeedback.swift +++ b/MTMR/HapticFeedback.swift @@ -9,7 +9,7 @@ import IOKit class HapticFeedback { - private var correctDeviceId: UInt64? + static let shared = HapticFeedback() // Here we have list of possible IDs for Haptic Generator Device. They are not constant // To find deviceID, you will need IORegistryExplorer app from Additional Tools for Xcode dmg @@ -20,16 +20,11 @@ class HapticFeedback { 0x200_0000_0100_0000, // MacBook Pro 2016/2017 0x300000080500000 // MacBook Pro 2019 (possibly 2018 as well) ] + private var correctDeviceID: UInt64? + private var actuatorRef: CFTypeRef? init() { - // Let's find our Haptic device - possibleDeviceIDs.forEach {(deviceID) in - guard correctDeviceId == nil else {return} - let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue() - if actuatorRef != nil { - correctDeviceId = deviceID - } - } + recreateDevice() } // Don't know how to do strong is enum one of @@ -44,15 +39,8 @@ class HapticFeedback { // you can get a plist `otool -s __TEXT __tpad_act_plist /System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/Current/MultitouchSupport|tail -n +3|awk -F'\t' '{print $2}'|xxd -r -p` func tap(strong: Int32) { - guard correctDeviceId != nil else { - print("guard correctDeviceId == nil (no haptic device found?)") - return - } - - let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(correctDeviceId!).takeRetainedValue() - - guard actuatorRef != nil else { - print("guard actuatorRef == nil") + guard correctDeviceID != nil, actuatorRef != nil else { + print("guard actuatorRef == nil (no haptic device found?)") return } @@ -61,10 +49,11 @@ class HapticFeedback { result = MTActuatorOpen(actuatorRef!) guard result == kIOReturnSuccess else { print("guard MTActuatorOpen") + recreateDevice() return } - result = MTActuatorActuate(actuatorRef!, strong, 0, 0.0, 0.0) + result = MTActuatorActuate(actuatorRef!, strong, 0, 0, 0) guard result == kIOReturnSuccess else { print("guard MTActuatorActuate") return @@ -76,4 +65,25 @@ class HapticFeedback { return } } + + private func recreateDevice() { + if let actuatorRef = actuatorRef { + MTActuatorClose(actuatorRef) + self.actuatorRef = nil // just in case %) + } + + if let correctDeviceID = correctDeviceID { + actuatorRef = MTActuatorCreateFromDeviceID(correctDeviceID).takeRetainedValue() + } else { + // Let's find our Haptic device + possibleDeviceIDs.forEach {(deviceID) in + guard correctDeviceID == nil else {return} + actuatorRef = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue() + + if actuatorRef != nil { + correctDeviceID = deviceID + } + } + } + } } diff --git a/MTMR/Widgets/AppScrubberTouchBarItem.swift b/MTMR/Widgets/AppScrubberTouchBarItem.swift index 7c5cd39..5ddfb50 100644 --- a/MTMR/Widgets/AppScrubberTouchBarItem.swift +++ b/MTMR/Widgets/AppScrubberTouchBarItem.swift @@ -10,8 +10,6 @@ import Cocoa class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrubberDataSource { private var scrubber: NSScrubber! - private let hf: HapticFeedback = HapticFeedback() - private var timer: Timer! private var ticks: Int = 0 private let minTicks: Int = 5 @@ -148,12 +146,12 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub ticks += 1 if ticks == minTicks { - hf.tap(strong: 2) + HapticFeedback.shared.tap(strong: 2) } if ticks > maxTicks { stopTimer() - hf.tap(strong: 6) + HapticFeedback.shared.tap(strong: 6) } } @@ -184,7 +182,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub NSWorkspace.shared.openFile(bundleIdentifier!.replacingOccurrences(of: "file://", with: "")) } else { NSWorkspace.shared.launchApplication(withBundleIdentifier: bundleIdentifier!, options: [.default], additionalEventParamDescriptor: nil, launchIdentifier: nil) - hf.tap(strong: 6) + HapticFeedback.shared.tap(strong: 6) } updateRunningApplication() @@ -203,7 +201,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub } } } else { - hf.tap(strong: 6) + HapticFeedback.shared.tap(strong: 6) if let index = self.persistentAppIdentifiers.index(of: bundleIdentifier!) { persistentAppIdentifiers.remove(at: index) } else { From 67aaa2abf4a9e9cc6335798a5b90bf0f05e794e5 Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Sat, 27 Jul 2019 18:10:48 +0200 Subject: [PATCH 2/2] CurrencyBarItem: fix text vertical alignment --- MTMR/Widgets/CurrencyBarItem.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index cd89e6c..1d1bbde 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -123,7 +123,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem { } let regularFont = attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15) - let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont]) + let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont, .baselineOffset: 1]) newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) attributedTitle = newTitle }