mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
225 lines
7.9 KiB
Swift
225 lines
7.9 KiB
Swift
//
|
|
// NetworkChart.swift
|
|
// Kit
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 19/01/2021.
|
|
// Using Swift 5.0.
|
|
// Running on macOS 11.1.
|
|
//
|
|
// Copyright © 2021 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
public class NetworkChart: WidgetWrapper {
|
|
private var boxState: Bool = false
|
|
private var frameState: Bool = false
|
|
private var labelState: Bool = false
|
|
|
|
private var chart: NetworkChartView = NetworkChartView(
|
|
frame: NSRect(
|
|
x: 0,
|
|
y: 0,
|
|
width: 34,
|
|
height: Constants.Widget.height - (2*Constants.Widget.margin.y)
|
|
),
|
|
num: 60, minMax: false
|
|
)
|
|
private let width: CGFloat = 34
|
|
|
|
private var boxSettingsView: NSView? = nil
|
|
private var frameSettingsView: NSView? = nil
|
|
|
|
public init(title: String, config: NSDictionary?, preview: Bool = false) {
|
|
var widgetTitle: String = title
|
|
if config != nil {
|
|
if let titleFromConfig = config!["Title"] as? String {
|
|
widgetTitle = titleFromConfig
|
|
}
|
|
}
|
|
|
|
super.init(.networkChart, title: widgetTitle, frame: CGRect(
|
|
x: Constants.Widget.margin.x,
|
|
y: Constants.Widget.margin.y,
|
|
width: self.width + (2*Constants.Widget.margin.x),
|
|
height: Constants.Widget.height - (2*Constants.Widget.margin.y)
|
|
))
|
|
|
|
self.canDrawConcurrently = true
|
|
|
|
if !preview {
|
|
self.boxState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_box", defaultValue: self.boxState)
|
|
self.frameState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_frame", defaultValue: self.frameState)
|
|
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
|
|
}
|
|
|
|
if preview {
|
|
var list: [(Double, Double)] = []
|
|
for _ in 0..<60 {
|
|
list.append((Double.random(in: 0..<23), Double.random(in: 0..<23)))
|
|
}
|
|
self.chart.points = list
|
|
}
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
public override func draw(_ dirtyRect: NSRect) {
|
|
super.draw(dirtyRect)
|
|
|
|
guard let context = NSGraphicsContext.current?.cgContext else { return }
|
|
|
|
let lineWidth = 1 / (NSScreen.main?.backingScaleFactor ?? 1)
|
|
let offset = lineWidth / 2
|
|
let boxSize: CGSize = CGSize(width: self.width - (Constants.Widget.margin.x*2), height: self.frame.size.height)
|
|
var x: CGFloat = 0
|
|
var width: CGFloat = self.width
|
|
|
|
if self.labelState {
|
|
let style = NSMutableParagraphStyle()
|
|
style.alignment = .center
|
|
let stringAttributes = [
|
|
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
|
|
NSAttributedString.Key.foregroundColor: NSColor.textColor,
|
|
NSAttributedString.Key.paragraphStyle: style
|
|
]
|
|
|
|
let letterHeight = self.frame.height / 3
|
|
let letterWidth: CGFloat = 6.0
|
|
|
|
var yMargin: CGFloat = 0
|
|
for char in String(self.title.prefix(3)).uppercased().reversed() {
|
|
let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight)
|
|
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
|
|
str.draw(with: rect)
|
|
yMargin += letterHeight
|
|
}
|
|
|
|
width += letterWidth + Constants.Widget.spacing
|
|
x = letterWidth + Constants.Widget.spacing
|
|
}
|
|
|
|
let box = NSBezierPath(roundedRect: NSRect(
|
|
x: x + offset,
|
|
y: offset,
|
|
width: boxSize.width - (offset*2),
|
|
height: boxSize.height - (offset*2)
|
|
), xRadius: 2, yRadius: 2)
|
|
|
|
if self.boxState {
|
|
(isDarkMode ? NSColor.white : NSColor.black).set()
|
|
box.stroke()
|
|
box.fill()
|
|
}
|
|
|
|
context.saveGState()
|
|
|
|
self.chart.draw(NSRect(
|
|
x: x+1,
|
|
y: 1,
|
|
width: box.bounds.width - offset,
|
|
height: box.bounds.height - ((box.bounds.origin.y + lineWidth)*2)
|
|
))
|
|
|
|
context.restoreGState()
|
|
|
|
if self.boxState || self.frameState {
|
|
(isDarkMode ? NSColor.white : NSColor.black).set()
|
|
box.lineWidth = lineWidth
|
|
box.stroke()
|
|
}
|
|
|
|
self.setWidth(width)
|
|
}
|
|
|
|
public func setValue(upload: Double, download: Double) {
|
|
DispatchQueue.main.async(execute: {
|
|
self.chart.addValue(upload: upload, download: download)
|
|
self.display()
|
|
})
|
|
}
|
|
|
|
// MARK: - Settings
|
|
|
|
public override func settings(width: CGFloat) -> NSView {
|
|
let view = SettingsContainerView(width: width)
|
|
|
|
view.addArrangedSubview(toggleTitleRow(
|
|
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
|
title: localizedString("Label"),
|
|
action: #selector(toggleLabel),
|
|
state: self.labelState
|
|
))
|
|
|
|
self.boxSettingsView = toggleTitleRow(
|
|
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
|
title: localizedString("Box"),
|
|
action: #selector(toggleBox),
|
|
state: self.boxState
|
|
)
|
|
view.addArrangedSubview(self.boxSettingsView!)
|
|
|
|
self.frameSettingsView = toggleTitleRow(
|
|
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
|
title: localizedString("Frame"),
|
|
action: #selector(toggleFrame),
|
|
state: self.frameState
|
|
)
|
|
view.addArrangedSubview(self.frameSettingsView!)
|
|
|
|
return view
|
|
}
|
|
|
|
@objc private func toggleLabel(_ sender: NSControl) {
|
|
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.labelState = state! == .on ? true : false
|
|
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_label", value: self.labelState)
|
|
self.display()
|
|
}
|
|
|
|
@objc private func toggleBox(_ sender: NSControl) {
|
|
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.boxState = state! == .on ? true : false
|
|
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_box", value: self.boxState)
|
|
|
|
if self.frameState {
|
|
findAndToggleNSControlState(self.frameSettingsView, state: .off)
|
|
self.frameState = false
|
|
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_frame", value: self.frameState)
|
|
}
|
|
|
|
self.display()
|
|
}
|
|
|
|
@objc private func toggleFrame(_ sender: NSControl) {
|
|
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.frameState = state! == .on ? true : false
|
|
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_frame", value: self.frameState)
|
|
|
|
if self.boxState {
|
|
findAndToggleNSControlState(self.boxSettingsView, state: .off)
|
|
self.boxState = false
|
|
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_box", value: self.boxState)
|
|
}
|
|
|
|
self.display()
|
|
}
|
|
}
|