-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
92 lines (74 loc) · 2.1 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"time"
"github.com/diamondburned/gotk4/pkg/cairo"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/pango"
)
type UI struct {
window *gtk.Window
drawing *gtk.DrawingArea
infoLabel *gtk.Label
plotter Plotter
collector *NVMLCollector
storage *DataStorage
}
func NewUI(plotter Plotter, collector *NVMLCollector, storage *DataStorage) *UI {
ui := &UI{
plotter: plotter,
collector: collector,
storage: storage,
}
ui.window = gtk.NewWindow()
ui.window.SetTitle("GPU Usage")
ui.window.SetDefaultSize(800, 600)
vbox := gtk.NewBox(gtk.OrientationVertical, 0)
titleLabel := gtk.NewLabel("GPU Usage")
titleLabel.SetHAlign(gtk.AlignCenter)
titleLabel.SetMarginTop(10)
titleLabel.SetMarginBottom(10)
boldAttrlist := pango.NewAttrList()
// arrts := []*pango.Attribute{pango.NewAttrWeight(pango.WeightBold), pango.NewAttrScale(1.5)}
boldAttrlist.Insert(pango.NewAttrWeight(pango.WeightBold))
boldAttrlist.Insert(pango.NewAttrScale(1.5))
titleLabel.SetAttributes(boldAttrlist)
ui.infoLabel = gtk.NewLabel("")
ui.infoLabel.SetHAlign(gtk.AlignCenter)
ui.infoLabel.SetMarginBottom(10)
ui.drawing = gtk.NewDrawingArea()
ui.drawing.SetVExpand(true)
ui.drawing.SetDrawFunc(ui.draw)
vbox.Append(titleLabel)
vbox.Append(ui.infoLabel)
vbox.Append(ui.drawing)
ui.window.SetChild(vbox)
return ui
}
func (ui *UI) draw(area *gtk.DrawingArea, cr *cairo.Context, width, height int) {
ui.plotter.Plot(area, cr, width, height)
}
func (ui *UI) Run() {
ui.window.SetVisible(true)
go ui.updateData()
}
func (ui *UI) updateData() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for range ticker.C {
memInfo, err := ui.collector.GetVRAMUsage()
if err != nil {
fmt.Println("Error getting VRAM usage:", err)
continue
}
ui.storage.AddDataPoint(memInfo)
ui.updateInfoLabel(memInfo)
ui.drawing.QueueDraw()
}
}
func (ui *UI) updateInfoLabel(memInfo MemoryInfo) {
ui.infoLabel.SetMarkup(fmt.Sprintf(
"<b>Total:</b> %d MB | <b>Used:</b> %d MB | <b>Free:</b> %d MB",
memInfo.TotalMB, memInfo.UsedMB, memInfo.FreeMB,
))
}