- add option to set up update interval for CPU module

- add setInterval() to reader protocol
- update reader start process (run initStoreValues() before start())

- add option to select update interval for RAM module

- add option to select update interval for Disk module
- small refactoring in cpu and ram settings

- add option to select update interval for Sensors module
This commit is contained in:
Serhiy Mytrovtsiy
2020-07-10 22:56:47 +02:00
parent c5df430a06
commit 8dbafa40ac
15 changed files with 252 additions and 53 deletions

View File

@@ -43,7 +43,7 @@ public class Battery: Module {
private var usageReader: UsageReader? = nil
private let popupView: Popup = Popup()
public init(_ store: UnsafePointer<Store>?) {
public init(_ store: UnsafePointer<Store>) {
super.init(
store: store,
popup: self.popupView,

View File

@@ -55,6 +55,9 @@ public class CPU: Module {
self.settingsView.callback = { [unowned self] in
self.loadReader?.read()
}
self.settingsView.setInterval = { [unowned self] value in
self.loadReader?.setInterval(value)
}
self.loadReader?.readyCallback = { [unowned self] in
self.readyHandler()

View File

@@ -29,8 +29,6 @@ internal class LoadReader: Reader<CPU_Load> {
private var usagePerCore: [Double] = []
public override func setup() {
self.interval = 1500
[CTL_HW, HW_NCPU].withUnsafeBufferPointer() { mib in
var sizeOfNumCPUs: size_t = MemoryLayout<uint>.size
let status = sysctl(processor_info_array_t(mutating: mib.baseAddress), 2, &numCPUs, &sizeOfNumCPUs, nil, 0)

View File

@@ -15,22 +15,30 @@ import ModuleKit
internal class Settings: NSView, Settings_v {
private var hyperthreadState: Bool = false
private var updateIntervalValue: String = "1"
private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"]
private let title: String
private let store: UnsafePointer<Store>?
private let store: UnsafePointer<Store>
public var callback: (() -> Void) = {}
public var setInterval: ((_ value: Double) -> Void) = {_ in }
public init(_ title: String, store: UnsafePointer<Store>?) {
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: Constants.Settings.width - (Constants.Settings.margin*2), height: 0))
self.hyperthreadState = store.pointee.bool(key: "\(self.title)_hyperhreading", defaultValue: self.hyperthreadState)
self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
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
if self.store != nil {
self.hyperthreadState = store!.pointee.bool(key: "\(self.title)_hyperhreading", defaultValue: self.hyperthreadState)
}
}
required init?(coder: NSCoder) {
@@ -40,8 +48,16 @@ internal class Settings: NSView, Settings_v {
public func load(widget: widget_t) {
self.subviews.forEach{ $0.removeFromSuperview() }
let rowHeight: CGFloat = 29
var height: CGFloat = 0
let rowHeight: CGFloat = 30
let num: CGFloat = widget == .barChart ? 1 : 0
self.addSubview(SelectTitleRow(
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * num, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
title: "Update interval",
action: #selector(changeUpdateInterval),
items: self.listOfUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateIntervalValue) sec"
))
if widget == .barChart {
self.addSubview(ToggleTitleRow(
@@ -50,13 +66,19 @@ internal class Settings: NSView, Settings_v {
action: #selector(toggleMultithreading),
state: self.hyperthreadState
))
height += rowHeight
}
if height != 0 {
height += (Constants.Settings.margin*2)
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*(num+1)) + (Constants.Settings.margin*(2+num))))
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
let newUpdateInterval = sender.title.replacingOccurrences(of: " sec", with: "")
self.updateIntervalValue = newUpdateInterval
store.pointee.set(key: "\(self.title)_updateInterval", value: self.updateIntervalValue)
if let value = Double(self.updateIntervalValue) {
self.setInterval(value)
}
self.setFrameSize(NSSize(width: self.frame.width, height: height))
}
@objc func toggleMultithreading(_ sender: NSControl) {
@@ -68,7 +90,7 @@ internal class Settings: NSView, Settings_v {
}
self.hyperthreadState = state! == .on ? true : false
self.store?.pointee.set(key: "\(self.title)_hyperhreading", value: self.hyperthreadState)
self.store.pointee.set(key: "\(self.title)_hyperhreading", value: self.hyperthreadState)
self.callback()
}
}

View File

@@ -69,8 +69,8 @@ public class Disk: Module {
private var settingsView: Settings
private var selectedDisk: String = ""
public init(_ store: UnsafePointer<Store>?) {
self.settingsView = Settings("Disk", store: store!)
public init(_ store: UnsafePointer<Store>) {
self.settingsView = Settings("Disk", store: store)
super.init(
store: store,
@@ -81,7 +81,7 @@ public class Disk: Module {
self.capacityReader = CapacityReader()
self.capacityReader?.store = store
self.selectedDisk = store!.pointee.string(key: "\(self.config.name)_disk", defaultValue: self.selectedDisk)
self.selectedDisk = store.pointee.string(key: "\(self.config.name)_disk", defaultValue: self.selectedDisk)
self.capacityReader?.readyCallback = { [unowned self] in
self.readyHandler()
@@ -97,6 +97,9 @@ public class Disk: Module {
self.settingsView.callback = { [unowned self] in
self.capacityReader?.read()
}
self.settingsView.setInterval = { [unowned self] value in
self.capacityReader?.setInterval(value)
}
if let reader = self.capacityReader {
self.addReader(reader)

View File

@@ -17,10 +17,6 @@ internal class CapacityReader: Reader<DiskList> {
private var disks: DiskList = DiskList()
public var store: UnsafePointer<Store>? = nil
public override func setup() {
self.interval = 10000
}
public override func read() {
let keys: [URLResourceKey] = [.volumeNameKey]
let removableState = store?.pointee.bool(key: "Disk_removable", defaultValue: false) ?? false

View File

@@ -14,22 +14,33 @@ import StatsKit
import ModuleKit
internal class Settings: NSView, Settings_v {
private var removableState: Bool = false
private var updateIntervalValue: String = "10"
private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"]
public var selectedDiskHandler: (String) -> Void = {_ in }
public var callback: (() -> Void) = {}
public var setInterval: ((_ value: Double) -> Void) = {_ in }
private let title: String
private let store: UnsafePointer<Store>
private var selectedDisk: String
private var button: NSPopUpButton?
private var removableState: Bool = false
public init(_ title: String, store: UnsafePointer<Store>) {
self.title = title
self.store = store
self.selectedDisk = store.pointee.string(key: "\(self.title)_disk", defaultValue: "")
self.removableState = store.pointee.bool(key: "\(self.title)_removable", defaultValue: self.removableState)
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: Constants.Settings.width - (Constants.Settings.margin*2), height: 0))
self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
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
}
@@ -42,6 +53,15 @@ internal class Settings: NSView, Settings_v {
self.subviews.forEach{ $0.removeFromSuperview() }
let rowHeight: CGFloat = 30
self.addSubview(SelectTitleRow(
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 2, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
title: "Update interval",
action: #selector(changeUpdateInterval),
items: self.listOfUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateIntervalValue) sec"
))
self.addDiskSelector()
self.addSubview(ToggleTitleRow(
@@ -51,7 +71,7 @@ internal class Settings: NSView, Settings_v {
state: self.removableState
))
self.setFrameSize(NSSize(width: self.frame.width, height: rowHeight*2 + (Constants.Settings.margin*3)))
self.setFrameSize(NSSize(width: self.frame.width, height: rowHeight*3 + (Constants.Settings.margin*4)))
}
private func addDiskSelector() {
@@ -106,4 +126,14 @@ internal class Settings: NSView, Settings_v {
self.store.pointee.set(key: "\(self.title)_removable", value: self.removableState)
self.callback()
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
let newUpdateInterval = sender.title.replacingOccurrences(of: " sec", with: "")
self.updateIntervalValue = newUpdateInterval
store.pointee.set(key: "\(self.title)_updateInterval", value: self.updateIntervalValue)
if let value = Double(self.updateIntervalValue) {
self.setInterval(value)
}
}
}

View File

@@ -36,15 +36,22 @@ public struct RAM_Usage: value_t {
public class Memory: Module {
private let popupView: Popup = Popup()
private var usageReader: UsageReader? = nil
private var settingsView: Settings
public init(_ store: UnsafePointer<Store>?) {
public init(_ store: UnsafePointer<Store>) {
self.settingsView = Settings("RAM", store: store)
super.init(
store: store,
popup: self.popupView,
settings: nil
settings: self.settingsView
)
guard self.available else { return }
self.settingsView.setInterval = { [unowned self] value in
self.usageReader?.setInterval(value)
}
self.usageReader = UsageReader()
self.usageReader?.readyCallback = { [unowned self] in

View File

@@ -0,0 +1,72 @@
//
// settings.swift
// Memory
//
// Created by Serhiy Mytrovtsiy on 11/07/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 var updateIntervalValue: String = "1"
private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"]
private let title: String
private let store: UnsafePointer<Store>
public var callback: (() -> Void) = {}
public var setInterval: ((_ value: Double) -> Void) = {_ in }
public init(_ title: String, store: UnsafePointer<Store>) {
self.title = title
self.store = store
self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
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")
}
public func load(widget: widget_t) {
self.subviews.forEach{ $0.removeFromSuperview() }
let rowHeight: CGFloat = 30
let num: CGFloat = 0
self.addSubview(SelectTitleRow(
frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * num, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
title: "Update interval",
action: #selector(changeUpdateInterval),
items: self.listOfUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateIntervalValue) sec"
))
self.setFrameSize(NSSize(width: self.frame.width, height: (rowHeight*(num+1)) + (Constants.Settings.margin*(2+num))))
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
let newUpdateInterval = sender.title.replacingOccurrences(of: " sec", with: "")
self.updateIntervalValue = newUpdateInterval
store.pointee.set(key: "\(self.title)_updateInterval", value: self.updateIntervalValue)
if let value = Double(self.updateIntervalValue) {
self.setInterval(value)
}
}
}

View File

@@ -62,8 +62,8 @@ public class Network: Module {
private let popupView: Popup = Popup()
private var settingsView: Settings
public init(_ store: UnsafePointer<Store>?) {
self.settingsView = Settings("Network", store: store!)
public init(_ store: UnsafePointer<Store>) {
self.settingsView = Settings("Network", store: store)
super.init(
store: store,

View File

@@ -18,9 +18,9 @@ public class Sensors: Module {
private let popupView: Popup = Popup()
private var settingsView: Settings
public init(_ store: UnsafePointer<Store>?, _ smc: UnsafePointer<SMCService>) {
public init(_ store: UnsafePointer<Store>, _ smc: UnsafePointer<SMCService>) {
self.sensorsReader = SensorsReader(smc)
self.settingsView = Settings("Disk", store: store!, list: &self.sensorsReader.list)
self.settingsView = Settings("Sensors", store: store, list: &self.sensorsReader.list)
super.init(
store: store,
@@ -36,6 +36,9 @@ public class Sensors: Module {
self.checkIfNoSensorsEnabled()
self.sensorsReader.read()
}
self.settingsView.setInterval = { [unowned self] value in
self.sensorsReader.setInterval(value)
}
self.sensorsReader.readyCallback = { [unowned self] in
self.readyHandler()

View File

@@ -14,19 +14,32 @@ import StatsKit
import ModuleKit
internal class Settings: NSView, Settings_v {
private var updateIntervalValue: String = "3"
private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"]
private let title: String
private let store: UnsafePointer<Store>
private var button: NSPopUpButton?
private let list: UnsafeMutablePointer<[Sensor_t]>
public var callback: (() -> Void) = {}
public var setInterval: ((_ value: Double) -> Void) = {_ in }
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))
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
self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
}
required init?(coder: NSCoder) {
@@ -45,10 +58,18 @@ internal class Settings: NSView, Settings_v {
}
let rowHeight: CGFloat = 30
let height: CGFloat = ((rowHeight+Constants.Settings.margin) * CGFloat(self.list.pointee.count)) + ((rowHeight+Constants.Settings.margin) * CGFloat(types.count))
let height: CGFloat = ((rowHeight+Constants.Settings.margin) * CGFloat(self.list.pointee.count) + rowHeight) + ((rowHeight+Constants.Settings.margin) * CGFloat(types.count) + 1)
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))
self.addSubview(SelectTitleRow(
frame: NSRect(x: Constants.Settings.margin, y: height - rowHeight, width: self.frame.width - (Constants.Settings.margin*2), height: rowHeight),
title: "Update interval",
action: #selector(changeUpdateInterval),
items: self.listOfUpdateIntervals.map{ "\($0) sec" },
selected: "\(self.updateIntervalValue) sec"
))
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 }
@@ -101,4 +122,14 @@ internal class Settings: NSView, Settings_v {
self.store.pointee.set(key: "sensor_\(id.rawValue)", value: state! == NSControl.StateValue.on)
self.callback()
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
let newUpdateInterval = sender.title.replacingOccurrences(of: " sec", with: "")
self.updateIntervalValue = newUpdateInterval
store.pointee.set(key: "\(self.title)_updateInterval", value: self.updateIntervalValue)
if let value = Double(self.updateIntervalValue) {
self.setInterval(value)
}
}
}