mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
CustomButtonTouchBarItem: add better haptic feedback right when button been touched
This commit is contained in:
parent
cfcda6e46f
commit
c254ee430d
@ -11,9 +11,10 @@ import Cocoa
|
|||||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
||||||
var tapClosure: (() -> Void)?
|
var tapClosure: (() -> Void)?
|
||||||
var longTapClosure: (() -> Void)?
|
var longTapClosure: (() -> Void)?
|
||||||
private var button: NSButton!
|
|
||||||
|
|
||||||
private var singleClick: NSClickGestureRecognizer!
|
private let hf: HapticFeedback = HapticFeedback()
|
||||||
|
private var button: NSButton!
|
||||||
|
private var singleClick: HapticClickGestureRecognizer!
|
||||||
private var longClick: NSPressGestureRecognizer!
|
private var longClick: NSPressGestureRecognizer!
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, title: String) {
|
init(identifier: NSTouchBarItem.Identifier, title: String) {
|
||||||
@ -26,7 +27,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
longClick.allowedTouchTypes = .direct
|
longClick.allowedTouchTypes = .direct
|
||||||
longClick.delegate = self
|
longClick.delegate = self
|
||||||
|
|
||||||
singleClick = NSClickGestureRecognizer(target: self, action: #selector(handleGestureSingle))
|
singleClick = HapticClickGestureRecognizer(target: self, action: #selector(handleGestureSingle))
|
||||||
singleClick.allowedTouchTypes = .direct
|
singleClick.allowedTouchTypes = .direct
|
||||||
singleClick.delegate = self
|
singleClick.delegate = self
|
||||||
|
|
||||||
@ -104,10 +105,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func handleGestureSingle(gr: NSClickGestureRecognizer) {
|
@objc func handleGestureSingle(gr: NSClickGestureRecognizer) {
|
||||||
let hf: HapticFeedback = HapticFeedback()
|
|
||||||
switch gr.state {
|
switch gr.state {
|
||||||
case .ended:
|
case .ended:
|
||||||
hf.tap(strong: 2)
|
|
||||||
tapClosure?()
|
tapClosure?()
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
@ -116,7 +115,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
||||||
let hf: HapticFeedback = HapticFeedback()
|
|
||||||
switch gr.state {
|
switch gr.state {
|
||||||
case .began:
|
case .began:
|
||||||
if let closure = self.longTapClosure {
|
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 {
|
extension String {
|
||||||
var defaultTouchbarAttributedString: NSAttributedString {
|
var defaultTouchbarAttributedString: NSAttributedString {
|
||||||
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
|
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user