diff --git a/Modules/CPU/popup.swift b/Modules/CPU/popup.swift index 16e23ccf..7d0e820b 100644 --- a/Modules/CPU/popup.swift +++ b/Modules/CPU/popup.swift @@ -31,7 +31,7 @@ internal class Popup: NSView, Popup_p { private var idleField: NSTextField? = nil private var chart: LineChartView? = nil - private var circle: CircleGraphView? = nil + private var circle: PieChartView? = nil private var temperatureCircle: HalfCircleGraphView? = nil private var frequencyCircle: HalfCircleGraphView? = nil private var initialized: Bool = false @@ -118,7 +118,12 @@ internal class Popup: NSView, Popup_p { let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.dashboardHeight)) let container: NSView = NSView(frame: NSRect(x: 0, y: 10, width: view.frame.width, height: self.dashboardHeight-20)) - self.circle = CircleGraphView(frame: NSRect(x: (container.frame.width - container.frame.height)/2, y: 0, width: container.frame.height, height: container.frame.height), segments: []) + self.circle = PieChartView(frame: NSRect( + x: (container.frame.width - container.frame.height)/2, + y: 0, + width: container.frame.height, + height: container.frame.height + ), segments: [], drawValue: true) self.circle!.toolTip = LocalizedString("CPU usage") container.addSubview(self.circle!) diff --git a/Modules/Memory/popup.swift b/Modules/Memory/popup.swift index 3ba16a00..76db4a6a 100644 --- a/Modules/Memory/popup.swift +++ b/Modules/Memory/popup.swift @@ -34,7 +34,7 @@ internal class Popup: NSView, Popup_p { private var compressedField: NSTextField? = nil private var chart: LineChartView? = nil - private var circle: CircleGraphView? = nil + private var circle: PieChartView? = nil private var level: PressureView? = nil private var initialized: Bool = false private var processesInitialized: Bool = false @@ -117,7 +117,12 @@ internal class Popup: NSView, Popup_p { let view: NSView = NSView(frame: NSRect(x: 0, y: self.frame.height - self.dashboardHeight, width: self.frame.width, height: self.dashboardHeight)) let container: NSView = NSView(frame: NSRect(x: 0, y: 10, width: view.frame.width, height: self.dashboardHeight-20)) - self.circle = CircleGraphView(frame: NSRect(x: (container.frame.width - container.frame.height)/2, y: 0, width: container.frame.height, height: container.frame.height), segments: []) + self.circle = PieChartView(frame: NSRect( + x: (container.frame.width - container.frame.height)/2, + y: 0, + width: container.frame.height, + height: container.frame.height + ), segments: [], drawValue: true) self.circle!.toolTip = LocalizedString("Memory usage") container.addSubview(self.circle!) diff --git a/StatsKit/Charts.swift b/StatsKit/Charts.swift index 668a2fc0..ad02ff0c 100644 --- a/StatsKit/Charts.swift +++ b/StatsKit/Charts.swift @@ -227,12 +227,18 @@ public class NetworkChartView: NSView { } } -public class CircleGraphView: NSView { +public class PieChartView: NSView { + private var filled: Bool = false + private var drawValue: Bool = false + private var value: Double? = nil private var segments: [circle_segment] = [] - public init(frame: NSRect, segments: [circle_segment]) { + public init(frame: NSRect, segments: [circle_segment], filled: Bool = false, drawValue: Bool = false) { + self.filled = filled + self.drawValue = drawValue self.segments = segments + super.init(frame: frame) } @@ -241,7 +247,7 @@ public class CircleGraphView: NSView { } public override func draw(_ rect: CGRect) { - let arcWidth: CGFloat = 7.0 + let arcWidth: CGFloat = self.filled ? min(rect.width, rect.height) / 2 : 7 let fullCircle = 2 * CGFloat.pi var segments = self.segments let totalAmount = segments.reduce(0) { $0 + $1.value } @@ -271,7 +277,7 @@ public class CircleGraphView: NSView { previousAngle = currentAngle } - if let value = self.value { + if let value = self.value, self.drawValue { let stringAttributes = [ NSAttributedString.Key.font: NSFont.systemFont(ofSize: 15, weight: .regular), NSAttributedString.Key.foregroundColor: isDarkMode ? NSColor.white : NSColor.textColor, @@ -280,7 +286,7 @@ public class CircleGraphView: NSView { let percentage = "\(Int(value*100))%" let width: CGFloat = percentage.widthOfString(usingFont: NSFont.systemFont(ofSize: 15)) - let rect = CGRect(x: (self.frame.width-width)/2, y: (self.frame.height-12)/2, width: width, height: 12) + let rect = CGRect(x: (self.frame.width-width)/2, y: (self.frame.height-11)/2, width: width, height: 12) let str = NSAttributedString.init(string: percentage, attributes: stringAttributes) str.draw(with: rect) }