From cc9622600863bcba75f1f6c74ac6dcabb2874f55 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Mon, 27 Jun 2022 19:15:48 +0200 Subject: [PATCH] feat: added an option to synchronize the fans (the mode only for now) control (#975) --- Kit/types.swift | 5 +---- Modules/Sensors/popup.swift | 38 ++++++++++++++++++++++++++++++++++ Modules/Sensors/settings.swift | 20 ++++++++++++++++++ Stats/Views/AppSettings.swift | 1 - 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Kit/types.swift b/Kit/types.swift index 1a05bb1d..a4ba878c 100644 --- a/Kit/types.swift +++ b/Kit/types.swift @@ -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 { diff --git a/Modules/Sensors/popup.swift b/Modules/Sensors/popup.swift index 71a7753a..dc55d3fb 100644 --- a/Modules/Sensors/popup.swift +++ b/Modules/Sensors/popup.swift @@ -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) { diff --git a/Modules/Sensors/settings.swift b/Modules/Sensors/settings.swift index e3fd329c..955b3033 100644 --- a/Modules/Sensors/settings.swift +++ b/Modules/Sensors/settings.swift @@ -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) + } } diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index 1abb32d2..7437db92 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -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) {