mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added details to the CPU portal view
This commit is contained in:
@@ -261,6 +261,7 @@ public class LabelField: NSTextField {
|
||||
self.alignment = .natural
|
||||
self.font = NSFont.systemFont(ofSize: 12, weight: .regular)
|
||||
|
||||
self.cell?.truncatesLastVisibleLine = true
|
||||
self.cell?.usesSingleLineMode = true
|
||||
}
|
||||
|
||||
@@ -374,6 +375,37 @@ public func popupWithColorRow(_ view: NSView, color: NSColor, n: CGFloat, title:
|
||||
return (colorView, labelView, valueView)
|
||||
}
|
||||
|
||||
public func portalWithColorRow(_ v: NSStackView, color: NSColor, title: String) -> (NSView, ValueField) {
|
||||
let view: NSStackView = NSStackView()
|
||||
view.orientation = .horizontal
|
||||
view.distribution = .fillProportionally
|
||||
view.spacing = 1
|
||||
|
||||
let colorView: NSView = NSView()
|
||||
colorView.widthAnchor.constraint(equalToConstant: 5).isActive = true
|
||||
colorView.wantsLayer = true
|
||||
colorView.layer?.backgroundColor = color.cgColor
|
||||
colorView.layer?.cornerRadius = 2
|
||||
|
||||
let labelView: LabelField = LabelField(title)
|
||||
labelView.font = NSFont.systemFont(ofSize: 11, weight: .regular)
|
||||
|
||||
let valueView: ValueField = ValueField()
|
||||
valueView.font = NSFont.systemFont(ofSize: 12, weight: .regular)
|
||||
valueView.widthAnchor.constraint(equalToConstant: 40).isActive = true
|
||||
|
||||
view.addArrangedSubview(colorView)
|
||||
view.addArrangedSubview(labelView)
|
||||
view.addArrangedSubview(NSView())
|
||||
view.addArrangedSubview(valueView)
|
||||
|
||||
v.addArrangedSubview(view)
|
||||
|
||||
view.widthAnchor.constraint(equalTo: v.widthAnchor).isActive = true
|
||||
|
||||
return (colorView, valueView)
|
||||
}
|
||||
|
||||
public extension Array where Element: Equatable {
|
||||
func allEqual() -> Bool {
|
||||
if let firstElem = first {
|
||||
|
||||
@@ -15,6 +15,50 @@ public protocol Portal_p: NSView {
|
||||
var name: String { get }
|
||||
}
|
||||
|
||||
open class PortalWrapper: NSStackView, Portal_p {
|
||||
public var name: String
|
||||
|
||||
private let header: PortalHeader
|
||||
|
||||
public init(_ type: ModuleType, height: CGFloat = Constants.Popup.portalHeight) {
|
||||
self.name = type.rawValue
|
||||
self.header = PortalHeader(type.rawValue)
|
||||
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: height))
|
||||
|
||||
self.wantsLayer = true
|
||||
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
|
||||
self.layer?.cornerRadius = 3
|
||||
|
||||
self.orientation = .vertical
|
||||
self.distribution = .fillEqually
|
||||
self.spacing = Constants.Popup.spacing*2
|
||||
self.edgeInsets = NSEdgeInsets(
|
||||
top: Constants.Popup.spacing*2,
|
||||
left: Constants.Popup.spacing*2,
|
||||
bottom: Constants.Popup.spacing*2,
|
||||
right: Constants.Popup.spacing*2
|
||||
)
|
||||
self.addArrangedSubview(self.header)
|
||||
|
||||
self.load()
|
||||
|
||||
self.heightAnchor.constraint(equalToConstant: Constants.Popup.portalHeight).isActive = true
|
||||
}
|
||||
|
||||
required public init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
public override func updateLayer() {
|
||||
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
|
||||
}
|
||||
|
||||
open func load() {
|
||||
self.addArrangedSubview(NSView())
|
||||
}
|
||||
}
|
||||
|
||||
public class PortalHeader: NSStackView {
|
||||
private let name: String
|
||||
|
||||
|
||||
@@ -829,7 +829,7 @@ public class TachometerGraphView: NSView {
|
||||
public class BarChartView: NSView {
|
||||
private var values: [ColorValue] = []
|
||||
|
||||
public init(frame: NSRect, num: Int) {
|
||||
public init(frame: NSRect = NSRect.zero, num: Int) {
|
||||
super.init(frame: frame)
|
||||
self.values = Array(repeating: ColorValue(0, color: .controlAccentColor), count: num)
|
||||
}
|
||||
@@ -858,17 +858,28 @@ public class BarChartView: NSView {
|
||||
let value = self.values[i]
|
||||
let color = value.color ?? .controlAccentColor
|
||||
let activeBlockNum = Int(round(value.value*Double(blocks)))
|
||||
let h = value.value*partitionSize.height
|
||||
|
||||
var y: CGFloat = spacing
|
||||
for b in 0..<blocks {
|
||||
if dirtyRect.height < 30 && h != 0 {
|
||||
let block = NSBezierPath(
|
||||
roundedRect: NSRect(x: x+spacing, y: y, width: blockSize.width, height: blockSize.height),
|
||||
roundedRect: NSRect(x: x+spacing, y: 1, width: partitionSize.width-(spacing*2), height: value.value*partitionSize.height),
|
||||
xRadius: 1, yRadius: 1
|
||||
)
|
||||
(activeBlockNum <= b ? NSColor.controlBackgroundColor.withAlphaComponent(0.4) : color).setFill()
|
||||
color.setFill()
|
||||
block.fill()
|
||||
block.close()
|
||||
y += blockSize.height + 1
|
||||
} else {
|
||||
var y: CGFloat = spacing
|
||||
for b in 0..<blocks {
|
||||
let block = NSBezierPath(
|
||||
roundedRect: NSRect(x: x+spacing, y: y, width: blockSize.width, height: blockSize.height),
|
||||
xRadius: 1, yRadius: 1
|
||||
)
|
||||
(activeBlockNum <= b ? NSColor.controlBackgroundColor.withAlphaComponent(0.4) : color).setFill()
|
||||
block.fill()
|
||||
block.close()
|
||||
y += blockSize.height + 1
|
||||
}
|
||||
}
|
||||
|
||||
x += partitionSize.width + spacing
|
||||
|
||||
@@ -91,7 +91,7 @@ public class CPU: Module {
|
||||
public init() {
|
||||
self.settingsView = Settings("CPU")
|
||||
self.popupView = Popup("CPU")
|
||||
self.portalView = Portal("CPU")
|
||||
self.portalView = Portal(.CPU)
|
||||
self.notificationsView = Notifications(.CPU)
|
||||
|
||||
super.init(
|
||||
@@ -192,7 +192,7 @@ public class CPU: Module {
|
||||
guard let value = raw, self.enabled else { return }
|
||||
|
||||
self.popupView.loadCallback(value)
|
||||
self.portalView.loadCallback(value)
|
||||
self.portalView.callback(value)
|
||||
self.notificationsView.loadCallback(value)
|
||||
|
||||
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
|
||||
|
||||
@@ -70,53 +70,17 @@ internal class Popup: PopupWrapper {
|
||||
private var maxFreq: Double = 0
|
||||
|
||||
private var systemColorState: Color = .secondRed
|
||||
private var systemColor: NSColor {
|
||||
var value = NSColor.systemRed
|
||||
if let color = self.systemColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var systemColor: NSColor { self.systemColorState.additional as? NSColor ?? NSColor.systemRed }
|
||||
private var userColorState: Color = .secondBlue
|
||||
private var userColor: NSColor {
|
||||
var value = NSColor.systemBlue
|
||||
if let color = self.userColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var userColor: NSColor { self.userColorState.additional as? NSColor ?? NSColor.systemBlue }
|
||||
private var idleColorState: Color = .lightGray
|
||||
private var idleColor: NSColor {
|
||||
var value = NSColor.lightGray
|
||||
if let color = self.idleColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var idleColor: NSColor { self.idleColorState.additional as? NSColor ?? NSColor.lightGray }
|
||||
private var chartColorState: Color = .systemAccent
|
||||
private var chartColor: NSColor {
|
||||
var value = NSColor.systemBlue
|
||||
if let color = self.chartColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var chartColor: NSColor { self.chartColorState.additional as? NSColor ?? NSColor.systemBlue }
|
||||
private var eCoresColorState: Color = .teal
|
||||
private var eCoresColor: NSColor {
|
||||
var value = NSColor.systemTeal
|
||||
if let color = self.eCoresColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var eCoresColor: NSColor { self.eCoresColorState.additional as? NSColor ?? NSColor.systemTeal }
|
||||
private var pCoresColorState: Color = .secondBlue
|
||||
private var pCoresColor: NSColor {
|
||||
var value = NSColor.systemBlue
|
||||
if let color = self.pCoresColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var pCoresColor: NSColor { self.pCoresColorState.additional as? NSColor ?? NSColor.systemBlue }
|
||||
|
||||
private var numberOfProcesses: Int {
|
||||
Store.shared.int(key: "\(self.title)_processes", defaultValue: 8)
|
||||
|
||||
@@ -12,101 +12,122 @@
|
||||
import Cocoa
|
||||
import Kit
|
||||
|
||||
public class Portal: NSStackView, Portal_p {
|
||||
public var name: String
|
||||
|
||||
public class Portal: PortalWrapper {
|
||||
private var circle: PieChartView? = nil
|
||||
private var barChart: BarChartView? = nil
|
||||
|
||||
private var initialized: Bool = false
|
||||
|
||||
private var systemColorState: Color = .secondRed
|
||||
private var systemColor: NSColor {
|
||||
var value = NSColor.systemRed
|
||||
if let color = self.systemColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var userColorState: Color = .secondBlue
|
||||
private var userColor: NSColor {
|
||||
var value = NSColor.systemBlue
|
||||
if let color = self.userColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var idleColorState: Color = .lightGray
|
||||
private var idleColor: NSColor {
|
||||
var value = NSColor.lightGray
|
||||
if let color = self.idleColorState.additional as? NSColor {
|
||||
value = color
|
||||
}
|
||||
return value
|
||||
}
|
||||
private var systemField: NSTextField? = nil
|
||||
private var userField: NSTextField? = nil
|
||||
private var idleField: NSTextField? = nil
|
||||
private var shedulerLimitField: NSTextField? = nil
|
||||
private var speedLimitField: NSTextField? = nil
|
||||
private var eCoresField: NSTextField? = nil
|
||||
private var pCoresField: NSTextField? = nil
|
||||
private var average1Field: NSTextField? = nil
|
||||
private var average5Field: NSTextField? = nil
|
||||
private var average15Field: NSTextField? = nil
|
||||
|
||||
init(_ name: String) {
|
||||
self.name = name
|
||||
|
||||
super.init(frame: NSRect.zero)
|
||||
|
||||
self.wantsLayer = true
|
||||
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
|
||||
self.layer?.cornerRadius = 3
|
||||
|
||||
self.orientation = .vertical
|
||||
self.distribution = .fillEqually
|
||||
self.spacing = Constants.Popup.spacing*2
|
||||
self.edgeInsets = NSEdgeInsets(
|
||||
top: Constants.Popup.spacing*2,
|
||||
private var systemColorView: NSView? = nil
|
||||
private var userColorView: NSView? = nil
|
||||
private var idleColorView: NSView? = nil
|
||||
private var eCoresColorView: NSView? = nil
|
||||
private var pCoresColorView: NSView? = nil
|
||||
|
||||
private var systemColorState: Color = .secondRed
|
||||
private var systemColor: NSColor { self.systemColorState.additional as? NSColor ?? NSColor.systemRed }
|
||||
private var userColorState: Color = .secondBlue
|
||||
private var userColor: NSColor { self.userColorState.additional as? NSColor ?? NSColor.systemBlue }
|
||||
private var idleColorState: Color = .lightGray
|
||||
private var idleColor: NSColor { self.idleColorState.additional as? NSColor ?? NSColor.lightGray }
|
||||
private var eCoresColorState: Color = .teal
|
||||
private var eCoresColor: NSColor { self.eCoresColorState.additional as? NSColor ?? NSColor.systemTeal }
|
||||
private var pCoresColorState: Color = .secondBlue
|
||||
private var pCoresColor: NSColor { self.pCoresColorState.additional as? NSColor ?? NSColor.systemBlue }
|
||||
|
||||
public override func load() {
|
||||
let view = NSStackView()
|
||||
view.orientation = .horizontal
|
||||
view.distribution = .fillEqually
|
||||
view.spacing = Constants.Popup.spacing*2
|
||||
view.edgeInsets = NSEdgeInsets(
|
||||
top: 0,
|
||||
left: Constants.Popup.spacing*2,
|
||||
bottom: Constants.Popup.spacing*2,
|
||||
bottom: 0,
|
||||
right: Constants.Popup.spacing*2
|
||||
)
|
||||
self.addArrangedSubview(PortalHeader(name))
|
||||
|
||||
let chartsView = self.charts()
|
||||
let detailsView = self.details()
|
||||
|
||||
view.addArrangedSubview(chartsView)
|
||||
view.addArrangedSubview(detailsView)
|
||||
|
||||
self.addArrangedSubview(view)
|
||||
|
||||
chartsView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
|
||||
detailsView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
|
||||
}
|
||||
|
||||
private func charts() -> NSView {
|
||||
let view = NSStackView()
|
||||
view.orientation = .vertical
|
||||
view.distribution = .fillEqually
|
||||
view.spacing = Constants.Popup.spacing*2
|
||||
|
||||
let circle = PieChartView(frame: NSRect.zero, segments: [], drawValue: true)
|
||||
circle.toolTip = localizedString("CPU usage")
|
||||
self.circle = circle
|
||||
self.addArrangedSubview(circle)
|
||||
view.addArrangedSubview(circle)
|
||||
|
||||
if let cores = SystemKit.shared.device.info.cpu?.logicalCores {
|
||||
let barChartContainer: NSView = {
|
||||
let box: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 30))
|
||||
let box: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 24))
|
||||
box.heightAnchor.constraint(equalToConstant: box.frame.height).isActive = true
|
||||
box.wantsLayer = true
|
||||
box.layer?.backgroundColor = NSColor.lightGray.withAlphaComponent(0.1).cgColor
|
||||
box.layer?.cornerRadius = 3
|
||||
|
||||
let chart = BarChartView(frame: NSRect(
|
||||
x: Constants.Popup.spacing,
|
||||
y: Constants.Popup.spacing,
|
||||
width: Constants.Popup.width/2 - (Constants.Popup.spacing*2),
|
||||
height: box.frame.height - (Constants.Popup.spacing*2)
|
||||
), num: Int(cores))
|
||||
let chart = BarChartView(num: Int(cores))
|
||||
self.barChart = chart
|
||||
|
||||
box.addArrangedSubview(chart)
|
||||
|
||||
return box
|
||||
}()
|
||||
self.addArrangedSubview(barChartContainer)
|
||||
view.addArrangedSubview(barChartContainer)
|
||||
}
|
||||
|
||||
self.heightAnchor.constraint(equalToConstant: Constants.Popup.portalHeight).isActive = true
|
||||
return view
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
private func details() -> NSView {
|
||||
let view = NSStackView()
|
||||
view.orientation = .vertical
|
||||
view.distribution = .fillEqually
|
||||
view.spacing = 2
|
||||
|
||||
(self.systemColorView, self.systemField) = portalWithColorRow(view, color: self.systemColor, title: "\(localizedString("System")):")
|
||||
(self.userColorView, self.userField) = portalWithColorRow(view, color: self.userColor, title: "\(localizedString("User")):")
|
||||
(self.idleColorView, self.idleField) = portalWithColorRow(view, color: self.idleColor.withAlphaComponent(0.5), title: "\(localizedString("Idle")):")
|
||||
|
||||
if SystemKit.shared.device.info.cpu?.eCores != nil {
|
||||
(self.eCoresColorView, self.eCoresField) = portalWithColorRow(view, color: self.eCoresColor, title: "E-cores:")
|
||||
}
|
||||
if SystemKit.shared.device.info.cpu?.pCores != nil {
|
||||
(self.pCoresColorView, self.pCoresField) = portalWithColorRow(view, color: self.pCoresColor, title: "P-cores:")
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
public override func updateLayer() {
|
||||
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
|
||||
}
|
||||
|
||||
public func loadCallback(_ value: CPU_Load) {
|
||||
internal func callback(_ value: CPU_Load) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if (self.window?.isVisible ?? false) || !self.initialized {
|
||||
self.systemField?.stringValue = "\(Int(value.systemLoad.rounded(toPlaces: 2) * 100))%"
|
||||
self.userField?.stringValue = "\(Int(value.userLoad.rounded(toPlaces: 2) * 100))%"
|
||||
self.idleField?.stringValue = "\(Int(value.idleLoad.rounded(toPlaces: 2) * 100))%"
|
||||
|
||||
self.circle?.setValue(value.totalUsage)
|
||||
self.circle?.setSegments([
|
||||
circle_segment(value: value.systemLoad, color: self.systemColor),
|
||||
@@ -114,6 +135,13 @@ public class Portal: NSStackView, Portal_p {
|
||||
])
|
||||
self.circle?.setNonActiveSegmentColor(self.idleColor)
|
||||
|
||||
if let field = self.eCoresField, let usage = value.usageECores {
|
||||
field.stringValue = "\(Int(usage * 100))%"
|
||||
}
|
||||
if let field = self.pCoresField, let usage = value.usagePCores {
|
||||
field.stringValue = "\(Int(usage * 100))%"
|
||||
}
|
||||
|
||||
var usagePerCore: [ColorValue] = []
|
||||
if let cores = SystemKit.shared.device.info.cpu?.cores, cores.count == value.usagePerCore.count {
|
||||
for i in 0..<value.usagePerCore.count {
|
||||
|
||||
Reference in New Issue
Block a user