mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38: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)
|
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()
|
||||||
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user