1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +00:00

public tap/longtap button action hanglers

This commit is contained in:
Serg 2018-05-12 11:41:13 +07:00
parent 9e1fcb9a35
commit f7eb49e5de
8 changed files with 32 additions and 31 deletions

View File

@ -5,9 +5,9 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
private let interval: TimeInterval
private var forceHideConstraint: NSLayoutConstraint!
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval, onTap: @escaping ()->(), onLongTap: @escaping ()->()) {
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval) {
self.interval = interval
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
super.init(identifier: identifier, title: "")
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
guard let script = source.appleScript else {
self.title = "no script"

View File

@ -9,16 +9,14 @@
import Cocoa
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
private let tapClosure: (() -> ())?
private let longTapClosure: (() -> ())?
var tapClosure: (() -> ())?
var longTapClosure: (() -> ())?
private(set) var button: NSButton! //todo hide completely
private var singleClick: NSClickGestureRecognizer!
private var longClick: NSPressGestureRecognizer!
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) {
self.tapClosure = callback
self.longTapClosure = callbackLong
init(identifier: NSTouchBarItem.Identifier, title: String) {
self.attributedTitle = title.defaultTouchbarAttributedString
super.init(identifier: identifier)

View File

@ -191,19 +191,17 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? {
let action = self.action(forItem: item)
let longAction = self.longAction(forItem: item)
var barItem: NSTouchBarItem!
switch item.type {
case .staticButton(title: let title):
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, onLongTap: longAction, bezelColor: NSColor.controlColor)
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title)
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action, onLongTap: longAction)
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
case .timeButton(formatTemplate: let template):
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, onTap: action, onLongTap: longAction)
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
case .battery():
barItem = BatteryBarItem(identifier: identifier, onTap: action, onLongTap: longAction)
barItem = BatteryBarItem(identifier: identifier)
case .dock:
barItem = AppScrubberTouchBarItem(identifier: identifier)
case .volume:
@ -219,13 +217,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
}
case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type):
barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type, onTap: action, onLongTap: longAction)
barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type)
case .currency(interval: let interval, from: let from, to: let to):
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: action, onLongTap: longAction)
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to)
case .inputsource():
barItem = InputSourceBarItem(identifier: identifier, onTap: action, onLongTap: longAction)
barItem = InputSourceBarItem(identifier: identifier)
}
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.tapClosure = action
}
if let longAction = self.longAction(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.longTapClosure = longAction
}
if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
item.isBordered = bordered
}
@ -241,7 +245,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return barItem
}
func action(forItem item: BarItemDefinition) -> ()->() {
func action(forItem item: BarItemDefinition) -> (()->())? {
switch item.action {
case .hidKey(keycode: let keycode):
return { HIDPostAuxKey(keycode) }
@ -279,12 +283,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
case .custom(closure: let closure):
return closure
case .none:
return {}
return nil
}
}
func longAction(forItem item: BarItemDefinition) -> ()->() {
func longAction(forItem item: BarItemDefinition) -> (()->())? {
switch item.longAction {
case .hidKey(keycode: let keycode):
return { HIDPostAuxKey(keycode) }
@ -322,7 +326,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
case .custom(closure: let closure):
return closure
case .none:
return {}
return nil
}
}
}

View File

@ -12,8 +12,8 @@ import Foundation
class BatteryBarItem: CustomButtonTouchBarItem {
private let batteryInfo = BatteryInfo()
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
init(identifier: NSTouchBarItem.Identifier) {
super.init(identifier: identifier, title: " ")
batteryInfo.start { [weak self] in
self?.refresh()

View File

@ -36,7 +36,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
"ETH": "Ξ",
]
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String) {
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck")
activity.interval = interval
self.from = from
@ -48,7 +48,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
self.prefix = from
}
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
super.init(identifier: identifier, title: "")
self.view = button

View File

@ -13,9 +13,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
fileprivate var notificationCenter: CFNotificationCenter
let buttonSize = NSSize(width: 21, height: 21)
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
init(identifier: NSTouchBarItem.Identifier) {
notificationCenter = CFNotificationCenterGetDistributedCenter();
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
super.init(identifier: identifier, title: "")
observeIputSourceChangedNotification();
textInputSourceDidChange()

View File

@ -3,11 +3,10 @@ import Cocoa
class TimeTouchBarItem: CustomButtonTouchBarItem {
private let dateFormatter = DateFormatter()
private var timer: Timer!
// private let button = NSButton(title: "", target: nil, action: nil)
init(identifier: NSTouchBarItem.Identifier, formatTemplate: String, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
init(identifier: NSTouchBarItem.Identifier, formatTemplate: String) {
dateFormatter.setLocalizedDateFormatFromTemplate(formatTemplate)
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
super.init(identifier: identifier, title: " ")
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
self.view = button
button.bezelColor = .clear

View File

@ -22,7 +22,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
private var manager:CLLocationManager!
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text", onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text") {
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck")
activity.interval = interval
self.units = units
@ -42,7 +42,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
iconsSource = iconsText
}
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
super.init(identifier: identifier, title: "")
self.view = button