feat: moved all modules settings to the new design

This commit is contained in:
Serhiy Mytrovtsiy
2024-04-24 06:10:27 +02:00
parent ab4dcbfd94
commit f873fdaa3d
36 changed files with 901 additions and 1131 deletions

View File

@@ -93,5 +93,12 @@
<integer>6</integer>
</dict>
</dict>
<key>Settings</key>
<dict>
<key>popup</key>
<true/>
<key>notifications</key>
<true/>
</dict>
</dict>
</plist>

View File

@@ -57,30 +57,28 @@ class Notifications: NotificationsWrapper {
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.addArrangedSubview(selectSettingsRow(
title: localizedString("Usage"),
action: #selector(self.changeTotalUsage),
items: notificationLevels,
selected: self.totalUsageLevel
))
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Free memory (less than)"),
action: #selector(self.changeFree),
items: notificationLevels,
selected: self.freeLevel
))
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Memory pressure"),
action: #selector(self.changePressure),
items: memoryPressureLevels.filter({ $0.key != "normal" }),
selected: self.pressureLevel
))
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Swap size"),
action: #selector(self.changeSwap),
items: swapSizes,
selected: self.swapSize
))
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Usage"), component: selectView(
action: #selector(self.changeTotalUsage),
items: notificationLevels,
selected: self.totalUsageLevel
)),
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
))
]))
}
required init?(coder: NSCoder) {

View File

@@ -55,6 +55,7 @@ internal class Popup: PopupWrapper {
private var lineChartHistory: Int = 180
private var lineChartScale: Scale = .none
private var lineChartFixedScale: Double = 1
private var chartPrefSection: PreferencesSection? = nil
private var appColorState: Color = .secondBlue
private var appColor: NSColor { self.appColorState.additional as? NSColor ?? NSColor.systemRed }
@@ -284,60 +285,54 @@ internal class Popup: PopupWrapper {
public override func settings() -> NSView? {
let view = SettingsContainerView()
view.addArrangedSubview(selectSettingsRow(
title: localizedString("App color"),
action: #selector(toggleAppColor),
items: Color.allColors,
selected: self.appColorState.key
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Wired color"),
action: #selector(toggleWiredColor),
items: Color.allColors,
selected: self.wiredColorState.key
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Compressed color"),
action: #selector(toggleCompressedColor),
items: Color.allColors,
selected: self.compressedColorState.key
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Free color"),
action: #selector(toggleFreeColor),
items: Color.allColors,
selected: self.freeColorState.key
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Chart color"),
action: #selector(toggleChartColor),
items: Color.allColors,
selected: self.chartColorState.key
))
view.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("App color"), component: selectView(
action: #selector(toggleAppColor),
items: Color.allColors,
selected: self.appColorState.key
)),
PreferencesRow(localizedString("Wired color"), component: selectView(
action: #selector(toggleWiredColor),
items: Color.allColors,
selected: self.wiredColorState.key
)),
PreferencesRow(localizedString("Compressed color"), component: selectView(
action: #selector(toggleCompressedColor),
items: Color.allColors,
selected: self.compressedColorState.key
)),
PreferencesRow(localizedString("Free color"), component: selectView(
action: #selector(toggleFreeColor),
items: Color.allColors,
selected: self.freeColorState.key
))
]))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Chart duration"),
action: #selector(self.toggleLineChartHistory),
items: LineChartHistory,
selected: "\(self.lineChartHistory)"
))
view.addArrangedSubview(selectSettingsRow(
title: localizedString("Main chart scaling"),
action: #selector(self.toggleLineChartScale),
items: Scale.allCases,
selected: self.lineChartScale.key
))
let slider = sliderSettingsRow(
title: localizedString("Scale value"),
self.sliderView = sliderView(
action: #selector(self.toggleLineChartFixedScale),
value: Int(self.lineChartFixedScale * 100),
initialValue: "\(Int(self.lineChartFixedScale * 100)) %",
isHidden: self.lineChartScale != .fixed
initialValue: "\(Int(self.lineChartFixedScale * 100)) %"
)
self.sliderView = slider
view.addArrangedSubview(slider)
self.chartPrefSection = PreferencesSection([
PreferencesRow(localizedString("Chart color"), component: selectView(
action: #selector(self.toggleChartColor),
items: Color.allColors,
selected: self.chartColorState.key
)),
PreferencesRow(localizedString("Chart history"), component: selectView(
action: #selector(self.toggleLineChartHistory),
items: LineChartHistory,
selected: "\(self.lineChartHistory)"
)),
PreferencesRow(localizedString("Main chart scaling"), component: selectView(
action: #selector(self.toggleLineChartScale),
items: Scale.allCases,
selected: self.lineChartScale.key
)),
PreferencesRow(localizedString("Scale value"), component: self.sliderView!)
])
self.chartPrefSection?.toggleVisibility(3, newState: self.lineChartScale == .fixed)
view.addArrangedSubview(self.chartPrefSection!)
return view
}
@@ -406,7 +401,7 @@ internal class Popup: PopupWrapper {
@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.chartPrefSection?.toggleVisibility(3, newState: value == .fixed)
self.lineChartScale = value
self.chart?.setScale(self.lineChartScale, fixedScale: self.lineChartFixedScale)
Store.shared.set(key: "\(self.title)_lineChartScale", value: key)
@@ -415,8 +410,7 @@ internal class Popup: PopupWrapper {
@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 {
if let field = self.sliderView?.subviews.first(where: { $0 is NSTextField }), let view = field as? NSTextField {
view.stringValue = "\(value) %"
}

View File

@@ -34,16 +34,10 @@ internal class Settings: NSStackView, Settings_v {
self.splitValueState = Store.shared.bool(key: "\(self.title)_splitValue", defaultValue: self.splitValueState)
self.notificationLevel = Store.shared.string(key: "\(self.title)_notificationLevel", defaultValue: self.notificationLevel)
super.init(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
super.init(frame: NSRect.zero)
self.orientation = .vertical
self.distribution = .gravityAreas
self.edgeInsets = NSEdgeInsets(
top: Constants.Settings.margin,
left: Constants.Settings.margin,
bottom: Constants.Settings.margin,
right: Constants.Settings.margin
)
self.spacing = Constants.Settings.margin
}
@@ -54,41 +48,35 @@ internal class Settings: NSStackView, Settings_v {
public func load(widgets: [widget_t]) {
self.subviews.forEach{ $0.removeFromSuperview() }
self.addArrangedSubview(selectSettingsRowV1(
title: localizedString("Update interval"),
action: #selector(changeUpdateInterval),
items: ReaderUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateIntervalValue) sec"
))
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Update interval"), component: selectView(
action: #selector(self.changeUpdateInterval),
items: ReaderUpdateIntervals.map{ KeyValue_t(key: "\($0)", value: "\($0) sec") },
selected: "\(self.updateIntervalValue) sec"
)),
PreferencesRow(localizedString("Update interval for top processes"), component: selectView(
action: #selector(self.changeUpdateTopInterval),
items: ReaderUpdateIntervals.map{ KeyValue_t(key: "\($0)", value: "\($0) sec") },
selected: "\(self.updateTopIntervalValue) sec"
))
]))
self.addArrangedSubview(selectSettingsRowV1(
title: localizedString("Update interval for top processes"),
action: #selector(changeUpdateTopInterval),
items: ReaderUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateTopIntervalValue) sec"
))
self.addArrangedSubview(selectSettingsRowV1(
title: localizedString("Number of top processes"),
action: #selector(changeNumberOfProcesses),
items: NumbersOfProcesses.map{ "\($0)" },
selected: "\(self.numberOfProcesses)"
))
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Number of top processes"), component: selectView(
action: #selector(changeNumberOfProcesses),
items: NumbersOfProcesses.map{ KeyValue_t(key: "\($0)", value: "\($0)") },
selected: "\(self.numberOfProcesses)"
))
]))
if !widgets.filter({ $0 == .barChart }).isEmpty {
self.addArrangedSubview(toggleSettingRow(
title: localizedString("Split the value (App/Wired/Compressed)"),
action: #selector(toggleSplitValue),
state: self.splitValueState
))
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Split the value (App/Wired/Compressed)"), component: switchView(
action: #selector(toggleSplitValue),
state: self.splitValueState
))
]))
}
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Notification level"),
action: #selector(changeNotificationLevel),
items: notificationLevels,
selected: self.notificationLevel == "disabled" ? self.notificationLevel : "\(Int((Double(self.notificationLevel) ?? 0)*100))%"
))
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
@@ -98,7 +86,6 @@ internal class Settings: NSStackView, Settings_v {
self.setInterval(value)
}
}
@objc private func changeUpdateTopInterval(_ sender: NSMenuItem) {
if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) {
self.updateTopIntervalValue = value
@@ -106,7 +93,6 @@ internal class Settings: NSStackView, Settings_v {
self.setTopInterval(value)
}
}
@objc private func changeNumberOfProcesses(_ sender: NSMenuItem) {
if let value = Int(sender.title) {
self.numberOfProcesses = value
@@ -114,27 +100,9 @@ internal class Settings: NSStackView, Settings_v {
self.callbackWhenUpdateNumberOfProcesses()
}
}
@objc func toggleSplitValue(_ 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.splitValueState = state! == .on ? true : false
@objc private func toggleSplitValue(_ sender: NSControl) {
self.splitValueState = controlState(sender)
Store.shared.set(key: "\(self.title)_splitValue", value: self.splitValueState)
self.callback()
}
@objc func changeNotificationLevel(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else { return }
if key == "Disabled" {
Store.shared.set(key: "\(self.title)_notificationLevel", value: key)
} else if let value = Double(key.replacingOccurrences(of: "%", with: "")) {
Store.shared.set(key: "\(self.title)_notificationLevel", value: "\(value/100)")
}
}
}