mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: add an option to select update interval for top processes to the CPU (#552)
This commit is contained in:
@@ -69,6 +69,9 @@ public class CPU: Module {
|
||||
self.settingsView.setInterval = { [unowned self] value in
|
||||
self.loadReader?.setInterval(value)
|
||||
}
|
||||
self.settingsView.setTopInterval = { [unowned self] value in
|
||||
self.processReader?.setInterval(value)
|
||||
}
|
||||
self.settingsView.IPGCallback = { [unowned self] value in
|
||||
if value {
|
||||
self.frequencyReader?.setup()
|
||||
|
||||
@@ -161,6 +161,7 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
|
||||
public override func setup() {
|
||||
self.popup = true
|
||||
self.setInterval(Store.shared.int(key: "\(self.title)_updateTopInterval", defaultValue: 1))
|
||||
}
|
||||
|
||||
public override func read() {
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
import Cocoa
|
||||
import Kit
|
||||
|
||||
internal class Settings: NSView, Settings_v {
|
||||
internal class Settings: NSStackView, Settings_v {
|
||||
private var usagePerCoreState: Bool = false
|
||||
private var hyperthreadState: Bool = false
|
||||
private var IPGState: Bool = false
|
||||
private var updateIntervalValue: Int = 1
|
||||
private var updateTopIntervalValue: Int = 1
|
||||
private var numberOfProcesses: Int = 8
|
||||
|
||||
private let title: String
|
||||
@@ -26,6 +27,7 @@ internal class Settings: NSView, Settings_v {
|
||||
public var callbackWhenUpdateNumberOfProcesses: (() -> Void) = {}
|
||||
public var IPGCallback: ((_ state: Bool) -> Void) = {_ in }
|
||||
public var setInterval: ((_ value: Int) -> Void) = {_ in }
|
||||
public var setTopInterval: ((_ value: Int) -> Void) = {_ in }
|
||||
|
||||
private var hyperthreadView: NSView? = nil
|
||||
|
||||
@@ -35,66 +37,64 @@ internal class Settings: NSView, Settings_v {
|
||||
self.usagePerCoreState = Store.shared.bool(key: "\(self.title)_usagePerCore", defaultValue: self.usagePerCoreState)
|
||||
self.IPGState = Store.shared.bool(key: "\(self.title)_IPG", defaultValue: self.IPGState)
|
||||
self.updateIntervalValue = Store.shared.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
|
||||
self.updateTopIntervalValue = Store.shared.int(key: "\(self.title)_updateTopInterval", defaultValue: self.updateTopIntervalValue)
|
||||
self.numberOfProcesses = Store.shared.int(key: "\(self.title)_processes", defaultValue: self.numberOfProcesses)
|
||||
if !self.usagePerCoreState {
|
||||
self.hyperthreadState = false
|
||||
}
|
||||
self.hasHyperthreadingCores = sysctlByName("hw.physicalcpu") != sysctlByName("hw.logicalcpu")
|
||||
|
||||
super.init(frame: CGRect(
|
||||
super.init(frame: NSRect(
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: Constants.Settings.width - (Constants.Settings.margin*2),
|
||||
height: 0
|
||||
))
|
||||
|
||||
self.wantsLayer = true
|
||||
self.canDrawConcurrently = true
|
||||
self.orientation = .vertical
|
||||
self.distribution = .gravityAreas
|
||||
self.edgeInsets = NSEdgeInsets(
|
||||
top: Constants.Settings.margin,
|
||||
left: Constants.Settings.margin,
|
||||
bottom: Constants.Settings.margin,
|
||||
right: Constants.Settings.margin
|
||||
)
|
||||
self.spacing = Constants.Settings.margin
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
// swiftlint:disable function_body_length
|
||||
public func load(widgets: [widget_t]) {
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
|
||||
var hasIPG = false
|
||||
|
||||
var hasIPG = false
|
||||
#if arch(x86_64)
|
||||
let path: CFString = "/Library/Frameworks/IntelPowerGadget.framework" as CFString
|
||||
let bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, CFURLPathStyle.cfurlposixPathStyle, true)
|
||||
hasIPG = CFBundleCreate(kCFAllocatorDefault, bundleURL) != nil
|
||||
#endif
|
||||
|
||||
let rowHeight: CGFloat = 30
|
||||
var num: CGFloat = !widgets.filter{ $0 == .barChart }.isEmpty ? self.hasHyperthreadingCores ? 3 : 2 : 1
|
||||
if hasIPG {
|
||||
num += 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
|
||||
),
|
||||
self.addArrangedSubview(selectTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: localizedString("Update interval"),
|
||||
action: #selector(changeUpdateInterval),
|
||||
items: ReaderUpdateIntervals.map{ "\($0) sec" },
|
||||
selected: "\(self.updateIntervalValue) sec"
|
||||
))
|
||||
|
||||
self.addArrangedSubview(selectTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: localizedString("Update interval for top processes"),
|
||||
action: #selector(changeUpdateTopInterval),
|
||||
items: ReaderUpdateIntervals.map{ "\($0) sec" },
|
||||
selected: "\(self.updateTopIntervalValue) sec"
|
||||
))
|
||||
|
||||
if !widgets.filter({ $0 == .barChart }).isEmpty {
|
||||
self.addSubview(toggleTitleRow(
|
||||
frame: NSRect(
|
||||
x: Constants.Settings.margin,
|
||||
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * (num-1),
|
||||
width: self.frame.width - (Constants.Settings.margin*2),
|
||||
height: rowHeight
|
||||
),
|
||||
self.addArrangedSubview(toggleTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: localizedString("Show usage per core"),
|
||||
action: #selector(toggleUsagePerCore),
|
||||
state: self.usagePerCoreState
|
||||
@@ -102,12 +102,7 @@ internal class Settings: NSView, Settings_v {
|
||||
|
||||
if self.hasHyperthreadingCores {
|
||||
self.hyperthreadView = toggleTitleRow(
|
||||
frame: NSRect(
|
||||
x: Constants.Settings.margin,
|
||||
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * (num-2),
|
||||
width: self.frame.width - (Constants.Settings.margin*2),
|
||||
height: rowHeight
|
||||
),
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: localizedString("Show hyper-threading cores"),
|
||||
action: #selector(toggleMultithreading),
|
||||
state: self.hyperthreadState
|
||||
@@ -116,33 +111,31 @@ internal class Settings: NSView, Settings_v {
|
||||
findAndToggleEnableNSControlState(self.hyperthreadView, state: false)
|
||||
findAndToggleNSControlState(self.hyperthreadView, state: .off)
|
||||
}
|
||||
self.addSubview(self.hyperthreadView!)
|
||||
self.addArrangedSubview(self.hyperthreadView!)
|
||||
}
|
||||
}
|
||||
|
||||
if hasIPG {
|
||||
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
|
||||
),
|
||||
self.addArrangedSubview(toggleTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: "\(localizedString("CPU frequency")) (IPG)",
|
||||
action: #selector(toggleIPG),
|
||||
state: self.IPGState
|
||||
))
|
||||
}
|
||||
|
||||
self.addSubview(selectTitleRow(
|
||||
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
|
||||
self.addArrangedSubview(selectTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: self.frame.width - (Constants.Settings.margin*2), height: Constants.Settings.row),
|
||||
title: localizedString("Number of top processes"),
|
||||
action: #selector(changeNumberOfProcesses),
|
||||
items: NumbersOfProcesses.map{ "\($0)" },
|
||||
selected: "\(self.numberOfProcesses)"
|
||||
))
|
||||
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*(num+1)) + (Constants.Settings.margin*(2+num))))
|
||||
let h = self.arrangedSubviews.map({ $0.bounds.height + self.spacing }).reduce(0, +) - self.spacing + self.edgeInsets.top + self.edgeInsets.bottom
|
||||
if self.frame.size.height != h {
|
||||
self.setFrameSize(NSSize(width: self.bounds.width, height: h))
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
|
||||
@@ -153,6 +146,14 @@ internal class Settings: NSView, Settings_v {
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeUpdateTopInterval(_ sender: NSMenuItem) {
|
||||
if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) {
|
||||
self.updateTopIntervalValue = value
|
||||
Store.shared.set(key: "\(self.title)_updateTopInterval", value: value)
|
||||
self.setTopInterval(value)
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeNumberOfProcesses(_ sender: NSMenuItem) {
|
||||
if let value = Int(sender.title) {
|
||||
self.numberOfProcesses = value
|
||||
|
||||
Reference in New Issue
Block a user