feat: moved RAM notification settings to the new selectors

This commit is contained in:
Serhiy Mytrovtsiy
2024-05-09 17:34:33 +02:00
parent eb882774bb
commit 2b70064eb5
4 changed files with 189 additions and 107 deletions

View File

@@ -205,6 +205,16 @@ public struct Units {
return String(format: "%.0f KB", kilobytes) 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 { 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 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 valueView: NSTextField = NSTextField()
private let stepper: NSStepper = NSStepper() private let stepperView: NSStepper = NSStepper()
private var symbol: NSTextField? = nil private var symbolView: NSTextField? = nil
private var unitsView: NSPopUpButton? = nil
private let range: NSRange? private let range: NSRange?
private var units: [KeyValue_p]? = nil
private var _isEnabled: Bool = true private var _isEnabled: Bool = true
public var isEnabled: Bool { public var isEnabled: Bool {
get { self._isEnabled } get { self._isEnabled }
set { set {
self.value.isEnabled = newValue self.valueView.isEnabled = newValue
self.stepper.isEnabled = newValue self.stepperView.isEnabled = newValue
self.symbol?.isEnabled = newValue self.symbolView?.isEnabled = newValue
self.unitsView?.isEnabled = newValue
self._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.range = range
self.callback = callback self.callback = callback
self.unitCallback = unitCallback
super.init(frame: .zero) super.init(frame: .zero)
self.orientation = .horizontal self.orientation = .horizontal
self.spacing = 2 self.spacing = 2
self.value.font = NSFont.systemFont(ofSize: 12, weight: .regular) self.valueView.font = NSFont.systemFont(ofSize: 12, weight: .regular)
self.value.textColor = .textColor self.valueView.textColor = .textColor
self.value.isEditable = true self.valueView.isEditable = true
self.value.isSelectable = true self.valueView.isSelectable = true
self.value.usesSingleLineMode = true self.valueView.usesSingleLineMode = true
self.value.maximumNumberOfLines = 1 self.valueView.maximumNumberOfLines = 1
self.value.focusRingType = .none self.valueView.focusRingType = .none
self.value.delegate = self self.valueView.delegate = self
self.value.stringValue = "\(value)" self.valueView.stringValue = "\(value)"
self.value.translatesAutoresizingMaskIntoConstraints = false self.valueView.translatesAutoresizingMaskIntoConstraints = false
self.stepper.font = NSFont.systemFont(ofSize: 12, weight: .regular) self.stepperView.font = NSFont.systemFont(ofSize: 12, weight: .regular)
self.stepper.doubleValue = Double(value)/100 self.stepperView.doubleValue = Double(value)/100
if let range { self.stepperView.minValue = Double(range.lowerBound)/100
self.stepper.minValue = Double(range.lowerBound)/100 self.stepperView.maxValue = Double(range.upperBound)/100
self.stepper.maxValue = Double(range.upperBound)/100 self.stepperView.increment = 0.01
} self.stepperView.valueWraps = false
self.stepper.increment = 0.01 self.stepperView.target = self
self.stepper.valueWraps = false self.stepperView.action = #selector(self.onStepperChange)
self.stepper.target = self
self.stepper.action = #selector(self.onStepperChange)
self.addArrangedSubview(self.value) self.addArrangedSubview(self.valueView)
self.addArrangedSubview(self.stepper) self.addArrangedSubview(self.stepperView)
if let symbol { if units == nil {
let symbol: NSTextField = LabelField(symbol) if unit == "%" {
self.widthAnchor.constraint(equalToConstant: 68).isActive = true
}
let symbol: NSTextField = LabelField(unit)
symbol.textColor = .textColor symbol.textColor = .textColor
self.addArrangedSubview(symbol) 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) { @objc private func onStepperChange(_ sender: NSStepper) {
let value = Int(sender.doubleValue*100) let value = Int(sender.doubleValue*100)
self.value.stringValue = "\(value)" self.valueView.stringValue = "\(value)"
self.callback(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 { public protocol PreferencesSwitchWith_p: NSView {
var isEnabled: Bool { get set } var isEnabled: Bool { get set }
} }
extension NSPopUpButton: PreferencesSwitchWith_p {}
public class PreferencesSwitch: NSStackView { public class PreferencesSwitch: NSStackView {
private let action: (_ sender: NSControl) -> Void private let action: (_ sender: NSControl) -> Void
private let with: PreferencesSwitchWith_p private let with: PreferencesSwitchWith_p

View File

@@ -91,15 +91,15 @@ class Notifications: NotificationsWrapper {
self.addArrangedSubview(PreferencesSection([ self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Total load"), component: PreferencesSwitch( PreferencesRow(localizedString("Total load"), component: PreferencesSwitch(
action: self.toggleTotalLoad, state: self.totalLoadState, 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( PreferencesRow(localizedString("System load"), component: PreferencesSwitch(
action: self.toggleSystemLoad, state: self.systemLoadState, 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( PreferencesRow(localizedString("User load"), component: PreferencesSwitch(
action: self.toggleUserLoad, state: self.userLoadState, 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([ self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Efficiency cores load"), component: PreferencesSwitch( PreferencesRow(localizedString("Efficiency cores load"), component: PreferencesSwitch(
action: self.toggleECoresLoad, state: self.eCoresLoadState, 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( PreferencesRow(localizedString("Performance cores load"), component: PreferencesSwitch(
action: self.togglePCoresLoad, state: self.pCoresLoadState, 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 #endif

View File

@@ -36,7 +36,7 @@ class Notifications: NotificationsWrapper {
self.addArrangedSubview(PreferencesSection([ self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Usage"), component: PreferencesSwitch( PreferencesRow(localizedString("Usage"), component: PreferencesSwitch(
action: self.toggleUsage, state: self.usageState, 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)
)) ))
])) ]))
} }

View File

@@ -13,70 +13,95 @@ import Cocoa
import Kit import Kit
internal let memoryPressureLevels: [KeyValue_t] = [ internal let memoryPressureLevels: [KeyValue_t] = [
KeyValue_t(key: "", value: "Disabled"),
KeyValue_t(key: "normal", value: "Normal", additional: DispatchSource.MemoryPressureEvent.normal), KeyValue_t(key: "normal", value: "Normal", additional: DispatchSource.MemoryPressureEvent.normal),
KeyValue_t(key: "warning", value: "Warning", additional: DispatchSource.MemoryPressureEvent.warning), KeyValue_t(key: "warning", value: "Warning", additional: DispatchSource.MemoryPressureEvent.warning),
KeyValue_t(key: "critical", value: "Critical", additional: DispatchSource.MemoryPressureEvent.critical) 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 { class Notifications: NotificationsWrapper {
private let totalUsageID: String = "totalUsage" private let totalID: String = "totalUsage"
private let freeID: String = "free" private let freeID: String = "free"
private let pressureID: String = "pressure" private let pressureID: String = "pressure"
private let swapID: String = "swap" private let swapID: String = "swap"
private var totalUsageLevel: String = "" private var totalState: Bool = false
private var freeLevel: String = "" private var freeState: Bool = false
private var pressureLevel: String = "" private var pressureState: Bool = false
private var swapSize: String = "" 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) { 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") { if Store.shared.exist(key: "\(self.module)_notifications_totalUsage") {
let value = Store.shared.string(key: "\(self.module)_notificationLevel", defaultValue: self.totalUsageLevel) let value = Store.shared.string(key: "\(self.module)_notifications_totalUsage", defaultValue: "")
Store.shared.set(key: "\(self.module)_notifications_totalUsage", value: value) if let v = Double(value) {
Store.shared.remove("\(self.module)_notificationLevel") 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.totalState = Store.shared.bool(key: "\(self.module)_notifications_total_state", defaultValue: self.totalState)
self.freeLevel = Store.shared.string(key: "\(self.module)_notifications_free", defaultValue: self.freeLevel) self.total = Store.shared.int(key: "\(self.module)_notifications_total_value", defaultValue: self.total)
self.pressureLevel = Store.shared.string(key: "\(self.module)_notifications_pressure", defaultValue: self.pressureLevel) self.freeState = Store.shared.bool(key: "\(self.module)_notifications_free_state", defaultValue: self.freeState)
self.swapSize = Store.shared.string(key: "\(self.module)_notifications_swap", defaultValue: self.swapSize) 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([ self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Usage"), component: selectView( PreferencesRow(localizedString("Usage"), component: PreferencesSwitch(
action: #selector(self.changeTotalUsage), action: self.toggleTotal, state: self.totalState, with: StepperInput(self.total, callback: self.changeTotal)
items: notificationLevels,
selected: self.totalUsageLevel
)), )),
PreferencesRow(localizedString("Free memory (less than)"), component: selectView( PreferencesRow(localizedString("Free memory (less than)"), component: PreferencesSwitch(
action: #selector(self.changeFree), action: self.toggleFree, state: self.freeState, with: StepperInput(self.free, callback: self.changeFree)
items: notificationLevels, ))
selected: self.freeLevel ]))
)),
PreferencesRow(localizedString("Memory pressure"), component: selectView( self.addArrangedSubview(PreferencesSection([
action: #selector(self.changePressure), PreferencesRow(localizedString("Memory pressure"), component: PreferencesSwitch(
items: memoryPressureLevels.filter({ $0.key != "normal" }), action: self.togglePressure, state: self.pressureState,
selected: self.pressureLevel with: selectView(action: #selector(self.changePressure), items: memoryPressureLevels, selected: self.pressure)
)), ))
PreferencesRow(localizedString("Swap size"), component: selectView( ]))
action: #selector(self.changeSwap),
items: swapSizes, self.addArrangedSubview(PreferencesSection([
selected: self.swapSize 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) { internal func loadCallback(_ value: RAM_Usage) {
let title = localizedString("RAM utilization threshold") 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))%") 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 free = value.free / value.total
let subtitle = localizedString("Free RAM is", "\(Int((free)*100))%") 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 { if let threshold = thresholdPair.additional as? DispatchSource.MemoryPressureEvent {
self.checkDouble( self.checkDouble(
id: self.pressureID, 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 value = Units(bytes: Int64(value.swap.used))
let subtitle = "\(localizedString("Swap size")): \(value.getReadableMemory())" 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) { @objc private func toggleTotal(_ sender: NSControl) {
guard let key = sender.representedObject as? String else { return } self.totalState = controlState(sender)
self.totalUsageLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" Store.shared.set(key: "\(self.module)_notifications_total_state", value: self.totalState)
Store.shared.set(key: "\(self.module)_notifications_totalUsage", value: self.totalUsageLevel) }
@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) { @objc private func toggleFree(_ sender: NSControl) {
guard let key = sender.representedObject as? String else { return } self.freeState = controlState(sender)
self.freeLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" Store.shared.set(key: "\(self.module)_notifications_free_state", value: self.freeState)
Store.shared.set(key: "\(self.module)_notifications_free", value: self.freeLevel) }
@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) { @objc private func changePressure(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else { return } guard let key = sender.representedObject as? String else { return }
self.pressureLevel = key self.pressure = key
Store.shared.set(key: "\(self.module)_notifications_pressure", value: self.pressureLevel) Store.shared.set(key: "\(self.module)_notifications_pressure_value", value: self.pressure)
} }
@objc private func changeSwap(_ sender: NSMenuItem) { @objc private func toggleSwap(_ sender: NSControl) {
guard let key = sender.representedObject as? String else { return } self.swapState = controlState(sender)
self.swapSize = key Store.shared.set(key: "\(self.module)_notifications_swap_state", value: self.swapState)
Store.shared.set(key: "\(self.module)_notifications_swap", value: self.swapSize) }
@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)
} }
} }