Skip to content

Commit

Permalink
feat: moved Sensors widget to the core graphics. Width of widget will…
Browse files Browse the repository at this point in the history
… be fixed from now (exelban#348). Also fixed different graphics glitches (exelban#374)
  • Loading branch information
exelban authored and gmcinalli committed Feb 28, 2022
1 parent c3d813e commit da3795a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 107 deletions.
174 changes: 69 additions & 105 deletions ModuleKit/Widgets/Sensors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import StatsKit

public class SensorsWidget: WidgetWrapper {
private var modeState: String = "automatic"

private var body: CALayer = CALayer()
private var values: [KeyValue_t] = []

private var oneRowWidth: CGFloat = 36
private var twoRowWidth: CGFloat = 26

public init(title: String, config: NSDictionary?, preview: Bool = false) {
if config != nil {
var configuration = config!
Expand All @@ -34,53 +35,47 @@ public class SensorsWidget: WidgetWrapper {
}
}

if title == "Fans" { // hack for fans. Because fan value contain RPM.
self.oneRowWidth = 66
self.twoRowWidth = 50
}

super.init(.sensors, title: title, frame: CGRect(
x: 0,
y: Constants.Widget.margin.y,
width: Constants.Widget.width,
height: Constants.Widget.height - (2*Constants.Widget.margin.y)
))

self.modeState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_mode", defaultValue: self.modeState)
self.canDrawConcurrently = true

self.wantsLayer = true
self.layerContentsRedrawPolicy = .onSetNeedsDisplay

self.body.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
self.layer?.addSublayer(self.body)

self.draw(self.values)
self.setFrameSize(NSSize(width: self.body.frame.width, height: self.frame.size.height))
if !preview {
self.modeState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_mode", defaultValue: self.modeState)
}
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

open override func updateLayer() {
self.body.sublayers?.forEach({ (l: CALayer) in
if let layer = l as? CATextLayer {
let color = NSColor.textColor.cgColor
if layer.foregroundColor != color {
layer.foregroundColor = color
}
}
})
}

private func draw(_ list: [KeyValue_t]) {
self.body.sublayers?.forEach{ $0.removeFromSuperlayer() }
public override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)

let num: Int = Int(round(Double(list.count) / 2))
var totalWidth: CGFloat = Constants.Widget.margin.x // opening space
var x: CGFloat = Constants.Widget.margin.x
guard self.values.count != 0 else {
self.setWidth(1)
return
}

let num: Int = Int(round(Double(self.values.count) / 2))
var totalWidth: CGFloat = Constants.Widget.spacing // opening space
var x: CGFloat = Constants.Widget.spacing

var i = 0
while i < list.count {
while i < self.values.count {
switch self.modeState {
case "automatic", "twoRows":
let firstSensor: KeyValue_t = list[i]
let secondSensor: KeyValue_t? = list.indices.contains(i+1) ? list[i+1] : nil
let firstSensor: KeyValue_t = self.values[i]
let secondSensor: KeyValue_t? = self.values.indices.contains(i+1) ? self.values[i+1] : nil

var width: CGFloat = 0
if self.modeState == "automatic" && secondSensor == nil {
Expand All @@ -93,8 +88,8 @@ public class SensorsWidget: WidgetWrapper {
totalWidth += width

if num != 1 && (i/2) != num {
x += Constants.Widget.margin.x
totalWidth += Constants.Widget.margin.x
x += Constants.Widget.spacing
totalWidth += Constants.Widget.spacing
}

i += 1
Expand All @@ -105,109 +100,79 @@ public class SensorsWidget: WidgetWrapper {
totalWidth += width

// add margins between columns
if list.count != 1 && i != list.count {
x += Constants.Widget.margin.x
totalWidth += Constants.Widget.margin.x
if self.values.count != 1 && i != self.values.count {
x += Constants.Widget.spacing
totalWidth += Constants.Widget.spacing
}
default: break
}

i += 1
}
totalWidth += Constants.Widget.margin.x // closing space
totalWidth += Constants.Widget.spacing // closing space

if abs(self.frame.width - totalWidth) < 2 {
return
}
var frame = self.body.frame
frame.size = CGSize(width: totalWidth, height: frame.height)
self.body.frame = frame
self.setWidth(totalWidth)
}

private func drawOneRow(_ sensor: KeyValue_t, x: CGFloat) -> CGFloat {
var font: NSFont = NSFont.systemFont(ofSize: 14, weight: .regular)
var font: NSFont = NSFont.systemFont(ofSize: 13, weight: .regular)
if #available(OSX 10.15, *) {
font = NSFont.monospacedSystemFont(ofSize: 14, weight: .regular)
font = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
}
let width = sensor.value.widthOfString(usingFont: font).rounded(.up) + 2

let value = CAText(fontSize: 14)
value.name = sensor.key
value.frame = CGRect(x: x, y: ((Constants.Widget.height-14)/2) - 2, width: width, height: 16)
value.font = font
value.string = sensor.value
value.alignmentMode = .right

self.body.addSublayer(value)

return width
let style = NSMutableParagraphStyle()
style.alignment = .center
let rect = CGRect(x: x, y: (Constants.Widget.height-13)/2, width: self.oneRowWidth, height: 13)
let str = NSAttributedString.init(string: sensor.value, attributes: [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
])
str.draw(with: rect)

return self.oneRowWidth
}

private func drawTwoRows(topSensor: KeyValue_t, bottomSensor: KeyValue_t?, x: CGFloat) -> CGFloat {
var font: NSFont = NSFont.systemFont(ofSize: 10, weight: .medium)
let rowHeight: CGFloat = self.frame.height / 2

var font: NSFont = NSFont.systemFont(ofSize: 10, weight: .light)
if #available(OSX 10.15, *) {
font = NSFont.monospacedSystemFont(ofSize: 10, weight: .medium)
font = NSFont.monospacedSystemFont(ofSize: 10, weight: .light)
}
let style = NSMutableParagraphStyle()
style.alignment = .right

let attributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
]

var rect = CGRect(x: x, y: rowHeight+1, width: self.twoRowWidth, height: rowHeight)
var str = NSAttributedString.init(string: topSensor.value, attributes: attributes)
str.draw(with: rect)

if bottomSensor != nil {
rect = CGRect(x: x, y: 1, width: self.twoRowWidth, height: rowHeight)
str = NSAttributedString.init(string: bottomSensor!.value, attributes: attributes)
str.draw(with: rect)
}

let firstRowWidth = topSensor.value.widthOfString(usingFont: font)
let secondRowWidth = bottomSensor?.value.widthOfString(usingFont: font)

let width = max(20, max(firstRowWidth, secondRowWidth ?? 0)).rounded(.up) + 2
let height: CGFloat = self.frame.height / 2

let topValue = CAText(fontSize: 10)
topValue.name = topSensor.key
topValue.frame = CGRect(x: x, y: height+1, width: width, height: height+1)
topValue.font = font
topValue.string = topSensor.value
topValue.alignmentMode = .right

let bottomValue = CAText(fontSize: 10)
bottomValue.name = bottomSensor?.key
bottomValue.frame = CGRect(x: x, y: 1, width: width, height: height+1)
bottomValue.font = font
bottomValue.string = bottomSensor?.value ?? ""
bottomValue.alignmentMode = .right

self.body.addSublayer(topValue)
self.body.addSublayer(bottomValue)

return width
return self.twoRowWidth
}

public func setValues(_ values: [KeyValue_t]) {
self.values = values

DispatchQueue.main.async(execute: {
if self.body.sublayers?.count != values.count {
self.redraw(values)
} else {
values.forEach { (new: KeyValue_t) in
guard let caLayer = self.body.sublayers!.first(where: { $0.name == new.key }) else {
self.redraw(values)
return
}

if let layer = caLayer as? CAText, layer.string as! String != new.value {
CATransaction.disableAnimations {
layer.string = new.value
if abs(layer.frame.width - layer.getWidth(add: 2)) > 2 {
self.redraw(values)
return
}
}
}
}
self.needsDisplay = true
}
self.needsDisplay = true
})
}

private func redraw(_ values: [KeyValue_t]) {
self.draw(values)
self.setWidth(self.body.frame.width)
}

// MARK: - Settings

public override func settings(width: CGFloat) -> NSView {
Expand Down Expand Up @@ -238,6 +203,5 @@ public class SensorsWidget: WidgetWrapper {
}
self.modeState = key
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_mode", value: key)
self.redraw(self.values)
}
}
2 changes: 1 addition & 1 deletion Modules/Sensors/values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct Sensor_t {
let val = value >= 100 ? "\(Int(value))" : String(format: "%.1f", value)
return "\(val)\(unit)"
case .fan:
return "\(Int(value)) \(unit)"
return "\(Int(value))"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Stats/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>231</string>
<string>235</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>
Expand Down

0 comments on commit da3795a

Please sign in to comment.