feat: added sleep mode to the reader, in some cases there is no need for reads since the widget is not visualizing anything. In this case, the reader could go to sleep mode and be active only when popup is open. It's available only for the sensor module for now.

This commit is contained in:
Serhiy Mytrovtsiy
2026-03-12 16:52:00 +01:00
parent 510d9132a6
commit 678d813d75
3 changed files with 26 additions and 5 deletions

View File

@@ -11,7 +11,7 @@
import Cocoa import Cocoa
internal class Label: WidgetWrapper { public class Label: WidgetWrapper {
private var label: String private var label: String
internal init(title: String, config: NSDictionary) { internal init(title: String, config: NSDictionary) {

View File

@@ -12,8 +12,9 @@
import Cocoa import Cocoa
public protocol Reader_p { public protocol Reader_p {
var optional: Bool { get }
var popup: Bool { get } var popup: Bool { get }
var preview: Bool { get }
var sleep: Bool { get }
func setup() func setup()
func read() func read()
@@ -28,6 +29,7 @@ public protocol Reader_p {
func initStoreValues(title: String) func initStoreValues(title: String)
func setInterval(_ value: Int) func setInterval(_ value: Int)
func sleepMode(state: Bool)
} }
public protocol ReaderInternal_p { public protocol ReaderInternal_p {
@@ -55,6 +57,8 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
public var defaultInterval: Int = 1 public var defaultInterval: Int = 1
public var optional: Bool = false public var optional: Bool = false
public var popup: Bool = false public var popup: Bool = false
public var preview: Bool = false
public var sleep: Bool = false
public var callbackHandler: (T?) -> Void public var callbackHandler: (T?) -> Void
@@ -73,8 +77,9 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
private var lastDBWrite: Date? = nil private var lastDBWrite: Date? = nil
public init(_ module: ModuleType, popup: Bool = false, history: Bool = false, callback: @escaping (T?) -> Void = {_ in }) { public init(_ module: ModuleType, popup: Bool = false, preview: Bool = false, history: Bool = false, callback: @escaping (T?) -> Void = {_ in }) {
self.popup = popup self.popup = popup
self.preview = preview
self.module = module self.module = module
self.history = history self.history = history
self.callbackHandler = callback self.callbackHandler = callback
@@ -121,7 +126,7 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
open func terminate() {} open func terminate() {}
open func start() { open func start() {
if self.popup && self.locked { if (self.popup || self.preview) && self.locked {
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
self.read() self.read()
} }
@@ -169,6 +174,19 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
public func save(_ value: T) { public func save(_ value: T) {
DB.shared.insert(key: "\(self.module.stringValue)@\(self.name)", value: value, ts: self.history, force: true) DB.shared.insert(key: "\(self.module.stringValue)@\(self.name)", value: value, ts: self.history, force: true)
} }
public func sleepMode(state: Bool) {
guard state != self.sleep else { return }
debug("Sleep mode: \(state ? "on" : "off")", log: self.log)
self.sleep = state
if state {
self.pause()
} else {
self.start()
}
}
} }
extension Reader: Reader_p { extension Reader: Reader_p {

View File

@@ -106,7 +106,10 @@ public class Sensors: Module {
self.portalView.usageCallback(value.sensors) self.portalView.usageCallback(value.sensors)
self.notificationsView.usageCallback(value.sensors) self.notificationsView.usageCallback(value.sensors)
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: SWidget) in let activeWidgets = self.menuBar.widgets.filter{ $0.isActive }
self.sensorsReader?.sleepMode(state: activeWidgets.contains(where: {$0.item is Label}) && activeWidgets.count == 1)
activeWidgets.forEach { (w: SWidget) in
switch w.item { switch w.item {
case let widget as Mini: case let widget as Mini:
if let active = value.sensors.first(where: { $0.key == self.selectedSensor }) { if let active = value.sensors.first(where: { $0.key == self.selectedSensor }) {