- move temperature units toggle from Sensors module to general settings (#219)

This commit is contained in:
Serhiy Mytrovtsiy
2020-12-12 13:35:40 +01:00
parent 347be6a86d
commit b201cdf760
2 changed files with 41 additions and 25 deletions

View File

@@ -14,7 +14,6 @@ import StatsKit
import ModuleKit
internal class Settings: NSView, Settings_v {
private var unitsValue: String = "system"
private var updateIntervalValue: Int = 3
private let title: String
@@ -40,14 +39,13 @@ internal class Settings: NSView, Settings_v {
self.canDrawConcurrently = true
self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
self.unitsValue = store.pointee.string(key: "temperature_units", defaultValue: self.unitsValue)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func load(widget: widget_t) {
public func load(widget: widget_t) {
guard !self.list.pointee.isEmpty else {
return
}
@@ -61,7 +59,7 @@ internal class Settings: NSView, Settings_v {
}
let rowHeight: CGFloat = 30
let settingsHeight: CGFloat = (rowHeight*2) + Constants.Settings.margin
let settingsHeight: CGFloat = (rowHeight*1) + Constants.Settings.margin
let sensorsListHeight: CGFloat = (rowHeight+Constants.Settings.margin) * CGFloat(self.list.pointee.count) + ((rowHeight+Constants.Settings.margin) * CGFloat(types.count) + 1)
let height: CGFloat = settingsHeight + sensorsListHeight
let x: CGFloat = height < 360 ? 0 : Constants.Settings.margin
@@ -80,14 +78,6 @@ internal class Settings: NSView, Settings_v {
selected: "\(self.updateIntervalValue) sec"
))
self.addSubview(SelectRow(
frame: NSRect(x: Constants.Settings.margin, y: height - (rowHeight*2) - Constants.Settings.margin, width: view.frame.width, height: rowHeight),
title: LocalizedString("Temperature unit"),
action: #selector(changeUnits),
items: TemperatureUnits,
selected: self.unitsValue
))
var y: CGFloat = 0
types.reversed().forEach { (typ: SensorType_t) in
let filtered = self.list.pointee.filter{ $0.type == typ }
@@ -129,7 +119,7 @@ internal class Settings: NSView, Settings_v {
self.setFrameSize(NSSize(width: self.frame.width, height: height + (Constants.Settings.margin*1)))
}
@objc func handleSelection(_ sender: NSControl) {
@objc private func handleSelection(_ sender: NSControl) {
guard let id = sender.identifier else { return }
var state: NSControl.StateValue? = nil
@@ -150,13 +140,4 @@ internal class Settings: NSView, Settings_v {
self.setInterval(value)
}
}
@objc private func changeUnits(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else {
return
}
self.unitsValue = key
store.pointee.set(key: "temperature_units", value: key)
self.callback()
}
}

View File

@@ -24,6 +24,15 @@ class ApplicationSettings: NSView {
}
}
private var temperatureUnitsValue: String {
get {
return store.string(key: "temperature_units", defaultValue: "system")
}
set {
store.set(key: "temperature_units", value: newValue)
}
}
private var updateButton: NSButton? = nil
private let updateWindow: UpdateWindow = UpdateWindow()
@@ -103,13 +112,32 @@ class ApplicationSettings: NSView {
let rightPanel: NSView = NSView(frame: NSRect(x: self.width/2, y: 0, width: view.frame.width/2, height: view.frame.height))
rightPanel.addSubview(makeSelectRow(
frame: NSRect(x: rowHorizontalPadding*0.5, y: rowHeight*2, width: rightPanel.frame.width - (rowHorizontalPadding*1.5), height: rowHeight),
frame: NSRect(x: rowHorizontalPadding*0.5, y: rowHeight*3, width: rightPanel.frame.width - (rowHorizontalPadding*1.5), height: rowHeight),
title: LocalizedString("Check for updates"),
action: #selector(self.toggleUpdateInterval),
items: AppUpdateIntervals.allCases.map{ $0.rawValue },
selected: self.updateIntervalValue
))
let temperature = SelectRow(
frame: NSRect(
x: rowHorizontalPadding*0.5,
y: rowHeight*2,
width: rightPanel.frame.width - (rowHorizontalPadding*1.5),
height: rowHeight
),
title: LocalizedString("Temperature"),
action: #selector(toggleTemperatureUnits),
items: TemperatureUnits,
selected: self.temperatureUnitsValue
)
temperature.subviews.forEach { (v: NSView) in
if let view = v as? LabelField {
view.textColor = .secondaryLabelColor
}
}
rightPanel.addSubview(temperature)
rightPanel.addSubview(makeSettingRow(
frame: NSRect(x: rowHorizontalPadding*0.5, y: rowHeight*1, width: rightPanel.frame.width - (rowHorizontalPadding*1.5), height: rowHeight),
title: LocalizedString("Show icon in dock"),
@@ -205,7 +233,7 @@ class ApplicationSettings: NSView {
switchButton.state = state
switchButton.action = action
switchButton.target = self
toggle = switchButton
} else {
let button: NSButton = NSButton(frame: NSRect(x: row.frame.width - 30, y: 0, width: 30, height: row.frame.height))
@@ -219,7 +247,7 @@ class ApplicationSettings: NSView {
toggle = button
}
row.addSubview(toggle)
row.addSubview(rowTitle)
@@ -319,6 +347,13 @@ class ApplicationSettings: NSView {
}
}
@objc private func toggleTemperatureUnits(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else {
return
}
self.temperatureUnitsValue = key
}
@objc func toggleDock(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {