mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
additional parameters as dictionary for easier mapping
This commit is contained in:
parent
bbc3d9dcfe
commit
dd0ecf047d
@ -12,13 +12,13 @@ extension Data {
|
|||||||
struct BarItemDefinition: Decodable {
|
struct BarItemDefinition: Decodable {
|
||||||
let type: ItemType
|
let type: ItemType
|
||||||
let action: ActionType
|
let action: ActionType
|
||||||
let additionalParameters: [GeneralParameter]
|
let additionalParameters: [GeneralParameters.CodingKeys: GeneralParameter]
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case type
|
case type
|
||||||
}
|
}
|
||||||
|
|
||||||
init(type: ItemType, action: ActionType, additionalParameters: [GeneralParameter]) {
|
init(type: ItemType, action: ActionType, additionalParameters: [GeneralParameters.CodingKeys:GeneralParameter]) {
|
||||||
self.type = type
|
self.type = type
|
||||||
self.action = action
|
self.action = action
|
||||||
self.additionalParameters = additionalParameters
|
self.additionalParameters = additionalParameters
|
||||||
@ -28,10 +28,11 @@ struct BarItemDefinition: Decodable {
|
|||||||
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 parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
|
||||||
let 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, parameters) = result {
|
case let (itemType, action, parameters) = result {
|
||||||
self.init(type: itemType, action: action, additionalParameters: additionalParameters + parameters)
|
parameters.forEach { additionalParameters[$0] = $1 }
|
||||||
|
self.init(type: itemType, action: action, additionalParameters: additionalParameters)
|
||||||
} else {
|
} else {
|
||||||
self.init(type: .staticButton(title: "unknown"), action: .none, additionalParameters: additionalParameters)
|
self.init(type: .staticButton(title: "unknown"), action: .none, additionalParameters: additionalParameters)
|
||||||
}
|
}
|
||||||
@ -40,30 +41,30 @@ struct BarItemDefinition: Decodable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SupportedTypesHolder {
|
class SupportedTypesHolder {
|
||||||
typealias ParametersDecoder = (Decoder) throws ->(item: ItemType, action: ActionType, parameters: [GeneralParameter])
|
typealias ParametersDecoder = (Decoder) throws ->(item: ItemType, action: ActionType, parameters: [GeneralParameters.CodingKeys: GeneralParameter])
|
||||||
private var supportedTypes: [String: ParametersDecoder] = [
|
private var supportedTypes: [String: ParametersDecoder] = [
|
||||||
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53), parameters: [.align(.left)]) },
|
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53), parameters: [.align: .align(.left)]) },
|
||||||
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113), parameters: []) },
|
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113), parameters: [:]) },
|
||||||
"brightnessDown": { _ in return (item: .staticButton(title: "🔅"), action: .keyPress(keycode: 107), parameters: []) },
|
"brightnessDown": { _ in return (item: .staticButton(title: "🔅"), action: .keyPress(keycode: 107), parameters: [:]) },
|
||||||
"volumeDown": { _ in
|
"volumeDown": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN), parameters: [imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN), parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"volumeUp": { _ in
|
"volumeUp": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeUpTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeUpTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP), parameters: [imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP), parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"previous": { _ in
|
"previous": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarRewindTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarRewindTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS), parameters: [imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS), parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"play": { _ in
|
"play": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarPlayPauseTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarPlayPauseTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PLAY), parameters: [imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PLAY), parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"next": { _ in
|
"next": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarFastForwardTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarFastForwardTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), parameters: [imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"weather": { decoder in
|
"weather": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||||
@ -71,7 +72,7 @@ class SupportedTypesHolder {
|
|||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
let scriptPath = Bundle.main.path(forResource: "Weather", ofType: "scpt")!
|
let scriptPath = Bundle.main.path(forResource: "Weather", ofType: "scpt")!
|
||||||
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
||||||
return (item: item, action: .none, parameters: [])
|
return (item: item, action: .none, parameters: [:])
|
||||||
},
|
},
|
||||||
"battery": { decoder in
|
"battery": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||||
@ -79,17 +80,17 @@ class SupportedTypesHolder {
|
|||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
let scriptPath = Bundle.main.path(forResource: "Battery", ofType: "scpt")!
|
let scriptPath = Bundle.main.path(forResource: "Battery", ofType: "scpt")!
|
||||||
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
||||||
return (item: item, action: .none, parameters: [])
|
return (item: item, action: .none, parameters: [:])
|
||||||
},
|
},
|
||||||
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), parameters: []) },
|
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), parameters: [:]) },
|
||||||
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), parameters: []) },
|
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), parameters: [:]) },
|
||||||
]
|
]
|
||||||
|
|
||||||
static let sharedInstance = SupportedTypesHolder()
|
static let sharedInstance = SupportedTypesHolder()
|
||||||
|
|
||||||
func lookup(by type: String) -> ParametersDecoder {
|
func lookup(by type: String) -> ParametersDecoder {
|
||||||
return supportedTypes[type] ?? { decoder in
|
return supportedTypes[type] ?? { decoder in
|
||||||
return (item: try ItemType(from: decoder), action: try ActionType(from: decoder), parameters: [])
|
return (item: try ItemType(from: decoder), action: try ActionType(from: decoder), parameters: [:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +100,7 @@ class SupportedTypesHolder {
|
|||||||
|
|
||||||
func register(typename: String, item: ItemType, action: ActionType) {
|
func register(typename: String, item: ItemType, action: ActionType) {
|
||||||
register(typename: typename) { _ in
|
register(typename: typename) { _ in
|
||||||
return (item: item, action: action, parameters: [])
|
return (item: item, action: action, parameters: [:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,24 +240,24 @@ enum GeneralParameter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct GeneralParameters: Decodable {
|
struct GeneralParameters: Decodable {
|
||||||
let parameters: [GeneralParameter]
|
let parameters: [GeneralParameters.CodingKeys: GeneralParameter]
|
||||||
|
|
||||||
fileprivate enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case width
|
case width
|
||||||
case image
|
case image
|
||||||
case align
|
case align
|
||||||
}
|
}
|
||||||
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)
|
||||||
var result: [GeneralParameter] = []
|
var result: [GeneralParameters.CodingKeys: GeneralParameter] = [:]
|
||||||
if let value = try container.decodeIfPresent(CGFloat.self, forKey: .width) {
|
if let value = try container.decodeIfPresent(CGFloat.self, forKey: .width) {
|
||||||
result.append(.width(value))
|
result[.width] = .width(value)
|
||||||
}
|
}
|
||||||
if let imageSource = try container.decodeIfPresent(Source.self, forKey: .image) {
|
if let imageSource = try container.decodeIfPresent(Source.self, forKey: .image) {
|
||||||
result.append(.image(source: imageSource))
|
result[.image] = .image(source: imageSource)
|
||||||
}
|
}
|
||||||
let align = try container.decodeIfPresent(Align.self, forKey: .align) ?? .center
|
let align = try container.decodeIfPresent(Align.self, forKey: .align) ?? .center
|
||||||
result.append(.align(align))
|
result[.align] = .align(align)
|
||||||
parameters = result
|
parameters = result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,7 +75,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
|
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
|
||||||
}
|
}
|
||||||
let jsonData = presetPath.fileData
|
let jsonData = presetPath.fileData
|
||||||
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [])]
|
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [:])]
|
||||||
|
|
||||||
for item in jsonItems {
|
for item in jsonItems {
|
||||||
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
||||||
@ -144,17 +144,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
// //todo:add item.view to scrollview
|
// //todo:add item.view to scrollview
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
for parameter in item.additionalParameters {
|
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||||
if case .width(let value) = parameter, let widthBarItem = barItem as? CanSetWidth {
|
widthBarItem.setWidth(value: value)
|
||||||
widthBarItem.setWidth(value: value)
|
}
|
||||||
}
|
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
|
||||||
if case .image(let source) = parameter, let item = barItem as? CustomButtonTouchBarItem {
|
let button = item.button!
|
||||||
let button = item.button!
|
button.image = source.image
|
||||||
button.image = source.image
|
button.imagePosition = .imageLeading
|
||||||
button.imagePosition = .imageLeading
|
button.imageHugsTitle = true
|
||||||
button.imageHugsTitle = true
|
button.bezelColor = .clear
|
||||||
button.bezelColor = .clear
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return barItem
|
return barItem
|
||||||
}
|
}
|
||||||
@ -203,19 +201,11 @@ extension NSCustomTouchBarItem: CanSetWidth {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Collection where Element == GeneralParameter {
|
|
||||||
var align: Align? {
|
|
||||||
for x in self {
|
|
||||||
if case .align(let result) = x {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
extension BarItemDefinition {
|
extension BarItemDefinition {
|
||||||
var centerAligned: Bool {
|
var centerAligned: Bool {
|
||||||
let align = self.additionalParameters.align
|
if case .align(let result)? = self.additionalParameters[.align] {
|
||||||
return align == .none || align == .center
|
return result == .center
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user