diff --git a/ModuleKit/reader.swift b/ModuleKit/reader.swift index 918d0041..2a7641e9 100644 --- a/ModuleKit/reader.swift +++ b/ModuleKit/reader.swift @@ -37,7 +37,7 @@ public protocol Reader_p { func unlock() -> Void func initStoreValues(title: String, store: UnsafePointer) -> Void - func setInterval(_ value: Double) -> Void + func setInterval(_ value: Int) -> Void } public protocol ReaderInternal_p { @@ -159,9 +159,9 @@ open class Reader: ReaderInternal_p { self.active = false } - public func setInterval(_ value: Double) { - os_log(.debug, log: self.log, "Set update interval: %.0f sec", value) - self.repeatTask?.reset(.seconds(value), restart: true) + public func setInterval(_ value: Int) { + os_log(.debug, log: self.log, "Set update interval: %d sec", value) + self.repeatTask?.reset(.seconds(Double(value)), restart: true) } } diff --git a/Modules/CPU/settings.swift b/Modules/CPU/settings.swift index ac0cc325..db147f7e 100644 --- a/Modules/CPU/settings.swift +++ b/Modules/CPU/settings.swift @@ -16,15 +16,14 @@ import ModuleKit internal class Settings: NSView, Settings_v { private var usagePerCoreState: Bool = false private var hyperthreadState: Bool = false - private var updateIntervalValue: String = "1" - private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"] + private var updateIntervalValue: Int = 1 private let title: String private let store: UnsafePointer private var hasHyperthreadingCores = false public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } private var hyperthreadView: NSView? = nil @@ -33,7 +32,7 @@ internal class Settings: NSView, Settings_v { self.store = store self.hyperthreadState = store.pointee.bool(key: "\(self.title)_hyperhreading", defaultValue: self.hyperthreadState) self.usagePerCoreState = store.pointee.bool(key: "\(self.title)_usagePerCore", defaultValue: self.usagePerCoreState) - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) if !self.usagePerCoreState { self.hyperthreadState = false } @@ -64,7 +63,7 @@ internal class Settings: NSView, Settings_v { 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: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -95,11 +94,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Modules/Disk/settings.swift b/Modules/Disk/settings.swift index e9bacc51..329d4ad3 100644 --- a/Modules/Disk/settings.swift +++ b/Modules/Disk/settings.swift @@ -15,12 +15,11 @@ 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"] + private var updateIntervalValue: Int = 10 public var selectedDiskHandler: (String) -> Void = {_ in } public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } private let title: String private let store: UnsafePointer @@ -32,7 +31,7 @@ internal class Settings: NSView, Settings_v { 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) - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) super.init(frame: CGRect( x: 0, @@ -60,7 +59,7 @@ internal class Settings: NSView, Settings_v { 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: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -136,11 +135,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Modules/Fans/settings.swift b/Modules/Fans/settings.swift index 53e01a5a..3ed132ba 100644 --- a/Modules/Fans/settings.swift +++ b/Modules/Fans/settings.swift @@ -14,8 +14,7 @@ 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 var updateIntervalValue: Int = 1 private let title: String private let store: UnsafePointer @@ -24,7 +23,7 @@ internal class Settings: NSView, Settings_v { private var labelState: Bool = false public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } public init(_ title: String, store: UnsafePointer, list: UnsafeMutablePointer<[Fan]>) { self.title = title @@ -41,7 +40,7 @@ internal class Settings: NSView, Settings_v { self.wantsLayer = true self.canDrawConcurrently = true - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) self.labelState = store.pointee.bool(key: "\(self.title)_label", defaultValue: self.labelState) } @@ -71,7 +70,7 @@ internal class Settings: NSView, Settings_v { frame: NSRect(x: Constants.Settings.margin, y: height - rowHeight, width: view.frame.width, height: rowHeight), title: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -124,11 +123,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Modules/GPU/settings.swift b/Modules/GPU/settings.swift index 4c99ea64..0f2aaf55 100644 --- a/Modules/GPU/settings.swift +++ b/Modules/GPU/settings.swift @@ -14,8 +14,7 @@ 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 var updateIntervalValue: Int = 1 private var selectedGPU: String private let title: String @@ -23,7 +22,7 @@ internal class Settings: NSView, Settings_v { public var selectedGPUHandler: (String) -> Void = {_ in } public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } private var hyperthreadView: NSView? = nil @@ -33,7 +32,7 @@ internal class Settings: NSView, Settings_v { self.title = title self.store = store self.selectedGPU = store.pointee.string(key: "\(self.title)_gpu", defaultValue: "") - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) super.init(frame: CGRect( x: 0, @@ -60,7 +59,7 @@ internal class Settings: NSView, Settings_v { 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: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -108,11 +107,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Modules/Memory/settings.swift b/Modules/Memory/settings.swift index 7f92dac5..861c8fa0 100644 --- a/Modules/Memory/settings.swift +++ b/Modules/Memory/settings.swift @@ -14,19 +14,18 @@ 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 var updateIntervalValue: Int = 1 private let title: String private let store: UnsafePointer public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } public init(_ title: String, store: UnsafePointer) { self.title = title self.store = store - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) super.init(frame: CGRect( x: 0, @@ -53,7 +52,7 @@ internal class Settings: NSView, Settings_v { 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: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -61,11 +60,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Modules/Sensors/settings.swift b/Modules/Sensors/settings.swift index 8609ec82..ba68ddad 100644 --- a/Modules/Sensors/settings.swift +++ b/Modules/Sensors/settings.swift @@ -15,15 +15,14 @@ import ModuleKit internal class Settings: NSView, Settings_v { private var unitsValue: String = "system" - private var updateIntervalValue: String = "3" - private let listOfUpdateIntervals: [String] = ["1", "2", "3", "5", "10", "15", "30"] + private var updateIntervalValue: Int = 3 private let title: String private let store: UnsafePointer private var button: NSPopUpButton? private let list: UnsafeMutablePointer<[Sensor_t]> public var callback: (() -> Void) = {} - public var setInterval: ((_ value: Double) -> Void) = {_ in } + public var setInterval: ((_ value: Int) -> Void) = {_ in } public init(_ title: String, store: UnsafePointer, list: UnsafeMutablePointer<[Sensor_t]>) { self.title = title @@ -40,7 +39,7 @@ internal class Settings: NSView, Settings_v { self.wantsLayer = true self.canDrawConcurrently = true - self.updateIntervalValue = store.pointee.string(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) + self.updateIntervalValue = store.pointee.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) self.unitsValue = store.pointee.string(key: "temperature_units", defaultValue: self.unitsValue) } @@ -77,7 +76,7 @@ internal class Settings: NSView, Settings_v { frame: NSRect(x: Constants.Settings.margin, y: height - rowHeight, width: view.frame.width, height: rowHeight), title: LocalizedString("Update interval"), action: #selector(changeUpdateInterval), - items: self.listOfUpdateIntervals.map{ "\($0) sec" }, + items: ReaderUpdateIntervals.map{ "\($0) sec" }, selected: "\(self.updateIntervalValue) sec" )) @@ -145,11 +144,9 @@ internal class Settings: NSView, Settings_v { } @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) { + if let value = Int(sender.title.replacingOccurrences(of: " sec", with: "")) { + self.updateIntervalValue = value + self.store.pointee.set(key: "\(self.title)_updateInterval", value: value) self.setInterval(value) } } diff --git a/Stats.xcodeproj/xcshareddata/xcschemes/CPU.xcscheme b/Stats.xcodeproj/xcshareddata/xcschemes/CPU.xcscheme new file mode 100644 index 00000000..c68d9506 --- /dev/null +++ b/Stats.xcodeproj/xcshareddata/xcschemes/CPU.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Stats/AppDelegate.swift b/Stats/AppDelegate.swift index 9c12710c..b03771af 100755 --- a/Stats/AppDelegate.swift +++ b/Stats/AppDelegate.swift @@ -91,7 +91,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.updateActivity.invalidate() self.updateActivity.repeats = true - guard let updateInterval = updateIntervals(rawValue: store.string(key: "update-interval", defaultValue: updateIntervals.atStart.rawValue)) else { + guard let updateInterval = AppUpdateIntervals(rawValue: store.string(key: "update-interval", defaultValue: AppUpdateIntervals.atStart.rawValue)) else { return } os_log(.debug, log: log, "Application update interval is '%s'", "\(updateInterval.rawValue)") diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index aeb2620f..aa1bd3bc 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -18,9 +18,9 @@ class ApplicationSettings: NSView { private let height: CGFloat = 480 private let deviceInfoHeight: CGFloat = 300 - private var updateIntervalValue: updateInterval { + private var updateIntervalValue: AppUpdateInterval { get { - return store.string(key: "update-interval", defaultValue: updateIntervals.atStart.rawValue) + return store.string(key: "update-interval", defaultValue: AppUpdateIntervals.atStart.rawValue) } } @@ -106,7 +106,7 @@ class ApplicationSettings: NSView { frame: NSRect(x: rowHorizontalPadding*0.5, y: rowHeight*2, width: rightPanel.frame.width - (rowHorizontalPadding*1.5), height: rowHeight), title: LocalizedString("Check for updates"), action: #selector(self.toggleUpdateInterval), - items: updateIntervals.allCases.map{ $0.rawValue }, + items: AppUpdateIntervals.allCases.map{ $0.rawValue }, selected: self.updateIntervalValue )) @@ -310,7 +310,7 @@ class ApplicationSettings: NSView { } @objc private func toggleUpdateInterval(_ sender: NSMenuItem) { - if let newUpdateInterval = updateIntervals(rawValue: sender.title) { + if let newUpdateInterval = AppUpdateIntervals(rawValue: sender.title) { store.set(key: "update-interval", value: newUpdateInterval.rawValue) NotificationCenter.default.post(name: .changeCronInterval, object: nil, userInfo: nil) } diff --git a/Stats/Views/Settings.swift b/Stats/Views/Settings.swift index 165d1c45..c2116347 100644 --- a/Stats/Views/Settings.swift +++ b/Stats/Views/Settings.swift @@ -34,8 +34,8 @@ class SettingsWindow: NSWindow, NSWindowDelegate { self.collectionBehavior = .moveToActiveSpace self.titlebarAppearsTransparent = true self.appearance = NSAppearance(named: .darkAqua) -// self.center() - self.setIsVisible(true) + self.center() + self.setIsVisible(false) let windowController = NSWindowController() windowController.window = self diff --git a/Stats/helpers.swift b/Stats/helpers.swift index f9cea225..c6ec79f9 100644 --- a/Stats/helpers.swift +++ b/Stats/helpers.swift @@ -91,7 +91,7 @@ extension AppDelegate { NSApp.setActivationPolicy(dockIconStatus) } - if updateIntervals(rawValue: store.string(key: "update-interval", defaultValue: updateIntervals.atStart.rawValue)) != .never { + if AppUpdateIntervals(rawValue: store.string(key: "update-interval", defaultValue: AppUpdateIntervals.atStart.rawValue)) != .never { self.checkForNewVersion() } } diff --git a/StatsKit/helpers.swift b/StatsKit/helpers.swift index 3811cae9..d3c16082 100644 --- a/StatsKit/helpers.swift +++ b/StatsKit/helpers.swift @@ -11,8 +11,8 @@ import Cocoa -public typealias updateInterval = String -public enum updateIntervals: updateInterval { +public typealias AppUpdateInterval = String +public enum AppUpdateIntervals: AppUpdateInterval { case atStart = "At start" case separator_1 = "separator_1" case oncePerDay = "Once per day" @@ -21,7 +21,7 @@ public enum updateIntervals: updateInterval { case separator_2 = "separator_2" case never = "Never" } -extension updateIntervals: CaseIterable {} +extension AppUpdateIntervals: CaseIterable {} public struct KeyValue_t { let key: String @@ -51,6 +51,8 @@ public let SpeedBase: [KeyValue_t] = [ KeyValue_t(key: "byte", value: "Byte", additional: DataSizeBase.byte) ] +public let ReaderUpdateIntervals: [Int] = [1, 2, 3, 5, 10, 15, 30] + public struct Units { public let bytes: Int64 diff --git a/StatsKit/store.swift b/StatsKit/store.swift index 6c04e0d9..1d93dccd 100644 --- a/StatsKit/store.swift +++ b/StatsKit/store.swift @@ -45,6 +45,10 @@ public class Store { self.defaults.set(value, forKey: key) } + public func set(key: String, value: Int) { + self.defaults.set(value, forKey: key) + } + public func reset() { self.defaults.dictionaryRepresentation().keys.forEach { key in self.defaults.removeObject(forKey: key)