mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
* first working
This commit is contained in:
parent
0808f0617a
commit
93d2f496d0
@ -59,7 +59,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
if (result != nil) {
|
if (result != nil) {
|
||||||
let path = result!.path
|
let path = result!.path
|
||||||
let jsonData = path.fileData
|
let jsonData = path.fileData
|
||||||
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
|
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none), additionalParameters: [:])]
|
||||||
|
|
||||||
TouchBarController.shared.createAndUpdatePreset(jsonItems: jsonItems)
|
TouchBarController.shared.createAndUpdatePreset(jsonItems: jsonItems)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,12 @@ import AppKit
|
|||||||
|
|
||||||
extension Data {
|
extension Data {
|
||||||
func barItemDefinitions() -> [BarItemDefinition]? {
|
func barItemDefinitions() -> [BarItemDefinition]? {
|
||||||
return try? JSONDecoder().decode([BarItemDefinition].self, from: self.utf8string!.stripComments().data(using: .utf8)!)
|
do {
|
||||||
|
return try JSONDecoder().decode([BarItemDefinition].self, from: self.utf8string!.stripComments().data(using: .utf8)!)
|
||||||
|
} catch {
|
||||||
|
print("\(error)")
|
||||||
|
}
|
||||||
|
return [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none), additionalParameters: [:])]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11,16 +16,19 @@ struct BarItemDefinition: Decodable {
|
|||||||
let type: ItemType
|
let type: ItemType
|
||||||
let action: ActionType
|
let action: ActionType
|
||||||
let longAction: LongActionType
|
let longAction: LongActionType
|
||||||
|
let shortPressAction: ShortPressAction
|
||||||
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 shortPressAction
|
||||||
}
|
}
|
||||||
|
|
||||||
init(type: ItemType, action: ActionType, longAction: LongActionType, additionalParameters: [GeneralParameters.CodingKeys:GeneralParameter]) {
|
init(type: ItemType, action: ActionType, longAction: LongActionType, shortPressAction: ShortPressAction, additionalParameters: [GeneralParameters.CodingKeys:GeneralParameter]) {
|
||||||
self.type = type
|
self.type = type
|
||||||
self.action = action
|
self.action = action
|
||||||
self.longAction = longAction
|
self.longAction = longAction
|
||||||
|
self.shortPressAction = shortPressAction
|
||||||
self.additionalParameters = additionalParameters
|
self.additionalParameters = additionalParameters
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,89 +36,107 @@ 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 shortPressAction = try container.decode(ShortPressAction.self, forKey: .shortPressAction)
|
||||||
var additionalParameters = try GeneralParameters(from: decoder).parameters
|
var additionalParameters = try GeneralParameters(from: decoder).parameters
|
||||||
if let result = try? parametersDecoder(decoder),
|
do {
|
||||||
case let (itemType, action, longAction, parameters) = result {
|
if let result = try? parametersDecoder(decoder),
|
||||||
parameters.forEach { additionalParameters[$0] = $1 }
|
case let (itemType, action, longAction, shortPressAction, parameters) = result {
|
||||||
self.init(type: itemType, action: action, longAction: longAction, additionalParameters: additionalParameters)
|
parameters.forEach { additionalParameters[$0] = $1 }
|
||||||
} else {
|
self.init(type: itemType, action: action, longAction: longAction, shortPressAction: shortPressAction, additionalParameters: additionalParameters)
|
||||||
self.init(type: .staticButton(title: "unknown"), action: .none, longAction: .none, additionalParameters: additionalParameters)
|
} else {
|
||||||
|
self.init(type: .staticButton(title: "unknown"), action: .none, longAction: .none, shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none), additionalParameters: additionalParameters)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("\(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SupportedTypesHolder {
|
class SupportedTypesHolder {
|
||||||
typealias ParametersDecoder = (Decoder) throws ->(item: ItemType, action: ActionType, longAction: LongActionType, parameters: [GeneralParameters.CodingKeys: GeneralParameter])
|
typealias ParametersDecoder = (Decoder) throws ->(item: ItemType, action: ActionType, longAction: LongActionType, shortPressAction: ShortPressAction, 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), longAction: .none, parameters: [.align: .align(.left)]) },
|
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53), longAction: .none, parameters: [.align: .align(.left)], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.keyPress, keycode: 53)) },
|
||||||
"delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:])},
|
"delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.keyPress, keycode: 117))},
|
||||||
"brightnessUp": { _ in
|
"brightnessUp": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
|
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
|
||||||
return (item: .staticButton(title: ""), action: .keyPress(keycode: 113), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .keyPress(keycode: 113), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.keyPress, keycode: 113))
|
||||||
},
|
},
|
||||||
"brightnessDown": { _ in
|
"brightnessDown": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
|
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
|
||||||
return (item: .staticButton(title: ""), action: .keyPress(keycode: 107), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .keyPress(keycode: 107), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.keyPress, keycode: 107))
|
||||||
},
|
},
|
||||||
"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), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_SOUND_DOWN))
|
||||||
},
|
},
|
||||||
"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), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_SOUND_UP))
|
||||||
},
|
},
|
||||||
"mute": { _ in
|
"mute": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarAudioOutputMuteTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarAudioOutputMuteTemplate)!)
|
||||||
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_MUTE), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_MUTE), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_MUTE))
|
||||||
},
|
},
|
||||||
"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), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_PREVIOUS))
|
||||||
},
|
},
|
||||||
"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), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PLAY), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_PLAY))
|
||||||
},
|
},
|
||||||
"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), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), longAction: .none, parameters: [.image: imageParameter], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.hidKey, keycode: NX_KEYTYPE_NEXT))
|
||||||
},
|
},
|
||||||
"weather": { decoder in
|
"weather": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval; case units; case api_key ; case icon_type }
|
enum CodingKeys: String, CodingKey { case refreshInterval; case units; case api_key ; case icon_type; case shortPressAction }
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
let units = try container.decodeIfPresent(String.self, forKey: .units)
|
let units = try container.decodeIfPresent(String.self, forKey: .units)
|
||||||
let api_key = try container.decodeIfPresent(String.self, forKey: .api_key)
|
let api_key = try container.decodeIfPresent(String.self, forKey: .api_key)
|
||||||
let icon_type = try container.decodeIfPresent(String.self, forKey: .icon_type)
|
let icon_type = try container.decodeIfPresent(String.self, forKey: .icon_type)
|
||||||
let action = try ActionType(from: decoder)
|
let shortPressAction = try container.decodeIfPresent(ShortPressAction.self, forKey: .shortPressAction)
|
||||||
let longAction = try LongActionType(from: decoder)
|
let longAction = try LongActionType(from: decoder)
|
||||||
return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6", icon_type: icon_type ?? "text"), action: action, longAction: longAction, parameters: [:])
|
return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6", icon_type: icon_type ?? "text"), action: .none, longAction: longAction, parameters: [:], shortPressAction: shortPressAction!)
|
||||||
},
|
},
|
||||||
"currency": { decoder in
|
"currency": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to }
|
enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to; case shortPressAction }
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
let from = try container.decodeIfPresent(String.self, forKey: .from)
|
let from = try container.decodeIfPresent(String.self, forKey: .from)
|
||||||
let to = try container.decodeIfPresent(String.self, forKey: .to)
|
let to = try container.decodeIfPresent(String.self, forKey: .to)
|
||||||
let action = try ActionType(from: decoder)
|
let shortPressAction = try container.decodeIfPresent(ShortPressAction.self, forKey: .shortPressAction)
|
||||||
let longAction = try LongActionType(from: decoder)
|
let longAction = try LongActionType(from: decoder)
|
||||||
return (item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), action: action, longAction: longAction, parameters: [:])
|
return (item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), action: .none, longAction: longAction, parameters: [:], shortPressAction: shortPressAction!)
|
||||||
},
|
},
|
||||||
"dock": { decoder in
|
"dock": { decoder in
|
||||||
return (item: .dock(), action: .none, longAction: .none, parameters: [:])
|
return (item: .dock(), action: .none, longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
},
|
},
|
||||||
"inputsource": { decoder in
|
"inputsource": { decoder in
|
||||||
return (item: .inputsource(), action: .none, longAction: .none, parameters: [:])
|
return (item: .inputsource(), action: .none, longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
|
},
|
||||||
|
"battery": { decoder in
|
||||||
|
enum CodingKeys: String, CodingKey { case shortPressAction }
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
let shortPressAction = try container.decodeIfPresent(ShortPressAction.self, forKey: .shortPressAction)
|
||||||
|
return (item: .battery(), action: .none, longAction: .none, parameters: [:], shortPressAction: shortPressAction!)
|
||||||
|
},
|
||||||
|
"timeButton": { decoder in
|
||||||
|
enum CodingKeys: String, CodingKey { case formatTemplate; case shortPressAction }
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
||||||
|
let shortPressAction = try container.decodeIfPresent(ShortPressAction.self, forKey: .shortPressAction)
|
||||||
|
return (item: .timeButton(formatTemplate: template), action: .none, longAction: .none, parameters: [:], shortPressAction: shortPressAction!)
|
||||||
},
|
},
|
||||||
"volume": { decoder in
|
"volume": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case image }
|
enum CodingKeys: String, CodingKey { case image }
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
||||||
return (item: .volume(), action: .none, longAction: .none, parameters: [.image: .image(source: img)])
|
return (item: .volume(), action: .none, longAction: .none, parameters: [.image: .image(source: img)], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
} else {
|
} else {
|
||||||
return (item: .volume(), action: .none, longAction: .none, parameters: [:])
|
return (item: .volume(), action: .none, longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"brightness": { decoder in
|
"brightness": { decoder in
|
||||||
@ -118,20 +144,20 @@ class SupportedTypesHolder {
|
|||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
||||||
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, longAction: .none, parameters: [.image: .image(source: img)])
|
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, longAction: .none, parameters: [.image: .image(source: img)], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
} else {
|
} else {
|
||||||
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, longAction: .none, parameters: [:])
|
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), longAction: .none, parameters: [:]) },
|
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.shellScript, executablePath: "/usr/bin/pmset", shellArguments: ["sleepnow"])) },
|
||||||
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), longAction: .none, parameters: [:])},
|
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), longAction: .none, parameters: [:], shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.shellScript, executablePath: "/usr/bin/pmset", shellArguments: ["displaysleepnow"]))},
|
||||||
]
|
]
|
||||||
|
|
||||||
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), longAction: try LongActionType(from: decoder), parameters: [:])
|
return (item: try ItemType(from: decoder), action: try ActionType(from: decoder), longAction: try LongActionType(from: decoder), shortPressAction: try ShortPressAction(from: decoder), parameters: [:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,13 +165,14 @@ class SupportedTypesHolder {
|
|||||||
supportedTypes[typename] = decoder
|
supportedTypes[typename] = decoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func register(typename: String, item: ItemType, action: ActionType, longAction: LongActionType) {
|
func register(typename: String, item: ItemType, action: ActionType, longAction: LongActionType, shortPressAction: ShortPressAction) {
|
||||||
register(typename: typename) { _ in
|
register(typename: typename) { _ in
|
||||||
return (item: item, action: action, longAction: longAction, parameters: [:])
|
return (item: item, action: action, longAction: longAction, shortPressAction: shortPressAction, parameters: [:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum ItemType: Decodable {
|
enum ItemType: Decodable {
|
||||||
case staticButton(title: String)
|
case staticButton(title: String)
|
||||||
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
|
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
|
||||||
@ -485,3 +512,97 @@ enum Align: String, Decodable {
|
|||||||
case center
|
case center
|
||||||
case right
|
case right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ShortPressAction: Codable {
|
||||||
|
let actionType: PressActionType
|
||||||
|
let url: String?
|
||||||
|
let keycode: Int
|
||||||
|
let appleScript: String?
|
||||||
|
let executablePath: String?
|
||||||
|
let shellArguments: [String]?
|
||||||
|
let custom: () -> ()?
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case actionType = "type"
|
||||||
|
case url
|
||||||
|
case keycode
|
||||||
|
case appleScript
|
||||||
|
case executablePath
|
||||||
|
case shellArguments
|
||||||
|
}
|
||||||
|
|
||||||
|
init(actionType: PressActionType, url: String? = "", keycode: Int? = -1, appleScript: String? = "", executablePath: String? = "", shellArguments: [String]? = [], custom: @escaping () -> Void? = {return}) {
|
||||||
|
self.actionType = actionType
|
||||||
|
self.url = url
|
||||||
|
self.keycode = keycode!
|
||||||
|
self.appleScript = appleScript
|
||||||
|
self.executablePath = executablePath
|
||||||
|
self.shellArguments = shellArguments
|
||||||
|
self.custom = custom
|
||||||
|
}
|
||||||
|
|
||||||
|
init(from decoder: Decoder) throws {
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
let actionType = try container.decode(PressActionType.self, forKey: .actionType)
|
||||||
|
let url = try container.decodeIfPresent(String.self, forKey: .url) ?? ""
|
||||||
|
let keycode = try container.decodeIfPresent(Int.self, forKey: .keycode) ?? -1
|
||||||
|
var appleScript = try container.decodeIfPresent(String.self, forKey: .appleScript)
|
||||||
|
do {
|
||||||
|
if let test = appleScript?.fileData?.utf8string {
|
||||||
|
appleScript = test
|
||||||
|
} else if let test = appleScript?.base64Data?.utf8string {
|
||||||
|
appleScript = test
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("error")
|
||||||
|
}
|
||||||
|
let executablePath = try container.decodeIfPresent(String.self, forKey: .executablePath) ?? ""
|
||||||
|
let shellArguments = try container.decodeIfPresent([String].self, forKey: .shellArguments) ?? []
|
||||||
|
self.init(actionType: actionType, url: url, keycode: keycode, appleScript: appleScript, executablePath: executablePath, shellArguments: shellArguments)
|
||||||
|
}
|
||||||
|
|
||||||
|
enum PressActionType: Codable {
|
||||||
|
func encode(to encoder: Encoder) throws {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
case none
|
||||||
|
case hidKey
|
||||||
|
case keyPress
|
||||||
|
case appleScript
|
||||||
|
case shellScript
|
||||||
|
case custom
|
||||||
|
case openUrl
|
||||||
|
|
||||||
|
private enum CodingKeys: String, CodingKey {
|
||||||
|
case type
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum ActionTypeRaw: String, Decodable {
|
||||||
|
case hidKey
|
||||||
|
case keyPress
|
||||||
|
case appleScript
|
||||||
|
case shellScript
|
||||||
|
case openUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
init(from decoder: Decoder) throws {
|
||||||
|
self = .none
|
||||||
|
|
||||||
|
let container = try decoder.singleValueContainer()
|
||||||
|
let actionType = try container.decode(ActionTypeRaw.self)
|
||||||
|
switch actionType {
|
||||||
|
case .hidKey:
|
||||||
|
self = .hidKey
|
||||||
|
case .keyPress:
|
||||||
|
self = .keyPress
|
||||||
|
case .appleScript:
|
||||||
|
self = .appleScript
|
||||||
|
case .shellScript:
|
||||||
|
self = .shellScript
|
||||||
|
case .openUrl:
|
||||||
|
self = .openUrl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -63,7 +63,7 @@ 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"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none, shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none ))
|
||||||
|
|
||||||
createAndUpdatePreset()
|
createAndUpdatePreset()
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
|
|
||||||
let jsonData = presetPath.fileData
|
let jsonData = presetPath.fileData
|
||||||
|
|
||||||
return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
|
return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, shortPressAction: ShortPressAction(actionType: ShortPressAction.PressActionType.none), additionalParameters: [:])]
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
|
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
|
||||||
@ -166,19 +166,20 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? {
|
func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? {
|
||||||
let action = self.action(forItem: item)
|
// let action = self.action(forItem: item)
|
||||||
let longAction = self.longAction(forItem: item)
|
let longAction = self.longAction(forItem: item)
|
||||||
|
let shortPressAction = self.shortPressAction(forItem: item)
|
||||||
|
|
||||||
var barItem: NSTouchBarItem!
|
var barItem: NSTouchBarItem!
|
||||||
switch item.type {
|
switch item.type {
|
||||||
case .staticButton(title: let title):
|
case .staticButton(title: let title):
|
||||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, onLongTap: longAction, bezelColor: NSColor.controlColor)
|
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: shortPressAction, onLongTap: longAction, bezelColor: NSColor.controlColor)
|
||||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||||
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action, onLongTap: longAction)
|
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: shortPressAction, onLongTap: longAction)
|
||||||
case .timeButton(formatTemplate: let template):
|
case .timeButton(formatTemplate: let template):
|
||||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, onTap: action, onLongTap: longAction)
|
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, onTap: shortPressAction, onLongTap: longAction)
|
||||||
case .battery():
|
case .battery():
|
||||||
barItem = BatteryBarItem(identifier: identifier, onTap: action, onLongTap: longAction)
|
barItem = BatteryBarItem(identifier: identifier, onTap: shortPressAction, onLongTap: longAction)
|
||||||
case .dock:
|
case .dock:
|
||||||
barItem = AppScrubberTouchBarItem(identifier: identifier)
|
barItem = AppScrubberTouchBarItem(identifier: identifier)
|
||||||
case .volume:
|
case .volume:
|
||||||
@ -194,11 +195,11 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
|
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
|
||||||
}
|
}
|
||||||
case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type):
|
case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type):
|
||||||
barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type, onTap: action, onLongTap: longAction)
|
barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type, onTap: shortPressAction, onLongTap: longAction)
|
||||||
case .currency(interval: let interval, from: let from, to: let to):
|
case .currency(interval: let interval, from: let from, to: let to):
|
||||||
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: action, onLongTap: longAction)
|
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: shortPressAction, onLongTap: longAction)
|
||||||
case .inputsource():
|
case .inputsource():
|
||||||
barItem = InputSourceBarItem(identifier: identifier, onTap: action, onLongTap: longAction)
|
barItem = InputSourceBarItem(identifier: identifier, onLongTap: longAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||||
@ -256,7 +257,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func longAction(forItem item: BarItemDefinition) -> ()->() {
|
func longAction(forItem item: BarItemDefinition) -> ()->() {
|
||||||
switch item.longAction {
|
switch item.longAction {
|
||||||
@ -299,6 +299,45 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shortPressAction(forItem item: BarItemDefinition) -> ()->() {
|
||||||
|
switch item.shortPressAction.actionType {
|
||||||
|
case ShortPressAction.PressActionType.hidKey:
|
||||||
|
return { HIDPostAuxKey(item.shortPressAction.keycode) }
|
||||||
|
case ShortPressAction.PressActionType.keyPress:
|
||||||
|
return { GenericKeyPress(keyCode: CGKeyCode(item.shortPressAction.keycode)).send() }
|
||||||
|
case ShortPressAction.PressActionType.appleScript:
|
||||||
|
return {
|
||||||
|
let appleScriptSource = NSAppleScript(source: item.shortPressAction.appleScript!)
|
||||||
|
var error: NSDictionary?
|
||||||
|
appleScriptSource?.executeAndReturnError(&error)
|
||||||
|
if let error = error {
|
||||||
|
print("error \(error) when handling \(item) ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case ShortPressAction.PressActionType.shellScript:
|
||||||
|
return {
|
||||||
|
let task = Process()
|
||||||
|
task.launchPath = item.shortPressAction.executablePath
|
||||||
|
task.arguments = item.shortPressAction.shellArguments
|
||||||
|
task.launch()
|
||||||
|
}
|
||||||
|
case ShortPressAction.PressActionType.openUrl:
|
||||||
|
return {
|
||||||
|
if let url = URL(string: item.shortPressAction.url!), NSWorkspace.shared.open(url) {
|
||||||
|
#if DEBUG
|
||||||
|
print("URL was successfully opened")
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case ShortPressAction.PressActionType.none:
|
||||||
|
return {}
|
||||||
|
case ShortPressAction.PressActionType.custom:
|
||||||
|
return item.shortPressAction.custom as! () -> ()
|
||||||
|
case .none:
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol CanSetWidth {
|
protocol CanSetWidth {
|
||||||
|
|||||||
@ -13,9 +13,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
|
|||||||
fileprivate var notificationCenter: CFNotificationCenter
|
fileprivate var notificationCenter: CFNotificationCenter
|
||||||
let buttonSize = NSSize(width: 21, height: 21)
|
let buttonSize = NSSize(width: 21, height: 21)
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
|
init(identifier: NSTouchBarItem.Identifier, onLongTap: @escaping () -> ()) {
|
||||||
notificationCenter = CFNotificationCenterGetDistributedCenter();
|
notificationCenter = CFNotificationCenterGetDistributedCenter();
|
||||||
super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap)
|
super.init(identifier: identifier, title: "⏳", onTap: onLongTap, onLongTap: onLongTap)
|
||||||
|
|
||||||
observeIputSourceChangedNotification();
|
observeIputSourceChangedNotification();
|
||||||
textInputSourceDidChange()
|
textInputSourceDidChange()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user