feat: added an option to synchronize the fans (the mode only for now) control (#975)

This commit is contained in:
Serhiy Mytrovtsiy
2022-06-27 19:15:48 +02:00
parent 93f63e7830
commit cc96226008
4 changed files with 59 additions and 5 deletions

View File

@@ -198,13 +198,10 @@ public extension Notification.Name {
static let togglePopup = Notification.Name("togglePopup")
static let toggleWidget = Notification.Name("toggleWidget")
static let openModuleSettings = Notification.Name("openModuleSettings")
static let settingsAppear = Notification.Name("settingsAppear")
static let switchWidget = Notification.Name("switchWidget")
static let checkForUpdates = Notification.Name("checkForUpdates")
static let changeCronInterval = Notification.Name("changeCronInterval")
static let clickInSettings = Notification.Name("clickInSettings")
static let refreshPublicIP = Notification.Name("refreshPublicIP")
static let resetTotalNetworkUsage = Notification.Name("resetTotalNetworkUsage")
static let syncFansControl = Notification.Name("syncFansControl")
}
public var isARM: Bool {

View File

@@ -637,6 +637,12 @@ private class ModeButtons: NSStackView {
public var callback: (FanMode) -> Void = {_ in }
public var turbo: () -> Void = {}
private var fansSyncState: Bool {
get {
return Store.shared.bool(key: "Sensors_fansSync", defaultValue: false)
}
}
private var autoBtn: NSButton = NSButton(title: localizedString("Automatic"), target: nil, action: #selector(autoMode))
private var manualBtn: NSButton = NSButton(title: localizedString("Manual"), target: nil, action: #selector(manualMode))
private var turboBtn: NSButton = NSButton(image: NSImage(named: NSImage.Name("ac_unit"))!, target: nil, action: #selector(turboMode))
@@ -683,12 +689,18 @@ private class ModeButtons: NSStackView {
self.addArrangedSubview(modes)
self.addArrangedSubview(self.turboBtn)
NotificationCenter.default.addObserver(self, selector: #selector(syncFanMode), name: .syncFansControl, object: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc private func autoMode(_ sender: NSButton) {
if sender.state.rawValue == 0 {
self.autoBtn.state = .on
@@ -698,6 +710,8 @@ private class ModeButtons: NSStackView {
self.manualBtn.state = .off
self.turboBtn.state = .off
self.callback(.automatic)
NotificationCenter.default.post(name: .syncFansControl, object: nil, userInfo: ["mode": "automatic"])
}
@objc private func manualMode(_ sender: NSButton) {
@@ -709,6 +723,8 @@ private class ModeButtons: NSStackView {
self.autoBtn.state = .off
self.turboBtn.state = .off
self.callback(.forced)
NotificationCenter.default.post(name: .syncFansControl, object: nil, userInfo: ["mode": "forced"])
}
@objc private func turboMode(_ sender: NSButton) {
@@ -719,7 +735,29 @@ private class ModeButtons: NSStackView {
self.manualBtn.state = .off
self.autoBtn.state = .off
self.turboBtn.state = .on
self.turbo()
if sender.title != "sync" {
NotificationCenter.default.post(name: .syncFansControl, object: nil, userInfo: ["mode": "turbo"])
}
}
@objc private func syncFanMode(_ notification: Notification) {
guard let mode = notification.userInfo?["mode"] as? String, self.fansSyncState else {
return
}
if mode == "automatic" {
self.setMode(.automatic)
} else if mode == "forced" {
self.setMode(.forced)
} else if mode == "turbo" {
let btn = NSButton()
btn.state = .on
btn.title = "sync"
self.turboMode(btn)
}
}
public func setMode(_ mode: FanMode) {

View File

@@ -16,6 +16,7 @@ internal class Settings: NSStackView, Settings_v {
private var updateIntervalValue: Int = 3
private var hidState: Bool
private var fanSpeedState: Bool = false
private var fansSyncState: Bool = false
private let title: String
private var button: NSPopUpButton?
@@ -47,6 +48,7 @@ internal class Settings: NSStackView, Settings_v {
self.updateIntervalValue = Store.shared.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
self.hidState = Store.shared.bool(key: "\(self.title)_hid", defaultValue: self.hidState)
self.fanSpeedState = Store.shared.bool(key: "\(self.title)_speed", defaultValue: self.fanSpeedState)
self.fansSyncState = Store.shared.bool(key: "\(self.title)_fansSync", defaultValue: self.fansSyncState)
}
required init?(coder: NSCoder) {
@@ -72,6 +74,12 @@ internal class Settings: NSStackView, Settings_v {
state: self.fanSpeedState
))
self.addArrangedSubview(toggleSettingRow(
title: localizedString("Synchronize the fans control"),
action: #selector(toggleFansSync),
state: self.fansSyncState
))
if isARM {
self.addArrangedSubview(toggleSettingRow(
title: localizedString("HID sensors"),
@@ -191,4 +199,16 @@ internal class Settings: NSStackView, Settings_v {
Store.shared.set(key: "\(self.title)_hid", value: self.hidState)
self.HIDcallback()
}
@objc func toggleFansSync(_ 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.fansSyncState = state! == .on ? true : false
Store.shared.set(key: "\(self.title)_fansSync", value: self.fansSyncState)
}
}

View File

@@ -227,7 +227,6 @@ class ApplicationSettings: NSStackView {
}
Store.shared.set(key: "update-interval", value: key)
NotificationCenter.default.post(name: .changeCronInterval, object: nil, userInfo: nil)
}
@objc private func toggleTemperatureUnits(_ sender: NSMenuItem) {