- rename CircleGraphView to PieWidget

- add option to show/hide the value in PieWidget
- add option to fill full pie in PieWidget
This commit is contained in:
Serhiy Mytrovtsiy
2020-11-30 17:27:20 +01:00
parent 461328b52c
commit cb267ad89e
3 changed files with 25 additions and 9 deletions

View File

@@ -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!)

View File

@@ -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!)

View File

@@ -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)
}