1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 09:28:38 +00:00

merge stash

This commit is contained in:
Toxblh 2018-04-20 22:57:51 +01:00
parent f78f82893a
commit d4108d848e
2 changed files with 56 additions and 15 deletions

View File

@ -13,7 +13,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
TouchBarController.shared.setupControlStripPresence()
if let button = statusItem.button {
@ -23,7 +22,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
@objc func openPrefereces(_ sender: Any?) {

View File

@ -18,7 +18,9 @@ class BatteryBarItem: NSCustomTouchBarItem {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
self.view = button
button.bezelColor = .clear
updateInfo()
// updateInfo()
BatteryMonitor(button: button)
}
required init?(coder: NSCoder) {
@ -34,32 +36,67 @@ class BatteryBarItem: NSCustomTouchBarItem {
let isCharging = info["isCharging"] as! Bool
let isCharged = info["isCharged"] as! Bool
if isCharging {
if isCharged {
title += "⚡️"
}
title += String(percentage) + "%"
if !isCharged {
title += " (" + timeRemainig + ")"
}
title += String(percentage) + "%" + timeRemainig
button.title = title
}
}
class BatteryInfo {
class BatteryInfo: NSObject {
var current: Int = 0
var timeToEmpty: Int = 0
var timeToFull: Int = 0
var isCharged: Bool = false
var isCharging: Bool = false
func getFormattedTime(time: Int) -> String {
let timeFormatted = NSString(format: "%d:%02d", time / 60, time % 60) as String
print(timeFormatted)
var button: NSButton?
var loop:CFRunLoopSource?
override convenience init(button: NSButton) {
super.init()
return timeFormatted
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
} else if (time == 0) {
return ""
}
return "(?)"
}
func getPSInfo() {
@ -101,8 +138,13 @@ class BatteryInfo {
var timeRemaining = ""
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)
} else {
timeRemaining = getFormattedTime(time: self.timeToEmpty)