From c5ea8563dd9fc89387d70e2acb8b43d51d7da980 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 6 Apr 2024 11:35:21 +0200 Subject: [PATCH] feat: added chart scale setting to the RAM popup settings --- Modules/RAM/popup.swift | 46 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Modules/RAM/popup.swift b/Modules/RAM/popup.swift index 29e2c9ad..e195efcd 100644 --- a/Modules/RAM/popup.swift +++ b/Modules/RAM/popup.swift @@ -35,6 +35,7 @@ internal class Popup: PopupWrapper { private var wiredColorView: NSView? = nil private var compressedColorView: NSView? = nil private var freeColorView: NSView? = nil + private var sliderView: NSView? = nil private var chart: LineChartView? = nil private var circle: PieChartView? = nil @@ -52,6 +53,8 @@ internal class Popup: PopupWrapper { } private var lineChartHistory: Int = 180 + private var lineChartScale: Scale = .none + private var lineChartFixedScale: Double = 1 private var appColorState: Color = .secondBlue private var appColor: NSColor { self.appColorState.additional as? NSColor ?? NSColor.systemRed } @@ -81,6 +84,8 @@ internal class Popup: PopupWrapper { self.freeColorState = Color.fromString(Store.shared.string(key: "\(self.title)_freeColor", defaultValue: self.freeColorState.key)) self.chartColorState = Color.fromString(Store.shared.string(key: "\(self.title)_chartColor", defaultValue: self.chartColorState.key)) self.lineChartHistory = Store.shared.int(key: "\(self.title)_lineChartHistory", defaultValue: self.lineChartHistory) + self.lineChartScale = Scale.fromString(Store.shared.string(key: "\(self.title)_lineChartScale", defaultValue: self.lineChartScale.key)) + self.lineChartFixedScale = Double(Store.shared.int(key: "\(self.title)_lineChartFixedScale", defaultValue: 100)) / 100 let gridView: NSGridView = NSGridView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)) gridView.rowSpacing = 0 @@ -162,7 +167,8 @@ internal class Popup: PopupWrapper { container.layer?.backgroundColor = NSColor.lightGray.withAlphaComponent(0.1).cgColor container.layer?.cornerRadius = 3 - self.chart = LineChartView(frame: NSRect(x: 1, y: 0, width: view.frame.width, height: container.frame.height), num: self.lineChartHistory) + let chartFrame = NSRect(x: 1, y: 0, width: view.frame.width, height: container.frame.height) + self.chart = LineChartView(frame: chartFrame, num: self.lineChartHistory, scale: self.lineChartScale, fixedScale: self.lineChartFixedScale) self.chart?.color = self.chartColor container.addSubview(self.chart!) @@ -316,6 +322,23 @@ internal class Popup: PopupWrapper { selected: "\(self.lineChartHistory)" )) + view.addArrangedSubview(selectSettingsRow( + title: localizedString("Scaling"), + action: #selector(self.toggleLineChartScale), + items: Scale.allCases, + selected: self.lineChartScale.key + )) + + let slider = sliderSettingsRow( + title: localizedString("Fixed value"), + action: #selector(self.toggleLineChartFixedScale), + value: Int(self.lineChartFixedScale * 100), + suffix: "%", + isHidden: self.lineChartScale != .fixed + ) + self.sliderView = slider + view.addArrangedSubview(slider) + return view } @@ -380,6 +403,27 @@ internal class Popup: PopupWrapper { Store.shared.set(key: "\(self.title)_lineChartHistory", value: value) self.chart?.reinit(self.lineChartHistory) } + @objc private func toggleLineChartScale(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String, + let value = Scale.allCases.first(where: { $0.key == key }) else { return } + self.sliderView?.isHidden = value != .fixed + self.lineChartScale = value + self.chart?.setScale(self.lineChartScale, fixedScale: self.lineChartFixedScale) + Store.shared.set(key: "\(self.title)_lineChartScale", value: key) + self.display() + } + @objc private func toggleLineChartFixedScale(_ sender: NSSlider) { + let value = Int(sender.doubleValue) + + if let container = self.sliderView?.subviews.first(where: { $0.identifier == NSUserInterfaceItemIdentifier("container") }), + let field = container.subviews.first(where: { $0 is NSTextField }), let view = field as? NSTextField { + view.stringValue = "\(value) %" + } + + self.lineChartFixedScale = sender.doubleValue / 100 + self.chart?.setScale(self.lineChartScale, fixedScale: self.lineChartFixedScale) + Store.shared.set(key: "\(self.title)_lineChartFixedScale", value: value) + } } public class PressureView: NSView {