diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 647aca9..d83d851 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -11,9 +11,10 @@ import Cocoa class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate { var tapClosure: (() -> Void)? var longTapClosure: (() -> Void)? + + private let hf: HapticFeedback = HapticFeedback() private var button: NSButton! - - private var singleClick: NSClickGestureRecognizer! + private var singleClick: HapticClickGestureRecognizer! private var longClick: NSPressGestureRecognizer! init(identifier: NSTouchBarItem.Identifier, title: String) { @@ -26,7 +27,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat longClick.allowedTouchTypes = .direct longClick.delegate = self - singleClick = NSClickGestureRecognizer(target: self, action: #selector(handleGestureSingle)) + singleClick = HapticClickGestureRecognizer(target: self, action: #selector(handleGestureSingle)) singleClick.allowedTouchTypes = .direct singleClick.delegate = self @@ -104,10 +105,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } @objc func handleGestureSingle(gr: NSClickGestureRecognizer) { - let hf: HapticFeedback = HapticFeedback() switch gr.state { case .ended: - hf.tap(strong: 2) tapClosure?() break default: @@ -116,7 +115,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } @objc func handleGestureLong(gr: NSPressGestureRecognizer) { - let hf: HapticFeedback = HapticFeedback() switch gr.state { case .began: if let closure = self.longTapClosure { @@ -172,6 +170,20 @@ class CustomButtonCell: NSButtonCell { } } +class HapticClickGestureRecognizer: NSClickGestureRecognizer { + let hf: HapticFeedback = HapticFeedback() + + override func touchesBegan(with event: NSEvent) { + hf.tap(strong: 2) + super.touchesBegan(with: event) + } + + override func touchesEnded(with event: NSEvent) { + hf.tap(strong: 1) + super.touchesEnded(with: event) + } +} + extension String { var defaultTouchbarAttributedString: NSAttributedString { let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])