From 1cf35f62ba6a0c37655e15a23bb24e40e2518259 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Wed, 20 Jan 2021 18:38:19 +0100 Subject: [PATCH] fix: prevent a crash when opening the battery popup for the first time (#295). Fixed by removing overridden `start` function. --- ModuleKit/module.swift | 25 ++++---------- Modules/Battery/readers.swift | 64 ----------------------------------- 2 files changed, 7 insertions(+), 82 deletions(-) diff --git a/ModuleKit/module.swift b/ModuleKit/module.swift index 44032625..12384592 100644 --- a/ModuleKit/module.swift +++ b/ModuleKit/module.swift @@ -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) - } } diff --git a/Modules/Battery/readers.swift b/Modules/Battery/readers.swift index 4ecd7f08..0e8a3815 100644 --- a/Modules/Battery/readers.swift +++ b/Modules/Battery/readers.swift @@ -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