fix: preinitialize the label string for the network widget (#1706)

This commit is contained in:
Serhiy Mytrovtsiy
2023-12-07 17:04:29 +01:00
parent de5b369839
commit 4b68b8e8e6

View File

@@ -53,6 +53,8 @@ public class NetworkChart: WidgetWrapper {
private var boxSettingsView: NSView? = nil
private var frameSettingsView: NSView? = nil
public var NSLabelCharts: [NSAttributedString] = []
public init(title: String, config: NSDictionary?, preview: Bool = false) {
var widgetTitle: String = title
if let config = config {
@@ -98,6 +100,19 @@ public class NetworkChart: WidgetWrapper {
}
self.chart.points = list
}
let style = NSMutableParagraphStyle()
style.alignment = .center
let stringAttributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
NSAttributedString.Key.foregroundColor: isDarkMode ? NSColor.white : NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
]
for char in String(title.prefix(3)).uppercased().reversed() {
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
self.NSLabelCharts.append(str)
}
}
required init?(coder: NSCoder) {
@@ -116,22 +131,13 @@ public class NetworkChart: WidgetWrapper {
var width = self.width + (Constants.Widget.margin.x*2)
if self.labelState {
let style = NSMutableParagraphStyle()
style.alignment = .center
let stringAttributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
]
let letterHeight = self.frame.height / 3
let letterWidth: CGFloat = 6.0
var yMargin: CGFloat = 0
for char in String(self.title.prefix(3)).uppercased().reversed() {
for char in self.NSLabelCharts {
let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight)
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
str.draw(with: rect)
char.draw(with: rect)
yMargin += letterHeight
}