mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
Added Native Battery
This commit is contained in:
parent
d4108d848e
commit
7874433d9b
@ -18,7 +18,9 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
var error: NSDictionary?
|
var error: NSDictionary?
|
||||||
guard script.compileAndReturnError(&error) else {
|
guard script.compileAndReturnError(&error) else {
|
||||||
// print(error?.description ?? "unknown error")
|
#if DEBUG
|
||||||
|
print(error?.description ?? "unknown error")
|
||||||
|
#endif
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.button.title = "error"
|
self.button.title = "error"
|
||||||
}
|
}
|
||||||
@ -33,7 +35,9 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func refreshAndSchedule() {
|
func refreshAndSchedule() {
|
||||||
// print("refresh happened")
|
#if DEBUG
|
||||||
|
print("refresh happened")
|
||||||
|
#endif
|
||||||
let scriptResult = self.execute()
|
let scriptResult = self.execute()
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.button.title = scriptResult
|
self.button.title = scriptResult
|
||||||
|
|||||||
@ -15,35 +15,17 @@ class BatteryBarItem: NSCustomTouchBarItem {
|
|||||||
|
|
||||||
override init(identifier: NSTouchBarItem.Identifier) {
|
override init(identifier: NSTouchBarItem.Identifier) {
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
|
|
||||||
self.view = button
|
self.view = button
|
||||||
button.bezelColor = .clear
|
button.bezelColor = .clear
|
||||||
// updateInfo()
|
|
||||||
|
|
||||||
BatteryMonitor(button: button)
|
let batteryInfo = BatteryInfo(button: button)
|
||||||
|
batteryInfo.start()
|
||||||
|
batteryInfo.updateInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func updateInfo() {
|
|
||||||
var title = ""
|
|
||||||
|
|
||||||
let info = BatteryInfo().getInfo()
|
|
||||||
let timeRemainig = info["timeRemainig"] as! String
|
|
||||||
let percentage = info["percentage"] as! Int
|
|
||||||
let isCharging = info["isCharging"] as! Bool
|
|
||||||
let isCharged = info["isCharged"] as! Bool
|
|
||||||
|
|
||||||
if isCharged {
|
|
||||||
title += "⚡️"
|
|
||||||
}
|
|
||||||
|
|
||||||
title += String(percentage) + "%" + timeRemainig
|
|
||||||
|
|
||||||
button.title = title
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class BatteryInfo: NSObject {
|
class BatteryInfo: NSObject {
|
||||||
@ -52,11 +34,13 @@ class BatteryInfo: NSObject {
|
|||||||
var timeToFull: Int = 0
|
var timeToFull: Int = 0
|
||||||
var isCharged: Bool = false
|
var isCharged: Bool = false
|
||||||
var isCharging: Bool = false
|
var isCharging: Bool = false
|
||||||
|
var ACPower: String = ""
|
||||||
|
var timeRemaining: String = ""
|
||||||
|
|
||||||
var button: NSButton?
|
var button: NSButton?
|
||||||
var loop:CFRunLoopSource?
|
var loop:CFRunLoopSource?
|
||||||
|
|
||||||
override convenience init(button: NSButton) {
|
init(button: NSButton) {
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
self.button = button
|
self.button = button
|
||||||
@ -72,13 +56,11 @@ class BatteryInfo: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
|
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
|
||||||
watcher.getInfo()
|
watcher.updateInfo()
|
||||||
}, context).takeRetainedValue() as CFRunLoopSource
|
}, context).takeRetainedValue() as CFRunLoopSource
|
||||||
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func stop() {
|
func stop() {
|
||||||
if !(self.loop != nil) {
|
if !(self.loop != nil) {
|
||||||
return
|
return
|
||||||
@ -87,18 +69,6 @@ class BatteryInfo: NSObject {
|
|||||||
self.loop = nil
|
self.loop = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFormattedTime(time: Int) -> String {
|
|
||||||
if (time > 0) {
|
|
||||||
let timeFormatted = NSString(format: " (%d:%02d)", time / 60, time % 60) as String
|
|
||||||
print(timeFormatted)
|
|
||||||
return timeFormatted
|
|
||||||
} else if (time == 0) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return "(?)"
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPSInfo() {
|
func getPSInfo() {
|
||||||
let psInfo = IOPSCopyPowerSourcesInfo().takeRetainedValue()
|
let psInfo = IOPSCopyPowerSourcesInfo().takeRetainedValue()
|
||||||
let psList = IOPSCopyPowerSourcesList(psInfo).takeRetainedValue() as [CFTypeRef]
|
let psList = IOPSCopyPowerSourcesList(psInfo).takeRetainedValue() as [CFTypeRef]
|
||||||
@ -129,32 +99,39 @@ class BatteryInfo: NSObject {
|
|||||||
if (isCharging != nil) {
|
if (isCharging != nil) {
|
||||||
self.isCharging = isCharging as! Bool
|
self.isCharging = isCharging as! Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let ACPower = psDesc[kIOPSPowerSourceStateKey]
|
||||||
|
if (ACPower != nil) {
|
||||||
|
self.ACPower = ACPower as! String
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getInfo() -> [String: Any] {
|
func getFormattedTime(time: Int) -> String {
|
||||||
var result: [String: Any] = [:]
|
if (time > 0) {
|
||||||
var timeRemaining = ""
|
let timeFormatted = NSString(format: " (%d:%02d)", time / 60, time % 60) as String
|
||||||
|
return timeFormatted
|
||||||
self.getPSInfo()
|
} else if (time == 0) {
|
||||||
// print(self.current)
|
return ""
|
||||||
// print(self.timeToEmpty)
|
|
||||||
// print(self.timeToFull)
|
|
||||||
// print(self.isCharged)
|
|
||||||
// print(self.isCharging)
|
|
||||||
|
|
||||||
if isCharged {
|
|
||||||
timeRemaining = getFormattedTime(time: self.timeToFull)
|
|
||||||
} else {
|
|
||||||
timeRemaining = getFormattedTime(time: self.timeToEmpty)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result["timeRemainig"] = timeRemaining
|
return " (?)"
|
||||||
result["percentage"] = self.current
|
|
||||||
result["isCharging"] = self.isCharging
|
|
||||||
result["isCharged"] = self.isCharged
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func updateInfo() {
|
||||||
|
var title = ""
|
||||||
|
self.getPSInfo()
|
||||||
|
|
||||||
|
if ACPower == "AC Power" {
|
||||||
|
title += "⚡️"
|
||||||
|
timeRemaining = getFormattedTime(time: timeToFull)
|
||||||
|
} else {
|
||||||
|
timeRemaining = getFormattedTime(time: timeToEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
title += String(current) + "%" + timeRemaining
|
||||||
|
button?.title = title
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,10 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
|
|||||||
"IDR": "Rp",
|
"IDR": "Rp",
|
||||||
"MXN": "$",
|
"MXN": "$",
|
||||||
"SGD": "$",
|
"SGD": "$",
|
||||||
"CHF": "Fr."
|
"CHF": "Fr.",
|
||||||
|
"BTC": "฿",
|
||||||
|
"LTC": "Ł",
|
||||||
|
"ETH": "Ξ",
|
||||||
]
|
]
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> ()) {
|
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> ()) {
|
||||||
|
|||||||
@ -117,15 +117,6 @@ class SupportedTypesHolder {
|
|||||||
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, parameters: [:])
|
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, parameters: [:])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"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 scriptPath = Bundle.main.path(forResource: "Battery", ofType: "scpt")!
|
|
||||||
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
|
||||||
let action = try ActionType(from: decoder)
|
|
||||||
return (item: item, action: action, parameters: [:])
|
|
||||||
},
|
|
||||||
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), parameters: [:]) },
|
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), parameters: [:]) },
|
||||||
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), parameters: [:]) },
|
"displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), parameters: [:]) },
|
||||||
]
|
]
|
||||||
@ -153,7 +144,7 @@ enum ItemType: Decodable {
|
|||||||
case staticButton(title: String)
|
case staticButton(title: String)
|
||||||
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
|
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
|
||||||
case timeButton(formatTemplate: String)
|
case timeButton(formatTemplate: String)
|
||||||
case batteryButton()
|
case battery()
|
||||||
case dock()
|
case dock()
|
||||||
case volume()
|
case volume()
|
||||||
case brightness(refreshInterval: Double)
|
case brightness(refreshInterval: Double)
|
||||||
@ -179,7 +170,7 @@ enum ItemType: Decodable {
|
|||||||
case staticButton
|
case staticButton
|
||||||
case appleScriptTitledButton
|
case appleScriptTitledButton
|
||||||
case timeButton
|
case timeButton
|
||||||
case batteryButton
|
case battery
|
||||||
case dock
|
case dock
|
||||||
case volume
|
case volume
|
||||||
case brightness
|
case brightness
|
||||||
@ -201,8 +192,8 @@ enum ItemType: Decodable {
|
|||||||
case .timeButton:
|
case .timeButton:
|
||||||
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
||||||
self = .timeButton(formatTemplate: template)
|
self = .timeButton(formatTemplate: template)
|
||||||
case .batteryButton:
|
case .battery:
|
||||||
self = .batteryButton()
|
self = .battery()
|
||||||
case .dock:
|
case .dock:
|
||||||
self = .dock()
|
self = .dock()
|
||||||
case .volume:
|
case .volume:
|
||||||
|
|||||||
@ -23,8 +23,8 @@ extension ItemType {
|
|||||||
return "com.toxblh.mtmr.appleScriptButton."
|
return "com.toxblh.mtmr.appleScriptButton."
|
||||||
case .timeButton(formatTemplate: _):
|
case .timeButton(formatTemplate: _):
|
||||||
return "com.toxblh.mtmr.timeButton."
|
return "com.toxblh.mtmr.timeButton."
|
||||||
case .batteryButton():
|
case .battery():
|
||||||
return "com.toxblh.mtmr.batteryButton."
|
return "com.toxblh.mtmr.battery."
|
||||||
case .dock():
|
case .dock():
|
||||||
return "com.toxblh.mtmr.dock"
|
return "com.toxblh.mtmr.dock"
|
||||||
case .volume():
|
case .volume():
|
||||||
@ -166,7 +166,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action)
|
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action)
|
||||||
case .timeButton(formatTemplate: let template):
|
case .timeButton(formatTemplate: let template):
|
||||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||||
case .batteryButton():
|
case .battery():
|
||||||
barItem = BatteryBarItem(identifier: identifier)
|
barItem = BatteryBarItem(identifier: identifier)
|
||||||
case .dock:
|
case .dock:
|
||||||
barItem = AppScrubberTouchBarItem(identifier: identifier)
|
barItem = AppScrubberTouchBarItem(identifier: identifier)
|
||||||
@ -229,7 +229,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .openUrl(url: let url):
|
case .openUrl(url: let url):
|
||||||
return {
|
return {
|
||||||
if let url = URL(string: url), NSWorkspace.shared.open(url) {
|
if let url = URL(string: url), NSWorkspace.shared.open(url) {
|
||||||
// print("URL was successfully opened")
|
#if DEBUG
|
||||||
|
print("URL was successfully opened")
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
print("error", url)
|
print("error", url)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user