mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
cleaner custom actions, potentially able to register from the outside
This commit is contained in:
parent
651ddbef5c
commit
4fb7cb9b99
@ -40,7 +40,6 @@ class SupportedTypesHolder {
|
|||||||
private var supportedTypes: [String: ParametersDecoder] = [
|
private var supportedTypes: [String: ParametersDecoder] = [
|
||||||
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) },
|
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) },
|
||||||
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) },
|
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) },
|
||||||
"exitTouchbar": { _ in return (item: .staticButton(title: "exit"), action: .exitTouchbar) },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
static let sharedInstance = SupportedTypesHolder()
|
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 {
|
enum ItemType: Decodable {
|
||||||
@ -92,7 +101,7 @@ enum ActionType: Decodable {
|
|||||||
case hidKey(keycode: Int)
|
case hidKey(keycode: Int)
|
||||||
case keyPress(keycode: Int)
|
case keyPress(keycode: Int)
|
||||||
case appleSctipt(source: String)
|
case appleSctipt(source: String)
|
||||||
case exitTouchbar
|
case custom(closure: ()->())
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case action
|
case action
|
||||||
|
|||||||
@ -36,6 +36,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
|
|
||||||
private override init() {
|
private override init() {
|
||||||
super.init()
|
super.init()
|
||||||
|
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in
|
||||||
|
self?.dismissTouchBar()
|
||||||
|
}))
|
||||||
|
|
||||||
loadItems()
|
loadItems()
|
||||||
|
|
||||||
@ -145,8 +148,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
|
|
||||||
func action(forItem item: BarItemDefinition) -> ()->() {
|
func action(forItem item: BarItemDefinition) -> ()->() {
|
||||||
switch item.action {
|
switch item.action {
|
||||||
case .exitTouchbar:
|
|
||||||
return { self.dismissTouchBar() }
|
|
||||||
case .hidKey(keycode: let keycode):
|
case .hidKey(keycode: let keycode):
|
||||||
return { HIDPostAuxKey(keycode) }
|
return { HIDPostAuxKey(keycode) }
|
||||||
case .keyPress(keycode: let keycode):
|
case .keyPress(keycode: let keycode):
|
||||||
@ -163,6 +164,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
print("error \(error) when handling \(item) ")
|
print("error \(error) when handling \(item) ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case .custom(closure: let closure):
|
||||||
|
return closure
|
||||||
case .none:
|
case .none:
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user