mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
generic actions sketched
This commit is contained in:
parent
970fb99e33
commit
1c45ca13a3
@ -13,6 +13,10 @@ protocol KeyPress {
|
||||
func send()
|
||||
}
|
||||
|
||||
struct GenericKeyPress: KeyPress {
|
||||
var keyCode: CGKeyCode
|
||||
}
|
||||
|
||||
extension KeyPress {
|
||||
func send () {
|
||||
let src = CGEventSource(stateID: .hidSystemState)
|
||||
|
||||
@ -92,14 +92,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
guard let item = self.items[identifier] else {
|
||||
return nil
|
||||
}
|
||||
let action = self.action(forItem: item)
|
||||
|
||||
switch item.type {
|
||||
case .staticButton(title: let title):
|
||||
return CustomButtonTouchBarItem(identifier: identifier, title: title) { _ in
|
||||
|
||||
}
|
||||
return CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action)
|
||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
||||
case .exitTouchbar:
|
||||
return CustomButtonTouchBarItem(identifier: identifier, title: "exit", onTap: action)
|
||||
}
|
||||
|
||||
switch identifier {
|
||||
@ -149,17 +150,31 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func action(forItem item: BarItemDefinition) -> ()->() {
|
||||
switch item.action {
|
||||
case .exitTouchbar:
|
||||
return { self.dismissTouchBar() }
|
||||
case .hidKey(keycode: let keycode):
|
||||
return { HIDPostAuxKey(keycode) }
|
||||
case .keyPress(keycode: let keycode):
|
||||
return { GenericKeyPress(keyCode: CGKeyCode(keycode)).send() }
|
||||
case .appleSctipt(source: let source):
|
||||
guard let appleScript = NSAppleScript(source: source) else {
|
||||
print("cannot create apple script for item \(item)")
|
||||
return {}
|
||||
}
|
||||
return {
|
||||
var error: NSDictionary?
|
||||
appleScript.executeAndReturnError(&error)
|
||||
if let error = error {
|
||||
print("error \(error) when handling \(item) ")
|
||||
}
|
||||
}
|
||||
case .none:
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension CustomButtonTouchBarItem {
|
||||
convenience init(identifier: NSTouchBarItem.Identifier, title: String, HIDKeycode: Int) {
|
||||
self.init(identifier: identifier, title: title) { _ in
|
||||
HIDPostAuxKey(HIDKeycode)
|
||||
}
|
||||
}
|
||||
convenience init(identifier: NSTouchBarItem.Identifier, title: String, key: KeyPress) {
|
||||
self.init(identifier: identifier, title: title) { _ in
|
||||
key.send()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,9 +36,9 @@ extension NSTouchBarItem.Identifier {
|
||||
}
|
||||
|
||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||
let tapClosure: (NSCustomTouchBarItem) -> ()
|
||||
let tapClosure: () -> ()
|
||||
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping (NSCustomTouchBarItem) -> ()) {
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) {
|
||||
self.tapClosure = callback
|
||||
super.init(identifier: identifier)
|
||||
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
@ -49,7 +49,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||
}
|
||||
|
||||
@objc func didTapped() {
|
||||
self.tapClosure(self)
|
||||
self.tapClosure()
|
||||
let hf: HapticFeedback = HapticFeedback()
|
||||
hf.tap(strong: 6)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user