mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
CustomButtonTouchBarItem: recognise longPress in more expected m… (#202)
CustomButtonTouchBarItem: recognise longPress in more expected manner without long wait and requirement to release the button
This commit is contained in:
commit
580f0275fc
@ -10,11 +10,15 @@ import Cocoa
|
|||||||
|
|
||||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
||||||
var tapClosure: (() -> Void)?
|
var tapClosure: (() -> Void)?
|
||||||
var longTapClosure: (() -> Void)?
|
var longTapClosure: (() -> Void)? {
|
||||||
|
didSet {
|
||||||
|
longClick.isEnabled = longTapClosure != nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private var button: NSButton!
|
private var button: NSButton!
|
||||||
private var singleClick: HapticClickGestureRecognizer!
|
private var singleClick: HapticClickGestureRecognizer!
|
||||||
private var longClick: NSPressGestureRecognizer!
|
private var longClick: LongPressGestureRecognizer!
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, title: String) {
|
init(identifier: NSTouchBarItem.Identifier, title: String) {
|
||||||
attributedTitle = title.defaultTouchbarAttributedString
|
attributedTitle = title.defaultTouchbarAttributedString
|
||||||
@ -22,7 +26,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
button = CustomHeightButton(title: title, target: nil, action: nil)
|
button = CustomHeightButton(title: title, target: nil, action: nil)
|
||||||
|
|
||||||
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
|
longClick = LongPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
|
||||||
|
longClick.isEnabled = false
|
||||||
longClick.allowedTouchTypes = .direct
|
longClick.allowedTouchTypes = .direct
|
||||||
longClick.delegate = self
|
longClick.delegate = self
|
||||||
|
|
||||||
@ -97,7 +102,9 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
|
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
|
||||||
if gestureRecognizer == singleClick && otherGestureRecognizer == longClick {
|
if gestureRecognizer == singleClick && otherGestureRecognizer == longClick
|
||||||
|
|| gestureRecognizer == longClick && otherGestureRecognizer == singleClick // need it
|
||||||
|
{
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -115,15 +122,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
|
|
||||||
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
||||||
switch gr.state {
|
switch gr.state {
|
||||||
case .began:
|
case .possible: // tiny hack because we're calling action manually
|
||||||
if let closure = self.longTapClosure {
|
(self.longTapClosure ?? self.tapClosure)?()
|
||||||
HapticFeedback.shared.tap(strong: 2)
|
|
||||||
closure()
|
|
||||||
} else if let closure = self.tapClosure {
|
|
||||||
HapticFeedback.shared.tap(strong: 6)
|
|
||||||
closure()
|
|
||||||
print("long click")
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
@ -181,6 +181,55 @@ class HapticClickGestureRecognizer: NSClickGestureRecognizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class LongPressGestureRecognizer: NSPressGestureRecognizer {
|
||||||
|
private let recognizeTimeout = 0.4
|
||||||
|
private var timer: Timer?
|
||||||
|
|
||||||
|
override func touchesBegan(with event: NSEvent) {
|
||||||
|
timerInvalidate()
|
||||||
|
|
||||||
|
let touches = event.touches(for: self.view!)
|
||||||
|
if touches.count == 1 { // to prevent it for built-in two/three-finger gestures
|
||||||
|
timer = Timer.scheduledTimer(timeInterval: recognizeTimeout, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
super.touchesBegan(with: event)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func touchesMoved(with event: NSEvent) {
|
||||||
|
timerInvalidate() // to prevent it for built-in two/three-finger gestures
|
||||||
|
super.touchesMoved(with: event)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func touchesCancelled(with event: NSEvent) {
|
||||||
|
timerInvalidate()
|
||||||
|
super.touchesCancelled(with: event)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func touchesEnded(with event: NSEvent) {
|
||||||
|
timerInvalidate()
|
||||||
|
super.touchesEnded(with: event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func timerInvalidate() {
|
||||||
|
if let timer = timer {
|
||||||
|
timer.invalidate()
|
||||||
|
self.timer = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func onTimer() {
|
||||||
|
if let target = self.target, let action = self.action {
|
||||||
|
target.performSelector(onMainThread: action, with: self, waitUntilDone: false)
|
||||||
|
HapticFeedback.shared.tap(strong: 6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
timerInvalidate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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])
|
||||||
|
|||||||
@ -127,4 +127,8 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
|
|||||||
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
||||||
attributedTitle = newTitle
|
attributedTitle = newTitle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
activity.invalidate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -135,4 +135,8 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
|
|||||||
// print("inside didChangeAuthorization ");
|
// print("inside didChangeAuthorization ");
|
||||||
updateWeather()
|
updateWeather()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
activity.invalidate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -121,6 +121,10 @@ class YandexWeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate
|
|||||||
func locationManager(_: CLLocationManager, didChangeAuthorization _: CLAuthorizationStatus) {
|
func locationManager(_: CLLocationManager, didChangeAuthorization _: CLAuthorizationStatus) {
|
||||||
updateWeather()
|
updateWeather()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
activity.invalidate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension String {
|
extension String {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user