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

cleaner custom actions, potentially able to register from the outside

This commit is contained in:
Serg 2018-04-10 18:16:28 +07:00
parent 651ddbef5c
commit 4fb7cb9b99
2 changed files with 16 additions and 4 deletions

View File

@ -40,7 +40,6 @@ class SupportedTypesHolder {
private var supportedTypes: [String: ParametersDecoder] = [
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) },
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) },
"exitTouchbar": { _ in return (item: .staticButton(title: "exit"), action: .exitTouchbar) },
]
static let sharedInstance = SupportedTypesHolder()
@ -54,6 +53,16 @@ class SupportedTypesHolder {
}
}
}
func register(typename: String, decoder: @escaping ParametersDecoder) {
supportedTypes[typename] = decoder
}
func register(typename: String, item: ItemType, action: ActionType) {
register(typename: typename) { _ in
return (item: item, action: action)
}
}
}
enum ItemType: Decodable {
@ -92,7 +101,7 @@ enum ActionType: Decodable {
case hidKey(keycode: Int)
case keyPress(keycode: Int)
case appleSctipt(source: String)
case exitTouchbar
case custom(closure: ()->())
private enum CodingKeys: String, CodingKey {
case action

View File

@ -36,6 +36,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
private override init() {
super.init()
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in
self?.dismissTouchBar()
}))
loadItems()
@ -145,8 +148,6 @@ 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):
@ -163,6 +164,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
print("error \(error) when handling \(item) ")
}
}
case .custom(closure: let closure):
return closure
case .none:
return {}
}