mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
CPU top processes MVP
This commit is contained in:
@@ -70,6 +70,7 @@ open class Module: Module_p {
|
||||
|
||||
private var settingsView: Settings_v? = nil
|
||||
private var popup: NSWindow = NSWindow()
|
||||
private var popupView: NSView? = nil
|
||||
|
||||
private let log: OSLog
|
||||
private var store: UnsafePointer<Store>
|
||||
@@ -91,6 +92,7 @@ open class Module: Module_p {
|
||||
self.log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: self.config.name)
|
||||
self.store = store
|
||||
self.settingsView = settings
|
||||
self.popupView = popup
|
||||
self.available = self.isAvailable()
|
||||
self.enabled = self.store.pointee.bool(key: "\(self.config.name)_state", defaultValue: self.config.defaultState)
|
||||
self.menuBarItem.isVisible = self.enabled
|
||||
@@ -117,7 +119,11 @@ open class Module: Module_p {
|
||||
self?.toggleEnabled()
|
||||
}
|
||||
|
||||
self.popup = PopupWindow(title: self.config.name, view: popup)
|
||||
self.popup = PopupWindow(title: self.config.name, view: self.popupView, visibilityCallback: self.visibilityCallback)
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
// load function which call when app start
|
||||
@@ -212,6 +218,13 @@ open class Module: Module_p {
|
||||
self.menuBarItem.length = width
|
||||
}
|
||||
|
||||
// replace a popup view
|
||||
public func replacePopup(_ view: NSView) {
|
||||
self.popup.setIsVisible(false)
|
||||
self.popupView = view
|
||||
self.popup = PopupWindow(title: self.config.name, view: self.popupView, visibilityCallback: self.visibilityCallback)
|
||||
}
|
||||
|
||||
// determine if module is available (can be overrided in module)
|
||||
open func isAvailable() -> Bool { return true }
|
||||
|
||||
@@ -262,6 +275,19 @@ open class Module: Module_p {
|
||||
self.settings?.setActiveWidget(self.widget)
|
||||
}
|
||||
|
||||
// call when popup appear/disappear
|
||||
private func visibilityCallback(_ state: Bool) {
|
||||
self.readers.filter{ $0.popup }.forEach { (reader: Reader_p) in
|
||||
if state {
|
||||
reader.unlock()
|
||||
reader.start()
|
||||
} else {
|
||||
reader.stop()
|
||||
reader.lock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func togglePopup(_ sender: Any) {
|
||||
let openedWindows = NSApplication.shared.windows.filter{ $0 is NSPanel }
|
||||
openedWindows.forEach{ $0.setIsVisible(false) }
|
||||
|
||||
@@ -15,8 +15,9 @@ import StatsKit
|
||||
internal class PopupWindow: NSPanel, NSWindowDelegate {
|
||||
private let viewController: PopupViewController = PopupViewController()
|
||||
|
||||
init(title: String, view: NSView?) {
|
||||
init(title: String, view: NSView?, visibilityCallback: @escaping (_ state: Bool) -> Void) {
|
||||
self.viewController.setup(title: title, view: view)
|
||||
self.viewController.visibilityCallback = visibilityCallback
|
||||
|
||||
super.init(
|
||||
contentRect: NSMakeRect(0, 0, self.viewController.view.frame.width, self.viewController.view.frame.height),
|
||||
@@ -40,6 +41,7 @@ internal class PopupWindow: NSPanel, NSWindowDelegate {
|
||||
}
|
||||
|
||||
internal class PopupViewController: NSViewController {
|
||||
public var visibilityCallback: (_ state: Bool) -> Void = {_ in }
|
||||
private var popup: PopupView
|
||||
|
||||
public init() {
|
||||
@@ -61,10 +63,12 @@ internal class PopupViewController: NSViewController {
|
||||
|
||||
override func viewWillAppear() {
|
||||
self.popup.appear()
|
||||
self.visibilityCallback(true)
|
||||
}
|
||||
|
||||
override func viewWillDisappear() {
|
||||
self.popup.disappear()
|
||||
self.visibilityCallback(false)
|
||||
}
|
||||
|
||||
public func setup(title: String, view: NSView?) {
|
||||
|
||||
@@ -20,6 +20,7 @@ public protocol value_t {
|
||||
|
||||
public protocol Reader_p {
|
||||
var optional: Bool { get }
|
||||
var popup: Bool { get }
|
||||
|
||||
func setup() -> Void
|
||||
func read() -> Void
|
||||
@@ -32,6 +33,9 @@ public protocol Reader_p {
|
||||
func pause() -> Void
|
||||
func stop() -> Void
|
||||
|
||||
func lock() -> Void
|
||||
func unlock() -> Void
|
||||
|
||||
func initStoreValues(title: String, store: UnsafePointer<Store>) -> Void
|
||||
func setInterval(_ value: Double) -> Void
|
||||
}
|
||||
@@ -47,7 +51,10 @@ open class Reader<T>: ReaderInternal_p {
|
||||
public let log: OSLog
|
||||
public var value: T?
|
||||
public var interval: Double? = nil
|
||||
public var defaultInterval: Double = 1
|
||||
public var optional: Bool = false
|
||||
public var popup: Bool = false
|
||||
open var enabled: Bool = true
|
||||
|
||||
public var readyCallback: () -> Void = {}
|
||||
public var callbackHandler: (T?) -> Void = {_ in }
|
||||
@@ -55,6 +62,7 @@ open class Reader<T>: ReaderInternal_p {
|
||||
private var repeatTask: Repeater?
|
||||
private var nilCallbackCounter: Int = 0
|
||||
private var ready: Bool = false
|
||||
private var locked: Bool = true
|
||||
|
||||
private var history: [T]? = []
|
||||
|
||||
@@ -71,7 +79,7 @@ open class Reader<T>: ReaderInternal_p {
|
||||
return
|
||||
}
|
||||
|
||||
let updateIntervalString = store.pointee.string(key: "\(title)_updateInterval", defaultValue: "1")
|
||||
let updateIntervalString = store.pointee.string(key: "\(title)_updateInterval", defaultValue: "\(self.defaultInterval)")
|
||||
if let updateInterval = Double(updateIntervalString) {
|
||||
self.interval = updateInterval
|
||||
}
|
||||
@@ -114,6 +122,10 @@ open class Reader<T>: ReaderInternal_p {
|
||||
open func terminate() {}
|
||||
|
||||
open func start() {
|
||||
if !self.enabled || (self.popup && self.locked) {
|
||||
return
|
||||
}
|
||||
|
||||
if let interval = self.interval, self.repeatTask == nil {
|
||||
os_log(.debug, log: self.log, "Set up update interval: %.0f sec", interval)
|
||||
|
||||
@@ -132,6 +144,7 @@ open class Reader<T>: ReaderInternal_p {
|
||||
|
||||
open func stop() {
|
||||
self.repeatTask?.removeAllObservers(thenStop: true)
|
||||
self.repeatTask = nil
|
||||
}
|
||||
|
||||
public func setInterval(_ value: Double) {
|
||||
@@ -148,4 +161,12 @@ extension Reader: Reader_p {
|
||||
public func getHistory<T>() -> [T] {
|
||||
return self.history as! [T]
|
||||
}
|
||||
|
||||
public func lock() {
|
||||
self.locked = true
|
||||
}
|
||||
|
||||
public func unlock() {
|
||||
self.locked = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,17 +25,12 @@ public struct CPU_Load: value_t {
|
||||
}
|
||||
}
|
||||
|
||||
public struct TopProcess {
|
||||
var pid: Int = 0
|
||||
var command: String = ""
|
||||
var usage: Double = 0
|
||||
}
|
||||
|
||||
public class CPU: Module {
|
||||
private let popupView: Popup = Popup()
|
||||
private var popupView: Popup
|
||||
private var settingsView: Settings
|
||||
|
||||
private var loadReader: LoadReader? = nil
|
||||
private var processReader: ProcessReader? = nil
|
||||
private let smc: UnsafePointer<SMCService>?
|
||||
private let store: UnsafePointer<Store>
|
||||
|
||||
@@ -49,6 +44,7 @@ public class CPU: Module {
|
||||
self.store = store
|
||||
self.smc = smc
|
||||
self.settingsView = Settings("CPU", store: store)
|
||||
self.popupView = Popup("CPU", store: store)
|
||||
|
||||
super.init(
|
||||
store: store,
|
||||
@@ -60,12 +56,19 @@ public class CPU: Module {
|
||||
self.loadReader = LoadReader()
|
||||
self.loadReader?.store = store
|
||||
|
||||
self.processReader = ProcessReader()
|
||||
self.processReader?.store = store
|
||||
|
||||
self.settingsView.callback = { [unowned self] in
|
||||
self.loadReader?.read()
|
||||
}
|
||||
self.settingsView.setInterval = { [unowned self] value in
|
||||
self.loadReader?.setInterval(value)
|
||||
}
|
||||
self.settingsView.topProcessesCallback = {
|
||||
self.popupView = Popup(self.config.name, store: self.store)
|
||||
self.replacePopup(self.popupView)
|
||||
}
|
||||
|
||||
self.loadReader?.readyCallback = { [unowned self] in
|
||||
self.readyHandler()
|
||||
@@ -74,13 +77,22 @@ public class CPU: Module {
|
||||
self.loadCallback(value)
|
||||
}
|
||||
|
||||
self.processReader?.callbackHandler = { [unowned self] value in
|
||||
if let list = value {
|
||||
self.popupView.processCallback(list)
|
||||
}
|
||||
}
|
||||
|
||||
if let reader = self.loadReader {
|
||||
self.addReader(reader)
|
||||
}
|
||||
if let reader = self.processReader {
|
||||
self.addReader(reader)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadCallback(_ value: CPU_Load?) {
|
||||
if value == nil {
|
||||
guard value != nil else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,12 @@ import ModuleKit
|
||||
import StatsKit
|
||||
|
||||
internal class Popup: NSView {
|
||||
private var store: UnsafePointer<Store>
|
||||
private var title: String
|
||||
|
||||
private let dashboardHeight: CGFloat = 90
|
||||
private let detailsHeight: CGFloat = 66 // -26
|
||||
private let processesHeight: CGFloat = 22*5
|
||||
|
||||
private var loadField: NSTextField? = nil
|
||||
private var temperatureField: NSTextField? = nil
|
||||
@@ -24,14 +28,32 @@ internal class Popup: NSView {
|
||||
private var userField: NSTextField? = nil
|
||||
private var idleField: NSTextField? = nil
|
||||
|
||||
public var chart: LineChartView? = nil
|
||||
private var chart: LineChartView? = nil
|
||||
private var ready: Bool = false
|
||||
|
||||
public init() {
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: dashboardHeight + Constants.Popup.separatorHeight + detailsHeight))
|
||||
private var processes: [ProcessView] = []
|
||||
private var processesView: NSView? = nil
|
||||
|
||||
private var topProcessState: Bool {
|
||||
get {
|
||||
return self.store.pointee.bool(key: "\(self.title)_topProcesses", defaultValue: false)
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ title: String, store: UnsafePointer<Store>) {
|
||||
self.store = store
|
||||
self.title = title
|
||||
|
||||
let topProcessState = store.pointee.bool(key: "\(title)_topProcesses", defaultValue: false)
|
||||
let height = topProcessState ? dashboardHeight + (Constants.Popup.separatorHeight*2) + detailsHeight + processesHeight : dashboardHeight + Constants.Popup.separatorHeight + detailsHeight
|
||||
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: height))
|
||||
|
||||
initDashboard()
|
||||
initDetails()
|
||||
if topProcessState {
|
||||
self.initProcesses()
|
||||
}
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
@@ -74,6 +96,24 @@ internal class Popup: NSView {
|
||||
self.addSubview(view)
|
||||
}
|
||||
|
||||
private func initProcesses() {
|
||||
let separator = SeparatorView("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))
|
||||
|
||||
processes.append(ProcessView(0))
|
||||
processes.append(ProcessView(1))
|
||||
processes.append(ProcessView(2))
|
||||
processes.append(ProcessView(3))
|
||||
processes.append(ProcessView(4))
|
||||
|
||||
processes.forEach{ view.addSubview($0) }
|
||||
|
||||
self.addSubview(view)
|
||||
self.processesView = view
|
||||
}
|
||||
|
||||
private func addFirstRow(mView: NSView, y: CGFloat, title: String, value: String) -> NSTextField {
|
||||
let rowView: NSView = NSView(frame: NSRect(x: 0, y: y, width: mView.frame.width, height: 16))
|
||||
|
||||
@@ -120,6 +160,19 @@ internal class Popup: NSView {
|
||||
self.chart?.addValue(value.totalUsage)
|
||||
})
|
||||
}
|
||||
|
||||
public func processCallback(_ list: [TopProcess]) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
for i in 0..<list.count {
|
||||
let process = list[i]
|
||||
let index = list.count-i-1
|
||||
if self.processes.indices.contains(index) {
|
||||
self.processes[index].label = process.command
|
||||
self.processes[index].value = "\(process.usage)%"
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private class ProcessView: NSView {
|
||||
@@ -149,10 +202,10 @@ private class ProcessView: NSView {
|
||||
init(_ n: CGFloat) {
|
||||
super.init(frame: NSRect(x: 0, y: n*22, width: Constants.Popup.width, height: 16))
|
||||
|
||||
let rowView: NSView = NSView(frame: NSRect(x: Constants.Popup.margins, y: 0, width: self.frame.width - (Constants.Popup.margins*2), height: 16))
|
||||
let rowView: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 16))
|
||||
|
||||
let labelView: LabelField = LabelField(frame: NSRect(x: 0, y: 0.5, width: 50, height: 15), "")
|
||||
let valueView: ValueField = ValueField(frame: NSRect(x: 50, y: 0, width: rowView.frame.width - 50, height: 16), "")
|
||||
let labelView: LabelField = LabelField(frame: NSRect(x: 0, y: 0.5, width: rowView.frame.width - 50, height: 15), "")
|
||||
let valueView: ValueField = ValueField(frame: NSRect(x: rowView.frame.width - 50, y: 0, width: 50, height: 16), "")
|
||||
|
||||
rowView.addSubview(labelView)
|
||||
rowView.addSubview(valueView)
|
||||
|
||||
@@ -148,3 +148,69 @@ internal class LoadReader: Reader<CPU_Load> {
|
||||
return cpuLoadInfo
|
||||
}
|
||||
}
|
||||
|
||||
public class ProcessReader: Reader<[TopProcess]> {
|
||||
public var store: UnsafePointer<Store>? = nil
|
||||
|
||||
private var loadPrevious = host_cpu_load_info()
|
||||
|
||||
public override var enabled: Bool {
|
||||
set {}
|
||||
get {
|
||||
return self.store?.pointee.bool(key: "CPU_topProcesses", defaultValue: false) ?? false
|
||||
}
|
||||
}
|
||||
|
||||
public override func setup() {
|
||||
self.popup = true
|
||||
}
|
||||
|
||||
public override func read() {
|
||||
let task = Process()
|
||||
task.launchPath = "/bin/ps"
|
||||
task.arguments = ["-Aceo pid,pcpu,comm", "-r"]
|
||||
|
||||
let outputPipe = Pipe()
|
||||
let errorPipe = Pipe()
|
||||
|
||||
task.standardOutput = outputPipe
|
||||
task.standardError = errorPipe
|
||||
|
||||
do {
|
||||
try task.run()
|
||||
} catch let error {
|
||||
print(error)
|
||||
return
|
||||
}
|
||||
|
||||
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(decoding: outputData, as: UTF8.self)
|
||||
_ = String(decoding: errorData, as: UTF8.self)
|
||||
|
||||
if output.isEmpty {
|
||||
return
|
||||
}
|
||||
|
||||
var index = 0
|
||||
var processes: [TopProcess] = []
|
||||
output.enumerateLines { (line, stop) -> () in
|
||||
if index != 0 {
|
||||
var str = line.trimmingCharacters(in: .whitespaces)
|
||||
let pidString = str.findAndCrop(pattern: "^\\d+")
|
||||
let usageString = str.findAndCrop(pattern: "^[0-9,.]+ ")
|
||||
let command = str.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
let pid = Int(pidString) ?? 0
|
||||
let usage = Double(usageString.replacingOccurrences(of: ",", with: ".")) ?? 0
|
||||
|
||||
processes.append(TopProcess(pid: pid, command: command, usage: usage))
|
||||
}
|
||||
|
||||
if index == 5 { stop = true }
|
||||
index += 1
|
||||
}
|
||||
|
||||
self.callback(processes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,13 @@ internal class Settings: NSView, Settings_v {
|
||||
private var hyperthreadState: Bool = false
|
||||
private var updateIntervalValue: String = "1"
|
||||
private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"]
|
||||
private var topProcessesState: Bool = false
|
||||
|
||||
private let title: String
|
||||
private let store: UnsafePointer<Store>
|
||||
|
||||
public var callback: (() -> Void) = {}
|
||||
public var topProcessesCallback: (() -> Void) = {}
|
||||
public var setInterval: ((_ value: Double) -> Void) = {_ in }
|
||||
|
||||
private var hyperthreadView: NSView? = nil
|
||||
@@ -32,6 +34,7 @@ internal class Settings: NSView, Settings_v {
|
||||
self.store = store
|
||||
self.hyperthreadState = store.pointee.bool(key: "\(self.title)_hyperhreading", defaultValue: self.hyperthreadState)
|
||||
self.usagePerCoreState = store.pointee.bool(key: "\(self.title)_usagePerCore", defaultValue: self.usagePerCoreState)
|
||||
self.topProcessesState = store.pointee.bool(key: "\(self.title)_topProcesses", defaultValue: self.topProcessesState)
|
||||
self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
|
||||
if !self.usagePerCoreState {
|
||||
self.hyperthreadState = false
|
||||
@@ -56,7 +59,7 @@ internal class Settings: NSView, Settings_v {
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
|
||||
let rowHeight: CGFloat = 30
|
||||
let num: CGFloat = widget == .barChart ? 2 : 0
|
||||
let num: CGFloat = widget == .barChart ? 3 : 1
|
||||
|
||||
self.addSubview(SelectTitleRow(
|
||||
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * num, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
|
||||
@@ -68,14 +71,14 @@ internal class Settings: NSView, Settings_v {
|
||||
|
||||
if widget == .barChart {
|
||||
self.addSubview(ToggleTitleRow(
|
||||
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 1, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
|
||||
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 2, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
|
||||
title: "Show usage per core",
|
||||
action: #selector(toggleUsagePerCore),
|
||||
state: self.usagePerCoreState
|
||||
))
|
||||
|
||||
self.hyperthreadView = ToggleTitleRow(
|
||||
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),
|
||||
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 1, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
|
||||
title: "Show hyper-threading cores",
|
||||
action: #selector(toggleMultithreading),
|
||||
state: self.hyperthreadState
|
||||
@@ -87,6 +90,13 @@ internal class Settings: NSView, Settings_v {
|
||||
self.addSubview(self.hyperthreadView!)
|
||||
}
|
||||
|
||||
self.addSubview(ToggleTitleRow(
|
||||
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: "Top processes",
|
||||
action: #selector(toggleTopProcesses),
|
||||
state: self.topProcessesState
|
||||
))
|
||||
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*(num+1)) + (Constants.Settings.margin*(2+num))))
|
||||
}
|
||||
|
||||
@@ -132,4 +142,17 @@ internal class Settings: NSView, Settings_v {
|
||||
self.store.pointee.set(key: "\(self.title)_hyperhreading", value: self.hyperthreadState)
|
||||
self.callback()
|
||||
}
|
||||
|
||||
@objc func toggleTopProcesses(_ sender: NSControl) {
|
||||
var state: NSControl.StateValue? = nil
|
||||
if #available(OSX 10.15, *) {
|
||||
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
|
||||
} else {
|
||||
state = sender is NSButton ? (sender as! NSButton).state: nil
|
||||
}
|
||||
|
||||
self.topProcessesState = state! == .on ? true : false
|
||||
self.store.pointee.set(key: "\(self.title)_topProcesses", value: self.topProcessesState)
|
||||
self.topProcessesCallback()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ extension AppDelegate {
|
||||
}
|
||||
|
||||
if IsNewestVersion(currentVersion: prevVersion, latestVersion: currentVersion) {
|
||||
showNotification(title: "Successfully updated", subtitle: "Stats was updated to v\(currentVersion)", id: "updated-from-\(prevVersion)-to-\(currentVersion)"
|
||||
_ = showNotification(title: "Successfully updated", subtitle: "Stats was updated to v\(currentVersion)", id: "updated-from-\(prevVersion)-to-\(currentVersion)"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -814,3 +814,15 @@ public func showNotification(title: String, subtitle: String, id: String = UUID(
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
public struct TopProcess {
|
||||
public var pid: Int
|
||||
public var command: String
|
||||
public var usage: Double
|
||||
|
||||
public init(pid: Int, command: String, usage: Double) {
|
||||
self.pid = pid
|
||||
self.command = command
|
||||
self.usage = usage
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user