mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
started with chart
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
9A5B1CBF229E78F0008B9D3C /* Observable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5B1CBE229E78F0008B9D3C /* Observable.swift */; };
|
||||
9A5B1CC5229E7B40008B9D3C /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5B1CC4229E7B40008B9D3C /* Extensions.swift */; };
|
||||
9A6CFC0122A1C9F5001E782D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9A6CFC0022A1C9F5001E782D /* Assets.xcassets */; };
|
||||
9A7B8F5B22A290A200DEB352 /* CPU.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9A7B8F5A22A290A200DEB352 /* CPU.xib */; };
|
||||
9A7B8F5E22A2A57600DEB352 /* CPUReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7B8F5D22A2A57600DEB352 /* CPUReader.swift */; };
|
||||
9A7B8F6522A2C19D00DEB352 /* Memory.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9A7B8F6422A2C19D00DEB352 /* Memory.xib */; };
|
||||
9A7B8F6722A2C1B900DEB352 /* Disk.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9A7B8F6622A2C1B900DEB352 /* Disk.xib */; };
|
||||
@@ -55,7 +54,6 @@
|
||||
9A5B1CBE229E78F0008B9D3C /* Observable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observable.swift; sourceTree = "<group>"; };
|
||||
9A5B1CC4229E7B40008B9D3C /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
|
||||
9A6CFC0022A1C9F5001E782D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
9A7B8F5A22A290A200DEB352 /* CPU.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CPU.xib; sourceTree = "<group>"; };
|
||||
9A7B8F5D22A2A57600DEB352 /* CPUReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPUReader.swift; sourceTree = "<group>"; };
|
||||
9A7B8F6422A2C19D00DEB352 /* Memory.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Memory.xib; sourceTree = "<group>"; };
|
||||
9A7B8F6622A2C1B900DEB352 /* Disk.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Disk.xib; sourceTree = "<group>"; };
|
||||
@@ -156,7 +154,6 @@
|
||||
9A7B8F5C22A2926500DEB352 /* CPU */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
9A7B8F5A22A290A200DEB352 /* CPU.xib */,
|
||||
9A57A19C22A1E3270033E318 /* CPU.swift */,
|
||||
9A7B8F5D22A2A57600DEB352 /* CPUReader.swift */,
|
||||
);
|
||||
@@ -284,7 +281,6 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
9A7B8F5B22A290A200DEB352 /* CPU.xib in Resources */,
|
||||
9A6CFC0122A1C9F5001E782D /* Assets.xcassets in Resources */,
|
||||
9A7B8F6722A2C1B900DEB352 /* Disk.xib in Resources */,
|
||||
9A7B8F6522A2C19D00DEB352 /* Memory.xib in Resources */,
|
||||
|
||||
@@ -8,9 +8,111 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ChartView: NSView {
|
||||
var valueLabel: NSTextField = NSTextField()
|
||||
|
||||
var label: Bool = false
|
||||
var points: [Double] {
|
||||
didSet {
|
||||
setNeedsDisplay(self.frame)
|
||||
}
|
||||
}
|
||||
|
||||
override init(frame: NSRect) {
|
||||
self.points = Array(repeating: 0.0, count: 50)
|
||||
super.init(frame: frame)
|
||||
|
||||
self.wantsLayer = true
|
||||
|
||||
if self.label {
|
||||
let valueLabel = NSTextField(frame: NSMakeRect(2, MODULE_HEIGHT - 11, self.frame.size.width, 10))
|
||||
valueLabel.textColor = NSColor.red
|
||||
valueLabel.isEditable = false
|
||||
valueLabel.isSelectable = false
|
||||
valueLabel.isBezeled = false
|
||||
valueLabel.wantsLayer = true
|
||||
valueLabel.textColor = .labelColor
|
||||
valueLabel.backgroundColor = .controlColor
|
||||
valueLabel.canDrawSubviewsIntoLayer = true
|
||||
valueLabel.alignment = .natural
|
||||
valueLabel.font = NSFont.systemFont(ofSize: 8, weight: .ultraLight)
|
||||
valueLabel.stringValue = ""
|
||||
valueLabel.addSubview(NSView())
|
||||
|
||||
self.valueLabel = valueLabel
|
||||
self.addSubview(self.valueLabel)
|
||||
} else {
|
||||
self.addSubview(NSView())
|
||||
}
|
||||
}
|
||||
|
||||
required init?(coder decoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
|
||||
if (self.points.count < 2) {
|
||||
return
|
||||
}
|
||||
|
||||
let xOffset: CGFloat = 4.0
|
||||
let yOffset: CGFloat = 3.0
|
||||
var height: Double = Double(self.frame.size.height) - Double((yOffset * 2))
|
||||
if self.label {
|
||||
height = 7.0
|
||||
}
|
||||
let xRatio = Double(self.frame.size.width - (xOffset * 2)) / (Double(self.points.count) - 1)
|
||||
|
||||
let chartLine = NSBezierPath()
|
||||
chartLine.lineWidth = 0.5
|
||||
|
||||
for i in 0..<self.points.count {
|
||||
let x: CGFloat = CGFloat((Double(i) * xRatio)) + xOffset
|
||||
let y: CGFloat = CGFloat((Double(truncating: points[i] as NSNumber) * height)) + yOffset
|
||||
let point = CGPoint(x: x, y: y)
|
||||
|
||||
if i == 0 {
|
||||
chartLine.move(to: point)
|
||||
} else {
|
||||
chartLine.line(to: point)
|
||||
}
|
||||
}
|
||||
// chartLine.close()
|
||||
|
||||
NSColor.blue.setStroke()
|
||||
chartLine.stroke()
|
||||
|
||||
// let gradient: NSGradient = NSGradient(starting: NSColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), ending: NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))!
|
||||
// gradient.draw(in: chartLine, angle: 0.0)
|
||||
}
|
||||
|
||||
func addValue(value: Double) {
|
||||
if self.label {
|
||||
self.valueLabel.stringValue = "\(Int(Float(Float(value).roundTo(decimalPlaces: 2))! * 100))%"
|
||||
self.valueLabel.textColor = Float(value).usageColor()
|
||||
}
|
||||
|
||||
if self.points.count < 50 {
|
||||
self.points.append(value)
|
||||
return
|
||||
}
|
||||
|
||||
for (i, _) in self.points.enumerated() {
|
||||
if i+1 < self.points.count {
|
||||
self.points[i] = self.points[i+1]
|
||||
} else {
|
||||
self.points[i] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CPU: Module {
|
||||
let name: String = "CPU"
|
||||
var view: NSView = NSView()
|
||||
var chart: ChartView = ChartView()
|
||||
let defaults = UserDefaults.standard
|
||||
|
||||
var active: Observable<Bool>
|
||||
@@ -20,26 +122,30 @@ class CPU: Module {
|
||||
|
||||
init() {
|
||||
self.active = Observable(defaults.object(forKey: name) != nil ? defaults.bool(forKey: name) : true)
|
||||
self.view = loadViewFromNib()
|
||||
self.chart = ChartView(frame: NSMakeRect(0, 0, MODULE_WIDTH + 7, MODULE_HEIGHT))
|
||||
self.view.wantsLayer = true
|
||||
self.view = self.chart
|
||||
}
|
||||
|
||||
func start() {
|
||||
if !self.reader.usage.value.isNaN {
|
||||
self.value.stringValue = "\(Int(Float(self.reader.usage.value.roundTo(decimalPlaces: 2))! * 100))%"
|
||||
self.value.textColor = self.reader.usage.value.usageColor()
|
||||
self.chart.addValue(value: Double(self.reader.usage!.value))
|
||||
// self.value.stringValue = "\(Int(Float(self.reader.usage.value.roundTo(decimalPlaces: 2))! * 100))%"
|
||||
// self.value.textColor = self.reader.usage.value.usageColor()
|
||||
}
|
||||
|
||||
//
|
||||
self.reader.start()
|
||||
self.reader.usage.subscribe(observer: self) { (value, _) in
|
||||
if !value.isNaN {
|
||||
self.value.stringValue = "\(Int(Float(value.roundTo(decimalPlaces: 2))! * 100))%"
|
||||
self.value.textColor = value.usageColor()
|
||||
self.chart.addValue(value: Double(self.reader.usage!.value))
|
||||
// self.value.stringValue = "\(Int(Float(value.roundTo(decimalPlaces: 2))! * 100))%"
|
||||
// self.value.textColor = value.usageColor()
|
||||
}
|
||||
}
|
||||
|
||||
colors.subscribe(observer: self) { (value, _) in
|
||||
self.value.textColor = self.reader.usage.value.usageColor()
|
||||
}
|
||||
//
|
||||
// colors.subscribe(observer: self) { (value, _) in
|
||||
// self.value.textColor = self.reader.usage.value.usageColor()
|
||||
// }
|
||||
}
|
||||
|
||||
func menu() -> NSMenuItem {
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<deployment identifier="macosx"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<customObject id="-2" userLabel="File's Owner">
|
||||
<connections>
|
||||
<outlet property="value" destination="TXn-kg-jq1" id="e0R-gJ-2zT"/>
|
||||
</connections>
|
||||
</customObject>
|
||||
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
|
||||
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
|
||||
<customView identifier="CPU" id="c22-O7-iKe">
|
||||
<rect key="frame" x="0.0" y="0.0" width="28" height="22"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
|
||||
<subviews>
|
||||
<customView translatesAutoresizingMaskIntoConstraints="NO" id="fuk-Yl-Mga">
|
||||
<rect key="frame" x="0.0" y="0.0" width="28" height="22"/>
|
||||
<subviews>
|
||||
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="LO0-sZ-E6r">
|
||||
<rect key="frame" x="-2" y="11" width="24" height="9"/>
|
||||
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="CPU" id="TrQ-4e-Tib">
|
||||
<font key="font" metaFont="systemLight" size="7"/>
|
||||
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
|
||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
</textFieldCell>
|
||||
</textField>
|
||||
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="TXn-kg-jq1">
|
||||
<rect key="frame" x="-2" y="0.0" width="8" height="13"/>
|
||||
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" id="3Xv-9c-3y5">
|
||||
<font key="font" metaFont="system" size="10"/>
|
||||
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
|
||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
</textFieldCell>
|
||||
</textField>
|
||||
</subviews>
|
||||
<constraints>
|
||||
<constraint firstItem="LO0-sZ-E6r" firstAttribute="top" secondItem="fuk-Yl-Mga" secondAttribute="top" constant="2" id="0SG-E2-zVZ"/>
|
||||
<constraint firstItem="TXn-kg-jq1" firstAttribute="top" secondItem="fuk-Yl-Mga" secondAttribute="top" constant="9" id="1WZ-E9-iKh"/>
|
||||
<constraint firstAttribute="width" constant="28" id="TeP-YH-zJb"/>
|
||||
<constraint firstItem="LO0-sZ-E6r" firstAttribute="leading" secondItem="fuk-Yl-Mga" secondAttribute="leading" id="qf8-uc-Nqz"/>
|
||||
<constraint firstItem="TXn-kg-jq1" firstAttribute="leading" secondItem="fuk-Yl-Mga" secondAttribute="leading" id="uie-Bo-uLC"/>
|
||||
<constraint firstAttribute="height" constant="22" id="yuY-bI-RAD"/>
|
||||
</constraints>
|
||||
</customView>
|
||||
</subviews>
|
||||
<constraints>
|
||||
<constraint firstItem="fuk-Yl-Mga" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" id="IlM-4E-if4"/>
|
||||
<constraint firstItem="fuk-Yl-Mga" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" id="n6Y-Dl-WZW"/>
|
||||
</constraints>
|
||||
<point key="canvasLocation" x="-248" y="-5"/>
|
||||
</customView>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -37,3 +37,15 @@ public enum Unit : Float {
|
||||
case gigabyte = 1073741824
|
||||
}
|
||||
|
||||
//extension NSView {
|
||||
// var backgroundColor: NSColor? {
|
||||
// get {
|
||||
// guard let color = layer?.backgroundColor else { return nil }
|
||||
// return NSColor(cgColor: color)
|
||||
// }
|
||||
// set {
|
||||
// wantsLayer = true
|
||||
// layer?.backgroundColor = newValue?.cgColor
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user