-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.go
More file actions
234 lines (189 loc) · 6.77 KB
/
Copy pathtile.go
File metadata and controls
234 lines (189 loc) · 6.77 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"image"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
)
// CoreState holds one CPU core's live data. It's the single source of truth
// that both the Tiles view (CoreTile) and the List view (CoreListRow)
// render from, so switching between views never shows stale data - whichever
// widgets exist for a core are refreshed together on every update.
type CoreState struct {
Index int
Percent float64
ClockMHz float64
UtilHistory []float64
tile *CoreTile
row *CoreListRow
}
// NewCoreState creates the state holder for one core.
func NewCoreState(index int) *CoreState {
return &CoreState{Index: index}
}
// Update records a new sample and refreshes any attached widgets.
func (c *CoreState) Update(percent, clockMHz float64, historySize int) {
c.Percent = percent
c.ClockMHz = clockMHz
c.UtilHistory = append(c.UtilHistory, percent)
if len(c.UtilHistory) > historySize {
c.UtilHistory = c.UtilHistory[1:]
}
if c.tile != nil {
c.tile.refresh()
}
if c.row != nil {
c.row.refresh()
}
}
// RefreshTheme re-applies theme-derived colors on this core's tile and row.
func (c *CoreState) RefreshTheme() {
if c.tile != nil {
c.tile.RefreshTheme()
}
if c.row != nil {
c.row.RefreshTheme()
}
}
// CoreTile is a single core's card in the CPU tab's Tiles view: core name +
// status dot, a large percentage, clock speed, and a sparkline that renders
// at the tile's actual displayed size.
type CoreTile struct {
state *CoreState
container *fyne.Container
bg *canvas.Rectangle
dot *canvas.Circle
coreLabel *canvas.Text
percentText *canvas.Text
clockLabel *canvas.Text
sparkline *canvas.Raster
}
// NewCoreTile creates a tile bound to the given core state.
func NewCoreTile(state *CoreState) *CoreTile {
bg := canvas.NewRectangle(theme.InputBackgroundColor())
bg.StrokeColor = theme.ShadowColor()
bg.StrokeWidth = 1
bg.CornerRadius = 8
coreLabel := canvas.NewText(formatCoreLabel(state.Index), theme.ForegroundColor())
coreLabel.TextStyle = fyne.TextStyle{Bold: true}
coreLabel.TextSize = 12
dot := canvas.NewCircle(GetGraphLineColor("green"))
dotBox := container.NewGridWrap(fyne.NewSize(7, 7), dot)
header := container.NewBorder(nil, nil, coreLabel, dotBox)
percentText := canvas.NewText("--%", theme.ForegroundColor())
percentText.TextStyle = fyne.TextStyle{Bold: true}
percentText.TextSize = 22
clockLabel := canvas.NewText("-- MHz", theme.PlaceHolderColor())
clockLabel.TextSize = 11
t := &CoreTile{state: state, bg: bg, dot: dot, coreLabel: coreLabel, percentText: percentText, clockLabel: clockLabel}
t.sparkline = canvas.NewRaster(func(w, h int) image.Image {
return DrawSparkline(w, h, t.state.UtilHistory)
})
info := container.NewVBox(header, percentText, clockLabel)
body := container.NewBorder(info, nil, nil, nil, t.sparkline)
padded := container.NewPadded(body)
t.container = container.NewStack(bg, padded)
state.tile = t
return t
}
// GetContainer returns the tile's canvas object.
func (t *CoreTile) GetContainer() fyne.CanvasObject {
return t.container
}
func (t *CoreTile) refresh() {
status := UtilizationStatus(t.state.Percent)
statusColor := GetGraphLineColor(status)
t.percentText.Text = formatUtilLabel(t.state.Percent)
t.percentText.Refresh()
t.clockLabel.Text = formatClockLabel(t.state.ClockMHz)
t.clockLabel.Refresh()
t.dot.FillColor = statusColor
t.dot.Refresh()
t.sparkline.Refresh()
if status == "red" {
t.bg.StrokeColor = statusColor
t.bg.StrokeWidth = 2
} else {
t.bg.StrokeColor = theme.ShadowColor()
t.bg.StrokeWidth = 1
}
t.bg.Refresh()
}
// RefreshTheme re-applies theme-derived colors. canvas.Text and
// canvas.Rectangle primitives only read theme colors once, at construction,
// so switching the app theme live (without restarting) would otherwise leave
// these stuck on the old theme's colors even though built-in widgets like
// the Tiles/List buttons update automatically.
func (t *CoreTile) RefreshTheme() {
t.bg.FillColor = theme.InputBackgroundColor()
t.coreLabel.Color = theme.ForegroundColor()
t.percentText.Color = theme.ForegroundColor()
t.clockLabel.Color = theme.PlaceHolderColor()
t.coreLabel.Refresh()
t.percentText.Refresh()
t.clockLabel.Refresh()
t.refresh()
}
// CoreListRow is a single core's row in the CPU tab's List view: core name,
// an inline utilization bar with its percentage overlaid, clock speed, and a
// compact sparkline.
type CoreListRow struct {
state *CoreState
container *fyne.Container
bar *Bar
coreLabel *canvas.Text
clockLabel *canvas.Text
sparkline *canvas.Raster
}
// NewCoreListRow creates a list row bound to the given core state.
func NewCoreListRow(state *CoreState) *CoreListRow {
coreLabel := canvas.NewText(formatCoreLabel(state.Index), theme.ForegroundColor())
coreLabel.TextStyle = fyne.TextStyle{Bold: true}
coreLabel.TextSize = 12
coreLabelBox := container.NewGridWrap(fyne.NewSize(64, 20), coreLabel)
bar := NewBar(GetGraphLineColor("green"), theme.InputBackgroundColor(), true)
clockLabel := canvas.NewText("--", theme.PlaceHolderColor())
clockLabel.TextSize = 11
clockLabel.Alignment = fyne.TextAlignTrailing
clockLabelBox := container.NewGridWrap(fyne.NewSize(44, 20), clockLabel)
r := &CoreListRow{state: state, bar: bar, coreLabel: coreLabel, clockLabel: clockLabel}
r.sparkline = canvas.NewRaster(func(w, h int) image.Image {
return DrawSparkline(w, h, r.state.UtilHistory)
})
sparklineBox := container.NewGridWrap(fyne.NewSize(64, 20), r.sparkline)
trailing := container.NewHBox(clockLabelBox, sparklineBox)
// NewBorder stretches its center object (the bar) to fill the remaining
// row width between the fixed-width label and trailer, which is what
// makes the bar flexible rather than a fixed cell size like the others.
r.container = container.NewBorder(nil, nil, coreLabelBox, trailing, bar.Container())
state.row = r
return r
}
// GetContainer returns the row's canvas object.
func (r *CoreListRow) GetContainer() fyne.CanvasObject {
return r.container
}
func (r *CoreListRow) refresh() {
status := UtilizationStatus(r.state.Percent)
statusColor := GetGraphLineColor(status)
labelColor := theme.ForegroundColor()
if status != "green" {
labelColor = theme.BackgroundColor()
}
r.bar.SetFillColor(statusColor)
r.bar.SetPercent(r.state.Percent, formatUtilLabel(r.state.Percent), labelColor)
r.clockLabel.Text = formatClockNumber(r.state.ClockMHz)
r.clockLabel.Refresh()
r.sparkline.Refresh()
}
// RefreshTheme re-applies theme-derived colors - see CoreTile.RefreshTheme
// for why this is needed.
func (r *CoreListRow) RefreshTheme() {
r.coreLabel.Color = theme.ForegroundColor()
r.coreLabel.Refresh()
r.clockLabel.Color = theme.PlaceHolderColor()
r.clockLabel.Refresh()
r.bar.SetTrackColor(theme.InputBackgroundColor())
r.refresh()
}