Skip to content

Commit b259d06

Browse files
committed
feat: added GPU preview (disabled for now)
1 parent ca5cd14 commit b259d06

10 files changed

Lines changed: 328 additions & 58 deletions

File tree

Kit/module/module.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ open class Module {
9898
private var popup: PopupWindow? = nil
9999
private var popupView: Popup_p? = nil
100100
private var notificationsView: NotificationsWrapper? = nil
101-
private var previewView: Preview_v? = nil
101+
private var previewView: PreviewWrapper? = nil
102102

103103
private let log: NextLog
104104
private var readers: [Reader_p] = []
@@ -114,7 +114,7 @@ open class Module {
114114
settings: Settings_v? = nil,
115115
portal: Portal_p? = nil,
116116
notifications: NotificationsWrapper? = nil,
117-
preview: Preview_v? = nil
117+
preview: PreviewWrapper? = nil
118118
) {
119119
self.moduleType = moduleType
120120
self.portal = portal

Kit/module/window.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@ public protocol Settings_v: NSView {
1515
func load(widgets: [widget_t])
1616
}
1717

18-
public protocol Preview_v: NSView {}
18+
open class PreviewWrapper: NSStackView {
19+
public let module: ModuleType
20+
21+
public init(type: ModuleType) {
22+
self.module = type
23+
super.init(frame: NSRect.zero)
24+
25+
self.orientation = .vertical
26+
self.distribution = .gravityAreas
27+
self.translatesAutoresizingMaskIntoConstraints = false
28+
self.spacing = Constants.Settings.margin
29+
}
30+
31+
required public init?(coder: NSCoder) {
32+
fatalError("init(coder:) has not been implemented")
33+
}
34+
}
1935

2036
open class Window: NSStackView {
2137
private var config: UnsafePointer<module_c>
@@ -24,7 +40,7 @@ open class Window: NSStackView {
2440
private var segmentedControl: NSSegmentedControl?
2541
private var tabView: NSTabView?
2642

27-
private var modulePreview: Preview_v?
43+
private var modulePreview: PreviewWrapper?
2844
private var moduleSettings: Settings_v?
2945
private var popupSettings: Popup_p?
3046
private var notificationsSettings: NotificationsWrapper?
@@ -59,7 +75,7 @@ open class Window: NSStackView {
5975
init(
6076
config: UnsafePointer<module_c>,
6177
widgets: UnsafeMutablePointer<[SWidget]>,
62-
modulePreview: Preview_v?,
78+
modulePreview: PreviewWrapper?,
6379
moduleSettings: Settings_v?,
6480
popupSettings: Popup_p?,
6581
notificationsSettings: NotificationsWrapper?

Kit/plugins/Charts.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ public class LineChartView: ChartView {
165165
private var cursor: NSPoint? = nil
166166
private var stop: Bool = false
167167

168+
private var tooltipEnabledSnapshot: Bool {
169+
self.read { self.isTooltipEnabled }
170+
}
171+
168172
public init(frame: NSRect = .zero, num: Int, suffix: String = "%", color: NSColor = .controlAccentColor, scale: Scale = .none, fixedScale: Double = 1, zeroValue: Double = 0.01) {
169173
self.points = Array(repeating: nil, count: max(num, 1))
170174
self.suffix = suffix
@@ -545,10 +549,6 @@ public class LineChartView: ChartView {
545549
self.displayIfVisible()
546550
}
547551

548-
private var tooltipEnabledSnapshot: Bool {
549-
self.read { self.isTooltipEnabled }
550-
}
551-
552552
public override func mouseEntered(with event: NSEvent) {
553553
guard self.tooltipEnabledSnapshot else { return }
554554
self.cursor = convert(event.locationInWindow, from: nil)
@@ -746,11 +746,11 @@ public class PieChartView: ChartView {
746746
let arcWidth: CGFloat = filled ? min(self.frame.width, self.frame.height) / 2 : 7
747747
let fullCircle: CGFloat = 2 * CGFloat.pi
748748
let arcSpan: CGFloat = openCircle ? (3/2) * CGFloat.pi : fullCircle
749+
if segments.isEmpty {
750+
segments = [ColorValue(value ?? 0, color: color)]
751+
}
749752

750753
if openCircle {
751-
if segments.isEmpty {
752-
segments = [ColorValue(value ?? 0, color: color)]
753-
}
754754
let totalAmount = segments.reduce(0) { $0 + $1.value }
755755
if totalAmount < 1 {
756756
segments.append(ColorValue(Double(1-totalAmount), color: NSColor.lightGray.withAlphaComponent(0.5)))
@@ -1203,8 +1203,6 @@ public class BarChartView: ChartView {
12031203
values = self.values
12041204
}
12051205

1206-
guard !values.isEmpty else { return }
1207-
12081206
let totalValue = values.reduce(0) { $0 + $1.value }
12091207
if totalValue < 1 {
12101208
values.append(ColorValue(1 - totalValue, color: NSColor.lightGray.withAlphaComponent(0.25)))

Modules/CPU/preview.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import Cocoa
1313
import Kit
1414

15-
internal class Preview: NSStackView, Preview_v {
15+
internal class Preview: PreviewWrapper {
1616
private var initialized: Bool = false
1717
private var initializedAverage: Bool = false
1818
private var initializedFrequency: Bool = false
@@ -28,23 +28,23 @@ internal class Preview: NSStackView, Preview_v {
2828

2929
private var eCoresColor: NSColor {
3030
let color = SColor.teal
31-
let key = Store.shared.string(key: "CPU_eCoresColor", defaultValue: color.key)
31+
let key = Store.shared.string(key: "\(self.module.stringValue)_eCoresColor", defaultValue: color.key)
3232
if let c = SColor.fromString(key).additional as? NSColor {
3333
return c
3434
}
3535
return color.additional as! NSColor
3636
}
3737
private var pCoresColor: NSColor {
3838
let color = SColor.indigo
39-
let key = Store.shared.string(key: "CPU_pCoresColor", defaultValue: color.key)
39+
let key = Store.shared.string(key: "\(self.module.stringValue)_pCoresColor", defaultValue: color.key)
4040
if let c = SColor.fromString(key).additional as? NSColor {
4141
return c
4242
}
4343
return color.additional as! NSColor
4444
}
4545
private var sCoresColor: NSColor {
4646
let color = SColor.orange
47-
let key = Store.shared.string(key: "CPU_sCoresColor", defaultValue: color.key)
47+
let key = Store.shared.string(key: "\(self.module.stringValue)_sCoresColor", defaultValue: color.key)
4848
if let c = SColor.fromString(key).additional as? NSColor {
4949
return c
5050
}
@@ -68,17 +68,8 @@ internal class Preview: NSStackView, Preview_v {
6868

6969
private var cores: [CoreView] = []
7070

71-
private var loadLineChartHistory: Int = 180
72-
private var loadLineChartScale: Scale = .none
73-
private var loadLineChartFixedScale: Double = 1
74-
7571
public init(_ module: ModuleType) {
76-
super.init(frame: NSRect.zero)
77-
78-
self.orientation = .vertical
79-
self.distribution = .gravityAreas
80-
self.translatesAutoresizingMaskIntoConstraints = false
81-
self.spacing = Constants.Settings.margin
72+
super.init(type: module)
8273

8374
self.addArrangedSubview(PreferencesSection([self.totalView()]))
8475
self.addArrangedSubview(PreferencesSection(label: localizedString("Usage history"), [self.historyView()]))
@@ -175,7 +166,7 @@ internal class Preview: NSStackView, Preview_v {
175166
view.spacing = Constants.Settings.margin*2
176167
view.heightAnchor.constraint(equalToConstant: 140).isActive = true
177168

178-
let chart = LineChartView(num: self.loadLineChartHistory, scale: self.loadLineChartScale, fixedScale: self.loadLineChartFixedScale)
169+
let chart = LineChartView(num: 600)
179170
chart.setColor(self.chartColor)
180171
chart.setLegend(x: true, y: true)
181172
self.loadLineChart = chart

Modules/Disk/main.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ public class Disks: Codable, RemoteType {
165165
}
166166

167167
public func remote() -> Data? {
168-
var string = "\(self.array.count),"
169-
for (i, v) in self.array.enumerated() {
168+
let arr = self.array.filter({ !$0.removable })
169+
var string = "\(arr.count),"
170+
for (i, v) in arr.enumerated() {
170171
string += v.remote()
171172
if i != self.array.count {
172173
string += ","

Modules/GPU/config.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,10 @@
8080
<key>notifications</key>
8181
<true/>
8282
</dict>
83+
<key>Preview</key>
84+
<dict>
85+
<key>enabled</key>
86+
<false/>
87+
</dict>
8388
</dict>
8489
</plist>

Modules/GPU/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public class GPU: Module {
111111
private let settingsView: Settings
112112
private let portalView: Portal
113113
private let notificationsView: Notifications
114+
private let previewView: Preview
114115

115116
private var infoReader: InfoReader? = nil
116117

@@ -131,13 +132,15 @@ public class GPU: Module {
131132
self.settingsView = Settings(.GPU)
132133
self.portalView = Portal(.GPU)
133134
self.notificationsView = Notifications(.GPU)
135+
self.previewView = Preview(.GPU)
134136

135137
super.init(
136138
moduleType: .GPU,
137139
popup: self.popupView,
138140
settings: self.settingsView,
139141
portal: self.portalView,
140-
notifications: self.notificationsView
142+
notifications: self.notificationsView,
143+
preview: self.previewView,
141144
)
142145
guard self.available else { return }
143146

@@ -179,6 +182,7 @@ public class GPU: Module {
179182

180183
self.portalView.callback(selectedGPU)
181184
self.notificationsView.usageCallback(utilization)
185+
self.previewView.loadCallback(selectedGPU)
182186

183187
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: SWidget) in
184188
switch w.item {

0 commit comments

Comments
 (0)