- now Sensor module will disable if no sensor is selected

- click on application will open Settings
This commit is contained in:
Serhiy Mytrovtsiy
2020-07-01 18:36:03 +02:00
parent 1e8737734c
commit d206da1be6
6 changed files with 81 additions and 16 deletions

View File

@@ -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()

View File

@@ -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 {

View File

@@ -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<module_c>, 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)
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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()
}