aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/network/NetworkPage.qml
blob: e0f32e8823eb67a1b97fcd74f2d7f3c5637e55e1 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell.Io
import Quickshell.Networking
import QtQuick
import "../.."
import "../perf"
import "../perf/Perf.js" as P

Column {
    id: page

    required property var net
    signal back

    spacing: 14

    function stateLabel(s) {
        return s === ConnectionState.Connected ? "connected"
             : s === ConnectionState.Connecting ? "connecting"
             : s === ConnectionState.Disconnecting ? "disconnecting"
             : "disconnected";
    }
    function last(list) { return list.length ? list[list.length - 1] : null; }

    // --- Traffic ---

    // The rates are Perf's, sampled always, so the graph opens on the last
    // minute rather than empty.
    Row {
        spacing: 6

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: String.fromCodePoint(0xF012A)
            font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.subtext
        }

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: "Traffic · " + Perf.iface
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: true }
            color: Theme.subtext
        }
    }

    Canvas {
        id: graph
        width: page.width
        height: 80
        onPaint: {
            const ctx = getContext("2d");
            ctx.reset();
            const max = Math.max(1, ...Perf.rx, ...Perf.tx);
            const line = (data, color) => {
                ctx.strokeStyle = color;
                ctx.lineWidth = 1.5;
                ctx.beginPath();
                data.forEach((v, i) => {
                    const x = width * i / 59;
                    const y = height - 1 - (height - 2) * v / max;
                    if (i) ctx.lineTo(x, y); else ctx.moveTo(x, y);
                });
                ctx.stroke();
            };
            line(Perf.rx, Theme.teal);
            line(Perf.tx, Theme.peach);
        }
        Connections {
            target: Perf
            function onRxChanged() { graph.requestPaint(); }
            function onTxChanged() { graph.requestPaint(); }
        }
    }

    Item {
        width: page.width
        implicitHeight: rxText.implicitHeight
        Text {
            id: rxText
            text: "↓ " + P.speed(page.last(Perf.rx))
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.teal
        }
        Text {
            anchors.right: parent.right
            text: "↑ " + P.speed(page.last(Perf.tx))
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.peach
        }
    }

    // The five busiest processes, from `ss -tinp` byte counters diffed every
    // 2s while the page is open; it stops with the page. TCP only, and only
    // the user's own processes: UDP (QUIC, so much of a browser's HTTP/3)
    // and system services are not counted. nethogs would see both but needs
    // capabilities it does not have here.
    property var socks: null        // {list, t} from the last sample
    property var netProcs: []

    Timer {
        interval: 2000
        running: true
        repeat: true
        triggeredOnStart: true
        onTriggered: { ssProc.running = false; ssProc.running = true; }
    }

    Process {
        id: ssProc
        command: ["/sbin/ss", "-tinpH"]
        stdout: StdioCollector {
            onStreamFinished: {
                const cur = { list: P.sockets(text), t: Date.now() / 1000 };
                if (page.socks) page.netProcs = P.netProcs(page.socks.list, cur.list, cur.t - page.socks.t).slice(0, 5);
                page.socks = cur;
            }
        }
    }

    // Five rows tall whatever the count, so what sits below never moves.
    Column {
        width: page.width
        height: 5 * rowMetrics.height + 4 * spacing
        spacing: 8

        FontMetrics {
            id: rowMetrics
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
        }

        Repeater {
            model: page.netProcs
            delegate: Item {
                required property var modelData
                width: page.width
                implicitHeight: procName.implicitHeight
                Text {
                    id: procName
                    anchors { left: parent.left; right: procRx.left; rightMargin: 8 }
                    text: modelData.name
                    elide: Text.ElideRight
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                    color: Theme.text
                }
                Text {
                    id: procRx
                    anchors.right: procTx.left
                    width: Theme.fontSize * 6
                    horizontalAlignment: Text.AlignRight
                    text: "↓ " + P.speed(modelData.rx)
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                    font.features: { "tnum": 1 }
                    color: Theme.teal
                }
                Text {
                    id: procTx
                    anchors.right: parent.right
                    width: Theme.fontSize * 6
                    horizontalAlignment: Text.AlignRight
                    text: "↑ " + P.speed(modelData.tx)
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                    font.features: { "tnum": 1 }
                    color: Theme.peach
                }
            }
        }
    }

    // The LAN address and the public IP, red when it differs from the
    // pinned one; Perf keeps both, since the mismatch also lights the perf
    // tile's dot. The pin stores the address shown and is lit while they
    // are the same.
    Item {
        width: page.width
        implicitHeight: lanText.implicitHeight
        Text {
            text: "LAN"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.subtext
        }
        Text {
            id: lanText
            anchors.right: parent.right
            text: Perf.lan || "--"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.text
        }
    }

    Item {
        width: page.width
        implicitHeight: pubText.implicitHeight
        Text {
            text: "Public"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.subtext
        }
        Text {
            id: pubText
            anchors { right: pin.left; rightMargin: 6 }
            text: Perf.ip.ip || "--"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Perf.ip.mismatch ? Theme.red : Theme.text
        }
        Text {
            id: pin
            anchors { right: parent.right; verticalCenter: pubText.verticalCenter }
            text: String.fromCodePoint(0xF0403)
            font { family: Theme.iconFamily; pixelSize: Theme.fontSize }
            color: Perf.ip.ip !== "" && Perf.ip.ip === Prefs.pinnedIp ? Theme.text : Theme.overlay

            MouseArea {
                anchors.fill: parent
                anchors.margins: -4
                enabled: Perf.ip.ip !== ""
                cursorShape: Qt.PointingHandCursor
                onClicked: Prefs.setPinnedIp(Perf.ip.ip)
            }
        }
    }

    Rectangle { width: page.width; height: 1; color: Qt.alpha(Theme.text, 0.12) }

    // --- Wired ---

    Row {
        visible: page.net.wiredDevices.length > 0
        spacing: 6

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: "\uef44"
            font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.subtext
        }

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: "Wired"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: true }
            color: Theme.subtext
        }
    }

    Repeater {
        model: page.net.wiredDevices

        Rectangle {
            required property var modelData

            width: page.width
            implicitHeight: wiredText.implicitHeight + 16
            radius: 8
            color: Qt.alpha(Theme.surface, 0.35)

            Text {
                id: wiredText
                anchors { left: parent.left; leftMargin: 10; verticalCenter: parent.verticalCenter }
                text: modelData.name + "  " + page.stateLabel(modelData.state)
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                color: modelData.connected ? Theme.text : Theme.subtext
            }

            Button {
                anchors { right: parent.right; rightMargin: 8; verticalCenter: parent.verticalCenter }
                visible: modelData.connected
                text: "Disconnect"
                onClicked: page.net.disconnectNetwork(modelData)
            }
        }
    }

    Rectangle { width: page.width; height: 1; visible: page.net.wiredDevices.length > 0; color: Qt.alpha(Theme.text, 0.12) }

    // --- Wifi ---

    Item {
        width: page.width
        implicitHeight: Math.max(wifiLabel.implicitHeight, wifiSwitch.implicitHeight)

        Text {
            id: wifiLabel
            anchors { left: wifiGlyph.right; leftMargin: 6; verticalCenter: parent.verticalCenter }
            text: "Wi-Fi"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1; bold: true }
            color: Theme.text
        }

        Text {
            id: wifiGlyph
            anchors.verticalCenter: parent.verticalCenter
            text: "\uf1eb"
            font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.subtext
        }

        Switch {
            id: wifiSwitch
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            enabled: Networking.wifiHardwareEnabled
            checked: page.net.wifiOn
            onToggled: on => Networking.wifiEnabled = on
        }
    }

    Text {
        width: page.width
        visible: !page.net.wifiOn
        text: "Wi-Fi off"
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
        color: Theme.subtext
    }

    Row {
        width: page.width
        visible: page.net.wifiOn
        spacing: 8

        Button {
            text: page.net.wifiDevice && page.net.wifiDevice.scannerEnabled ? "Scanning…" : "Scan"
            onClicked: if (page.net.wifiDevice) page.net.wifiDevice.scannerEnabled = !page.net.wifiDevice.scannerEnabled
        }

        // The scanner repeats until disabled, and closing the drawer does not
        // disable it. A continuous scan also costs throughput on the connected
        // link. See the matching timer in BluetoothPage.
        Timer {
            interval: 60000
            running: page.net.wifiDevice ? page.net.wifiDevice.scannerEnabled : false
            onTriggered: if (page.net.wifiDevice) page.net.wifiDevice.scannerEnabled = false
        }
    }

    Repeater {
        model: page.net.wifiOn ? page.net.wifiNetworksSorted : []

        NetworkRow {
            required property var modelData
            net: page.net
            entry: modelData
            width: page.width
        }
    }

    Text {
        width: page.width
        visible: page.net.error !== ""
        wrapMode: Text.Wrap
        text: page.net.error
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        color: Theme.red
    }
}