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

Refactor new actions parameter moving it to main definition

Renamed old action and longAction to legacy
This commit is contained in:
Matteo Piccina 2020-07-30 19:17:12 +02:00
parent 2c179606d3
commit a45c72b3f3
4 changed files with 131 additions and 97 deletions

View File

@ -9,41 +9,46 @@ extension Data {
struct BarItemDefinition: Decodable {
let type: ItemType
let action: ActionType
let longAction: LongActionType
let actions: [Action]
let legacyAction: LegacyActionType
let legacyLongAction: LegacyLongActionType
let additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter]
private enum CodingKeys: String, CodingKey {
case type
case actions
}
init(type: ItemType, action: ActionType, longAction: LongActionType, additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter]) {
init(type: ItemType, actions: [Action], action: LegacyActionType, legacyLongAction: LegacyLongActionType, additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter]) {
self.type = type
self.action = action
self.longAction = longAction
self.actions = actions
self.legacyAction = action
self.legacyLongAction = legacyLongAction
self.additionalParameters = additionalParameters
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type)
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
let actions = try container.decodeIfPresent([Action].self, forKey: .actions)
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type, actions: actions ?? [])
var additionalParameters = try GeneralParameters(from: decoder).parameters
if let result = try? parametersDecoder(decoder),
case let (itemType, action, longAction, parameters) = result {
case let (itemType, actions, action, longAction, parameters) = result {
parameters.forEach { additionalParameters[$0] = $1 }
self.init(type: itemType, action: action, longAction: longAction, additionalParameters: additionalParameters)
self.init(type: itemType, actions: actions, action: action, legacyLongAction: longAction, additionalParameters: additionalParameters)
} else {
self.init(type: .staticButton(title: "unknown"), action: .none, longAction: .none, additionalParameters: additionalParameters)
self.init(type: .staticButton(title: "unknown"), actions: [], action: .none, legacyLongAction: .none, additionalParameters: additionalParameters)
}
}
}
typealias ParametersDecoder = (Decoder) throws -> (
item: ItemType,
action: ActionType,
longAction: LongActionType,
actions: [Action],
legacyAction: LegacyActionType,
legacyLongAction: LegacyLongActionType,
parameters: [GeneralParameters.CodingKeys: GeneralParameter]
)
@ -51,31 +56,34 @@ class SupportedTypesHolder {
private var supportedTypes: [String: ParametersDecoder] = [
"escape": { _ in (
item: .staticButton(title: "esc"),
action: .none,
longAction: .none,
parameters: [.align: .align(.left), .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .keyPress(keycode: 53))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.align: .align(.left)]
) },
"delete": { _ in (
item: .staticButton(title: "del"),
action: .none,
longAction: .none,
parameters: [.actions: .actions([
actions: [
Action(trigger: .singleTap, value: .keyPress(keycode: 117))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) },
"brightnessUp": { _ in
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .keyPress(keycode: 144))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -83,11 +91,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .keyPress(keycode: 145))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -95,11 +104,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "ill_up"))
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_ILLUMINATION_UP))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -107,11 +117,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "ill_down"))
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_ILLUMINATION_DOWN))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -119,11 +130,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarVolumeDownTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -131,11 +143,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarVolumeUpTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_SOUND_UP))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -143,11 +156,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarAudioOutputMuteTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_MUTE))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -155,11 +169,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarRewindTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_PREVIOUS))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -167,11 +182,12 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarPlayPauseTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_PLAY))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
@ -179,41 +195,45 @@ class SupportedTypesHolder {
let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarFastForwardTemplateName)!)
return (
item: .staticButton(title: ""),
action: .none,
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
actions: [
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_NEXT))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
)
},
"sleep": { _ in (
item: .staticButton(title: "☕️"),
action: .none,
longAction: .none,
parameters: [.actions: .actions([
actions: [
Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) },
"displaySleep": { _ in (
item: .staticButton(title: "☕️"),
action: .none,
longAction: .none,
parameters: [.actions: .actions([
actions: [
Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]))
])]
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) },
]
static let sharedInstance = SupportedTypesHolder()
func lookup(by type: String) -> ParametersDecoder {
func lookup(by type: String, actions: [Action]) -> ParametersDecoder {
return supportedTypes[type] ?? { decoder in (
item: try ItemType(from: decoder),
action: try ActionType(from: decoder),
longAction: try LongActionType(from: decoder),
actions: actions,
legacyAction: try LegacyActionType(from: decoder),
legacyLongAction: try LegacyLongActionType(from: decoder),
parameters: [:]
) }
}
@ -222,12 +242,13 @@ class SupportedTypesHolder {
supportedTypes[typename] = decoder
}
func register(typename: String, item: ItemType, action: ActionType, longAction: LongActionType) {
func register(typename: String, item: ItemType, actions: [Action], legacyAction: LegacyActionType, legacyLongAction: LegacyLongActionType) {
register(typename: typename) { _ in
(
item: item,
action,
longAction,
actions,
legacyAction,
legacyLongAction,
parameters: [:]
)
}
@ -507,7 +528,7 @@ struct Action: Decodable {
}
}
enum ActionType: Decodable {
enum LegacyActionType: Decodable {
case none
case hidKey(keycode: Int32)
case keyPress(keycode: Int)
@ -565,7 +586,7 @@ enum ActionType: Decodable {
}
}
enum LongActionType: Decodable {
enum LegacyLongActionType: Decodable {
case none
case hidKey(keycode: Int32)
case keyPress(keycode: Int)
@ -630,7 +651,6 @@ enum GeneralParameter {
case bordered(_: Bool)
case background(_: NSColor)
case title(_: String)
case actions(_: [Action])
}
struct GeneralParameters: Decodable {
@ -643,7 +663,6 @@ struct GeneralParameters: Decodable {
case bordered
case background
case title
case actions
}
init(from decoder: Decoder) throws {
@ -672,10 +691,6 @@ struct GeneralParameters: Decodable {
if let title = try container.decodeIfPresent(String.self, forKey: .title) {
result[.title] = .title(title)
}
if let actions = try container.decodeIfPresent([Action].self, forKey: .actions) {
result[.actions] = .actions(actions)//.compactMap { $0.base })
}
parameters = result
}

View File

@ -92,13 +92,28 @@ 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() }), longAction: .none)
SupportedTypesHolder.sharedInstance.register(
typename: "exitTouchbar",
item: .staticButton(title: "exit"),
actions: [
Action(trigger: .singleTap, value: .custom(closure: { [weak self] in self?.dismissTouchBar() }))
],
legacyAction: .none,
legacyLongAction: .none
)
SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in
(item: .staticButton(title: ""), action: .custom(closure: { [weak self] in
guard let `self` = self else { return }
self.reloadPreset(path: self.lastPresetPath)
}), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: NSImage.stopProgressFreestandingTemplateName))!)])
(
item: .staticButton(title: ""),
actions: [
Action(trigger: .singleTap, value: .custom(closure: { [weak self] in
guard let `self` = self else { return }
self.reloadPreset(path: self.lastPresetPath)
}))
],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.width: .width(30), .image: .image(source: (NSImage(named: NSImage.stopProgressFreestandingTemplateName))!)])
}
blacklistAppIdentifiers = AppSettings.blacklistedAppIds
@ -170,7 +185,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
func reloadPreset(path: String) {
lastPresetPath = path
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), actions: [], action: .none, legacyLongAction: .none, additionalParameters: [:])]
createAndUpdatePreset(newJsonItems: items)
}
@ -313,9 +328,10 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
if let longAction = self.longAction(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.longTapClosure = longAction
}
if case let .actions(actions)? = item.additionalParameters[.actions], let item = barItem as? CustomButtonTouchBarItem {
for action in actions {
item.actions[action.trigger] = self.action(for: action)
if let touchBarItem = barItem as? CustomButtonTouchBarItem {
for action in item.actions {
touchBarItem.actions[action.trigger] = self.closure(for: action)
}
}
if case let .bordered(bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
@ -340,7 +356,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return barItem
}
func action(for action: Action) -> (() -> Void)? {
func closure(for action: Action) -> (() -> Void)? {
switch action.value {
case let .hidKey(keycode: keycode):
return { HIDPostAuxKey(keycode) }
@ -385,7 +401,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
func action(forItem item: BarItemDefinition) -> (() -> Void)? {
switch item.action {
switch item.legacyAction {
case let .hidKey(keycode: keycode):
return { HIDPostAuxKey(keycode) }
case let .keyPress(keycode: keycode):
@ -429,7 +445,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
func longAction(forItem item: BarItemDefinition) -> (() -> Void)? {
switch item.longAction {
switch item.legacyLongAction {
case let .hidKey(keycode: keycode):
return { HIDPostAuxKey(keycode) }
case let .keyPress(keycode: keycode):

View File

@ -23,8 +23,9 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
return (
item: .pomodoro(workTime: workTime ?? 1500.00, restTime: restTime ?? 300),
action: .none,
longAction: .none,
actions: [],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
)
}

View File

@ -40,7 +40,8 @@ class ParseConfig: XCTestCase {
XCTFail()
return
}
guard case .keyPress(keycode: 53)? = result?.first?.action else {
let parameter = result?.first?.additionalParameters[.actions]
guard case .actions(let actions) = parameter, case .keyPress(keycode: 53)? = actions.filter({ $0.trigger == .singleTap }).first?.value else {
XCTFail()
return
}
@ -55,7 +56,8 @@ class ParseConfig: XCTestCase {
XCTFail()
return
}
guard case .keyPress(keycode: 53)? = result?.first?.action else {
let parameter = result?.first?.additionalParameters[.actions]
guard case .actions(let actions) = parameter, case .keyPress(keycode: 53)? = actions.filter({ $0.trigger == .singleTap }).first?.value else {
XCTFail()
return
}