feat: added an option to select a scaling for line chart and network chart widgets (#844)

This commit is contained in:
Serhiy Mytrovtsiy
2022-08-02 11:06:53 +02:00
parent 4635820530
commit 0ba2af6c3a
2 changed files with 43 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ public class LineChart: WidgetWrapper {
private var valueColorState: Bool = false
private var colorState: Color = .systemAccent
private var historyCount: Int = 60
private var scaleState: Scale = .none
private var chart: LineChartView = LineChartView(frame: NSRect(
x: 0,
@@ -98,7 +99,9 @@ public class LineChart: WidgetWrapper {
self.valueColorState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_valueColor", defaultValue: self.valueColorState)
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
self.historyCount = Store.shared.int(key: "\(self.title)_\(self.type.rawValue)_historyCount", defaultValue: self.historyCount)
self.scaleState = Scale.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_scale", defaultValue: self.scaleState.key))
self.chart.setScale(self.scaleState)
self.chart.reinit(self.historyCount)
}
@@ -304,6 +307,13 @@ public class LineChart: WidgetWrapper {
selected: "\(self.historyCount)"
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Scaling"),
action: #selector(toggleScale),
items: Scale.allCases,
selected: self.scaleState.key
))
return view
}
@@ -404,4 +414,16 @@ public class LineChart: WidgetWrapper {
self.chart.reinit(value)
self.display()
}
@objc private func toggleScale(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else {
return
}
guard let value = Scale.allCases.first(where: { $0.key == key }) else { return }
self.scaleState = value
self.chart.setScale(value)
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_scale", value: key)
self.display()
}
}

View File

@@ -18,6 +18,7 @@ public class NetworkChart: WidgetWrapper {
private var historyCount: Int = 60
private var downloadColor: Color = .secondRed
private var uploadColor: Color = .secondBlue
private var scaleState: Scale = .linear
private var chart: NetworkChartView = NetworkChartView(
frame: NSRect(x: 0, y: 0, width: 30, height: Constants.Widget.height - (2*Constants.Widget.margin.y)),
@@ -78,6 +79,7 @@ public class NetworkChart: WidgetWrapper {
self.historyCount = Store.shared.int(key: "\(self.title)_\(self.type.rawValue)_historyCount", defaultValue: self.historyCount)
self.downloadColor = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_downloadColor", defaultValue: self.downloadColor.key))
self.uploadColor = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_uploadColor", defaultValue: self.uploadColor.key))
self.scaleState = Scale.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_scale", defaultValue: self.scaleState.key))
if let downloadColor = self.downloadColor.additional as? NSColor,
let uploadColor = self.uploadColor.additional as? NSColor {
@@ -222,6 +224,13 @@ public class NetworkChart: WidgetWrapper {
selected: "\(self.historyCount)"
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Scaling"),
action: #selector(toggleScale),
items: Scale.allCases.filter({ $0 != .none && $0 != .separator }),
selected: self.scaleState.key
))
return view
}
@@ -317,4 +326,16 @@ public class NetworkChart: WidgetWrapper {
}
self.display()
}
@objc private func toggleScale(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else {
return
}
guard let value = Scale.allCases.first(where: { $0.key == key }) else { return }
self.scaleState = value
self.chart.setScale(value)
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_scale", value: key)
self.display()
}
}