From 732fb5829ced4d5be88ab71a3aa0d459b5ed700a Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 4 Feb 2023 16:45:46 +0100 Subject: [PATCH] feat: added a new BarChart for usage per core visualization in the CPU popup --- Kit/plugins/Charts.swift | 66 ++++++++++++++++++++++++++++----- Modules/CPU/popup.swift | 80 ++++++++++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 25 deletions(-) diff --git a/Kit/plugins/Charts.swift b/Kit/plugins/Charts.swift index be0d73cc..7705bc53 100644 --- a/Kit/plugins/Charts.swift +++ b/Kit/plugins/Charts.swift @@ -11,15 +11,6 @@ import Cocoa -public enum chart_t: Int { - case line = 0 - case bar = 1 - - init?(value: Int) { - self.init(rawValue: value) - } -} - public struct circle_segment { public let value: Double public var color: NSColor @@ -701,3 +692,60 @@ public class TachometerGraphView: NSView { self.frame = original } } + +public class BarChartView: NSView { + private var values: [ColorValue] = [] + + public init(frame: NSRect, num: Int) { + super.init(frame: frame) + self.values = Array(repeating: ColorValue(0, color: controlAccentColor), count: num) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + public override func draw(_ dirtyRect: NSRect) { + let blocks: Int = 16 + let spacing: CGFloat = 2 + let count: CGFloat = CGFloat(self.values.count) + let partitionSize: CGSize = CGSize(width: (self.frame.width - (count*spacing)) / count, height: self.frame.height) + let blockSize = CGSize(width: partitionSize.width-(spacing*2), height: ((partitionSize.height - spacing - 1)/CGFloat(blocks))-1) + + var x: CGFloat = 0 + for i in 0.. NSView { - let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.chartHeight)) - let separator = separatorView(localizedString("Usage history"), origin: NSPoint(x: 0, y: self.chartHeight-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)) - container.wantsLayer = true - container.layer?.backgroundColor = NSColor.lightGray.withAlphaComponent(0.1).cgColor - container.layer?.cornerRadius = 3 + let view: NSStackView = NSStackView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.chartHeight)) + view.orientation = .vertical + view.spacing = 0 - self.chart = LineChartView(frame: NSRect(x: 1, y: 0, width: view.frame.width, height: container.frame.height), num: 120) - self.chart?.color = self.chartColor - container.addSubview(self.chart!) + let separator = separatorView(localizedString("Usage history"), origin: NSPoint(x: 0, y: 0), width: self.frame.width) - view.addSubview(separator) - view.addSubview(container) + let lineChartContainer: NSView = { + let box: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 70)) + 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 = LineChartView(frame: NSRect( + x: Constants.Popup.spacing, + y: Constants.Popup.spacing, + width: view.frame.width - (Constants.Popup.spacing*2), + height: box.frame.height - (Constants.Popup.spacing*2) + ), num: 120) + chart.color = self.chartColor + self.lineChart = chart + + box.addSubview(chart) + + return box + }() + + view.addArrangedSubview(separator) + view.addArrangedSubview(lineChartContainer) + + if let cores = SystemKit.shared.device.info.cpu?.cores { + let barChartContainer: NSView = { + let box: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 50)) + 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: view.frame.width - (Constants.Popup.spacing*2), + height: box.frame.height - (Constants.Popup.spacing*2) + ), num: cores.count) + self.barChart = chart + + box.addSubview(chart) + + return box + }() + view.addArrangedSubview(barChartContainer) + } return view } @@ -312,9 +352,17 @@ internal class Popup: NSView, Popup_p { field.stringValue = "\(Int(usage * 100))%" } + if let cores = SystemKit.shared.device.info.cpu?.cores, cores.count == value.usagePerCore.count { + var list: [ColorValue] = [] + for i in 0..