fix: fixed circles charts in the new version of Cocoa

This commit is contained in:
Serhiy Mytrovtsiy
2023-09-28 17:33:55 +02:00
parent 0394470e11
commit 48322fc7e8
8 changed files with 45 additions and 44 deletions

View File

@@ -335,17 +335,17 @@ public class NetworkChartView: NSView {
}
let lineWidth = 1 / (NSScreen.main?.backingScaleFactor ?? 1)
let zero: CGFloat = (dirtyRect.height/2) + dirtyRect.origin.y
let xRatio: CGFloat = (dirtyRect.width + (lineWidth*3)) / CGFloat(points.count)
let zero: CGFloat = (self.frame.height/2) + self.frame.origin.y
let xRatio: CGFloat = (self.frame.width + (lineWidth*3)) / CGFloat(points.count)
let columnXPoint = { (point: Int) -> CGFloat in
return (CGFloat(point) * xRatio) + (dirtyRect.origin.x - lineWidth)
return (CGFloat(point) * xRatio) + (self.frame.origin.x - lineWidth)
}
let uploadYPoint = { (point: Int) -> CGFloat in
return scaleValue(scale: self.scale, value: points[point].0, maxValue: uploadMax, maxHeight: dirtyRect.height/2) + (dirtyRect.height/2 + dirtyRect.origin.y)
return scaleValue(scale: self.scale, value: points[point].0, maxValue: uploadMax, maxHeight: self.frame.height/2) + (self.frame.height/2 + self.frame.origin.y)
}
let downloadYPoint = { (point: Int) -> CGFloat in
return (dirtyRect.height/2 + dirtyRect.origin.y) - scaleValue(scale: self.scale, value: points[point].1, maxValue: downloadMax, maxHeight: dirtyRect.height/2)
return (self.frame.height/2 + self.frame.origin.y) - scaleValue(scale: self.scale, value: points[point].1, maxValue: downloadMax, maxHeight: self.frame.height/2)
}
let uploadlinePath = NSBezierPath()
@@ -401,7 +401,7 @@ public class NetworkChartView: NSView {
let uploadTextWidth = uploadText.widthOfString(usingFont: stringAttributes[NSAttributedString.Key.font] as! NSFont)
let downloadTextWidth = downloadText.widthOfString(usingFont: stringAttributes[NSAttributedString.Key.font] as! NSFont)
var rect = CGRect(x: 1, y: dirtyRect.height - 9, width: uploadTextWidth, height: 8)
var rect = CGRect(x: 1, y: self.frame.height - 9, width: uploadTextWidth, height: 8)
NSAttributedString.init(string: uploadText, attributes: stringAttributes).draw(with: rect)
rect = CGRect(x: 1, y: 2, width: downloadTextWidth, height: 8)
@@ -480,7 +480,7 @@ public class PieChartView: NSView {
}
public override func draw(_ rect: CGRect) {
let arcWidth: CGFloat = self.filled ? min(rect.width, rect.height) / 2 : 7
let arcWidth: CGFloat = self.filled ? min(self.frame.width, self.frame.height) / 2 : 7
let fullCircle = 2 * CGFloat.pi
var segments = self.segments
let totalAmount = segments.reduce(0) { $0 + $1.value }
@@ -488,8 +488,8 @@ public class PieChartView: NSView {
segments.append(circle_segment(value: Double(1-totalAmount), color: self.nonActiveSegmentColor.withAlphaComponent(0.5)))
}
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
let radius = (min(rect.width, rect.height) - arcWidth) / 2
let centerPoint = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
let radius = (min(self.frame.width, self.frame.height) - arcWidth) / 2
guard let context = NSGraphicsContext.current?.cgContext else { return }
context.setShouldAntialias(true)
@@ -564,8 +564,8 @@ public class HalfCircleGraphView: NSView {
public override func draw(_ rect: CGRect) {
let arcWidth: CGFloat = 7.0
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
let radius = (min(rect.width, rect.height) - arcWidth) / 2
let radius = (min(self.frame.width, self.frame.height) - arcWidth) / 2
let centerPoint = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
guard let context = NSGraphicsContext.current?.cgContext else { return }
context.setShouldAntialias(true)
@@ -585,7 +585,7 @@ public class HalfCircleGraphView: NSView {
var previousAngle = startAngle
context.saveGState()
context.translateBy(x: rect.width, y: 0)
context.translateBy(x: self.frame.width, y: 0)
context.scaleBy(x: -1, y: 1)
for segment in segments {
@@ -647,22 +647,22 @@ public class TachometerGraphView: NSView {
}
public override func draw(_ rect: CGRect) {
let arcWidth: CGFloat = self.filled ? min(rect.width, rect.height) / 2 : 7
let arcWidth: CGFloat = self.filled ? min(self.frame.width, self.frame.height) / 2 : 7
var segments = self.segments
let totalAmount = segments.reduce(0) { $0 + $1.value }
if totalAmount < 1 {
segments.append(circle_segment(value: Double(1-totalAmount), color: NSColor.lightGray.withAlphaComponent(0.5)))
}
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
let radius = (min(rect.width, rect.height) - arcWidth) / 2
let centerPoint = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
let radius = (min(self.frame.width, self.frame.height) - arcWidth) / 2
guard let context = NSGraphicsContext.current?.cgContext else { return }
context.setShouldAntialias(true)
context.setLineWidth(arcWidth)
context.setLineCap(.butt)
context.translateBy(x: rect.width, y: -4)
context.translateBy(x: self.frame.width, y: -4)
context.scaleBy(x: -1, y: 1)
let startAngle: CGFloat = 0

View File

@@ -204,29 +204,30 @@ internal class Popup: PopupWrapper {
private func initDashboard() -> NSView {
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 = 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!)
let usageSize = self.dashboardHeight-20
let usageX = (view.frame.width - usageSize)/2
let centralWidth: CGFloat = self.dashboardHeight-20
let sideWidth: CGFloat = (view.frame.width - centralWidth - (Constants.Popup.margins*2))/2
self.temperatureCircle = HalfCircleGraphView(frame: NSRect(x: (sideWidth - 60)/2, y: 10, width: 60, height: 50))
let usage = NSView(frame: NSRect(x: usageX, y: (view.frame.height - usageSize)/2, width: usageSize, height: usageSize))
let temperature = NSView(frame: NSRect(x: (usageX - 50)/2, y: (view.frame.height - 50)/2 - 3, width: 50, height: 50))
let frequency = NSView(frame: NSRect(x: (usageX+usageSize) + (usageX - 50)/2, y: (view.frame.height - 50)/2 - 3, width: 50, height: 50))
self.circle = PieChartView(frame: NSRect(x: 0, y: 0, width: usage.frame.width, height: usage.frame.height), segments: [], drawValue: true)
self.circle!.toolTip = localizedString("CPU usage")
usage.addSubview(self.circle!)
self.temperatureCircle = HalfCircleGraphView(frame: NSRect(x: 0, y: 0, width: temperature.frame.width, height: temperature.frame.height))
self.temperatureCircle!.toolTip = localizedString("CPU temperature")
(self.temperatureCircle! as NSView).isHidden = true
temperature.addSubview(self.temperatureCircle!)
self.frequencyCircle = HalfCircleGraphView(frame: NSRect(x: view.frame.width - 60 - Constants.Popup.margins*2, y: 10, width: 60, height: 50))
self.frequencyCircle = HalfCircleGraphView(frame: NSRect(x: 0, y: 0, width: frequency.frame.width, height: frequency.frame.height))
self.frequencyCircle!.toolTip = localizedString("CPU frequency")
(self.frequencyCircle! as NSView).isHidden = true
frequency.addSubview(self.frequencyCircle!)
view.addSubview(self.temperatureCircle!)
view.addSubview(container)
view.addSubview(self.frequencyCircle!)
view.addSubview(temperature)
view.addSubview(usage)
view.addSubview(frequency)
return view
}

View File

@@ -46,7 +46,7 @@ internal class Popup: PopupWrapper {
private func recalculateHeight() {
let h = self.arrangedSubviews.map({ $0.bounds.height + self.spacing }).reduce(0, +) - self.spacing
if self.frame.size.height != h {
if self.frame.size.height != h && h >= 0 {
self.setFrameSize(NSSize(width: self.frame.width, height: h))
self.sizeCallback?(self.frame.size)
}
@@ -181,9 +181,7 @@ private class GPUView: NSStackView {
}
private func addStats(id: String, _ val: Double? = nil) {
guard let value = val else {
return
}
guard let value = val else { return }
var circle: HalfCircleGraphView
var chart: LineChartView
@@ -196,7 +194,7 @@ private class GPUView: NSStackView {
circle.toolTip = localizedString("GPU \(id)")
if let row = self.circleRow {
row.setFrameSize(NSSize(width: row.frame.width, height: self.circleSize + 20))
row.edgeInsets = NSEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
row.edgeInsets = NSEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
row.heightAnchor.constraint(equalToConstant: row.bounds.height).isActive = true
row.addArrangedSubview(circle)
}

View File

@@ -419,8 +419,8 @@ public class PressureView: NSView {
public override func draw(_ rect: CGRect) {
let arcWidth: CGFloat = 7.0
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
let radius = (min(rect.width, rect.height) - arcWidth) / 2
let centerPoint = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
let radius = (min(self.frame.width, self.frame.height) - arcWidth) / 2
guard let context = NSGraphicsContext.current?.cgContext else { return }
context.setShouldAntialias(true)
@@ -433,7 +433,7 @@ public class PressureView: NSView {
var previousAngle = startAngle
context.saveGState()
context.translateBy(x: rect.width, y: 0)
context.translateBy(x: self.frame.width, y: 0)
context.scaleBy(x: -1, y: 1)
for segment in self.segments {

View File

@@ -107,7 +107,7 @@ internal class Popup: PopupWrapper {
if let fan = f as? Fan {
let view = FanView(fan, width: self.frame.width) { [weak self] in
let h = container.arrangedSubviews.map({ $0.bounds.height + container.spacing }).reduce(0, +) - container.spacing
if container.frame.size.height != h {
if container.frame.size.height != h && h >= 0 {
container.setFrameSize(NSSize(width: container.frame.width, height: h))
}
self?.recalculateHeight()

View File

@@ -1407,7 +1407,7 @@
New,
);
LastSwiftUpdateCheck = 1410;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1500;
ORGANIZATIONNAME = "Serhiy Mytrovtsiy";
TargetAttributes = {
5C22299C29CCB3C400F00E69 = {
@@ -2234,6 +2234,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
@@ -2302,6 +2303,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"