mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
- sensors module mvp
- improve module settings responsibility - change labelText color to textColor
This commit is contained in:
@@ -88,7 +88,7 @@ public class BarChart: Widget {
|
||||
style.alignment = .center
|
||||
let stringAttributes = [
|
||||
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
|
||||
NSAttributedString.Key.foregroundColor: NSColor.labelColor,
|
||||
NSAttributedString.Key.foregroundColor: NSColor.textColor,
|
||||
NSAttributedString.Key.paragraphStyle: style
|
||||
]
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ public class LineChart: Widget {
|
||||
style.alignment = .center
|
||||
let stringAttributes = [
|
||||
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
|
||||
NSAttributedString.Key.foregroundColor: NSColor.labelColor,
|
||||
NSAttributedString.Key.foregroundColor: NSColor.textColor,
|
||||
NSAttributedString.Key.paragraphStyle: style
|
||||
]
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ public class NetworkWidget: Widget {
|
||||
if self.downloadValue >= 1_024 {
|
||||
NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).setFill()
|
||||
} else {
|
||||
NSColor.labelColor.setFill()
|
||||
NSColor.textColor.setFill()
|
||||
}
|
||||
downloadCircle.fill()
|
||||
|
||||
@@ -124,7 +124,7 @@ public class NetworkWidget: Widget {
|
||||
if self.uploadValue >= 1_024 {
|
||||
NSColor.red.setFill()
|
||||
} else {
|
||||
NSColor.labelColor.setFill()
|
||||
NSColor.textColor.setFill()
|
||||
}
|
||||
uploadCircle.fill()
|
||||
}
|
||||
@@ -143,7 +143,7 @@ public class NetworkWidget: Widget {
|
||||
if self.downloadValue >= 1_024 {
|
||||
NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).set()
|
||||
} else {
|
||||
NSColor.labelColor.set()
|
||||
NSColor.textColor.set()
|
||||
}
|
||||
downloadArrow.lineWidth = 1
|
||||
downloadArrow.stroke()
|
||||
@@ -157,7 +157,7 @@ public class NetworkWidget: Widget {
|
||||
if self.uploadValue >= 1_024 {
|
||||
NSColor.red.set()
|
||||
} else {
|
||||
NSColor.labelColor.set()
|
||||
NSColor.textColor.set()
|
||||
}
|
||||
uploadArrow.lineWidth = 1
|
||||
uploadArrow.stroke()
|
||||
@@ -169,7 +169,7 @@ public class NetworkWidget: Widget {
|
||||
|
||||
let downloadAttributes = [
|
||||
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9, weight: .regular),
|
||||
NSAttributedString.Key.foregroundColor: downloadValue >= 1_024 ? NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8) : NSColor.labelColor,
|
||||
NSAttributedString.Key.foregroundColor: downloadValue >= 1_024 ? NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8) : NSColor.textColor,
|
||||
NSAttributedString.Key.paragraphStyle: NSMutableParagraphStyle()
|
||||
]
|
||||
var rect = CGRect(x: Constants.Widget.margin, y: 1, width: 8, height: rowHeight)
|
||||
@@ -178,7 +178,7 @@ public class NetworkWidget: Widget {
|
||||
|
||||
let uploadAttributes = [
|
||||
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9, weight: .regular),
|
||||
NSAttributedString.Key.foregroundColor: uploadValue >= 1_024 ? NSColor.red : NSColor.labelColor,
|
||||
NSAttributedString.Key.foregroundColor: uploadValue >= 1_024 ? NSColor.red : NSColor.textColor,
|
||||
NSAttributedString.Key.paragraphStyle: NSMutableParagraphStyle()
|
||||
]
|
||||
rect = CGRect(x: Constants.Widget.margin, y: rect.height+1, width: 8, height: rowHeight)
|
||||
|
||||
@@ -79,13 +79,13 @@ public class SensorsWidget: Widget {
|
||||
var x: CGFloat = Constants.Widget.margin
|
||||
for i in 0..<num {
|
||||
if self.values.indices.contains(i*2) {
|
||||
let rect = CGRect(x: x, y: 1, width: rowWidth, height: rowHeight)
|
||||
let rect = CGRect(x: x, y: rowHeight+1, width: rowWidth, height: rowHeight)
|
||||
let str = NSAttributedString.init(string: self.values[i*2], attributes: attributes)
|
||||
str.draw(with: rect)
|
||||
}
|
||||
|
||||
if self.values.indices.contains((i*2)+1) {
|
||||
let rect = CGRect(x: x, y: rowHeight+1, width: rowWidth, height: rowHeight)
|
||||
let rect = CGRect(x: x, y: 1, width: rowWidth, height: rowHeight)
|
||||
let str = NSAttributedString.init(string: self.values[(i*2)+1], attributes: attributes)
|
||||
str.draw(with: rect)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ internal class Popup: NSView {
|
||||
var i: CGFloat = 0
|
||||
groups.sorted{ $0.1 < $1.1 }.forEach { (g: (key: SensorGroup_t, value: Int)) in
|
||||
filtered.reversed().filter{ $0.group == g.key }.forEach { (s: Sensor_t) in
|
||||
print(s.name)
|
||||
self.list[s.key] = PopupRow(view, n: i, title: "\(s.name):", value: s.formattedValue)
|
||||
i += 1
|
||||
}
|
||||
|
||||
104
ModuleKit/Widgets/settings.swift
Normal file
104
ModuleKit/Widgets/settings.swift
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// settings.swift
|
||||
// Stats
|
||||
//
|
||||
// Created by Serhiy Mytrovtsiy on 23/06/2020.
|
||||
// Using Swift 5.0.
|
||||
// Running on macOS 10.15.
|
||||
//
|
||||
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import StatsKit
|
||||
import ModuleKit
|
||||
|
||||
internal class Settings: NSView, Settings_v {
|
||||
private let title: String
|
||||
private let store: UnsafePointer<Store>
|
||||
private var button: NSPopUpButton?
|
||||
private let list: UnsafeMutablePointer<[Sensor_t]>
|
||||
public var callback: (() -> Void) = {}
|
||||
|
||||
public init(_ title: String, store: UnsafePointer<Store>, list: UnsafeMutablePointer<[Sensor_t]>) {
|
||||
self.title = title
|
||||
self.store = store
|
||||
self.list = list
|
||||
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
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
func load(widget: widget_t) {
|
||||
guard !self.list.pointee.isEmpty else {
|
||||
return
|
||||
}
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
|
||||
var types: [SensorType_t: Int] = [:]
|
||||
self.list.pointee.forEach { (s: Sensor_t) in
|
||||
types[s.type] = (types[s.type] ?? 0) + 1
|
||||
}
|
||||
|
||||
let rowHeight: CGFloat = 30
|
||||
let height: CGFloat = ((rowHeight+Constants.Settings.margin) * CGFloat(self.list.pointee.count)) + ((rowHeight+Constants.Settings.margin) * CGFloat(types.count))
|
||||
let x: CGFloat = height < 360 ? 0 : Constants.Settings.margin
|
||||
let view: NSView = NSView(frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: self.frame.width - (Constants.Settings.margin*2) - x, height: height))
|
||||
|
||||
var y: CGFloat = 0
|
||||
types.sorted{ $0.1 < $1.1 }.forEach { (t: (key: SensorType_t, value: Int)) in
|
||||
let filtered = self.list.pointee.filter{ $0.type == t.key }
|
||||
var groups: [SensorGroup_t: Int] = [:]
|
||||
filtered.forEach { (s: Sensor_t) in
|
||||
groups[s.group] = (groups[s.group] ?? 0) + 1
|
||||
}
|
||||
|
||||
groups.sorted{ $0.1 < $1.1 }.forEach { (g: (key: SensorGroup_t, value: Int)) in
|
||||
filtered.reversed().filter{ $0.group == g.key }.forEach { (s: Sensor_t) in
|
||||
let row: NSView = ToggleTitleRow(
|
||||
frame: NSRect(x: 0, y: y, width: view.frame.width, height: rowHeight),
|
||||
title: s.name,
|
||||
action: #selector(self.handleSelection),
|
||||
state: s.state
|
||||
)
|
||||
row.subviews.filter{ $0 is NSControl }.forEach { (control: NSView) in
|
||||
control.identifier = NSUserInterfaceItemIdentifier(rawValue: s.key)
|
||||
}
|
||||
view.addSubview(row)
|
||||
y += rowHeight + Constants.Settings.margin
|
||||
}
|
||||
}
|
||||
|
||||
let rowTitleView: NSView = NSView(frame: NSRect(x: 0, y: y, width: view.frame.width, height: rowHeight))
|
||||
let rowTitle: NSTextField = LabelField(frame: NSRect(x: 0, y: (rowHeight-19)/2, width: view.frame.width, height: 19), t.key)
|
||||
rowTitle.font = NSFont.systemFont(ofSize: 14, weight: .regular)
|
||||
rowTitle.textColor = .secondaryLabelColor
|
||||
rowTitle.alignment = .center
|
||||
rowTitleView.addSubview(rowTitle)
|
||||
|
||||
view.addSubview(rowTitleView)
|
||||
y += rowHeight + Constants.Settings.margin
|
||||
}
|
||||
|
||||
self.addSubview(view)
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: height + (Constants.Settings.margin*1)))
|
||||
}
|
||||
|
||||
@objc func handleSelection(_ sender: NSControl) {
|
||||
guard let id = sender.identifier else { return }
|
||||
|
||||
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.store.pointee.set(key: "sensor_\(id.rawValue)", value: state! == NSControl.StateValue.on)
|
||||
self.callback()
|
||||
}
|
||||
}
|
||||
@@ -103,13 +103,7 @@ open class Module: Module_p {
|
||||
os_log(.debug, log: log, "Module started without widget")
|
||||
}
|
||||
|
||||
self.settings = Settings(config: &self.config, enabled: self.enabled, activeWidget: self.widget, moduleSettings: { [weak self] (_ superview: NSView) in
|
||||
if self != nil && self?.settingsView != nil {
|
||||
self!.settingsView!.load(rect: superview.frame, widget: self!.activeWidget)
|
||||
superview.setFrameSize(NSSize(width: superview.frame.width, height: self!.settingsView!.frame.height))
|
||||
superview.addSubview(self!.settingsView!)
|
||||
}
|
||||
})
|
||||
self.settings = Settings(config: &self.config, enabled: self.enabled, activeWidget: self.widget, moduleSettings: self.settingsView)
|
||||
self.settings?.toggleCallback = { [weak self] in
|
||||
self?.toggleEnabled()
|
||||
}
|
||||
|
||||
@@ -161,11 +161,11 @@ internal class HeaderView: NSView {
|
||||
titleView.isSelectable = false
|
||||
titleView.isBezeled = false
|
||||
titleView.wantsLayer = true
|
||||
titleView.textColor = .labelColor
|
||||
titleView.textColor = .textColor
|
||||
titleView.backgroundColor = .clear
|
||||
titleView.canDrawSubviewsIntoLayer = true
|
||||
titleView.alignment = .center
|
||||
titleView.font = NSFont.systemFont(ofSize: 16, weight: .medium)
|
||||
titleView.font = NSFont.systemFont(ofSize: 16, weight: .regular)
|
||||
titleView.stringValue = ""
|
||||
|
||||
self.titleView = titleView
|
||||
|
||||
@@ -17,7 +17,8 @@ public protocol Settings_p: NSView {
|
||||
}
|
||||
|
||||
public protocol Settings_v: NSView {
|
||||
func load(rect: NSRect, widget: widget_t)
|
||||
var callback: (() -> Void) { get set }
|
||||
func load(widget: widget_t)
|
||||
}
|
||||
|
||||
open class Settings: NSView, Settings_p {
|
||||
@@ -33,38 +34,57 @@ open class Settings: NSView, Settings_p {
|
||||
private var config: UnsafePointer<module_c>
|
||||
private var activeWidget: Widget_p?
|
||||
|
||||
private var moduleSettings: (_ superview: NSView) -> ()
|
||||
private var moduleSettings: Settings_v?
|
||||
|
||||
init(config: UnsafePointer<module_c>, enabled: Bool, activeWidget: Widget_p?, moduleSettings: @escaping (_ superview: NSView) -> ()) {
|
||||
init(config: UnsafePointer<module_c>, enabled: Bool, activeWidget: Widget_p?, moduleSettings: Settings_v?) {
|
||||
self.config = config
|
||||
self.activeWidget = activeWidget
|
||||
self.moduleSettings = moduleSettings
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Settings.width, height: Constants.Settings.height))
|
||||
self.wantsLayer = true
|
||||
self.appearance = NSAppearance(named: .aqua)
|
||||
self.layer?.backgroundColor = NSColor(hexString: "#ececec").cgColor
|
||||
|
||||
addHeader(state: enabled)
|
||||
addWidgetSelector()
|
||||
addWidgetSettings()
|
||||
addModuleSettings()
|
||||
if self.moduleSettings != nil {
|
||||
self.moduleSettings?.load(widget: self.activeWidget?.type ?? .unknown)
|
||||
addModuleSettings()
|
||||
}
|
||||
}
|
||||
|
||||
private func addModuleSettings() {
|
||||
let y: CGFloat = self.frame.height - headerHeight - widgetSelectorHeight - (self.widgetSettingsView?.frame.height ?? 0)
|
||||
let view: NSView = NSView(frame: NSRect(x: Constants.Settings.margin, y: y - (Constants.Settings.margin*3), width: self.frame.width - (Constants.Settings.margin*2), height: 0))
|
||||
guard self.moduleSettings?.frame.height != 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
let maxHeight: CGFloat = Constants.Settings.height - self.headerHeight - self.widgetSelectorHeight - (self.widgetSettingsView?.frame.height ?? 0) - (Constants.Settings.margin*3)
|
||||
let h: CGFloat = self.moduleSettings!.frame.height > maxHeight ? maxHeight : self.moduleSettings!.frame.height
|
||||
var y: CGFloat = Constants.Settings.height - self.headerHeight - self.widgetSelectorHeight - (self.widgetSettingsView?.frame.height ?? 0) - (Constants.Settings.margin*3) - h
|
||||
if y == 0 {
|
||||
y = Constants.Settings.margin
|
||||
}
|
||||
|
||||
let view: NSScrollView = NSScrollView(frame: NSRect(
|
||||
x: Constants.Settings.margin,
|
||||
y: y,
|
||||
width: self.frame.width - (Constants.Settings.margin*2),
|
||||
height: h
|
||||
))
|
||||
view.wantsLayer = true
|
||||
view.layer?.backgroundColor = .white
|
||||
view.layer!.cornerRadius = 3
|
||||
view.translatesAutoresizingMaskIntoConstraints = true
|
||||
view.borderType = .noBorder
|
||||
view.hasVerticalScroller = true
|
||||
view.autohidesScrollers = true
|
||||
|
||||
self.appearance = NSAppearance(named: .aqua)
|
||||
view.documentView = self.moduleSettings
|
||||
view.documentView?.scroll(NSPoint(x: 0, y: view.documentView!.bounds.size.height))
|
||||
|
||||
self.moduleSettings(view)
|
||||
|
||||
if view.frame.height != 0 {
|
||||
view.setFrameOrigin(NSPoint(x: view.frame.origin.x, y: view.frame.origin.y - view.frame.height))
|
||||
self.addSubview(view)
|
||||
self.moduleSettingsView = view
|
||||
}
|
||||
self.addSubview(view)
|
||||
self.moduleSettingsView = view
|
||||
}
|
||||
|
||||
private func addWidgetSettings() {
|
||||
@@ -184,7 +204,10 @@ open class Settings: NSView, Settings_p {
|
||||
|
||||
self.subviews.filter{ $0 == self.widgetSettingsView || $0 == self.moduleSettingsView }.forEach{ $0.removeFromSuperview() }
|
||||
self.addWidgetSettings()
|
||||
self.addModuleSettings()
|
||||
if self.moduleSettings != nil {
|
||||
self.moduleSettings?.load(widget: self.activeWidget?.type ?? .unknown)
|
||||
addModuleSettings()
|
||||
}
|
||||
}
|
||||
|
||||
required public init?(coder: NSCoder) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 } ?? [])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -63,6 +63,8 @@
|
||||
9A944D6124492B6D0058F32A /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A944D6024492B6D0058F32A /* popup.swift */; };
|
||||
9A9D728A24471FAE005CF997 /* SMC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9D728924471FAE005CF997 /* SMC.swift */; };
|
||||
9A9EA9452476D34500E3B883 /* Update.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9EA9442476D34500E3B883 /* Update.swift */; };
|
||||
9A9FB99E24A2865B00FD072C /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9FB99D24A2865B00FD072C /* settings.swift */; };
|
||||
9A9FB99F24A2866500FD072C /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9FB99D24A2865B00FD072C /* settings.swift */; };
|
||||
9AA4A00A2443656D00ECCF07 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9AA4A0092443656D00ECCF07 /* Assets.xcassets */; };
|
||||
9AA64260244B274200416A33 /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AA6425F244B274200416A33 /* popup.swift */; };
|
||||
9AA64262244B57C800416A33 /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AA64261244B57C800416A33 /* settings.swift */; };
|
||||
@@ -430,6 +432,7 @@
|
||||
9A998CD922A199970087ADE7 /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
|
||||
9A9D728924471FAE005CF997 /* SMC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMC.swift; sourceTree = "<group>"; };
|
||||
9A9EA9442476D34500E3B883 /* Update.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Update.swift; sourceTree = "<group>"; };
|
||||
9A9FB99D24A2865B00FD072C /* settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = settings.swift; path = ModuleKit/Widgets/settings.swift; sourceTree = SOURCE_ROOT; };
|
||||
9AA4A0092443656D00ECCF07 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
9AA6425F244B274200416A33 /* popup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = popup.swift; sourceTree = "<group>"; };
|
||||
9AA64261244B57C800416A33 /* settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = settings.swift; sourceTree = "<group>"; };
|
||||
@@ -765,6 +768,7 @@
|
||||
9AE29AF1249A50CD0071B02D /* main.swift */,
|
||||
9AE29AF9249A53780071B02D /* readers.swift */,
|
||||
9A2DD8CB24A1190A00F6F48D /* popup.swift */,
|
||||
9A9FB99D24A2865B00FD072C /* settings.swift */,
|
||||
9AE29AF7249A53420071B02D /* values.swift */,
|
||||
9AE29AEC249A50960071B02D /* Info.plist */,
|
||||
9AE29AF4249A52870071B02D /* config.plist */,
|
||||
@@ -1252,6 +1256,7 @@
|
||||
9A2DD8CC24A1190A00F6F48D /* popup.swift in Sources */,
|
||||
9AE29AFA249A53780071B02D /* readers.swift in Sources */,
|
||||
9A9EA9452476D34500E3B883 /* Update.swift in Sources */,
|
||||
9A9FB99E24A2865B00FD072C /* settings.swift in Sources */,
|
||||
9A81C74E24499C7000825D92 /* Settings.swift in Sources */,
|
||||
9A81C74D24499C7000825D92 /* AppSettings.swift in Sources */,
|
||||
9AE29AF8249A53420071B02D /* values.swift in Sources */,
|
||||
@@ -1330,6 +1335,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
9A9FB99F24A2866500FD072C /* settings.swift in Sources */,
|
||||
9A2DD8CD24A1193500F6F48D /* popup.swift in Sources */,
|
||||
9AE29AFB249A53DC0071B02D /* readers.swift in Sources */,
|
||||
9AE29AFC249A53DC0071B02D /* values.swift in Sources */,
|
||||
|
||||
@@ -17,10 +17,13 @@ class SettingsWindow: NSWindow, NSWindowDelegate {
|
||||
private let viewController: SettingsViewController = SettingsViewController()
|
||||
|
||||
init() {
|
||||
let w = NSScreen.main!.frame.width
|
||||
let h = NSScreen.main!.frame.height
|
||||
super.init(
|
||||
contentRect: NSMakeRect(w - self.viewController.view.frame.width, h - self.viewController.view.frame.height, self.viewController.view.frame.width, self.viewController.view.frame.height),
|
||||
contentRect: NSMakeRect(
|
||||
NSScreen.main!.frame.width - self.viewController.view.frame.width,
|
||||
NSScreen.main!.frame.height - self.viewController.view.frame.height,
|
||||
self.viewController.view.frame.width,
|
||||
self.viewController.view.frame.height
|
||||
),
|
||||
styleMask: [.closable, .titled, .miniaturizable],
|
||||
backing: .buffered,
|
||||
defer: true
|
||||
|
||||
@@ -291,7 +291,7 @@ public extension NSView {
|
||||
|
||||
let rowTitle: NSTextField = LabelField(frame: NSRect(x: 0, y: (row.frame.height - 16)/2, width: row.frame.width - 52, height: 17), title)
|
||||
rowTitle.font = NSFont.systemFont(ofSize: 13, weight: .light)
|
||||
rowTitle.textColor = .labelColor
|
||||
rowTitle.textColor = .textColor
|
||||
|
||||
var toggle: NSControl = NSControl()
|
||||
if #available(OSX 10.15, *) {
|
||||
@@ -325,7 +325,7 @@ public extension NSView {
|
||||
|
||||
let rowTitle: NSTextField = LabelField(frame: NSRect(x: 0, y: (row.frame.height - 16)/2, width: row.frame.width - 52, height: 17), title)
|
||||
rowTitle.font = NSFont.systemFont(ofSize: 13, weight: .light)
|
||||
rowTitle.textColor = .labelColor
|
||||
rowTitle.textColor = .textColor
|
||||
|
||||
let select: NSPopUpButton = NSPopUpButton(frame: NSRect(x: row.frame.width - 50, y: 0, width: 50, height: row.frame.height))
|
||||
select.target = self
|
||||
|
||||
Reference in New Issue
Block a user