1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +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 { struct BarItemDefinition: Decodable {
let type: ItemType let type: ItemType
let action: ActionType let actions: [Action]
let longAction: LongActionType let legacyAction: LegacyActionType
let legacyLongAction: LegacyLongActionType
let additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter] let additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter]
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case type 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.type = type
self.action = action self.actions = actions
self.longAction = longAction self.legacyAction = action
self.legacyLongAction = legacyLongAction
self.additionalParameters = additionalParameters self.additionalParameters = additionalParameters
} }
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type) 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 var additionalParameters = try GeneralParameters(from: decoder).parameters
if let result = try? parametersDecoder(decoder), 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 } 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 { } 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 -> ( typealias ParametersDecoder = (Decoder) throws -> (
item: ItemType, item: ItemType,
action: ActionType, actions: [Action],
longAction: LongActionType, legacyAction: LegacyActionType,
legacyLongAction: LegacyLongActionType,
parameters: [GeneralParameters.CodingKeys: GeneralParameter] parameters: [GeneralParameters.CodingKeys: GeneralParameter]
) )
@ -51,31 +56,34 @@ class SupportedTypesHolder {
private var supportedTypes: [String: ParametersDecoder] = [ private var supportedTypes: [String: ParametersDecoder] = [
"escape": { _ in ( "escape": { _ in (
item: .staticButton(title: "esc"), item: .staticButton(title: "esc"),
action: .none, actions: [
longAction: .none,
parameters: [.align: .align(.left), .actions: .actions([
Action(trigger: .singleTap, value: .keyPress(keycode: 53)) Action(trigger: .singleTap, value: .keyPress(keycode: 53))
])] ],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.align: .align(.left)]
) }, ) },
"delete": { _ in ( "delete": { _ in (
item: .staticButton(title: "del"), item: .staticButton(title: "del"),
action: .none, actions: [
longAction: .none,
parameters: [.actions: .actions([
Action(trigger: .singleTap, value: .keyPress(keycode: 117)) Action(trigger: .singleTap, value: .keyPress(keycode: 117))
])] ],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) }, ) },
"brightnessUp": { _ in "brightnessUp": { _ in
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp")) let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .keyPress(keycode: 144)) 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")) let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .keyPress(keycode: 145)) 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")) let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "ill_up"))
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_ILLUMINATION_UP)) 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")) let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "ill_down"))
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_ILLUMINATION_DOWN)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarVolumeDownTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarVolumeUpTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_SOUND_UP)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarAudioOutputMuteTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_MUTE)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarRewindTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_PREVIOUS)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarPlayPauseTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_PLAY)) 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)!) let imageParameter = GeneralParameter.image(source: NSImage(named: NSImage.touchBarFastForwardTemplateName)!)
return ( return (
item: .staticButton(title: ""), item: .staticButton(title: ""),
action: .none, actions: [
longAction: .none,
parameters: [.image: imageParameter, .actions: .actions([
Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_NEXT)) Action(trigger: .singleTap, value: .hidKey(keycode: NX_KEYTYPE_NEXT))
])] ],
legacyAction: .none,
legacyLongAction: .none,
parameters: [.image: imageParameter]
) )
}, },
"sleep": { _ in ( "sleep": { _ in (
item: .staticButton(title: "☕️"), item: .staticButton(title: "☕️"),
action: .none, actions: [
longAction: .none,
parameters: [.actions: .actions([
Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"])) Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]))
])] ],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) }, ) },
"displaySleep": { _ in ( "displaySleep": { _ in (
item: .staticButton(title: "☕️"), item: .staticButton(title: "☕️"),
action: .none, actions: [
longAction: .none,
parameters: [.actions: .actions([
Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"])) Action(trigger: .singleTap, value: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]))
])] ],
legacyAction: .none,
legacyLongAction: .none,
parameters: [:]
) }, ) },
] ]
static let sharedInstance = SupportedTypesHolder() static let sharedInstance = SupportedTypesHolder()
func lookup(by type: String) -> ParametersDecoder { func lookup(by type: String, actions: [Action]) -> ParametersDecoder {
return supportedTypes[type] ?? { decoder in ( return supportedTypes[type] ?? { decoder in (
item: try ItemType(from: decoder), item: try ItemType(from: decoder),
action: try ActionType(from: decoder), actions: actions,
longAction: try LongActionType(from: decoder), legacyAction: try LegacyActionType(from: decoder),
legacyLongAction: try LegacyLongActionType(from: decoder),
parameters: [:] parameters: [:]
) } ) }
} }
@ -222,12 +242,13 @@ class SupportedTypesHolder {
supportedTypes[typename] = decoder 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 register(typename: typename) { _ in
( (
item: item, item: item,
action, actions,
longAction, legacyAction,
legacyLongAction,
parameters: [:] parameters: [:]
) )
} }
@ -507,7 +528,7 @@ struct Action: Decodable {
} }
} }
enum ActionType: Decodable { enum LegacyActionType: Decodable {
case none case none
case hidKey(keycode: Int32) case hidKey(keycode: Int32)
case keyPress(keycode: Int) case keyPress(keycode: Int)
@ -565,7 +586,7 @@ enum ActionType: Decodable {
} }
} }
enum LongActionType: Decodable { enum LegacyLongActionType: Decodable {
case none case none
case hidKey(keycode: Int32) case hidKey(keycode: Int32)
case keyPress(keycode: Int) case keyPress(keycode: Int)
@ -630,7 +651,6 @@ enum GeneralParameter {
case bordered(_: Bool) case bordered(_: Bool)
case background(_: NSColor) case background(_: NSColor)
case title(_: String) case title(_: String)
case actions(_: [Action])
} }
struct GeneralParameters: Decodable { struct GeneralParameters: Decodable {
@ -643,7 +663,6 @@ struct GeneralParameters: Decodable {
case bordered case bordered
case background case background
case title case title
case actions
} }
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
@ -672,10 +691,6 @@ struct GeneralParameters: Decodable {
if let title = try container.decodeIfPresent(String.self, forKey: .title) { if let title = try container.decodeIfPresent(String.self, forKey: .title) {
result[.title] = .title(title) result[.title] = .title(title)
} }
if let actions = try container.decodeIfPresent([Action].self, forKey: .actions) {
result[.actions] = .actions(actions)//.compactMap { $0.base })
}
parameters = result parameters = result
} }

View File

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

View File

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

View File

@ -40,7 +40,8 @@ class ParseConfig: XCTestCase {
XCTFail() XCTFail()
return 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() XCTFail()
return return
} }
@ -55,7 +56,8 @@ class ParseConfig: XCTestCase {
XCTFail() XCTFail()
return 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() XCTFail()
return return
} }