Skip to content

Commit

Permalink
feat: redraw ProcessView to the NSStackView (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Aug 17, 2021
1 parent 54fbe17 commit bf5b4c4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 61 deletions.
79 changes: 33 additions & 46 deletions Kit/helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,70 +633,57 @@ public func sysctlByName(_ name: String) -> Int64 {
}

public class ProcessView: NSStackView {
public var width: CGFloat {
get { return 0 }
set {
self.setFrameSize(NSSize(width: newValue, height: self.frame.height))
}
}

public var icon: NSImage? {
get { return NSImage() }
set {
self.imageView?.image = newValue
}
}
public var label: String {
get { return "" }
set {
self.labelView?.stringValue = newValue
}
}
public var value: String {
get { return "" }
set {
self.valueView?.stringValue = newValue
}
get { return self.valueView.stringValue }
set { self.valueView.stringValue = newValue }
}

private var imageView: NSImageView? = nil
private var labelView: LabelField? = nil
private var valueView: ValueField? = nil
private var pid: Int? = nil

public init(_ n: CGFloat) {
super.init(frame: NSRect(x: 0, y: n*22, width: 264, height: 16))

let rowView: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 16))
private var imageView: NSImageView = NSImageView(frame: NSRect(x: 5, y: 5, width: 12, height: 12))
private var labelView: LabelField = LabelField(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
private var valueView: ValueField = ValueField(frame: NSRect(x: 0, y: 0, width: 70, height: 0))

public init() {
super.init(frame: NSRect(x: 0, y: 0, width: 264, height: 22))

let imageView: NSImageView = NSImageView(frame: NSRect(x: 2, y: 2, width: 12, height: 12))
let labelView: LabelField = LabelField(frame: NSRect(x: 18, y: 0, width: rowView.frame.width - 70 - 18, height: 16), "")
let valueView: ValueField = ValueField(frame: NSRect(x: 18 + labelView.frame.width, y: 0, width: 70, height: 16), "")
self.orientation = .horizontal
self.distribution = .fillProportionally
self.spacing = 0

rowView.addSubview(imageView)
rowView.addSubview(labelView)
rowView.addSubview(valueView)
let imageBox = NSView(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
imageBox.addSubview(self.imageView)

self.imageView = imageView
self.labelView = labelView
self.valueView = valueView
self.addArrangedSubview(imageBox)
self.addArrangedSubview(self.labelView)
self.addArrangedSubview(self.valueView)

self.addSubview(rowView)
NSLayoutConstraint.activate([
imageBox.widthAnchor.constraint(equalToConstant: self.bounds.height),
imageBox.heightAnchor.constraint(equalToConstant: self.bounds.height),
self.labelView.heightAnchor.constraint(equalToConstant: 16),
self.valueView.widthAnchor.constraint(equalToConstant: self.valueView.bounds.width),
self.widthAnchor.constraint(equalToConstant: self.bounds.width),
self.heightAnchor.constraint(equalToConstant: self.bounds.height)
])
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public func attachProcess(_ process: TopProcess) {
self.label = process.name != nil ? process.name! : process.command
self.icon = process.icon
public func set(_ process: TopProcess) {
self.labelView.stringValue = process.name != nil ? process.name! : process.command
self.imageView.image = process.icon
self.pid = process.pid
self.toolTip = "pid: \(process.pid)"
}

public func clear() {
self.label = ""
self.value = ""
self.icon = nil
self.labelView.stringValue = ""
self.valueView.stringValue = ""
self.imageView.image = nil
self.pid = nil
self.toolTip = ""
}
}
Expand Down
12 changes: 7 additions & 5 deletions Modules/Battery/popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,14 @@ internal class Popup: NSView, Popup_p {
private func initProcesses() -> NSView {
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.processesHeight))
let separator = separatorView(localizedString("Top processes"), origin: NSPoint(x: 0, y: self.processesHeight-Constants.Popup.separatorHeight), width: self.frame.width)
let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
let container: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
container.orientation = .vertical
container.spacing = 0

for i in 0..<self.numberOfProcesses {
let processView = ProcessView(CGFloat(i))
for _ in 0..<self.numberOfProcesses {
let processView = ProcessView()
self.processes.append(processView)
container.addSubview(processView)
container.addArrangedSubview(processView)
}

view.addSubview(separator)
Expand Down Expand Up @@ -308,7 +310,7 @@ internal class Popup: NSView, Popup_p {
for i in 0..<list.count {
let process = list[i]
let index = list.count-i-1
self.processes[index].attachProcess(process)
self.processes[index].set(process)
self.processes[index].value = "\(process.usage)%"
}

Expand Down
12 changes: 7 additions & 5 deletions Modules/CPU/popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,14 @@ internal class Popup: NSView, Popup_p {
private func initProcesses() -> NSView {
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.processesHeight))
let separator = separatorView(localizedString("Top processes"), origin: NSPoint(x: 0, y: self.processesHeight-Constants.Popup.separatorHeight), width: self.frame.width)
let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
let container: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
container.orientation = .vertical
container.spacing = 0

for i in 0..<self.numberOfProcesses {
let processView = ProcessView(CGFloat(i))
for _ in 0..<self.numberOfProcesses {
let processView = ProcessView()
self.processes.append(processView)
container.addSubview(processView)
container.addArrangedSubview(processView)
}

view.addSubview(separator)
Expand Down Expand Up @@ -262,7 +264,7 @@ internal class Popup: NSView, Popup_p {
for i in 0..<list.count {
let process = list[i]
let index = list.count-i-1
self.processes[index].attachProcess(process)
self.processes[index].set(process)
self.processes[index].value = "\(process.usage)%"
}

Expand Down
12 changes: 7 additions & 5 deletions Modules/RAM/popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ internal class Popup: NSView, Popup_p {
private func initProcesses() -> NSView {
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.processesHeight))
let separator = separatorView(localizedString("Top processes"), origin: NSPoint(x: 0, y: self.processesHeight-Constants.Popup.separatorHeight), width: self.frame.width)
let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
let container: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y))
container.orientation = .vertical
container.spacing = 0

for i in 0..<self.numberOfProcesses {
let processView = ProcessView(CGFloat(i))
for _ in 0..<self.numberOfProcesses {
let processView = ProcessView()
self.processes.append(processView)
container.addSubview(processView)
container.addArrangedSubview(processView)
}

view.addSubview(separator)
Expand Down Expand Up @@ -249,7 +251,7 @@ internal class Popup: NSView, Popup_p {
for i in 0..<list.count {
let process = list[i]
let index = list.count-i-1
self.processes[index].attachProcess(process)
self.processes[index].set(process)
self.processes[index].value = Units(bytes: Int64(process.usage)).getReadableMemory()
}

Expand Down

0 comments on commit bf5b4c4

Please sign in to comment.