mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
conflict resolved
This commit is contained in:
commit
ed14f34a2d
@ -1,18 +1,23 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||||
private let script: NSAppleScript!
|
private var script: NSAppleScript!
|
||||||
private let button = NSButton(title: "", target: nil, action: nil)
|
private let button = NSButton(title: "", target: nil, action: nil)
|
||||||
private let interval: TimeInterval
|
private let interval: TimeInterval
|
||||||
|
|
||||||
init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) {
|
init?(identifier: NSTouchBarItem.Identifier, source: Source, interval: TimeInterval) {
|
||||||
guard let script = NSAppleScript(source: appleScript) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
self.script = script
|
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
self.view = button
|
self.view = button
|
||||||
|
guard let source = source.string else {
|
||||||
|
button.title = "no script"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard let script = NSAppleScript(source: source) else {
|
||||||
|
button.title = "isn't a script"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.script = script
|
||||||
button.bezelColor = .clear
|
button.bezelColor = .clear
|
||||||
button.title = "compile"
|
button.title = "compile"
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|||||||
@ -53,17 +53,16 @@ class SupportedTypesHolder {
|
|||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||||
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 scriptURL = Bundle.main.url(forResource: "Weather", withExtension: "scpt")!
|
let scriptPath = Bundle.main.path(forResource: "Weather", ofType: "scpt")!
|
||||||
let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0)
|
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
||||||
return (item: item, action: .none)
|
return (item: item, action: .none)
|
||||||
},
|
},
|
||||||
"battery": { decoder in
|
"battery": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||||
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 scriptURL = Bundle.main.url(forResource: "Battery", withExtension: "scpt")!
|
let scriptPath = Bundle.main.path(forResource: "Battery", ofType: "scpt")!
|
||||||
print(try! String(contentsOf: scriptURL))
|
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), 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)
|
||||||
},
|
},
|
||||||
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]) ) },
|
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]) ) },
|
||||||
@ -92,14 +91,14 @@ class SupportedTypesHolder {
|
|||||||
enum ItemType: Decodable {
|
enum ItemType: Decodable {
|
||||||
case staticButton(title: String)
|
case staticButton(title: String)
|
||||||
case staticImageButton(title: String, image: NSImage)
|
case staticImageButton(title: String, image: NSImage)
|
||||||
case appleScriptTitledButton(source: String, refreshInterval: Double)
|
case appleScriptTitledButton(source: Source, refreshInterval: Double)
|
||||||
case timeButton(formatTemplate: String)
|
case timeButton(formatTemplate: String)
|
||||||
case flexSpace()
|
case flexSpace()
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case type
|
case type
|
||||||
case title
|
case title
|
||||||
case titleAppleScript
|
case source
|
||||||
case refreshInterval
|
case refreshInterval
|
||||||
case formatTemplate
|
case formatTemplate
|
||||||
case image
|
case image
|
||||||
@ -118,9 +117,9 @@ enum ItemType: Decodable {
|
|||||||
let type = try container.decode(ItemTypeRaw.self, forKey: .type)
|
let type = try container.decode(ItemTypeRaw.self, forKey: .type)
|
||||||
switch type {
|
switch type {
|
||||||
case .appleScriptTitledButton:
|
case .appleScriptTitledButton:
|
||||||
let source = try container.decode(String.self, forKey: .titleAppleScript)
|
let source = try container.decode(Source.self, forKey: .source)
|
||||||
let interval = try container.decode(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
|
||||||
self = .appleScriptTitledButton(source: try String(contentsOfFile: source), refreshInterval: interval)
|
self = .appleScriptTitledButton(source: source, refreshInterval: interval)
|
||||||
case .staticImageButton:
|
case .staticImageButton:
|
||||||
let title = try container.decode(String.self, forKey: .title)
|
let title = try container.decode(String.self, forKey: .title)
|
||||||
let imageRaw = try container.decode(String.self, forKey: .image)
|
let imageRaw = try container.decode(String.self, forKey: .image)
|
||||||
@ -239,3 +238,43 @@ struct GeneralParameters: Decodable {
|
|||||||
parameters = result
|
parameters = result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Source: Decodable {
|
||||||
|
let filePath: String?
|
||||||
|
let base64: String?
|
||||||
|
let inline: String?
|
||||||
|
|
||||||
|
var data: Data? {
|
||||||
|
return base64?.base64Data ?? inline?.data(using: .utf8) ?? filePath?.fileData
|
||||||
|
}
|
||||||
|
var string: String? {
|
||||||
|
return inline ?? self.data?.base64Content
|
||||||
|
}
|
||||||
|
|
||||||
|
private init(filePath: String?, base64: String?, inline: String?) {
|
||||||
|
self.filePath = filePath
|
||||||
|
self.base64 = base64
|
||||||
|
self.inline = inline
|
||||||
|
}
|
||||||
|
init(filePath: String) {
|
||||||
|
self.init(filePath: filePath, base64: nil, inline: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extension Source: Equatable {}
|
||||||
|
func ==(left: Source, right: Source) -> Bool {
|
||||||
|
return left.data == right.data
|
||||||
|
}
|
||||||
|
|
||||||
|
extension String {
|
||||||
|
var base64Data: Data? {
|
||||||
|
return Data(base64Encoded: self)
|
||||||
|
}
|
||||||
|
var fileData: Data? {
|
||||||
|
return try? Data(contentsOf: URL(fileURLWithPath: self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extension Data {
|
||||||
|
var base64Content: String? {
|
||||||
|
return String(data: self, encoding: .utf8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -60,7 +60,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
|
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
|
||||||
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
|
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
|
||||||
}
|
}
|
||||||
let jsonData = try? Data(contentsOf: URL(fileURLWithPath: presetPath))
|
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 {
|
||||||
@ -105,7 +105,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .staticImageButton(title: let title, image: let image):
|
case .staticImageButton(title: let title, image: let image):
|
||||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
||||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||||
barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
|
||||||
case .timeButton(formatTemplate: let template):
|
case .timeButton(formatTemplate: let template):
|
||||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||||
case .flexSpace:
|
case .flexSpace:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user