feat: reload the app settings values when opening the app settings

This commit is contained in:
Serhiy Mytrovtsiy
2022-07-30 13:15:06 +02:00
parent c42c3c6e86
commit 309bf50f42
2 changed files with 28 additions and 9 deletions

View File

@@ -29,6 +29,8 @@ class ApplicationSettings: NSStackView {
}
private let updateWindow: UpdateWindow = UpdateWindow()
private var updateSelector: NSPopUpButton?
private var startAtLoginBtn: NSButton?
init() {
super.init(frame: NSRect(
@@ -53,6 +55,20 @@ class ApplicationSettings: NSStackView {
fatalError("init(coder:) has not been implemented")
}
public func viewWillAppear() {
self.startAtLoginBtn?.state = LaunchAtLogin.isEnabled ? .on : .off
var idx = self.updateSelector?.indexOfSelectedItem ?? 0
if let items = self.updateSelector?.menu?.items {
for (i, item) in items.enumerated() {
if let obj = item.representedObject as? String, obj == self.updateIntervalValue {
idx = i
}
}
}
self.updateSelector?.selectItem(at: idx)
}
private func informationView() -> NSView {
let view = NSStackView()
view.heightAnchor.constraint(equalToConstant: 240).isActive = true
@@ -118,13 +134,14 @@ class ApplicationSettings: NSStackView {
grid.setContentHuggingPriority(.defaultHigh, for: .horizontal)
grid.setContentHuggingPriority(.defaultHigh, for: .vertical)
self.updateSelector = selectView(
action: #selector(self.toggleUpdateInterval),
items: AppUpdateIntervals,
selected: self.updateIntervalValue
)
grid.addRow(with: [
self.titleView(localizedString("Check for updates")),
selectView(
action: #selector(self.toggleUpdateInterval),
items: AppUpdateIntervals,
selected: self.updateIntervalValue
)
self.updateSelector!
])
grid.addRow(with: [
self.titleView(localizedString("Temperature")),
@@ -139,11 +156,12 @@ class ApplicationSettings: NSStackView {
state: Store.shared.bool(key: "dockIcon", defaultValue: false),
text: localizedString("Show icon in dock")
)])
grid.addRow(with: [NSGridCell.emptyContentView, self.toggleView(
self.startAtLoginBtn = self.toggleView(
action: #selector(self.toggleLaunchAtLogin),
state: LaunchAtLogin.isEnabled,
text: localizedString("Start at login")
)])
)
grid.addRow(with: [NSGridCell.emptyContentView, self.startAtLoginBtn!])
view.addSubview(grid)
@@ -187,7 +205,7 @@ class ApplicationSettings: NSStackView {
return field
}
private func toggleView(action: Selector, state: Bool, text: String) -> NSView {
private func toggleView(action: Selector, state: Bool, text: String) -> NSButton {
let button: NSButton = NSButton(frame: NSRect(x: 0, y: 0, width: 30, height: 20))
button.setButtonType(.switch)
button.state = state ? .on : .off

View File

@@ -138,7 +138,7 @@ private class SettingsView: NSView {
private var mainView: NSView = NSView()
private var dashboard: NSView = Dashboard()
private var settings: NSView = ApplicationSettings()
private var settings: ApplicationSettings = ApplicationSettings()
private let supportPopover = NSPopover()
@@ -237,6 +237,7 @@ private class SettingsView: NSView {
} else if title == "Dashboard" {
view = self.dashboard
} else if title == "settings" {
self.settings.viewWillAppear()
view = self.settings
}