summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/quickshell/Modules/ProcessList/PerformanceView.qml
blob: c793d92fce85121cf37f26dc88e4cdc49b8be0ff (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import QtQuick
import QtQuick.Layouts
import Quickshell
import qs.Common
import qs.Services
import qs.Widgets

Item {
    id: root

    readonly property int historySize: 60

    property var cpuHistory: []
    property var memoryHistory: []
    property var networkRxHistory: []
    property var networkTxHistory: []
    property var diskReadHistory: []
    property var diskWriteHistory: []

    function formatBytes(bytes) {
        if (bytes < 1024)
            return bytes.toFixed(0) + " B/s";
        if (bytes < 1024 * 1024)
            return (bytes / 1024).toFixed(1) + " KB/s";
        if (bytes < 1024 * 1024 * 1024)
            return (bytes / (1024 * 1024)).toFixed(1) + " MB/s";
        return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB/s";
    }

    function addToHistory(arr, val) {
        const newArr = arr.slice();
        newArr.push(val);
        if (newArr.length > historySize)
            newArr.shift();
        return newArr;
    }

    function sampleData() {
        cpuHistory = addToHistory(cpuHistory, DgopService.cpuUsage);
        memoryHistory = addToHistory(memoryHistory, DgopService.memoryUsage);
        networkRxHistory = addToHistory(networkRxHistory, DgopService.networkRxRate);
        networkTxHistory = addToHistory(networkTxHistory, DgopService.networkTxRate);
        diskReadHistory = addToHistory(diskReadHistory, DgopService.diskReadRate);
        diskWriteHistory = addToHistory(diskWriteHistory, DgopService.diskWriteRate);
    }

    Component.onCompleted: {
        DgopService.addRef(["cpu", "memory", "network", "disk", "diskmounts", "system"]);
    }

    Component.onDestruction: {
        DgopService.removeRef(["cpu", "memory", "network", "disk", "diskmounts", "system"]);
    }

    SystemClock {
        id: sampleClock
        precision: SystemClock.Seconds
        onDateChanged: {
            if (date.getSeconds() % 1 === 0)
                root.sampleData();
        }
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: Theme.spacingM

        RowLayout {
            Layout.fillWidth: true
            Layout.preferredHeight: (root.height - Theme.spacingM * 2) / 2
            spacing: Theme.spacingM

            PerformanceCard {
                Layout.fillWidth: true
                Layout.fillHeight: true
                title: "CPU"
                icon: "memory"
                value: DgopService.cpuUsage.toFixed(1) + "%"
                subtitle: DgopService.cpuModel || (DgopService.cpuCores + " cores")
                accentColor: Theme.primary
                history: root.cpuHistory
                maxValue: 100
                showSecondary: false
                extraInfo: DgopService.cpuTemperature > 0 ? (DgopService.cpuTemperature.toFixed(0) + "°C") : ""
                extraInfoColor: DgopService.cpuTemperature > 80 ? Theme.error : (DgopService.cpuTemperature > 60 ? Theme.warning : Theme.surfaceVariantText)
            }

            PerformanceCard {
                Layout.fillWidth: true
                Layout.fillHeight: true
                title: I18n.tr("Memory")
                icon: "sd_card"
                value: DgopService.memoryUsage.toFixed(1) + "%"
                subtitle: DgopService.formatSystemMemory(DgopService.usedMemoryKB) + " / " + DgopService.formatSystemMemory(DgopService.totalMemoryKB)
                accentColor: Theme.secondary
                history: root.memoryHistory
                maxValue: 100
                showSecondary: false
                extraInfo: DgopService.totalSwapKB > 0 ? ("Swap: " + DgopService.formatSystemMemory(DgopService.usedSwapKB)) : ""
                extraInfoColor: Theme.surfaceVariantText
            }
        }

        RowLayout {
            Layout.fillWidth: true
            Layout.preferredHeight: (root.height - Theme.spacingM * 2) / 2
            spacing: Theme.spacingM

            PerformanceCard {
                Layout.fillWidth: true
                Layout.fillHeight: true
                title: I18n.tr("Network")
                icon: "swap_horiz"
                value: "↓ " + root.formatBytes(DgopService.networkRxRate)
                subtitle: "↑ " + root.formatBytes(DgopService.networkTxRate)
                accentColor: Theme.info
                history: root.networkRxHistory
                history2: root.networkTxHistory
                maxValue: 0
                showSecondary: true
                extraInfo: ""
                extraInfoColor: Theme.surfaceVariantText
            }

            PerformanceCard {
                Layout.fillWidth: true
                Layout.fillHeight: true
                title: I18n.tr("Disk")
                icon: "storage"
                value: "R: " + root.formatBytes(DgopService.diskReadRate)
                subtitle: "W: " + root.formatBytes(DgopService.diskWriteRate)
                accentColor: Theme.warning
                history: root.diskReadHistory
                history2: root.diskWriteHistory
                maxValue: 0
                showSecondary: true
                extraInfo: {
                    const rootMount = DgopService.diskMounts.find(m => m.mountpoint === "/");
                    if (rootMount) {
                        const usedPct = ((rootMount.used || 0) / Math.max(1, rootMount.total || 1) * 100).toFixed(0);
                        return "/ " + usedPct + "% used";
                    }
                    return "";
                }
                extraInfoColor: Theme.surfaceVariantText
            }
        }
    }

    component PerformanceCard: Rectangle {
        id: card

        property string title: ""
        property string icon: ""
        property string value: ""
        property string subtitle: ""
        property color accentColor: Theme.primary
        property var history: []
        property var history2: null
        property real maxValue: 100
        property bool showSecondary: false
        property string extraInfo: ""
        property color extraInfoColor: Theme.surfaceVariantText

        radius: Theme.cornerRadius
        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
        border.color: Theme.outlineLight
        border.width: 1

        Canvas {
            id: graphCanvas
            anchors.fill: parent
            anchors.margins: 4
            renderStrategy: Canvas.Cooperative

            property var hist: card.history
            property var hist2: card.history2

            onHistChanged: requestPaint()
            onHist2Changed: requestPaint()
            onWidthChanged: requestPaint()
            onHeightChanged: requestPaint()

            onPaint: {
                const ctx = getContext("2d");
                ctx.reset();
                ctx.clearRect(0, 0, width, height);

                if (!hist || hist.length < 2)
                    return;

                let max = card.maxValue;
                if (max <= 0) {
                    max = 1;
                    for (let k = 0; k < hist.length; k++)
                        max = Math.max(max, hist[k]);
                    if (hist2) {
                        for (let l = 0; l < hist2.length; l++)
                            max = Math.max(max, hist2[l]);
                    }
                    max *= 1.1;
                }

                const c = card.accentColor;
                const grad = ctx.createLinearGradient(0, 0, 0, height);
                grad.addColorStop(0, Qt.rgba(c.r, c.g, c.b, 0.25));
                grad.addColorStop(1, Qt.rgba(c.r, c.g, c.b, 0.02));

                ctx.fillStyle = grad;
                ctx.beginPath();
                ctx.moveTo(0, height);
                for (let i = 0; i < hist.length; i++) {
                    const x = (width / (root.historySize - 1)) * i;
                    const y = height - (hist[i] / max) * height * 0.8;
                    ctx.lineTo(x, y);
                }
                ctx.lineTo((width / (root.historySize - 1)) * (hist.length - 1), height);
                ctx.closePath();
                ctx.fill();

                ctx.strokeStyle = Qt.rgba(c.r, c.g, c.b, 0.8);
                ctx.lineWidth = 2;
                ctx.beginPath();
                for (let j = 0; j < hist.length; j++) {
                    const px = (width / (root.historySize - 1)) * j;
                    const py = height - (hist[j] / max) * height * 0.8;
                    j === 0 ? ctx.moveTo(px, py) : ctx.lineTo(px, py);
                }
                ctx.stroke();

                if (hist2 && hist2.length >= 2 && card.showSecondary) {
                    ctx.strokeStyle = Qt.rgba(c.r, c.g, c.b, 0.4);
                    ctx.lineWidth = 1.5;
                    ctx.setLineDash([4, 4]);
                    ctx.beginPath();
                    for (let m = 0; m < hist2.length; m++) {
                        const sx = (width / (root.historySize - 1)) * m;
                        const sy = height - (hist2[m] / max) * height * 0.8;
                        m === 0 ? ctx.moveTo(sx, sy) : ctx.lineTo(sx, sy);
                    }
                    ctx.stroke();
                    ctx.setLineDash([]);
                }
            }
        }

        ColumnLayout {
            anchors.fill: parent
            anchors.margins: Theme.spacingM
            spacing: Theme.spacingXS

            RowLayout {
                Layout.fillWidth: true
                spacing: Theme.spacingS

                DankIcon {
                    name: card.icon
                    size: Theme.iconSize
                    color: card.accentColor
                }

                StyledText {
                    text: card.title
                    font.pixelSize: Theme.fontSizeLarge
                    font.weight: Font.Bold
                    color: Theme.surfaceText
                }

                Item {
                    Layout.fillWidth: true
                }

                StyledText {
                    text: card.extraInfo
                    font.pixelSize: Theme.fontSizeSmall
                    font.family: SettingsData.monoFontFamily
                    color: card.extraInfoColor
                    visible: card.extraInfo.length > 0
                }
            }

            Item {
                Layout.fillHeight: true
            }

            StyledText {
                text: card.value
                font.pixelSize: Theme.fontSizeXLarge
                font.family: SettingsData.monoFontFamily
                font.weight: Font.Bold
                color: Theme.surfaceText
            }

            StyledText {
                text: card.subtitle
                font.pixelSize: Theme.fontSizeSmall
                font.family: SettingsData.monoFontFamily
                color: Theme.surfaceVariantText
                elide: Text.ElideRight
                Layout.fillWidth: true
            }
        }
    }
}