mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
- add top view initialization to the network popup
- add an option to select number of top processes to the Battery module
This commit is contained in:
@@ -42,7 +42,7 @@ struct Battery_Usage: value_t {
|
||||
public class Battery: Module {
|
||||
private var usageReader: UsageReader? = nil
|
||||
private var processReader: ProcessReader? = nil
|
||||
private let popupView: Popup = Popup()
|
||||
private let popupView: Popup
|
||||
private var settingsView: Settings
|
||||
|
||||
private let store: UnsafePointer<Store>
|
||||
@@ -52,6 +52,7 @@ public class Battery: Module {
|
||||
public init(_ store: UnsafePointer<Store>) {
|
||||
self.store = store
|
||||
self.settingsView = Settings("Battery", store: store)
|
||||
self.popupView = Popup("Battery", store: store)
|
||||
|
||||
super.init(
|
||||
store: store,
|
||||
@@ -61,7 +62,12 @@ public class Battery: Module {
|
||||
guard self.available else { return }
|
||||
|
||||
self.usageReader = UsageReader()
|
||||
self.processReader = ProcessReader()
|
||||
self.processReader = ProcessReader(self.config.name, store: store)
|
||||
|
||||
self.settingsView.callbackWhenUpdateNumberOfProcesses = {
|
||||
self.popupView.numberOfProcessesUpdated()
|
||||
self.processReader?.read()
|
||||
}
|
||||
|
||||
self.usageReader?.readyCallback = { [unowned self] in
|
||||
self.readyHandler()
|
||||
|
||||
@@ -14,11 +14,16 @@ import ModuleKit
|
||||
import StatsKit
|
||||
|
||||
internal class Popup: NSView {
|
||||
private var store: UnsafePointer<Store>
|
||||
private var title: String
|
||||
|
||||
private var grid: NSGridView? = nil
|
||||
|
||||
private let dashboardHeight: CGFloat = 90
|
||||
private let detailsHeight: CGFloat = 88
|
||||
private let batteryHeight: CGFloat = 66
|
||||
private let adapterHeight: CGFloat = 44
|
||||
private let processesHeight: CGFloat = 22*5
|
||||
private let detailsHeight: CGFloat = 88 + Constants.Popup.separatorHeight
|
||||
private let batteryHeight: CGFloat = 66 + Constants.Popup.separatorHeight
|
||||
private let adapterHeight: CGFloat = 44 + Constants.Popup.separatorHeight
|
||||
private let processHeight: CGFloat = 22
|
||||
|
||||
private var dashboardView: NSView? = nil
|
||||
private var dashboardBatteryView: BatteryView? = nil
|
||||
@@ -42,54 +47,152 @@ internal class Popup: NSView {
|
||||
private var processes: [ProcessView] = []
|
||||
private var processesInitialized: Bool = false
|
||||
|
||||
public init() {
|
||||
private var numberOfProcesses: Int {
|
||||
get {
|
||||
return self.store.pointee.int(key: "\(self.title)_processes", defaultValue: 8)
|
||||
}
|
||||
}
|
||||
private var processesHeight: CGFloat {
|
||||
get {
|
||||
return (self.processHeight*CGFloat(self.numberOfProcesses))+Constants.Popup.separatorHeight
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ title: String, store: UnsafePointer<Store>) {
|
||||
self.store = store
|
||||
self.title = title
|
||||
|
||||
super.init(frame: NSRect(
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: Constants.Popup.width,
|
||||
height: dashboardHeight + detailsHeight + batteryHeight + adapterHeight + (Constants.Popup.separatorHeight * 4) + processesHeight
|
||||
height: self.dashboardHeight + self.detailsHeight + self.batteryHeight + self.adapterHeight
|
||||
))
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: self.frame.height+self.processesHeight))
|
||||
|
||||
self.initDashboard()
|
||||
self.initDetails()
|
||||
self.initBattery()
|
||||
self.initAdapter()
|
||||
self.initProcesses()
|
||||
let gridView: NSGridView = NSGridView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
|
||||
gridView.rowSpacing = 0
|
||||
gridView.yPlacement = .fill
|
||||
|
||||
gridView.addRow(with: [self.initDashboard()])
|
||||
gridView.addRow(with: [self.initDetails()])
|
||||
gridView.addRow(with: [self.initBattery()])
|
||||
gridView.addRow(with: [self.initAdapter()])
|
||||
gridView.addRow(with: [self.initProcesses()])
|
||||
|
||||
gridView.row(at: 0).height = self.dashboardHeight
|
||||
gridView.row(at: 1).height = self.detailsHeight
|
||||
gridView.row(at: 2).height = self.batteryHeight
|
||||
gridView.row(at: 3).height = self.adapterHeight
|
||||
|
||||
self.addSubview(gridView)
|
||||
self.grid = gridView
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
private func initDashboard() {
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: self.frame.height - self.dashboardHeight, width: self.frame.width, height: self.dashboardHeight))
|
||||
public func numberOfProcessesUpdated() {
|
||||
if self.processes.count == self.numberOfProcesses {
|
||||
return
|
||||
}
|
||||
|
||||
let batteryView: BatteryView = BatteryView(frame: NSRect(x: Constants.Popup.margins, y: Constants.Popup.margins, width: view.frame.width - (Constants.Popup.margins*2), height: view.frame.height - (Constants.Popup.margins*2)))
|
||||
view.addSubview(batteryView)
|
||||
|
||||
self.addSubview(view)
|
||||
self.dashboardView = view
|
||||
self.dashboardBatteryView = batteryView
|
||||
DispatchQueue.main.async(execute: {
|
||||
self.processes = []
|
||||
|
||||
let h: CGFloat = self.dashboardHeight + self.detailsHeight + self.batteryHeight + self.adapterHeight + self.processesHeight
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: h))
|
||||
|
||||
NotificationCenter.default.post(name: .updatePopupSize, object: nil, userInfo: ["module": self.title])
|
||||
|
||||
self.grid?.setFrameSize(NSSize(width: self.frame.width, height: h))
|
||||
|
||||
self.grid?.row(at: 4).cell(at: 0).contentView?.removeFromSuperview()
|
||||
self.grid?.removeRow(at: 4)
|
||||
self.grid?.addRow(with: [self.initProcesses()])
|
||||
self.processesInitialized = false
|
||||
})
|
||||
}
|
||||
|
||||
private func initDetails() {
|
||||
let y: CGFloat = self.dashboardView!.frame.origin.y - Constants.Popup.separatorHeight
|
||||
let separator = SeparatorView(LocalizedString("Details"), origin: NSPoint(x: 0, y: y), width: self.frame.width)
|
||||
self.addSubview(separator)
|
||||
private func initDashboard() -> NSView {
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: self.frame.height - self.dashboardHeight, width: self.frame.width, height: self.dashboardHeight))
|
||||
let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: view.frame.width, height: self.dashboardHeight))
|
||||
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: separator.frame.origin.y - self.detailsHeight, width: self.frame.width, height: self.detailsHeight))
|
||||
self.dashboardBatteryView = BatteryView(frame: NSRect(x: Constants.Popup.margins, y: Constants.Popup.margins, width: view.frame.width - (Constants.Popup.margins*2), height: view.frame.height - (Constants.Popup.margins*2)))
|
||||
container.addSubview(self.dashboardBatteryView!)
|
||||
|
||||
self.levelField = PopupRow(view, n: 3, title: "\(LocalizedString("Level")):", value: "")
|
||||
self.sourceField = PopupRow(view, n: 2, title: "\(LocalizedString("Source")):", value: "")
|
||||
let t = self.labelValue(view, n: 1, title: "\(LocalizedString("Time")):", value: "")
|
||||
view.addSubview(container)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
private func initDetails() -> NSView {
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.detailsHeight))
|
||||
let separator = SeparatorView(LocalizedString("Details"), origin: NSPoint(x: 0, y: self.detailsHeight-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))
|
||||
|
||||
self.levelField = PopupRow(container, n: 3, title: "\(LocalizedString("Level")):", value: "")
|
||||
self.sourceField = PopupRow(container, n: 2, title: "\(LocalizedString("Source")):", value: "")
|
||||
let t = self.labelValue(container, n: 1, title: "\(LocalizedString("Time")):", value: "")
|
||||
self.timeLabelField = t.0
|
||||
self.timeField = t.1
|
||||
self.healthField = PopupRow(view, n: 0, title: "\(LocalizedString("Health")):", value: "")
|
||||
self.healthField = PopupRow(container, n: 0, title: "\(LocalizedString("Health")):", value: "")
|
||||
|
||||
self.addSubview(view)
|
||||
self.detailsView = view
|
||||
view.addSubview(separator)
|
||||
view.addSubview(container)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
|
||||
private func initBattery() -> NSView {
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.batteryHeight))
|
||||
let separator = SeparatorView(LocalizedString("Battery"), origin: NSPoint(x: 0, y: self.batteryHeight-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))
|
||||
|
||||
self.amperageField = PopupRow(container, n: 2, title: "\(LocalizedString("Amperage")):", value: "")
|
||||
self.voltageField = PopupRow(container, n: 1, title: "\(LocalizedString("Voltage")):", value: "")
|
||||
self.temperatureField = PopupRow(container, n: 0, title: "\(LocalizedString("Temperature")):", value: "")
|
||||
|
||||
view.addSubview(separator)
|
||||
view.addSubview(container)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
private func initAdapter() -> NSView {
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.adapterHeight))
|
||||
let separator = SeparatorView(LocalizedString("Power adapter"), origin: NSPoint(x: 0, y: self.adapterHeight-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))
|
||||
|
||||
self.powerField = PopupRow(container, n: 1, title: "\(LocalizedString("Power")):", value: "")
|
||||
self.chargingStateField = PopupRow(container, n: 0, title: "\(LocalizedString("Is charging")):", value: "")
|
||||
|
||||
self.adapterView = view
|
||||
|
||||
view.addSubview(separator)
|
||||
view.addSubview(container)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
for i in 0...self.numberOfProcesses {
|
||||
let processView = ProcessView(CGFloat(i))
|
||||
self.processes.append(processView)
|
||||
container.addSubview(processView)
|
||||
}
|
||||
|
||||
view.addSubview(separator)
|
||||
view.addSubview(container)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
private func labelValue(_ view: NSView, n: CGFloat, title: String, value: String) -> (NSTextField, NSTextField) {
|
||||
let rowView: NSView = NSView(frame: NSRect(x: 0, y: 22*n, width: view.frame.width, height: 22))
|
||||
|
||||
@@ -103,52 +206,6 @@ internal class Popup: NSView {
|
||||
return (labelView, valueView)
|
||||
}
|
||||
|
||||
private func initBattery() {
|
||||
let y: CGFloat = self.detailsView!.frame.origin.y - Constants.Popup.separatorHeight
|
||||
let separator = SeparatorView(LocalizedString("Battery"), origin: NSPoint(x: 0, y: y), width: self.frame.width)
|
||||
self.addSubview(separator)
|
||||
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: separator.frame.origin.y - self.batteryHeight, width: self.frame.width, height: self.batteryHeight))
|
||||
|
||||
self.amperageField = PopupRow(view, n: 2, title: "\(LocalizedString("Amperage")):", value: "")
|
||||
self.voltageField = PopupRow(view, n: 1, title: "\(LocalizedString("Voltage")):", value: "")
|
||||
self.temperatureField = PopupRow(view, n: 0, title: "\(LocalizedString("Temperature")):", value: "")
|
||||
|
||||
self.addSubview(view)
|
||||
self.batteryView = view
|
||||
}
|
||||
|
||||
private func initAdapter() {
|
||||
let y: CGFloat = self.batteryView!.frame.origin.y - Constants.Popup.separatorHeight
|
||||
let separator = SeparatorView(LocalizedString("Power adapter"), origin: NSPoint(x: 0, y: y), width: self.frame.width)
|
||||
self.addSubview(separator)
|
||||
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: separator.frame.origin.y - self.adapterHeight, width: self.frame.width, height: self.adapterHeight))
|
||||
|
||||
self.powerField = PopupRow(view, n: 1, title: "\(LocalizedString("Power")):", value: "")
|
||||
self.chargingStateField = PopupRow(view, n: 0, title: "\(LocalizedString("Is charging")):", value: "")
|
||||
|
||||
self.addSubview(view)
|
||||
self.adapterView = view
|
||||
}
|
||||
|
||||
private func initProcesses() {
|
||||
let separator = SeparatorView(LocalizedString("Top processes"), origin: NSPoint(x: 0, y: self.processesHeight), width: self.frame.width)
|
||||
self.addSubview(separator)
|
||||
|
||||
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.processesHeight))
|
||||
|
||||
self.processes.append(ProcessView(0))
|
||||
self.processes.append(ProcessView(1))
|
||||
self.processes.append(ProcessView(2))
|
||||
self.processes.append(ProcessView(3))
|
||||
self.processes.append(ProcessView(4))
|
||||
|
||||
self.processes.forEach{ view.addSubview($0) }
|
||||
|
||||
self.addSubview(view)
|
||||
}
|
||||
|
||||
public func usageCallback(_ value: Battery_Usage) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
self.dashboardBatteryView?.setValue(abs(value.level))
|
||||
|
||||
@@ -143,6 +143,21 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
private var task: Process? = nil
|
||||
private var initialized: Bool = false
|
||||
|
||||
private let store: UnsafePointer<Store>
|
||||
private let title: String
|
||||
|
||||
private var numberOfProcesses: Int {
|
||||
get {
|
||||
return self.store.pointee.int(key: "\(self.title)_processes", defaultValue: 8)
|
||||
}
|
||||
}
|
||||
|
||||
init(_ title: String, store: UnsafePointer<Store>) {
|
||||
self.title = title
|
||||
self.store = store
|
||||
super.init()
|
||||
}
|
||||
|
||||
public override func setup() {
|
||||
self.popup = true
|
||||
}
|
||||
@@ -162,7 +177,7 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
|
||||
self.task?.standardOutput = pipe
|
||||
self.task?.launchPath = "/usr/bin/top"
|
||||
self.task?.arguments = ["-o", "power", "-n", "5", "-stats", "pid,command,power"]
|
||||
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)
|
||||
@@ -214,7 +229,7 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
public override func read() {
|
||||
let task = Process()
|
||||
task.launchPath = "/usr/bin/top"
|
||||
task.arguments = ["-l", "1", "-o", "power", "-n", "5", "-stats", "pid,command,power"]
|
||||
task.arguments = ["-l", "1", "-o", "power", "-n", "\(self.numberOfProcesses)", "-stats", "pid,command,power"]
|
||||
|
||||
let outputPipe = Pipe()
|
||||
let errorPipe = Pipe()
|
||||
|
||||
@@ -16,11 +16,13 @@ import SystemConfiguration
|
||||
|
||||
internal class Settings: NSView, Settings_v {
|
||||
public var callback: (() -> Void) = {}
|
||||
public var callbackWhenUpdateNumberOfProcesses: (() -> Void) = {}
|
||||
|
||||
private let title: String
|
||||
private let store: UnsafePointer<Store>
|
||||
private var button: NSPopUpButton?
|
||||
|
||||
private var numberOfProcesses: Int = 8
|
||||
private let levelsList: [String] = ["Disabled", "0.03", "0.05", "0.1", "0.15", "0.2", "0.25", "0.3", "0.4", "0.5"]
|
||||
private var lowLevelNotification: String {
|
||||
get {
|
||||
@@ -31,6 +33,7 @@ internal class Settings: NSView, Settings_v {
|
||||
public init(_ title: String, store: UnsafePointer<Store>) {
|
||||
self.title = title
|
||||
self.store = store
|
||||
self.numberOfProcesses = store.pointee.int(key: "\(self.title)_processes", defaultValue: self.numberOfProcesses)
|
||||
|
||||
super.init(frame: CGRect(
|
||||
x: 0,
|
||||
@@ -39,7 +42,6 @@ internal class Settings: NSView, Settings_v {
|
||||
height: 0
|
||||
))
|
||||
|
||||
self.wantsLayer = true
|
||||
self.canDrawConcurrently = true
|
||||
}
|
||||
|
||||
@@ -62,7 +64,7 @@ internal class Settings: NSView, Settings_v {
|
||||
self.addSubview(SelectTitleRow(
|
||||
frame: NSRect(
|
||||
x:Constants.Settings.margin,
|
||||
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 0,
|
||||
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 1,
|
||||
width: self.frame.width - (Constants.Settings.margin*2),
|
||||
height: rowHeight
|
||||
),
|
||||
@@ -72,6 +74,19 @@ internal class Settings: NSView, Settings_v {
|
||||
selected: self.lowLevelNotification == "Disabled" ? self.lowLevelNotification : "\(Int((Double(self.lowLevelNotification) ?? 0)*100))%"
|
||||
))
|
||||
|
||||
self.addSubview(SelectTitleRow(
|
||||
frame: NSRect(
|
||||
x:Constants.Settings.margin,
|
||||
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 0,
|
||||
width: self.frame.width - (Constants.Settings.margin*2),
|
||||
height: rowHeight
|
||||
),
|
||||
title: LocalizedString("Number of top processes"),
|
||||
action: #selector(changeNumberOfProcesses),
|
||||
items: NumbersOfProcesses.map{ "\($0)" },
|
||||
selected: "\(self.numberOfProcesses)"
|
||||
))
|
||||
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: 30 + (Constants.Settings.margin*2)))
|
||||
}
|
||||
|
||||
@@ -82,4 +97,12 @@ internal class Settings: NSView, Settings_v {
|
||||
store.pointee.set(key: "\(self.title)_lowLevelNotification", value: "\(value/100)")
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeNumberOfProcesses(_ sender: NSMenuItem) {
|
||||
if let value = Int(sender.title) {
|
||||
self.numberOfProcesses = value
|
||||
self.store.pointee.set(key: "\(self.title)_processes", value: value)
|
||||
self.callbackWhenUpdateNumberOfProcesses()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ public class CPU: Module {
|
||||
}
|
||||
self.settingsView.callbackWhenUpdateNumberOfProcesses = {
|
||||
self.popupView.numberOfProcessesUpdated()
|
||||
self.processReader?.read()
|
||||
}
|
||||
self.settingsView.setInterval = { [unowned self] value in
|
||||
self.loadReader?.setInterval(value)
|
||||
|
||||
@@ -60,6 +60,7 @@ public class Memory: Module {
|
||||
|
||||
self.settingsView.callbackWhenUpdateNumberOfProcesses = {
|
||||
self.popupView.numberOfProcessesUpdated()
|
||||
self.processReader?.read()
|
||||
}
|
||||
|
||||
self.usageReader?.readyCallback = { [unowned self] in
|
||||
|
||||
@@ -91,6 +91,7 @@ public class Network: Module {
|
||||
|
||||
self.settingsView.callbackWhenUpdateNumberOfProcesses = {
|
||||
self.popupView.numberOfProcessesUpdated()
|
||||
self.processReader?.read()
|
||||
}
|
||||
|
||||
self.usageReader?.readyCallback = { [unowned self] in
|
||||
|
||||
@@ -334,7 +334,7 @@ internal class Popup: NSView {
|
||||
|
||||
public func processCallback(_ list: [Network_Process]) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if (self.window?.isVisible ?? false) {
|
||||
if (self.window?.isVisible ?? false) || !self.processesInitialized {
|
||||
for i in 0..<list.count {
|
||||
let process = list[i]
|
||||
let index = list.count-i-1
|
||||
@@ -345,6 +345,8 @@ internal class Popup: NSView {
|
||||
self.processes[index].icon = process.icon
|
||||
}
|
||||
}
|
||||
|
||||
self.processesInitialized = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user