Skip to content

Commit

Permalink
feat: added colorize option to the Memory widget (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Apr 20, 2024
1 parent 445b842 commit 0f54422
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
55 changes: 53 additions & 2 deletions Kit/Widgets/Memory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import Cocoa
public class MemoryWidget: WidgetWrapper {
private var orderReversedState: Bool = false
private var value: (String, String) = ("0", "0")
private var percentage: Double = 0
private var pressureLevel: DispatchSource.MemoryPressureEvent = .normal
private var symbolsState: Bool = true
private var colorState: Color = .monochrome

private let width: CGFloat = 50

Expand Down Expand Up @@ -48,6 +51,7 @@ public class MemoryWidget: WidgetWrapper {
if !preview {
self.orderReversedState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_orderReversed", defaultValue: self.orderReversedState)
self.symbolsState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_symbols", defaultValue: self.symbolsState)
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
}

if preview {
Expand All @@ -72,7 +76,7 @@ public class MemoryWidget: WidgetWrapper {

let style = NSMutableParagraphStyle()
style.alignment = .right
let attributes = [
var attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9, weight: .light),
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
Expand All @@ -91,25 +95,56 @@ public class MemoryWidget: WidgetWrapper {
width += x
}

var freeColor: NSColor = .controlAccentColor
var usedColor: NSColor = .controlAccentColor
switch self.colorState {
case .systemAccent:
freeColor = .controlAccentColor
usedColor = .controlAccentColor
case .utilization:
freeColor = (1 - self.percentage).usageColor()
usedColor = self.percentage.usageColor()
case .pressure:
usedColor = self.pressureLevel.pressureColor()
freeColor = self.pressureLevel.pressureColor()
case .monochrome:
freeColor = (isDarkMode ? NSColor.white : NSColor.black)
usedColor = (isDarkMode ? NSColor.white : NSColor.black)
default:
freeColor = self.colorState.additional as? NSColor ?? .controlAccentColor
usedColor = self.colorState.additional as? NSColor ?? .controlAccentColor
}

attributes[NSAttributedString.Key.foregroundColor] = freeColor
var rect = CGRect(x: x, y: freeY, width: width - x, height: rowHeight)
var str = NSAttributedString.init(string: self.value.0, attributes: attributes)
str.draw(with: rect)

attributes[NSAttributedString.Key.foregroundColor] = usedColor
rect = CGRect(x: x, y: usedY, width: width - x, height: rowHeight)
str = NSAttributedString.init(string: self.value.1, attributes: attributes)
str.draw(with: rect)

self.setWidth(width + (Constants.Widget.margin.x*2))
}

public func setValue(_ value: (String, String)) {
public func setValue(_ value: (String, String), usedPercentage: Double) {
self.value = value
self.percentage = usedPercentage

DispatchQueue.main.async(execute: {
self.display()
})
}

public func setPressure(_ newPressureLevel: DispatchSource.MemoryPressureEvent) {
guard self.pressureLevel != newPressureLevel else { return }
self.pressureLevel = newPressureLevel
DispatchQueue.main.async(execute: {
self.display()
})
}

public override func settings() -> NSView {
let view = SettingsContainerView()

Expand All @@ -125,6 +160,13 @@ public class MemoryWidget: WidgetWrapper {
state: self.symbolsState
))

view.addArrangedSubview(selectSettingsRow(
title: localizedString("Color"),
action: #selector(self.toggleColor),
items: Color.allCases.filter({ $0 != .cluster }),
selected: self.colorState.key
))

return view
}

Expand All @@ -139,4 +181,13 @@ public class MemoryWidget: WidgetWrapper {
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_symbols", value: self.symbolsState)
self.display()
}

@objc private func toggleColor(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else { return }
if let newColor = Color.allCases.first(where: { $0.key == key }) {
self.colorState = newColor
}
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: key)
self.display()
}
}
3 changes: 2 additions & 1 deletion Modules/RAM/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ public class RAM: Module {
case let widget as MemoryWidget:
let free = Units(bytes: Int64(value.free)).getReadableMemory()
let used = Units(bytes: Int64(value.used)).getReadableMemory()
widget.setValue((free, used))
widget.setValue((free, used), usedPercentage: value.usage)
widget.setPressure(value.pressureLevel)
case let widget as Tachometer:
widget.setValue([
circle_segment(value: value.app/total, color: self.appColor),
Expand Down

0 comments on commit 0f54422

Please sign in to comment.