Skip to content

Commit

Permalink
fix: preinitialize the label string for the network widget (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Dec 7, 2023
1 parent de5b369 commit 4b68b8e
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions Kit/Widgets/NetworkChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down

0 comments on commit 4b68b8e

Please sign in to comment.