- sensors module mvp

- improve module settings responsibility
- change labelText color to textColor
This commit is contained in:
Serhiy Mytrovtsiy
2020-06-24 00:23:06 +02:00
parent c3368ddd19
commit c5ba0f885e
19 changed files with 214 additions and 67 deletions

View File

@@ -49,6 +49,10 @@ public class CPU: Module {
settings: self.settingsView
)
self.settingsView.callback = { [unowned self] in
self.loadReader.read()
}
self.loadReader.readyCallback = { [unowned self] in
self.readyHandler()
}

View File

@@ -19,10 +19,12 @@ internal class Settings: NSView, Settings_v {
private let title: String
private let store: UnsafePointer<Store>?
public var callback: (() -> Void) = {}
public init(_ title: String, store: UnsafePointer<Store>?) {
self.title = title
self.store = store
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: 0, height: 0))
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: Constants.Settings.width - (Constants.Settings.margin*2), height: 0))
self.wantsLayer = true
self.canDrawConcurrently = true
@@ -35,15 +37,15 @@ internal class Settings: NSView, Settings_v {
fatalError("init(coder:) has not been implemented")
}
public func load(rect: NSRect, widget: widget_t) {
public func load(widget: widget_t) {
self.subviews.forEach{ $0.removeFromSuperview() }
let rowHeight: CGFloat = 30
let rowHeight: CGFloat = 29
var height: CGFloat = 0
if widget == .barChart {
self.addSubview(ToggleTitleRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: rect.width - (Constants.Settings.margin*2), height: rowHeight),
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 0, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
title: "Show hyper-threading cores",
action: #selector(toggleMultithreading),
state: self.hyperthreadState
@@ -54,7 +56,7 @@ internal class Settings: NSView, Settings_v {
if height != 0 {
height += (Constants.Settings.margin*2)
}
self.setFrameSize(NSSize(width: rect.width - (Constants.Settings.margin*2), height: height))
self.setFrameSize(NSSize(width: self.frame.width, height: height))
}
@objc func toggleMultithreading(_ sender: NSControl) {
@@ -67,5 +69,6 @@ internal class Settings: NSView, Settings_v {
self.hyperthreadState = state! == .on ? true : false
self.store?.pointee.set(key: "\(self.title)_hyperhreading", value: self.hyperthreadState)
self.callback()
}
}

View File

@@ -15,6 +15,7 @@ import ModuleKit
internal class Settings: NSView, Settings_v {
public var selectedDiskHandler: (String) -> Void = {_ in }
public var callback: (() -> Void) = {}
private let title: String
private let store: UnsafePointer<Store>
@@ -25,7 +26,7 @@ internal class Settings: NSView, Settings_v {
self.title = title
self.store = store
self.selectedDisk = store.pointee.string(key: "\(self.title)_disk", defaultValue: "")
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: 0, height: 0))
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: Constants.Settings.width - (Constants.Settings.margin*2), height: 0))
self.wantsLayer = true
self.canDrawConcurrently = true
}
@@ -34,22 +35,22 @@ internal class Settings: NSView, Settings_v {
fatalError("init(coder:) has not been implemented")
}
public func load(rect: NSRect, widget: widget_t) {
public func load(widget: widget_t) {
self.subviews.forEach{ $0.removeFromSuperview() }
self.addDiskSelector(rect: rect)
self.addDiskSelector()
self.setFrameSize(NSSize(width: rect.width - (Constants.Settings.margin*2), height: 30 + (Constants.Settings.margin*2)))
self.setFrameSize(NSSize(width: self.frame.width, height: 30 + (Constants.Settings.margin*2)))
}
private func addDiskSelector(rect: NSRect) {
let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: rect.width, height: 30))
private func addDiskSelector() {
let view: NSView = NSView(frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: self.frame.width, height: 29))
let rowTitle: NSTextField = LabelField(frame: NSRect(x: 0, y: (view.frame.height - 16)/2, width: view.frame.width - 52, height: 17), "Disk to show")
rowTitle.font = NSFont.systemFont(ofSize: 13, weight: .light)
rowTitle.textColor = .labelColor
rowTitle.textColor = .textColor
self.button = NSPopUpButton(frame: NSRect(x: view.frame.width - 164, y: 0, width: 140, height: 30))
self.button = NSPopUpButton(frame: NSRect(x: view.frame.width - 164, y: -1, width: 140, height: 30))
self.button!.target = self
self.button?.action = #selector(self.handleSelection)
@@ -70,7 +71,7 @@ internal class Settings: NSView, Settings_v {
}
})
}
@objc func handleSelection(_ sender: NSPopUpButton) {
guard let item = sender.selectedItem else { return }
self.selectedDisk = item.title

View File

@@ -80,7 +80,7 @@ internal class Popup: NSView {
let valueField = LabelField(frame: NSRect(x: 0, y: 0, width: valueWidth, height: 30), "0")
valueField.font = NSFont.systemFont(ofSize: 26, weight: .light)
valueField.textColor = .labelColor
valueField.textColor = .textColor
valueField.alignment = .right
let unitField = LabelField(frame: NSRect(x: valueField.frame.width, y: 4, width: unitWidth, height: 15), "KB/s")

View File

@@ -16,17 +16,24 @@ import StatsKit
public class Sensors: Module {
private var sensorsReader: SensorsReader
private let popupView: Popup = Popup()
private var settingsView: Settings
public init(_ store: UnsafePointer<Store>?, _ smc: UnsafePointer<SMCService>) {
self.sensorsReader = SensorsReader(smc)
self.settingsView = Settings("Disk", store: store!, list: &self.sensorsReader.list)
super.init(
store: store,
popup: self.popupView,
settings: nil
settings: self.settingsView
)
self.popupView.setup(self.sensorsReader.list)
self.settingsView.callback = { [unowned self] in
self.sensorsReader.read()
}
self.sensorsReader.readyCallback = { [unowned self] in
self.readyHandler()
}
@@ -43,12 +50,8 @@ public class Sensors: Module {
}
self.popupView.usageCallback(value!)
let value_1 = value?.first{ $0.key == "TC0F" }
let value_2 = value?.first{ $0.key == "TC0P" }
if let widget = self.widget as? SensorsWidget {
widget.setValues([value_1!.formattedMiniValue, value_2!.formattedMiniValue])
widget.setValues(value?.filter{ $0.state }.map{ $0.formattedMiniValue } ?? [])
}
}
}

View File

@@ -19,10 +19,9 @@ internal class SensorsReader: Reader<[Sensor_t]> {
init(_ smc: UnsafePointer<SMCService>) {
self.smc = smc
}
public override func setup() {
var available: [String] = self.smc.pointee.getAllKeys()
var list: [Sensor_t] = []
available = available.filter({ (key: String) -> Bool in
switch key.prefix(1) {
@@ -33,19 +32,20 @@ internal class SensorsReader: Reader<[Sensor_t]> {
available.forEach { (key: String) in
if var sensor = SensorsDict[key] {
sensor.value = self.smc.pointee.getValue(key)
sensor.value = smc.pointee.getValue(key)
if sensor.value != nil {
sensor.key = key
self.list.append(sensor)
list.append(sensor)
}
}
}
self.list = list
}
public override func read() {
for i in 0..<self.list.count {
if let newValue = self.smc.pointee.getValue(self.list[i].key) {
// print(self.list[i].type, measurement.unit)
self.list[i].value = newValue
}
}

View File

@@ -29,6 +29,7 @@ enum SensorType: SensorType_t {
}
struct Sensor_t {
let store: Store = Store()
var name: String
var key: String = ""
@@ -76,6 +77,12 @@ struct Sensor_t {
}
}
}
var state: Bool {
get {
return store.bool(key: "sensor_\(self.key)", defaultValue: false)
}
}
}
// List of keys: https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCSensorKeys.txt