mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
fix: prevent a crash when opening the battery popup for the first time (#295). Fixed by removing overridden start function.
This commit is contained in:
@@ -122,7 +122,6 @@ open class Module: Module_p {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(listenForWidgetSwitch), name: .switchWidget, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(listenForMouseDownInSettings), name: .clickInSettings, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(listenForModuleToggle), name: .toggleModule, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(listenResignActive), name: NSApplication.willResignActiveNotification, object: nil)
|
||||
|
||||
if self.config.widgetsConfig.count != 0 {
|
||||
self.initWidget()
|
||||
@@ -302,15 +301,13 @@ open class Module: Module_p {
|
||||
|
||||
// call when popup appear/disappear
|
||||
private func visibilityCallback(_ state: Bool) {
|
||||
DispatchQueue.global(qos: .background).async {
|
||||
self.readers.filter{ $0.popup }.forEach { (reader: Reader_p) in
|
||||
if state {
|
||||
reader.unlock()
|
||||
reader.start()
|
||||
} else {
|
||||
reader.stop()
|
||||
reader.lock()
|
||||
}
|
||||
self.readers.filter{ $0.popup }.forEach { (reader: Reader_p) in
|
||||
if state {
|
||||
reader.unlock()
|
||||
reader.start()
|
||||
} else {
|
||||
reader.pause()
|
||||
reader.lock()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -383,12 +380,4 @@ open class Module: Module_p {
|
||||
self.popup?.setIsVisible(false)
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func listenResignActive(_ notification: Notification) {
|
||||
if let popup = self.popup, popup.locked {
|
||||
return
|
||||
}
|
||||
|
||||
self.visibilityCallback(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,70 +164,6 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
self.popup = true
|
||||
}
|
||||
|
||||
public override func start() {
|
||||
if !self.initialized {
|
||||
DispatchQueue.global().async {
|
||||
self.read()
|
||||
}
|
||||
self.initialized = true
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.global().async {
|
||||
self.task = Process()
|
||||
let pipe = Pipe()
|
||||
|
||||
self.task?.standardOutput = pipe
|
||||
self.task?.launchPath = "/usr/bin/top"
|
||||
self.task?.arguments = ["-o", "power", "-n", "\(self.numberOfProcesses)", "-stats", "pid,command,power"]
|
||||
|
||||
pipe.fileHandleForReading.readabilityHandler = { (fileHandle) -> Void in
|
||||
let output = String(decoding: fileHandle.availableData, as: UTF8.self)
|
||||
var processes: [TopProcess] = []
|
||||
|
||||
output.enumerateLines { (line, _) -> () in
|
||||
if line.matches("^\\d* +.+ \\d*.?\\d*$") {
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
let pidString = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageString = str.findAndCrop(pattern: " +[0-9]+.*[0-9]*$")
|
||||
let command = str.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
let pid = Int(pidString) ?? 0
|
||||
guard let usage = Double(usageString.filter("01234567890.".contains)) else {
|
||||
return
|
||||
}
|
||||
|
||||
var name: String? = nil
|
||||
var icon: NSImage? = nil
|
||||
if let app = NSRunningApplication(processIdentifier: pid_t(pid) ) {
|
||||
name = app.localizedName ?? nil
|
||||
icon = app.icon
|
||||
}
|
||||
|
||||
processes.append(TopProcess(pid: pid, command: command, name: name, usage: usage, icon: icon))
|
||||
}
|
||||
}
|
||||
|
||||
if processes.count != 0 {
|
||||
self.callback(processes)
|
||||
}
|
||||
}
|
||||
|
||||
self.task?.launch()
|
||||
self.task?.waitUntilExit()
|
||||
}
|
||||
}
|
||||
|
||||
public override func stop() {
|
||||
if self.task == nil || !self.task!.isRunning {
|
||||
return
|
||||
}
|
||||
|
||||
self.task?.interrupt()
|
||||
self.task = nil
|
||||
}
|
||||
|
||||
public override func read() {
|
||||
if self.numberOfProcesses == 0 {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user