mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-12 09:58:38 +00:00
add buttons: battery and time with format
This commit is contained in:
parent
34b51b2dd8
commit
753cfd8a31
@ -54,6 +54,21 @@ class SupportedTypesHolder {
|
||||
let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0)
|
||||
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()
|
||||
@ -78,17 +93,20 @@ class SupportedTypesHolder {
|
||||
enum ItemType: Decodable {
|
||||
case staticButton(title: String)
|
||||
case appleScriptTitledButton(source: String, refreshInterval: Double)
|
||||
case timeButton(formatTemplate: String)
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case type
|
||||
case title
|
||||
case titleAppleScript
|
||||
case refreshInterval
|
||||
case formatTemplate
|
||||
}
|
||||
|
||||
enum ItemTypeRaw: String, Decodable {
|
||||
case staticButton
|
||||
case appleScriptTitledButton
|
||||
case timeButton
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
@ -102,6 +120,9 @@ enum ItemType: Decodable {
|
||||
case .staticButton:
|
||||
let title = try container.decode(String.self, forKey: .title)
|
||||
self = .staticButton(title: title)
|
||||
case .timeButton:
|
||||
let template = try container.decode(String.self, forKey: .formatTemplate)
|
||||
self = .timeButton(formatTemplate: template)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,6 +21,8 @@ extension ItemType {
|
||||
return "com.toxblh.mtmr.staticButton."
|
||||
case .appleScriptTitledButton(source: _):
|
||||
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)
|
||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
||||
case .timeButton(formatTemplate: let template):
|
||||
return TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user