mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
89 lines
3.0 KiB
Swift
89 lines
3.0 KiB
Swift
//
|
|
// Text.swift
|
|
// Kit
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 08/09/2024
|
|
// Using Swift 5.0
|
|
// Running on macOS 14.6
|
|
//
|
|
// Copyright © 2024 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
public class TextWidget: WidgetWrapper {
|
|
private var value: String = ""
|
|
|
|
public init(title: String, config: NSDictionary?, preview: Bool = false) {
|
|
super.init(.text, title: title, frame: CGRect(
|
|
x: 0,
|
|
y: Constants.Widget.margin.y,
|
|
width: 30 + (2*Constants.Widget.margin.x),
|
|
height: Constants.Widget.height - (2*Constants.Widget.margin.y)
|
|
))
|
|
|
|
if preview {
|
|
self.value = "Text"
|
|
}
|
|
|
|
self.canDrawConcurrently = true
|
|
}
|
|
|
|
required public init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
public override func draw(_ dirtyRect: NSRect) {
|
|
super.draw(dirtyRect)
|
|
|
|
var value: String = ""
|
|
self.queue.sync {
|
|
value = self.value
|
|
}
|
|
|
|
if value.isEmpty {
|
|
self.setWidth(0)
|
|
return
|
|
}
|
|
|
|
let valueSize: CGFloat = 12
|
|
let style = NSMutableParagraphStyle()
|
|
style.alignment = .center
|
|
let stringAttributes = [
|
|
NSAttributedString.Key.font: NSFont.systemFont(ofSize: valueSize, weight: .regular),
|
|
NSAttributedString.Key.foregroundColor: NSColor.textColor,
|
|
NSAttributedString.Key.paragraphStyle: style
|
|
]
|
|
let attributedString = NSAttributedString(string: value, attributes: stringAttributes)
|
|
let size = attributedString.boundingRect(
|
|
with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
options: [.usesLineFragmentOrigin, .usesFontLeading]
|
|
)
|
|
let width = (size.width+Constants.Widget.margin.x*2).roundedUpToNearestTen()
|
|
let origin: CGPoint = CGPoint(x: Constants.Widget.margin.x, y: ((Constants.Widget.height-valueSize-1)/2))
|
|
let rect = CGRect(x: origin.x, y: origin.y, width: width - (Constants.Widget.margin.x*2), height: valueSize)
|
|
attributedString.draw(with: rect)
|
|
|
|
self.setWidth(width)
|
|
}
|
|
|
|
public func setValue(_ newValue: String) {
|
|
guard self.value != newValue else { return }
|
|
self.value = newValue
|
|
DispatchQueue.main.async(execute: {
|
|
self.display()
|
|
})
|
|
}
|
|
|
|
static public func parseText(_ raw: String) -> [KeyValue_t] {
|
|
var pairs: [KeyValue_t] = []
|
|
raw.split(separator: " ", omittingEmptySubsequences: true).filter({ $0.hasPrefix("$") }).forEach { v in
|
|
let arr = v.split(separator: ".", omittingEmptySubsequences: true)
|
|
guard let key = arr.first else { return }
|
|
let value = arr.count == 1 ? nil : arr.last
|
|
pairs.append(KeyValue_t(key: String(key), value: String(value ?? "")))
|
|
}
|
|
return pairs
|
|
}
|
|
}
|