From d206da1be64ec788de2cdc7fd845c939738819f6 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Wed, 1 Jul 2020 18:36:03 +0200 Subject: [PATCH] - now Sensor module will disable if no sensor is selected - click on application will open Settings --- ModuleKit/Widgets/BarChart.swift | 4 ++-- ModuleKit/module.swift | 15 +++++++++++++++ ModuleKit/settings.swift | 27 +++++++++++++++++++++++---- Modules/Sensors/main.swift | 10 +++++++++- Stats/AppDelegate.swift | 9 +++++++++ StatsKit/extensions.swift | 32 +++++++++++++++++++++++--------- 6 files changed, 81 insertions(+), 16 deletions(-) diff --git a/ModuleKit/Widgets/BarChart.swift b/ModuleKit/Widgets/BarChart.swift index ac59adc3..56963813 100644 --- a/ModuleKit/Widgets/BarChart.swift +++ b/ModuleKit/Widgets/BarChart.swift @@ -275,7 +275,7 @@ public class BarChart: Widget { if self.colorState { self.pressureState = false self.store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_pressure", value: self.pressureState) - ToggleNSControlState(self.pressureLevelSettingsView, state: .off) + FindAndToggleNSControlState(self.pressureLevelSettingsView, state: .off) } self.display() @@ -294,7 +294,7 @@ public class BarChart: Widget { if self.pressureState { self.colorState = false self.store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_color", value: self.colorState) - ToggleNSControlState(self.colorizeSettingsView, state: .off) + FindAndToggleNSControlState(self.colorizeSettingsView, state: .off) } self.display() diff --git a/ModuleKit/module.swift b/ModuleKit/module.swift index c81a8a8d..6a5d20b8 100644 --- a/ModuleKit/module.swift +++ b/ModuleKit/module.swift @@ -98,6 +98,7 @@ open class Module: Module_p { NotificationCenter.default.addObserver(self, selector: #selector(listenForWidgetSwitch), name: .switchWidget, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(listenForMouseDownInSettings), name: .clickInSettings, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(listenForModuleToggle), name: .toggleModule, object: nil) if self.config.widgetsConfig.count != 0 { self.setWidget() @@ -266,6 +267,20 @@ open class Module: Module_p { } } + @objc private func listenForModuleToggle(_ notification: Notification) { + if let name = notification.userInfo?["module"] as? String { + if name == self.config.name { + if let state = notification.userInfo?["state"] as? Bool { + if state && !self.enabled { + self.enable() + } else if !state && self.enabled { + self.disable() + } + } + } + } + } + @objc private func listenForWidgetSwitch(_ notification: Notification) { if let moduleName = notification.userInfo?["module"] as? String { if let widgetName = notification.userInfo?["widget"] as? String { diff --git a/ModuleKit/settings.swift b/ModuleKit/settings.swift index d36844f8..89d8db89 100644 --- a/ModuleKit/settings.swift +++ b/ModuleKit/settings.swift @@ -10,6 +10,7 @@ // import Cocoa +import StatsKit public protocol Settings_p: NSView { var toggleCallback: () -> () { get set } @@ -35,6 +36,7 @@ open class Settings: NSView, Settings_p { private var activeWidget: Widget_p? private var moduleSettings: Settings_v? + private var enableControl: NSControl? init(config: UnsafePointer, enabled: Bool, activeWidget: Widget_p?, moduleSettings: Settings_v?) { self.config = config @@ -45,12 +47,28 @@ open class Settings: NSView, Settings_p { self.appearance = NSAppearance(named: .aqua) self.layer?.backgroundColor = NSColor(hexString: "#ececec").cgColor - addHeader(state: enabled) - addWidgetSelector() - addWidgetSettings() + NotificationCenter.default.addObserver(self, selector: #selector(externalModuleToggle), name: .toggleModule, object: nil) + + self.addHeader(state: enabled) + self.addWidgetSelector() + self.addWidgetSettings() if self.moduleSettings != nil { self.moduleSettings?.load(widget: self.activeWidget?.type ?? .unknown) - addModuleSettings() + self.addModuleSettings() + } + } + + deinit { + NotificationCenter.default.removeObserver(self) + } + + @objc func externalModuleToggle(_ notification: Notification) { + if let name = notification.userInfo?["module"] as? String { + if name == self.config.pointee.name { + if let state = notification.userInfo?["state"] as? Bool { + ToggleNSControlState(self.enableControl, state: state ? .on : .off) + } + } } } @@ -192,6 +210,7 @@ open class Settings: NSView, Settings_p { view.addSubview(toggle) view.addSubview(line) + self.enableControl = toggle self.addSubview(view) } diff --git a/Modules/Sensors/main.swift b/Modules/Sensors/main.swift index c59656a2..155243c8 100644 --- a/Modules/Sensors/main.swift +++ b/Modules/Sensors/main.swift @@ -27,10 +27,12 @@ public class Sensors: Module { popup: self.popupView, settings: self.settingsView ) - + + self.checkIfNoSensorsEnabled() self.popupView.setup(self.sensorsReader.list) self.settingsView.callback = { [unowned self] in + self.checkIfNoSensorsEnabled() self.sensorsReader.read() } @@ -44,6 +46,12 @@ public class Sensors: Module { self.addReader(self.sensorsReader) } + private func checkIfNoSensorsEnabled() { + if self.sensorsReader.list.filter({ $0.state }).count == 0 { + NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.config.name, "state": false]) + } + } + private func usageCallback(_ value: [Sensor_t]?) { if value == nil { return diff --git a/Stats/AppDelegate.swift b/Stats/AppDelegate.swift index 1bc245a2..84808617 100755 --- a/Stats/AppDelegate.swift +++ b/Stats/AppDelegate.swift @@ -51,6 +51,15 @@ class AppDelegate: NSObject, NSApplicationDelegate { NotificationCenter.default.removeObserver(self) } + func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { + if flag { + self.settingsWindow.makeKeyAndOrderFront(self) + } else { + self.settingsWindow.setIsVisible(true) + } + return true + } + @objc private func toggleSettingsHandler(_ notification: Notification) { if !self.settingsWindow.isVisible { self.settingsWindow.setIsVisible(true) diff --git a/StatsKit/extensions.swift b/StatsKit/extensions.swift index ae3433cf..02f9b7c5 100644 --- a/StatsKit/extensions.swift +++ b/StatsKit/extensions.swift @@ -575,16 +575,30 @@ public extension Array where Element : Equatable { } } -public func ToggleNSControlState(_ view: NSView?, state: NSControl.StateValue) { +public func FindAndToggleNSControlState(_ view: NSView?, state: NSControl.StateValue) { if let control = view?.subviews.first(where: { $0 is NSControl }) { - if #available(OSX 10.15, *) { - if let checkbox = control as? NSSwitch { - checkbox.state = state - } - } else { - if let checkbox = control as? NSButton { - checkbox.state = state - } + ToggleNSControlState(control as? NSControl, state: state) + } +} + +public func ToggleNSControlState(_ control: NSControl?, state: NSControl.StateValue) { + if #available(OSX 10.15, *) { + if let checkbox = control as? NSSwitch { + checkbox.state = state + } + } else { + if let checkbox = control as? NSButton { + checkbox.state = state } } } + +public func dialogOKCancel(question: String, text: String) { + let alert = NSAlert() + alert.messageText = question + alert.informativeText = text + alert.alertStyle = .warning + alert.addButton(withTitle: "OK") + alert.addButton(withTitle: "Cancel") + alert.runModal() +}