diff --git a/MTMR/KeyPress.swift b/MTMR/KeyPress.swift index 80d4d1c..2e72323 100644 --- a/MTMR/KeyPress.swift +++ b/MTMR/KeyPress.swift @@ -13,6 +13,10 @@ protocol KeyPress { func send() } +struct GenericKeyPress: KeyPress { + var keyCode: CGKeyCode +} + extension KeyPress { func send () { let src = CGEventSource(stateID: .hidSystemState) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index c6c976f..46e8b95 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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 { @@ -148,18 +149,32 @@ class TouchBarController: NSObject, NSTouchBarDelegate { 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() - } - } -} diff --git a/MTMR/TouchBarItems.swift b/MTMR/TouchBarItems.swift index 2f93810..a1adf35 100644 --- a/MTMR/TouchBarItems.swift +++ b/MTMR/TouchBarItems.swift @@ -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) }