feat: added an option to separate download and upload scale in the Network chart (#966)

fix: restore the scale in the Network chart after a restart
This commit is contained in:
Serhiy Mytrovtsiy
2022-08-30 18:18:18 +02:00
parent 13be2d03d7
commit 22c84ff305
2 changed files with 36 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ public class NetworkChart: WidgetWrapper {
private var downloadColor: Color = .secondRed
private var uploadColor: Color = .secondBlue
private var scaleState: Scale = .linear
private var commonScaleState: Bool = true
private var chart: NetworkChartView = NetworkChartView(
frame: NSRect(x: 0, y: 0, width: 30, height: Constants.Widget.height - (2*Constants.Widget.margin.y)),
@@ -80,11 +81,13 @@ public class NetworkChart: WidgetWrapper {
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))
self.commonScaleState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_commonScale", defaultValue: self.commonScaleState)
if let downloadColor = self.downloadColor.additional as? NSColor,
let uploadColor = self.uploadColor.additional as? NSColor {
self.chart.colors = [downloadColor, uploadColor]
}
self.chart.setScale(self.scaleState, self.commonScaleState)
self.chart.reinit(self.historyCount)
}
@@ -231,6 +234,12 @@ public class NetworkChart: WidgetWrapper {
selected: self.scaleState.key
))
view.addArrangedSubview(toggleSettingRow(
title: localizedString("Common scale"),
action: #selector(toggleCommonScale),
state: self.commonScaleState
))
return view
}
@@ -334,8 +343,22 @@ public class NetworkChart: WidgetWrapper {
guard let value = Scale.allCases.first(where: { $0.key == key }) else { return }
self.scaleState = value
self.chart.setScale(value)
self.chart.setScale(value, self.commonScaleState)
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_scale", value: key)
self.display()
}
@objc private func toggleCommonScale(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
} else {
state = sender is NSButton ? (sender as! NSButton).state: nil
}
self.commonScaleState = state! == .on ? true : false
self.chart.setScale(self.scaleState, self.commonScaleState)
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_commonScale", value: self.commonScaleState)
self.display()
}
}

View File

@@ -303,6 +303,7 @@ public class NetworkChartView: NSView {
private var minMax: Bool = false
private var scale: Scale = .none
private var commonScale: Bool = true
public init(frame: NSRect, num: Int, minMax: Bool = true) {
self.minMax = minMax
@@ -330,6 +331,14 @@ public class NetworkChartView: NSView {
downloadMax = 1
}
if !self.commonScale {
if downloadMax > uploadMax {
uploadMax = downloadMax
} else {
downloadMax = uploadMax
}
}
let lineWidth = 1 / (NSScreen.main?.backingScaleFactor ?? 1)
let offset = lineWidth / 2
let zero: CGFloat = (dirtyRect.height/2) + dirtyRect.origin.y
@@ -427,8 +436,10 @@ public class NetworkChartView: NSView {
}
}
public func setScale(_ newScale: Scale) {
public func setScale(_ newScale: Scale, _ commonScale: Bool) {
self.scale = newScale
self.commonScale = commonScale
if self.window?.isVisible ?? false {
self.display()
}