mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
240 lines
7.2 KiB
Swift
240 lines
7.2 KiB
Swift
//
|
|
// TouchBarItems.swift
|
|
// MTMR
|
|
//
|
|
// Created by Anton Palgunov on 18/03/2018.
|
|
// Copyright © 2018 Anton Palgunov. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
|
var tapClosure: (() -> Void)?
|
|
var longTapClosure: (() -> Void)? {
|
|
didSet {
|
|
longClick.isEnabled = longTapClosure != nil
|
|
}
|
|
}
|
|
|
|
private var button: NSButton!
|
|
private var singleClick: HapticClickGestureRecognizer!
|
|
private var longClick: LongPressGestureRecognizer!
|
|
|
|
init(identifier: NSTouchBarItem.Identifier, title: String) {
|
|
attributedTitle = title.defaultTouchbarAttributedString
|
|
|
|
super.init(identifier: identifier)
|
|
button = CustomHeightButton(title: title, target: nil, action: nil)
|
|
|
|
longClick = LongPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
|
|
longClick.isEnabled = false
|
|
longClick.allowedTouchTypes = .direct
|
|
longClick.delegate = self
|
|
|
|
singleClick = HapticClickGestureRecognizer(target: self, action: #selector(handleGestureSingle))
|
|
singleClick.allowedTouchTypes = .direct
|
|
singleClick.delegate = self
|
|
|
|
reinstallButton()
|
|
button.attributedTitle = attributedTitle
|
|
}
|
|
|
|
required init?(coder _: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
var isBordered: Bool = true {
|
|
didSet {
|
|
reinstallButton()
|
|
}
|
|
}
|
|
|
|
var backgroundColor: NSColor? {
|
|
didSet {
|
|
reinstallButton()
|
|
}
|
|
}
|
|
|
|
var title: String {
|
|
get {
|
|
return attributedTitle.string
|
|
}
|
|
set {
|
|
attributedTitle = newValue.defaultTouchbarAttributedString
|
|
}
|
|
}
|
|
|
|
var attributedTitle: NSAttributedString {
|
|
didSet {
|
|
button?.imagePosition = attributedTitle.length > 0 ? .imageLeading : .imageOnly
|
|
button?.attributedTitle = attributedTitle
|
|
}
|
|
}
|
|
|
|
var image: NSImage? {
|
|
didSet {
|
|
button.image = image
|
|
}
|
|
}
|
|
|
|
private func reinstallButton() {
|
|
let title = button.attributedTitle
|
|
let image = button.image
|
|
let cell = CustomButtonCell(parentItem: self)
|
|
button.cell = cell
|
|
if let color = backgroundColor {
|
|
cell.isBordered = true
|
|
button.bezelColor = color
|
|
cell.backgroundColor = color
|
|
} else {
|
|
button.isBordered = isBordered
|
|
button.bezelStyle = isBordered ? .rounded : .inline
|
|
}
|
|
button.imageScaling = .scaleProportionallyDown
|
|
button.imageHugsTitle = true
|
|
button.attributedTitle = title
|
|
button?.imagePosition = title.length > 0 ? .imageLeading : .imageOnly
|
|
button.image = image
|
|
view = button
|
|
|
|
view.addGestureRecognizer(longClick)
|
|
view.addGestureRecognizer(singleClick)
|
|
}
|
|
|
|
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
|
|
if gestureRecognizer == singleClick && otherGestureRecognizer == longClick
|
|
|| gestureRecognizer == longClick && otherGestureRecognizer == singleClick // need it
|
|
{
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
@objc func handleGestureSingle(gr: NSClickGestureRecognizer) {
|
|
switch gr.state {
|
|
case .ended:
|
|
tapClosure?()
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
|
switch gr.state {
|
|
case .possible: // tiny hack because we're calling action manually
|
|
(self.longTapClosure ?? self.tapClosure)?()
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
class CustomHeightButton: NSButton {
|
|
override var intrinsicContentSize: NSSize {
|
|
var size = super.intrinsicContentSize
|
|
size.height = 30
|
|
return size
|
|
}
|
|
}
|
|
|
|
class CustomButtonCell: NSButtonCell {
|
|
weak var parentItem: CustomButtonTouchBarItem?
|
|
|
|
init(parentItem: CustomButtonTouchBarItem) {
|
|
super.init(textCell: "")
|
|
self.parentItem = parentItem
|
|
}
|
|
|
|
override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
|
|
super.highlight(flag, withFrame: cellFrame, in: controlView)
|
|
if !isBordered {
|
|
if flag {
|
|
setAttributedTitle(attributedTitle, withColor: .lightGray)
|
|
} else if let parentItem = self.parentItem {
|
|
attributedTitle = parentItem.attributedTitle
|
|
}
|
|
}
|
|
}
|
|
|
|
required init(coder _: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func setAttributedTitle(_ title: NSAttributedString, withColor color: NSColor) {
|
|
let attrTitle = NSMutableAttributedString(attributedString: title)
|
|
attrTitle.addAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attrTitle.length))
|
|
attributedTitle = attrTitle
|
|
}
|
|
}
|
|
|
|
class HapticClickGestureRecognizer: NSClickGestureRecognizer {
|
|
override func touchesBegan(with event: NSEvent) {
|
|
HapticFeedback.shared.tap(strong: 2)
|
|
super.touchesBegan(with: event)
|
|
}
|
|
|
|
override func touchesEnded(with event: NSEvent) {
|
|
HapticFeedback.shared.tap(strong: 1)
|
|
super.touchesEnded(with: event)
|
|
}
|
|
}
|
|
|
|
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: 0.25, 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 {
|
|
var defaultTouchbarAttributedString: NSAttributedString {
|
|
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
|
|
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: count))
|
|
return attrTitle
|
|
}
|
|
}
|