1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 09:28:38 +00:00

add buttons: battery and time with format

This commit is contained in:
Toxblh 2018-04-10 14:07:52 +01:00
parent 34b51b2dd8
commit 753cfd8a31
2 changed files with 38 additions and 13 deletions

View File

@ -1,26 +1,26 @@
import Foundation import Foundation
extension Data { extension Data {
func barItemDefinitions() -> [BarItemDefinition]? { func barItemDefinitions() -> [BarItemDefinition]? {
return try? JSONDecoder().decode([BarItemDefinition].self, from: self) return try? JSONDecoder().decode([BarItemDefinition].self, from: self)
} }
} }
struct BarItemDefinition: Decodable { struct BarItemDefinition: Decodable {
let type: ItemType let type: ItemType
let action: ActionType let action: ActionType
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case type case type
} }
init(type: ItemType, action: ActionType) { init(type: ItemType, action: ActionType) {
self.type = type self.type = type
self.action = action self.action = action
} }
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)
@ -54,20 +54,35 @@ class SupportedTypesHolder {
let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0) let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0)
return (item: item, action: .none) return (item: item, action: .none)
}, },
"battery": { decoder in
enum CodingKeys: String, CodingKey { case refreshInterval }
let container = try decoder.container(keyedBy: CodingKeys.self)
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
let scriptURL = Bundle.main.url(forResource: "battery", withExtension: "scpt")!
let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0)
return (item: item, action: .none)
},
"time": { decoder in
enum CodingKeys: String, CodingKey { case formatTemplate }
let container = try decoder.container(keyedBy: CodingKeys.self)
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate)
let item = ItemType.timeButton(formatTemplate: template ?? "HH:mm" )
return (item: item, action: .none)
},
] ]
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)) return (item: try ItemType(from: decoder), action: try ActionType(from: decoder))
} }
} }
func register(typename: String, decoder: @escaping ParametersDecoder) { func register(typename: String, decoder: @escaping ParametersDecoder) {
supportedTypes[typename] = decoder supportedTypes[typename] = decoder
} }
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) return (item: item, action: action)
@ -78,17 +93,20 @@ class SupportedTypesHolder {
enum ItemType: Decodable { enum ItemType: Decodable {
case staticButton(title: String) case staticButton(title: String)
case appleScriptTitledButton(source: String, refreshInterval: Double) case appleScriptTitledButton(source: String, refreshInterval: Double)
case timeButton(formatTemplate: String)
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case type case type
case title case title
case titleAppleScript case titleAppleScript
case refreshInterval case refreshInterval
case formatTemplate
} }
enum ItemTypeRaw: String, Decodable { enum ItemTypeRaw: String, Decodable {
case staticButton case staticButton
case appleScriptTitledButton case appleScriptTitledButton
case timeButton
} }
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
@ -102,6 +120,9 @@ enum ItemType: Decodable {
case .staticButton: case .staticButton:
let title = try container.decode(String.self, forKey: .title) let title = try container.decode(String.self, forKey: .title)
self = .staticButton(title: title) self = .staticButton(title: title)
case .timeButton:
let template = try container.decode(String.self, forKey: .formatTemplate)
self = .timeButton(formatTemplate: template)
} }
} }
} }
@ -112,19 +133,19 @@ enum ActionType: Decodable {
case keyPress(keycode: Int) case keyPress(keycode: Int)
case appleSctipt(source: String) case appleSctipt(source: String)
case custom(closure: ()->()) case custom(closure: ()->())
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case action case action
case keycode case keycode
case actionAppleScript case actionAppleScript
} }
private enum ActionTypeRaw: String, Decodable { private enum ActionTypeRaw: String, Decodable {
case hidKey case hidKey
case keyPress case keyPress
case appleScript case appleScript
} }
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.decodeIfPresent(ActionTypeRaw.self, forKey: .action) let type = try container.decodeIfPresent(ActionTypeRaw.self, forKey: .action)

View File

@ -21,6 +21,8 @@ extension ItemType {
return "com.toxblh.mtmr.staticButton." return "com.toxblh.mtmr.staticButton."
case .appleScriptTitledButton(source: _): case .appleScriptTitledButton(source: _):
return "com.toxblh.mtmr.appleScriptButton." return "com.toxblh.mtmr.appleScriptButton."
case .timeButton(formatTemplate: _):
return "com.toxblh.mtmr.timeButton."
} }
} }
@ -103,6 +105,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action) return CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action)
case .appleScriptTitledButton(source: let source, refreshInterval: let interval): case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
case .timeButton(formatTemplate: let template):
return TimeTouchBarItem(identifier: identifier, formatTemplate: template)
} }
} }