feat: initialized ProcessesView that will be common for all popups

This commit is contained in:
Serhiy Mytrovtsiy
2024-01-05 21:02:29 +01:00
parent c1d006ada6
commit b21630218c
10 changed files with 330 additions and 177 deletions

View File

@@ -199,12 +199,12 @@ public class ProcessReader: Reader<[TopProcess]> {
return
}
var name: String? = nil
if let app = NSRunningApplication(processIdentifier: pid_t(pid) ) {
name = app.localizedName ?? nil
var name: String = command
if let app = NSRunningApplication(processIdentifier: pid_t(pid)), let n = app.localizedName {
name = n
}
processes.append(TopProcess(pid: pid, command: command, name: name, usage: usage))
processes.append(TopProcess(pid: pid, name: name, usage: usage))
}
}

View File

@@ -230,12 +230,12 @@ public class ProcessReader: Reader<[TopProcess]> {
let pid = Int(pidFind.cropped) ?? 0
let usage = Double(usageFind.cropped.replacingOccurrences(of: ",", with: ".")) ?? 0
var name: String? = nil
if let app = NSRunningApplication(processIdentifier: pid_t(pid) ) {
name = app.localizedName ?? nil
var name: String = command
if let app = NSRunningApplication(processIdentifier: pid_t(pid)), let n = app.localizedName {
name = n
}
processes.append(TopProcess(pid: pid, command: command, name: name, usage: usage))
processes.append(TopProcess(pid: pid, name: name, usage: usage))
}
if index == self.numberOfProcesses { stop = true }

View File

@@ -158,15 +158,15 @@ public class Disks: Codable {
}
}
public struct Disk_process: IOProcess_p, Codable {
private var base: DataSizeBase {
public struct Disk_process: Process_p, Codable {
public var base: DataSizeBase {
DataSizeBase(rawValue: Store.shared.string(key: "\(Disk.name)_base", defaultValue: "byte")) ?? .byte
}
public var pid: Int32
public var pid: Int
public var name: String
public var icon: NSImage {
if let app = NSRunningApplication(processIdentifier: self.pid) {
if let app = NSRunningApplication(processIdentifier: pid_t(self.pid)) {
return app.icon ?? Constants.defaultProcessIcon
}
return Constants.defaultProcessIcon
@@ -182,13 +182,13 @@ public struct Disk_process: IOProcess_p, Codable {
Units(bytes: Int64(self.write)).getReadableSpeed(base: self.base)
}
init(pid: Int32, name: String, read: Int, write: Int) {
init(pid: Int, name: String, read: Int, write: Int) {
self.pid = pid
self.name = name
self.read = read
self.write = write
if let app = NSRunningApplication(processIdentifier: pid) {
if let app = NSRunningApplication(processIdentifier: pid_t(pid)) {
if let name = app.localizedName {
self.name = name
}

View File

@@ -407,7 +407,7 @@ public class ProcessReader: Reader<[Disk_process]> {
let read = bytesRead - v.read
let write = bytesWritten - v.write
if read != 0 || write != 0 {
processes.append(Disk_process(pid: pid, name: name, read: read, write: write))
processes.append(Disk_process(pid: Int(pid), name: name, read: read, write: write))
}
}

View File

@@ -99,20 +99,28 @@ public struct Network_Connectivity: Codable {
var latency: Double = 0
}
public struct Network_Process: Codable {
var time: Date = Date()
var name: String = ""
var pid: String = ""
var download: Int = 0
var upload: Int = 0
var icon: NSImage {
public struct Network_Process: Codable, Process_p {
public var pid: Int
public var name: String
public var time: Date
public var download: Int
public var upload: Int
public var icon: NSImage {
get {
if let pid = pid_t(self.pid), let app = NSRunningApplication(processIdentifier: pid) {
return app.icon ?? Constants.defaultProcessIcon
if let app = NSRunningApplication(processIdentifier: pid_t(self.pid)), let icon = app.icon {
return icon
}
return Constants.defaultProcessIcon
}
}
public init(pid: Int = 0, name: String = "", time: Date = Date(), download: Int = 0, upload: Int = 0) {
self.pid = pid
self.name = name
self.time = time
self.download = download
self.upload = upload
}
}
public class Network: Module {

View File

@@ -500,16 +500,16 @@ public class ProcessReader: Reader<[Network_Process]> {
let nameArray = parsedLine[0].split(separator: ".")
if let pid = nameArray.last {
process.pid = String(pid)
process.pid = Int(pid) ?? 0
}
if let app = NSRunningApplication(processIdentifier: pid_t(process.pid) ?? 0) {
if let app = NSRunningApplication(processIdentifier: pid_t(process.pid) ) {
process.name = app.localizedName ?? nameArray.dropLast().joined(separator: ".")
} else {
process.name = nameArray.dropLast().joined(separator: ".")
}
if process.name == "" {
process.name = process.pid
process.name = "\(process.pid)"
}
if let download = Int(parsedLine[1]) {
@@ -543,7 +543,7 @@ public class ProcessReader: Reader<[Network_Process]> {
upload = 0
}
processes.append(Network_Process(time: time, name: p.name, pid: p.pid, download: download, upload: upload))
processes.append(Network_Process(pid: p.pid, name: p.name, time: time, download: download, upload: upload))
}
}
self.previous = list

View File

@@ -189,10 +189,10 @@ public class ProcessReader: Reader<[TopProcess]> {
}
var name: String = command
if let app = NSRunningApplication(processIdentifier: pid_t(pid) ) {
name = app.localizedName ?? command
if let app = NSRunningApplication(processIdentifier: pid_t(pid)), let n = app.localizedName {
name = n
}
return TopProcess(pid: pid, command: command, name: name, usage: usage * Double(1024 * 1024))
return TopProcess(pid: pid, name: name, usage: usage * Double(1024 * 1024))
}
}