mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-12 09:58:38 +00:00
merge stash
This commit is contained in:
parent
f78f82893a
commit
d4108d848e
@ -13,7 +13,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
|
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||||
// Insert code here to initialize your application
|
|
||||||
TouchBarController.shared.setupControlStripPresence()
|
TouchBarController.shared.setupControlStripPresence()
|
||||||
|
|
||||||
if let button = statusItem.button {
|
if let button = statusItem.button {
|
||||||
@ -23,7 +22,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func applicationWillTerminate(_ aNotification: Notification) {
|
func applicationWillTerminate(_ aNotification: Notification) {
|
||||||
// Insert code here to tear down your application
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func openPrefereces(_ sender: Any?) {
|
@objc func openPrefereces(_ sender: Any?) {
|
||||||
|
|||||||
@ -18,7 +18,9 @@ class BatteryBarItem: NSCustomTouchBarItem {
|
|||||||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
|
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()
|
// updateInfo()
|
||||||
|
|
||||||
|
BatteryMonitor(button: button)
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
@ -34,32 +36,67 @@ class BatteryBarItem: NSCustomTouchBarItem {
|
|||||||
let isCharging = info["isCharging"] as! Bool
|
let isCharging = info["isCharging"] as! Bool
|
||||||
let isCharged = info["isCharged"] as! Bool
|
let isCharged = info["isCharged"] as! Bool
|
||||||
|
|
||||||
if isCharging {
|
if isCharged {
|
||||||
title += "⚡️"
|
title += "⚡️"
|
||||||
}
|
}
|
||||||
|
|
||||||
title += String(percentage) + "%"
|
title += String(percentage) + "%" + timeRemainig
|
||||||
|
|
||||||
if !isCharged {
|
|
||||||
title += " (" + timeRemainig + ")"
|
|
||||||
}
|
|
||||||
|
|
||||||
button.title = title
|
button.title = title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BatteryInfo {
|
class BatteryInfo: NSObject {
|
||||||
var current: Int = 0
|
var current: Int = 0
|
||||||
var timeToEmpty: Int = 0
|
var timeToEmpty: Int = 0
|
||||||
var timeToFull: Int = 0
|
var timeToFull: Int = 0
|
||||||
var isCharged: Bool = false
|
var isCharged: Bool = false
|
||||||
var isCharging: Bool = false
|
var isCharging: Bool = false
|
||||||
|
|
||||||
func getFormattedTime(time: Int) -> String {
|
var button: NSButton?
|
||||||
let timeFormatted = NSString(format: "%d:%02d", time / 60, time % 60) as String
|
var loop:CFRunLoopSource?
|
||||||
print(timeFormatted)
|
|
||||||
|
|
||||||
|
override convenience init(button: NSButton) {
|
||||||
|
super.init()
|
||||||
|
|
||||||
|
self.button = button
|
||||||
|
self.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func start() {
|
||||||
|
let opaque = Unmanaged.passRetained(self).toOpaque()
|
||||||
|
let context = UnsafeMutableRawPointer(opaque)
|
||||||
|
loop = IOPSNotificationCreateRunLoopSource({ (context) in
|
||||||
|
guard let ctx = context else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
|
||||||
|
watcher.getInfo()
|
||||||
|
}, context).takeRetainedValue() as CFRunLoopSource
|
||||||
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func stop() {
|
||||||
|
if !(self.loop != nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.loop, CFRunLoopMode.defaultMode)
|
||||||
|
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
|
return timeFormatted
|
||||||
|
} else if (time == 0) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return "(?)"
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPSInfo() {
|
func getPSInfo() {
|
||||||
@ -101,8 +138,13 @@ class BatteryInfo {
|
|||||||
var timeRemaining = ""
|
var timeRemaining = ""
|
||||||
|
|
||||||
self.getPSInfo()
|
self.getPSInfo()
|
||||||
|
// print(self.current)
|
||||||
|
// print(self.timeToEmpty)
|
||||||
|
// print(self.timeToFull)
|
||||||
|
// print(self.isCharged)
|
||||||
|
// print(self.isCharging)
|
||||||
|
|
||||||
if isCharging {
|
if isCharged {
|
||||||
timeRemaining = getFormattedTime(time: self.timeToFull)
|
timeRemaining = getFormattedTime(time: self.timeToFull)
|
||||||
} else {
|
} else {
|
||||||
timeRemaining = getFormattedTime(time: self.timeToEmpty)
|
timeRemaining = getFormattedTime(time: self.timeToEmpty)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user