mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
generic actions sketched
This commit is contained in:
parent
970fb99e33
commit
1c45ca13a3
@ -13,6 +13,10 @@ protocol KeyPress {
|
|||||||
func send()
|
func send()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct GenericKeyPress: KeyPress {
|
||||||
|
var keyCode: CGKeyCode
|
||||||
|
}
|
||||||
|
|
||||||
extension KeyPress {
|
extension KeyPress {
|
||||||
func send () {
|
func send () {
|
||||||
let src = CGEventSource(stateID: .hidSystemState)
|
let src = CGEventSource(stateID: .hidSystemState)
|
||||||
|
|||||||
@ -92,14 +92,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
guard let item = self.items[identifier] else {
|
guard let item = self.items[identifier] else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
let action = self.action(forItem: item)
|
||||||
|
|
||||||
switch item.type {
|
switch item.type {
|
||||||
case .staticButton(title: let title):
|
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):
|
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||||
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
||||||
|
case .exitTouchbar:
|
||||||
|
return CustomButtonTouchBarItem(identifier: identifier, title: "exit", onTap: action)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch identifier {
|
switch identifier {
|
||||||
@ -148,18 +149,32 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
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
|
self.tapClosure = callback
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||||
@ -49,7 +49,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func didTapped() {
|
@objc func didTapped() {
|
||||||
self.tapClosure(self)
|
self.tapClosure()
|
||||||
let hf: HapticFeedback = HapticFeedback()
|
let hf: HapticFeedback = HapticFeedback()
|
||||||
hf.tap(strong: 6)
|
hf.tap(strong: 6)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user