mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
fix: duplicating GPU the toggle the module
This commit is contained in:
@@ -13,9 +13,19 @@ import Cocoa
|
||||
import ModuleKit
|
||||
import StatsKit
|
||||
|
||||
public typealias GPU_type = String
|
||||
public enum GPU_types: GPU_type {
|
||||
case unknown = ""
|
||||
|
||||
case integrated = "i"
|
||||
case external = "e"
|
||||
case discrete = "d"
|
||||
}
|
||||
|
||||
public struct GPU_Info {
|
||||
public let model: String
|
||||
public let IOClass: String
|
||||
public let type: GPU_type
|
||||
public var state: Bool = false
|
||||
|
||||
public var utilization: Double = 0
|
||||
@@ -29,10 +39,6 @@ public struct GPUs: value_t {
|
||||
return self.list.filter{ $0.state }
|
||||
}
|
||||
|
||||
internal func igpu() -> GPU_Info? {
|
||||
return self.active().first{ $0.IOClass == "IntelAccelerator" }
|
||||
}
|
||||
|
||||
public var widget_value: Double {
|
||||
get {
|
||||
return list.isEmpty ? 0 : list[0].utilization
|
||||
@@ -50,6 +56,12 @@ public class GPU: Module {
|
||||
|
||||
private var selectedGPU: String = ""
|
||||
|
||||
private var showType: Bool {
|
||||
get {
|
||||
return self.store.pointee.bool(key: "\(self.config.name)_showType", defaultValue: false)
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ store: UnsafePointer<Store>, _ smc: UnsafePointer<SMCService>) {
|
||||
self.store = store
|
||||
self.smc = smc
|
||||
@@ -80,6 +92,9 @@ public class GPU: Module {
|
||||
self.settingsView.setInterval = { [unowned self] value in
|
||||
self.infoReader?.setInterval(value)
|
||||
}
|
||||
self.settingsView.callback = {
|
||||
self.infoReader?.read()
|
||||
}
|
||||
|
||||
if let reader = self.infoReader {
|
||||
self.addReader(reader)
|
||||
@@ -97,11 +112,14 @@ public class GPU: Module {
|
||||
self.settingsView.setList(value)
|
||||
|
||||
let activeGPUs = value.active()
|
||||
let activeGPU = activeGPUs.first{ $0.state } ?? activeGPUs[0]
|
||||
let selectedGPU: GPU_Info = activeGPUs.first{ $0.model == self.selectedGPU } ?? value.igpu() ?? activeGPU
|
||||
guard let activeGPU = activeGPUs.first(where: { $0.state }) ?? activeGPUs.first else {
|
||||
return
|
||||
}
|
||||
let selectedGPU: GPU_Info = activeGPUs.first{ $0.model == self.selectedGPU } ?? activeGPU
|
||||
|
||||
if let widget = self.widget as? Mini {
|
||||
widget.setValue(selectedGPU.utilization)
|
||||
widget.setTitle(self.showType ? "\(selectedGPU.type)GPU" : nil)
|
||||
}
|
||||
if let widget = self.widget as? LineChart {
|
||||
widget.setValue(selectedGPU.utilization)
|
||||
|
||||
@@ -53,10 +53,7 @@ internal class InfoReader: Reader<GPUs> {
|
||||
guard let accelerators = fetchIOService(kIOAcceleratorClassName) else {
|
||||
return
|
||||
}
|
||||
|
||||
for (i, _) in self.devices.enumerated() {
|
||||
self.devices[i].used = false
|
||||
}
|
||||
var devices = self.devices
|
||||
|
||||
accelerators.forEach { (accelerator: NSDictionary) in
|
||||
guard let IOClass = accelerator.object(forKey: "IOClass") as? String else {
|
||||
@@ -72,33 +69,38 @@ internal class InfoReader: Reader<GPUs> {
|
||||
var model: String = ""
|
||||
let accMatch = (accelerator["IOPCIMatch"] as? String ?? accelerator["IOPCIPrimaryMatch"] as? String ?? "").lowercased()
|
||||
|
||||
for (i, device) in self.devices.enumerated() {
|
||||
let matched = accMatch.range(of: device.pci)
|
||||
if matched != nil && !device.used {
|
||||
for (i, device) in devices.enumerated() {
|
||||
if accMatch.range(of: device.pci) != nil && !device.used {
|
||||
model = device.model
|
||||
self.devices[i].used = true
|
||||
} else if device.used {
|
||||
print("Device `\(device.model)` with pci `\(device.pci)` is already used", to: &Log.log)
|
||||
} else {
|
||||
print("`\(device.pci)` and `\(accMatch)` not match", to: &Log.log)
|
||||
devices[i].used = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
let ioClass = IOClass.lowercased()
|
||||
var predictModel = ""
|
||||
var type: GPU_types = .unknown
|
||||
|
||||
if ioClass == "nvAccelerator" || ioClass.contains("nvidia") {
|
||||
predictModel = "Nvidia Graphics"
|
||||
type = .discrete
|
||||
} else if ioClass.contains("amd") {
|
||||
predictModel = "AMD Graphics"
|
||||
type = .discrete
|
||||
} else if ioClass.contains("intel") {
|
||||
predictModel = "Intel Graphics"
|
||||
type = .integrated
|
||||
} else {
|
||||
predictModel = "Unknown"
|
||||
type = .unknown
|
||||
}
|
||||
|
||||
if model == "" {
|
||||
let ioClass = IOClass.lowercased()
|
||||
if ioClass == "nvAccelerator" || ioClass.contains("nvidia") {
|
||||
model = "Nvidia Graphics"
|
||||
} else if ioClass.contains("amd") {
|
||||
model = "AMD Graphics"
|
||||
} else if ioClass.contains("intel") {
|
||||
model = "Intel Graphics"
|
||||
} else {
|
||||
model = "Unknown"
|
||||
}
|
||||
model = predictModel
|
||||
}
|
||||
|
||||
if self.gpus.list.first(where: { $0.model == model }) == nil {
|
||||
self.gpus.list.append(GPU_Info(model: model, IOClass: IOClass))
|
||||
self.gpus.list.append(GPU_Info(model: model, IOClass: IOClass, type: type.rawValue))
|
||||
}
|
||||
guard let idx = self.gpus.list.firstIndex(where: { $0.model == model }) else {
|
||||
return
|
||||
|
||||
@@ -16,6 +16,7 @@ import ModuleKit
|
||||
internal class Settings: NSView, Settings_v {
|
||||
private var updateIntervalValue: Int = 1
|
||||
private var selectedGPU: String
|
||||
private var showTypeValue: Bool = false
|
||||
|
||||
private let title: String
|
||||
private let store: UnsafePointer<Store>
|
||||
@@ -32,6 +33,7 @@ internal class Settings: NSView, Settings_v {
|
||||
self.store = store
|
||||
self.selectedGPU = store.pointee.string(key: "\(self.title)_gpu", defaultValue: "")
|
||||
self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
|
||||
self.showTypeValue = store.pointee.bool(key: "\(self.title)_showType", defaultValue: self.showTypeValue)
|
||||
|
||||
super.init(frame: CGRect(
|
||||
x: 0,
|
||||
@@ -52,28 +54,47 @@ internal class Settings: NSView, Settings_v {
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
|
||||
let rowHeight: CGFloat = 30
|
||||
let num: CGFloat = 1
|
||||
let num: CGFloat = widget == .mini ? 3 : 2
|
||||
|
||||
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),
|
||||
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
|
||||
),
|
||||
title: LocalizedString("Update interval"),
|
||||
action: #selector(changeUpdateInterval),
|
||||
items: ReaderUpdateIntervals.map{ "\($0) sec" },
|
||||
selected: "\(self.updateIntervalValue) sec"
|
||||
))
|
||||
|
||||
self.addGPUSelector()
|
||||
if widget == .mini {
|
||||
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
|
||||
),
|
||||
title: LocalizedString("Show GPU type"),
|
||||
action: #selector(toggleShowType),
|
||||
state: self.showTypeValue
|
||||
))
|
||||
}
|
||||
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*(num+1)) + (Constants.Settings.margin*(2+num))))
|
||||
self.addGPUSelector(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
|
||||
))
|
||||
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*num) + (Constants.Settings.margin*(num+1))))
|
||||
}
|
||||
|
||||
private func addGPUSelector() {
|
||||
let view: NSView = NSView(frame: NSRect(
|
||||
x: Constants.Settings.margin,
|
||||
y: Constants.Settings.margin,
|
||||
width: self.frame.width - Constants.Settings.margin*2,
|
||||
height: 30
|
||||
))
|
||||
private func addGPUSelector(frame: NSRect) {
|
||||
let view: NSView = NSView(frame: frame)
|
||||
|
||||
let rowTitle: NSTextField = LabelField(frame: NSRect(
|
||||
x: 0,
|
||||
@@ -145,4 +166,17 @@ internal class Settings: NSView, Settings_v {
|
||||
self.store.pointee.set(key: "\(self.title)_gpu", value: key)
|
||||
self.selectedGPUHandler(key)
|
||||
}
|
||||
|
||||
@objc func toggleShowType(_ 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.showTypeValue = state! == .on ? true : false
|
||||
self.store.pointee.set(key: "\(self.title)_showType", value: self.showTypeValue)
|
||||
self.callback()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU auswählen";
|
||||
"Show GPU type" = "GPU Typ anzeigen";
|
||||
"GPU temperature" = "GPU Temperatur";
|
||||
"GPU utilization" = "GPU Nutzung";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU to show";
|
||||
"Show GPU type" = "Show GPU type";
|
||||
"GPU temperature" = "GPU temperature";
|
||||
"GPU utilization" = "GPU utilization";
|
||||
|
||||
|
||||
@@ -98,6 +98,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU a mostrar";
|
||||
"Show GPU type" = "Mostrar tipo de GPU";
|
||||
"GPU temperature" = "Temperatura de la GPU";
|
||||
"GPU utilization" = "Utilización de la GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU à afficher";
|
||||
"Show GPU type" = "Afficher le type de GPU";
|
||||
"GPU temperature" = "Température du GPU";
|
||||
"GPU utilization" = "Utilisation du GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU da mostrare";
|
||||
"Show GPU type" = "Mostra il tipo di GPU";
|
||||
"GPU temperature" = "Temperatura GPU";
|
||||
"GPU utilization" = "Utilizzo GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "표시할 GPU";
|
||||
"Show GPU type" = "GPU 유형 표시";
|
||||
"GPU temperature" = "GPU 온도";
|
||||
"GPU utilization" = "GPU 활용";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU å vise";
|
||||
"Show GPU type" = "Vis GPU-typen";
|
||||
"GPU temperature" = "GPU-temperatur";
|
||||
"GPU utilization" = "GPU-bruk";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU do wyświetlania";
|
||||
"Show GPU type" = "Pokaż typ GPU";
|
||||
"GPU temperature" = "Temperatura GPU";
|
||||
"GPU utilization" = "Obciążenie GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU para mostrar";
|
||||
"Show GPU type" = "Mostrar tipo de GPU";
|
||||
"GPU temperature" = "Temperatura da GPU";
|
||||
"GPU utilization" = "Utilização da GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "Активный графический процессор";
|
||||
"Show GPU type" = "Отображать тип графического процессора";
|
||||
"GPU temperature" = "Температура графического процессора";
|
||||
"GPU utilization" = "Использование графического процессора";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "Gösterilecek GPU";
|
||||
"Show GPU type" = "GPU türünü göster";
|
||||
"GPU temperature" = "GPU sıcaklığı";
|
||||
"GPU utilization" = "GPU kullanımı";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "Активний графічний процесор";
|
||||
"Show GPU type" = "Показувати тип графічного процесора";
|
||||
"GPU temperature" = "Температура графічного процесора";
|
||||
"GPU utilization" = "Навантаженість графічного процесора";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "GPU để hiển thị";
|
||||
"Show GPU type" = "Hiển thị loại GPU";
|
||||
"GPU temperature" = "Nhiệt độ GPU";
|
||||
"GPU utilization" = "Sử dụng GPU";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "显示的GPU";
|
||||
"Show GPU type" = "显示GPU类型";
|
||||
"GPU temperature" = "GPU温度";
|
||||
"GPU utilization" = "GPU利用率";
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
// GPU
|
||||
"GPU to show" = "顯示 GPU";
|
||||
"Show GPU type" = "顯示GPU類型";
|
||||
"GPU temperature" = "GPU 溫度";
|
||||
"GPU utilization" = "GPU 使用率";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user