From 2b70064eb5a8b7d7028c6df4eb2cc0b8b1fcf102 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Thu, 9 May 2024 17:34:33 +0200 Subject: [PATCH] feat: moved RAM notification settings to the new selectors --- Kit/helpers.swift | 107 +++++++++++++------ Modules/CPU/notifications.swift | 10 +- Modules/GPU/notifications.swift | 2 +- Modules/RAM/notifications.swift | 177 ++++++++++++++++++++------------ 4 files changed, 189 insertions(+), 107 deletions(-) diff --git a/Kit/helpers.swift b/Kit/helpers.swift index 666e6d7d..e69673e6 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -205,6 +205,16 @@ public struct Units { return String(format: "%.0f KB", kilobytes) } } + + public func toUnit(_ unit: SizeUnit) -> Double { + switch unit { + case .KB: return self.kilobytes + case .MB: return self.megabytes + case .GB: return self.gigabytes + case .TB: return self.terabytes + default: return Double(self.bytes) + } + } } public struct DiskSize { @@ -1341,64 +1351,86 @@ public func restartApp(_ sender: Any, afterDelay seconds: TimeInterval = 0.5) -> } public class StepperInput: NSStackView, NSTextFieldDelegate, PreferencesSwitchWith_p { - public var callback: ((Int) -> Void) + private var callback: ((Int) -> Void) + private var unitCallback: ((KeyValue_p) -> Void) - private let value: NSTextField = NSTextField() - private let stepper: NSStepper = NSStepper() - private var symbol: NSTextField? = nil + private let valueView: NSTextField = NSTextField() + private let stepperView: NSStepper = NSStepper() + private var symbolView: NSTextField? = nil + private var unitsView: NSPopUpButton? = nil private let range: NSRange? + private var units: [KeyValue_p]? = nil private var _isEnabled: Bool = true public var isEnabled: Bool { get { self._isEnabled } set { - self.value.isEnabled = newValue - self.stepper.isEnabled = newValue - self.symbol?.isEnabled = newValue + self.valueView.isEnabled = newValue + self.stepperView.isEnabled = newValue + self.symbolView?.isEnabled = newValue + self.unitsView?.isEnabled = newValue self._isEnabled = newValue } } - public init(_ value: Int, range: NSRange? = nil, symbol: String? = nil, callback: @escaping (Int) -> Void = {_ in }) { + public init( + _ value: Int, + range: NSRange = NSRange(location: 1, length: 99), + unit: String = "%", + units: [KeyValue_p]? = nil, + callback: @escaping (Int) -> Void = {_ in }, + unitCallback: @escaping (KeyValue_p) -> Void = {_ in } + ) { self.range = range self.callback = callback + self.unitCallback = unitCallback super.init(frame: .zero) self.orientation = .horizontal self.spacing = 2 - self.value.font = NSFont.systemFont(ofSize: 12, weight: .regular) - self.value.textColor = .textColor - self.value.isEditable = true - self.value.isSelectable = true - self.value.usesSingleLineMode = true - self.value.maximumNumberOfLines = 1 - self.value.focusRingType = .none - self.value.delegate = self - self.value.stringValue = "\(value)" - self.value.translatesAutoresizingMaskIntoConstraints = false + self.valueView.font = NSFont.systemFont(ofSize: 12, weight: .regular) + self.valueView.textColor = .textColor + self.valueView.isEditable = true + self.valueView.isSelectable = true + self.valueView.usesSingleLineMode = true + self.valueView.maximumNumberOfLines = 1 + self.valueView.focusRingType = .none + self.valueView.delegate = self + self.valueView.stringValue = "\(value)" + self.valueView.translatesAutoresizingMaskIntoConstraints = false - self.stepper.font = NSFont.systemFont(ofSize: 12, weight: .regular) - self.stepper.doubleValue = Double(value)/100 - if let range { - self.stepper.minValue = Double(range.lowerBound)/100 - self.stepper.maxValue = Double(range.upperBound)/100 - } - self.stepper.increment = 0.01 - self.stepper.valueWraps = false - self.stepper.target = self - self.stepper.action = #selector(self.onStepperChange) + self.stepperView.font = NSFont.systemFont(ofSize: 12, weight: .regular) + self.stepperView.doubleValue = Double(value)/100 + self.stepperView.minValue = Double(range.lowerBound)/100 + self.stepperView.maxValue = Double(range.upperBound)/100 + self.stepperView.increment = 0.01 + self.stepperView.valueWraps = false + self.stepperView.target = self + self.stepperView.action = #selector(self.onStepperChange) - self.addArrangedSubview(self.value) - self.addArrangedSubview(self.stepper) + self.addArrangedSubview(self.valueView) + self.addArrangedSubview(self.stepperView) - if let symbol { - let symbol: NSTextField = LabelField(symbol) + if units == nil { + if unit == "%" { + self.widthAnchor.constraint(equalToConstant: 68).isActive = true + } + let symbol: NSTextField = LabelField(unit) symbol.textColor = .textColor self.addArrangedSubview(symbol) - self.symbol = symbol + self.symbolView = symbol + } else if let units { + self.units = units + self.unitsView = selectView( + action: #selector(self.onUnitChange), + items: units, + selected: unit + ) + self.addArrangedSubview(self.unitsView!) + self.widthAnchor.constraint(equalToConstant: 124).isActive = true } } @@ -1429,14 +1461,21 @@ public class StepperInput: NSStackView, NSTextFieldDelegate, PreferencesSwitchWi @objc private func onStepperChange(_ sender: NSStepper) { let value = Int(sender.doubleValue*100) - self.value.stringValue = "\(value)" + self.valueView.stringValue = "\(value)" self.callback(value) } + + @objc private func onUnitChange(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String, let units = self.units, + let value = units.first(where: { $0.key == key }) else { return } + self.unitCallback(value) + } } public protocol PreferencesSwitchWith_p: NSView { var isEnabled: Bool { get set } } +extension NSPopUpButton: PreferencesSwitchWith_p {} public class PreferencesSwitch: NSStackView { private let action: (_ sender: NSControl) -> Void private let with: PreferencesSwitchWith_p diff --git a/Modules/CPU/notifications.swift b/Modules/CPU/notifications.swift index 1a1dfb58..31dda77c 100644 --- a/Modules/CPU/notifications.swift +++ b/Modules/CPU/notifications.swift @@ -91,15 +91,15 @@ class Notifications: NotificationsWrapper { self.addArrangedSubview(PreferencesSection([ PreferencesRow(localizedString("Total load"), component: PreferencesSwitch( action: self.toggleTotalLoad, state: self.totalLoadState, - with: StepperInput(self.totalLoad, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changeTotalLoad) + with: StepperInput(self.totalLoad, callback: self.changeTotalLoad) )), PreferencesRow(localizedString("System load"), component: PreferencesSwitch( action: self.toggleSystemLoad, state: self.systemLoadState, - with: StepperInput(self.systemLoad, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changeSystemLoad) + with: StepperInput(self.systemLoad, callback: self.changeSystemLoad) )), PreferencesRow(localizedString("User load"), component: PreferencesSwitch( action: self.toggleUserLoad, state: self.userLoadState, - with: StepperInput(self.userLoad, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changeUserLoad) + with: StepperInput(self.userLoad, callback: self.changeUserLoad) )) ])) @@ -107,11 +107,11 @@ class Notifications: NotificationsWrapper { self.addArrangedSubview(PreferencesSection([ PreferencesRow(localizedString("Efficiency cores load"), component: PreferencesSwitch( action: self.toggleECoresLoad, state: self.eCoresLoadState, - with: StepperInput(self.eCoresLoad, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changeECoresLoad) + with: StepperInput(self.eCoresLoad, callback: self.changeECoresLoad) )), PreferencesRow(localizedString("Performance cores load"), component: PreferencesSwitch( action: self.togglePCoresLoad, state: self.pCoresLoadState, - with: StepperInput(self.pCoresLoad, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changePCoresLoad) + with: StepperInput(self.pCoresLoad, callback: self.changePCoresLoad) )) ])) #endif diff --git a/Modules/GPU/notifications.swift b/Modules/GPU/notifications.swift index d26b9d42..cc1b8c3f 100644 --- a/Modules/GPU/notifications.swift +++ b/Modules/GPU/notifications.swift @@ -36,7 +36,7 @@ class Notifications: NotificationsWrapper { self.addArrangedSubview(PreferencesSection([ PreferencesRow(localizedString("Usage"), component: PreferencesSwitch( action: self.toggleUsage, state: self.usageState, - with: StepperInput(self.usageLevel, range: NSRange(location: 1, length: 99), symbol: "%", callback: self.changeUsage) + with: StepperInput(self.usageLevel, callback: self.changeUsage) )) ])) } diff --git a/Modules/RAM/notifications.swift b/Modules/RAM/notifications.swift index dfac72b5..dbe30e90 100644 --- a/Modules/RAM/notifications.swift +++ b/Modules/RAM/notifications.swift @@ -13,70 +13,95 @@ import Cocoa import Kit internal let memoryPressureLevels: [KeyValue_t] = [ - KeyValue_t(key: "", value: "Disabled"), KeyValue_t(key: "normal", value: "Normal", additional: DispatchSource.MemoryPressureEvent.normal), KeyValue_t(key: "warning", value: "Warning", additional: DispatchSource.MemoryPressureEvent.warning), KeyValue_t(key: "critical", value: "Critical", additional: DispatchSource.MemoryPressureEvent.critical) ] -internal let swapSizes: [KeyValue_t] = [ - KeyValue_t(key: "", value: "Disabled"), - KeyValue_t(key: "512", value: "0.5 GB"), - KeyValue_t(key: "1024", value: "1.0 GB"), - KeyValue_t(key: "1536", value: "1.5 GB"), - KeyValue_t(key: "2048", value: "2.0 GB"), - KeyValue_t(key: "2560", value: "2.5 GB"), - KeyValue_t(key: "5120", value: "5.0 GB"), - KeyValue_t(key: "7680", value: "7.5 GB"), - KeyValue_t(key: "10240", value: "10 GB"), - KeyValue_t(key: "16384", value: "16 GB") -] - class Notifications: NotificationsWrapper { - private let totalUsageID: String = "totalUsage" + private let totalID: String = "totalUsage" private let freeID: String = "free" private let pressureID: String = "pressure" private let swapID: String = "swap" - private var totalUsageLevel: String = "" - private var freeLevel: String = "" - private var pressureLevel: String = "" - private var swapSize: String = "" + private var totalState: Bool = false + private var freeState: Bool = false + private var pressureState: Bool = false + private var swapState: Bool = false + + private var total: Int = 75 + private var free: Int = 75 + private var pressure: String = "" + private var swap: Int = 1 + private var swapUnit: SizeUnit = .GB public init(_ module: ModuleType) { - super.init(module, [self.totalUsageID, self.freeID, self.pressureID, self.swapID]) + super.init(module, [self.totalID, self.freeID, self.pressureID, self.swapID]) - if Store.shared.exist(key: "\(self.module)_notificationLevel") { - let value = Store.shared.string(key: "\(self.module)_notificationLevel", defaultValue: self.totalUsageLevel) - Store.shared.set(key: "\(self.module)_notifications_totalUsage", value: value) - Store.shared.remove("\(self.module)_notificationLevel") + if Store.shared.exist(key: "\(self.module)_notifications_totalUsage") { + let value = Store.shared.string(key: "\(self.module)_notifications_totalUsage", defaultValue: "") + if let v = Double(value) { + Store.shared.set(key: "\(self.module)_notifications_total_state", value: true) + Store.shared.set(key: "\(self.module)_notifications_total_value", value: Int(v*100)) + Store.shared.remove("\(self.module)_notifications_totalUsage") + } + } + if Store.shared.exist(key: "\(self.module)_notifications_free") { + let value = Store.shared.string(key: "\(self.module)_notifications_free", defaultValue: "") + if let v = Double(value) { + Store.shared.set(key: "\(self.module)_notifications_free_state", value: true) + Store.shared.set(key: "\(self.module)_notifications_free_value", value: Int(v*100)) + Store.shared.remove("\(self.module)_notifications_free") + } + } + if Store.shared.exist(key: "\(self.module)_notifications_pressure") { + let value = Store.shared.string(key: "\(self.module)_notifications_pressure", defaultValue: "") + if value != "" { + Store.shared.set(key: "\(self.module)_notifications_pressure_state", value: true) + Store.shared.set(key: "\(self.module)_notifications_pressure_value", value: value) + Store.shared.remove("\(self.module)_notifications_pressure") + } + } + if Store.shared.exist(key: "\(self.module)_notifications_swap") { + let value = Store.shared.string(key: "\(self.module)_notifications_swap", defaultValue: "") + if value != "" { + Store.shared.set(key: "\(self.module)_notifications_swap_state", value: true) + Store.shared.remove("\(self.module)_notifications_swap") + } } - self.totalUsageLevel = Store.shared.string(key: "\(self.module)_notifications_totalUsage", defaultValue: self.totalUsageLevel) - self.freeLevel = Store.shared.string(key: "\(self.module)_notifications_free", defaultValue: self.freeLevel) - self.pressureLevel = Store.shared.string(key: "\(self.module)_notifications_pressure", defaultValue: self.pressureLevel) - self.swapSize = Store.shared.string(key: "\(self.module)_notifications_swap", defaultValue: self.swapSize) + self.totalState = Store.shared.bool(key: "\(self.module)_notifications_total_state", defaultValue: self.totalState) + self.total = Store.shared.int(key: "\(self.module)_notifications_total_value", defaultValue: self.total) + self.freeState = Store.shared.bool(key: "\(self.module)_notifications_free_state", defaultValue: self.freeState) + self.free = Store.shared.int(key: "\(self.module)_notifications_free_value", defaultValue: self.free) + self.pressureState = Store.shared.bool(key: "\(self.module)_notifications_pressure_state", defaultValue: self.pressureState) + self.pressure = Store.shared.string(key: "\(self.module)_notifications_pressure_value", defaultValue: self.pressure) + self.swapState = Store.shared.bool(key: "\(self.module)_notifications_swap_state", defaultValue: self.swapState) + self.swap = Store.shared.int(key: "\(self.module)_notifications_swap_value", defaultValue: self.swap) + self.swapUnit = SizeUnit.fromString(Store.shared.string(key: "\(self.module)_notifications_swap_unit", defaultValue: self.swapUnit.key)) self.addArrangedSubview(PreferencesSection([ - PreferencesRow(localizedString("Usage"), component: selectView( - action: #selector(self.changeTotalUsage), - items: notificationLevels, - selected: self.totalUsageLevel + PreferencesRow(localizedString("Usage"), component: PreferencesSwitch( + action: self.toggleTotal, state: self.totalState, with: StepperInput(self.total, callback: self.changeTotal) )), - PreferencesRow(localizedString("Free memory (less than)"), component: selectView( - action: #selector(self.changeFree), - items: notificationLevels, - selected: self.freeLevel - )), - PreferencesRow(localizedString("Memory pressure"), component: selectView( - action: #selector(self.changePressure), - items: memoryPressureLevels.filter({ $0.key != "normal" }), - selected: self.pressureLevel - )), - PreferencesRow(localizedString("Swap size"), component: selectView( - action: #selector(self.changeSwap), - items: swapSizes, - selected: self.swapSize + PreferencesRow(localizedString("Free memory (less than)"), component: PreferencesSwitch( + action: self.toggleFree, state: self.freeState, with: StepperInput(self.free, callback: self.changeFree) + )) + ])) + + self.addArrangedSubview(PreferencesSection([ + PreferencesRow(localizedString("Memory pressure"), component: PreferencesSwitch( + action: self.togglePressure, state: self.pressureState, + with: selectView(action: #selector(self.changePressure), items: memoryPressureLevels, selected: self.pressure) + )) + ])) + + self.addArrangedSubview(PreferencesSection([ + PreferencesRow(localizedString("Swap size"), component: PreferencesSwitch( + action: self.toggleSwap, state: self.swapState, with: StepperInput( + self.swap, range: NSRange(location: 1, length: 1023), unit: self.swapUnit.key, units: SizeUnit.allCases, + callback: self.changeSwap, unitCallback: self.changeSwapUnit + ) )) ])) } @@ -88,17 +113,17 @@ class Notifications: NotificationsWrapper { internal func loadCallback(_ value: RAM_Usage) { let title = localizedString("RAM utilization threshold") - if let threshold = Double(self.totalUsageLevel) { + if self.totalState { let subtitle = localizedString("RAM utilization is", "\(Int((value.usage)*100))%") - self.checkDouble(id: self.totalUsageID, value: value.usage, threshold: threshold, title: title, subtitle: subtitle) + self.checkDouble(id: self.totalID, value: value.usage, threshold: Double(self.total)/100, title: title, subtitle: subtitle) } - if let threshold = Double(self.freeLevel) { + if self.freeState { let free = value.free / value.total let subtitle = localizedString("Free RAM is", "\(Int((free)*100))%") - self.checkDouble(id: self.freeID, value: free, threshold: threshold, title: title, subtitle: subtitle, less: true) + self.checkDouble(id: self.freeID, value: free, threshold: Double(self.free)/100, title: title, subtitle: subtitle, less: true) } - if self.pressureLevel != "", let thresholdPair = memoryPressureLevels.first(where: {$0.key == self.pressureLevel}) { + if self.pressureState, self.pressure != "", let thresholdPair = memoryPressureLevels.first(where: {$0.key == self.pressure}) { if let threshold = thresholdPair.additional as? DispatchSource.MemoryPressureEvent { self.checkDouble( id: self.pressureID, @@ -110,34 +135,52 @@ class Notifications: NotificationsWrapper { } } - if let threshold = Double(self.swapSize) { + if self.swapState { let value = Units(bytes: Int64(value.swap.used)) let subtitle = "\(localizedString("Swap size")): \(value.getReadableMemory())" - self.checkDouble(id: self.freeID, value: value.megabytes, threshold: threshold, title: title, subtitle: subtitle) + self.checkDouble(id: self.freeID, value: value.toUnit(self.swapUnit), threshold: Double(self.swap), title: title, subtitle: subtitle) } } - @objc private func changeTotalUsage(_ sender: NSMenuItem) { - guard let key = sender.representedObject as? String else { return } - self.totalUsageLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" - Store.shared.set(key: "\(self.module)_notifications_totalUsage", value: self.totalUsageLevel) + @objc private func toggleTotal(_ sender: NSControl) { + self.totalState = controlState(sender) + Store.shared.set(key: "\(self.module)_notifications_total_state", value: self.totalState) + } + @objc private func changeTotal(_ newValue: Int) { + self.total = newValue + Store.shared.set(key: "\(self.module)_notifications_total_value", value: self.total) } - @objc private func changeFree(_ sender: NSMenuItem) { - guard let key = sender.representedObject as? String else { return } - self.freeLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" - Store.shared.set(key: "\(self.module)_notifications_free", value: self.freeLevel) + @objc private func toggleFree(_ sender: NSControl) { + self.freeState = controlState(sender) + Store.shared.set(key: "\(self.module)_notifications_free_state", value: self.freeState) + } + @objc private func changeFree(_ newValue: Int) { + self.free = newValue + Store.shared.set(key: "\(self.module)_notifications_free_value", value: self.free) } + @objc private func togglePressure(_ sender: NSControl) { + self.pressureState = controlState(sender) + Store.shared.set(key: "\(self.module)_notifications_pressure_state", value: self.pressureState) + } @objc private func changePressure(_ sender: NSMenuItem) { guard let key = sender.representedObject as? String else { return } - self.pressureLevel = key - Store.shared.set(key: "\(self.module)_notifications_pressure", value: self.pressureLevel) + self.pressure = key + Store.shared.set(key: "\(self.module)_notifications_pressure_value", value: self.pressure) } - @objc private func changeSwap(_ sender: NSMenuItem) { - guard let key = sender.representedObject as? String else { return } - self.swapSize = key - Store.shared.set(key: "\(self.module)_notifications_swap", value: self.swapSize) + @objc private func toggleSwap(_ sender: NSControl) { + self.swapState = controlState(sender) + Store.shared.set(key: "\(self.module)_notifications_swap_state", value: self.swapState) + } + @objc private func changeSwap(_ newValue: Int) { + self.swap = newValue + Store.shared.set(key: "\(self.module)_notifications_swap_value", value: self.swap) + } + private func changeSwapUnit(_ newValue: KeyValue_p) { + guard let newUnit = newValue as? SizeUnit else { return } + self.swapUnit = newUnit + Store.shared.set(key: "\(self.module)_notifications_swap_unit", value: self.swapUnit.key) } }