aboutsummaryrefslogtreecommitdiff
path: root/modules/system/quickshell/Wifi.qml
blob: 9960575aaad4f2a0248d0bd45d6aa98b3e4a6905 (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
365
366
367
368
369
370
371
372
373
374
375
376
import QtQuick
import QtQuick.Layouts
import Quickshell
import Quickshell.Networking
import Quickshell.Widgets

Item {
    id: root

    Scope {
        id: internal

        readonly property var device: {
            if (!Networking.devices)
                return null;
            for (const d of Networking.devices.values || []) {
                if (d && d.scannerEnabled !== undefined)
                    return d;
            }
            return null;
        }

        readonly property var allNetworks: device?.networks ? device.networks.values : []
        readonly property bool hasKnown: hasKnownNetwork()
        readonly property bool hasOther: hasOtherNetwork()
        readonly property var activeNetwork: findActiveNetwork()

        function hasKnownNetwork() {
            for (const n of internal.allNetworks) {
                if (n?.known)
                    return true;
            }
            return false;
        }

        function hasOtherNetwork() {
            for (const n of internal.allNetworks) {
                if (n && !n.known && n.name)
                    return true;
            }
            return false;
        }

        function findActiveNetwork() {
            for (const n of internal.allNetworks) {
                if (n?.connected)
                    return n;
            }
            return null;
        }
    }

    function _getWifiIcon(strength) {
        if (!Networking.wifiEnabled)
            return "network-wireless-offline-symbolic";
        if (!internal.activeNetwork && !internal.device?.enabled)
            return "network-wireless-offline-symbolic";

        const s = strength ?? 0;
        if (s >= 0.75)
            return "network-wireless-signal-excellent-symbolic";
        if (s >= 0.50)
            return "network-wireless-signal-good-symbolic";
        if (s >= 0.25)
            return "network-wireless-signal-ok-symbolic";
        if (s > 0)
            return "network-wireless-signal-weak-symbolic";
        return "network-wireless-signal-none-symbolic";
    }

    function _onNetworkClick(net) {
        if (!net)
            return;
        if (net.connected) {
            net.disconnect();
            return;
        }
        if (net.stateChanging)
            return;
        if (net.known) {
            net.connect();
        } else if ((net.security ?? 0) === 0) {
            net.connect();
        } else {
            pskPrompt.network = net;
            pskPrompt.open(net.name ?? "");
        }
    }

    width: childrenRect.width
    height: parent.height

    Row {
        anchors {
            verticalCenter: parent.verticalCenter
        }
        spacing: 4
        Image {
            width: 20
            height: 20
            source: Quickshell.iconPath(root._getWifiIcon(internal.activeNetwork?.signalStrength ?? 0))
            sourceSize: Qt.size(width, height)
            smooth: true
            mipmap: true
            opacity: !Networking.wifiEnabled ? 0.35 : 1.0
        }
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: mouse => {
            if (mouse.button === Qt.LeftButton) {
                GlobalState.toggle("Wifi");
            } else if (mouse.button === Qt.RightButton) {
                Networking.wifiEnabled = !Networking.wifiEnabled;
            }
        }
    }

    AnchoredPopup {
        popupName: "Wifi"
        anchorWindow: barWindow
        anchorItem: root
        onOpened: if (internal.device)
            internal.device.scannerEnabled = true
        onClosed: if (internal.device?.scannerEnabled)
            internal.device.scannerEnabled = false

        PopupCard {
            id: card
            margins: 16

            RowLayout {
                Layout.fillWidth: true
                Layout.topMargin: 4
                Layout.bottomMargin: 4
                Layout.leftMargin: 8
                Layout.rightMargin: 8
                spacing: 8

                Text {
                    text: "Wi-Fi"
                    color: Theme.text
                    font {
                        family: Theme.mainFont
                        pixelSize: 14
                        weight: Font.DemiBold
                    }
                }

                Item {
                    Layout.fillWidth: true
                }

                Toggle {
                    checked: Networking.wifiEnabled
                    enabled: Networking.wifiHardwareEnabled
                    onToggled: Networking.wifiEnabled = !Networking.wifiEnabled
                }
            }

            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 1
                color: Theme.border
                Layout.topMargin: 4
                Layout.bottomMargin: 4
            }

            Text {
                visible: internal.hasKnown
                text: "Known Network"
                color: Theme.textMuted
                font {
                    family: Theme.mainFont
                    pixelSize: 12
                    weight: Font.DemiBold
                }
                Layout.leftMargin: 8
                Layout.topMargin: 2
                Layout.bottomMargin: 2
            }

            ColumnLayout {
                Layout.fillWidth: true
                spacing: 2
                visible: internal.hasKnown

                Repeater {
                    model: internal.device?.networks ?? null

                    delegate: Squircle {
                        id: knownItem
                        required property var modelData
                        readonly property bool isConnected: knownItem.modelData?.connected ?? false
                        visible: knownItem.modelData?.known ?? false
                        Layout.fillWidth: true
                        Layout.preferredHeight: visible ? 36 : 0
                        fillColor: knownArea.containsMouse ? Theme.surface : Theme.transparent
                        cornerRadius: 6

                        MouseArea {
                            id: knownArea
                            anchors.fill: parent
                            hoverEnabled: true
                            enabled: !(knownItem.modelData?.stateChanging ?? false)
                            onClicked: root._onNetworkClick(knownItem.modelData)
                        }

                        RowLayout {
                            anchors {
                                fill: parent
                                leftMargin: 8
                                rightMargin: 8
                            }
                            spacing: 12

                            IconCircle {
                                size: 24
                                source: root._getWifiIcon(knownItem.modelData?.signalStrength ?? 0)
                                active: knownItem.isConnected
                            }

                            Text {
                                Layout.fillWidth: true
                                text: knownItem.modelData?.name ?? ""
                                color: Theme.text
                                font {
                                    family: Theme.mainFont
                                    pixelSize: 13
                                }
                                elide: Text.ElideRight
                            }

                            IconImage {
                                visible: (knownItem.modelData?.security ?? 0) !== 0
                                Layout.preferredWidth: 14
                                Layout.preferredHeight: 14
                                source: Quickshell.iconPath("changes-prevent-symbolic")
                            }
                        }
                    }
                }
            }

            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 1
                color: Theme.border
                Layout.topMargin: 4
                Layout.bottomMargin: 4
            }

            RowLayout {
                Layout.fillWidth: true
                Layout.leftMargin: 8
                Layout.rightMargin: 8
                Layout.topMargin: 2
                Layout.bottomMargin: 2

                Text {
                    text: "Other Networks"
                    color: Theme.textMuted
                    font {
                        family: Theme.mainFont
                        pixelSize: 12
                        weight: Font.DemiBold
                    }
                    Layout.fillWidth: true
                }

                Image {
                    id: refreshIcon
                    width: 14
                    height: 14
                    source: Quickshell.iconPath("view-refresh-symbolic")
                    sourceSize: Qt.size(width, height)
                    opacity: refreshMouse.containsMouse ? 1.0 : 0.6

                    RotationAnimation on rotation {
                        running: internal.device?.scannerEnabled ?? false
                        from: 0
                        to: 360
                        duration: 1000
                        loops: Animation.Infinite
                    }

                    MouseArea {
                        id: refreshMouse
                        anchors.fill: parent
                        hoverEnabled: true
                        cursorShape: Qt.PointingHandCursor
                        onClicked: {
                            if (internal.device) {
                                internal.device.scannerEnabled = !internal.device.scannerEnabled;
                            }
                        }
                    }
                }
            }

            ColumnLayout {
                Layout.fillWidth: true
                spacing: 2
                visible: internal.hasOther

                Repeater {
                    model: internal.device?.networks ?? null

                    delegate: Squircle {
                        id: otherItem
                        required property var modelData
                        visible: !(otherItem.modelData?.known ?? true) && (otherItem.modelData?.name ?? "") !== ""
                        Layout.fillWidth: true
                        Layout.preferredHeight: visible ? 36 : 0
                        fillColor: otherArea.containsMouse ? Theme.surface : Theme.transparent
                        cornerRadius: 6

                        MouseArea {
                            id: otherArea
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            enabled: !(otherItem.modelData?.stateChanging ?? false)
                            onClicked: root._onNetworkClick(otherItem.modelData)
                        }

                        RowLayout {
                            anchors {
                                fill: parent
                                leftMargin: 8
                                rightMargin: 8
                            }
                            spacing: 12

                            IconCircle {
                                size: 24
                                source: root._getWifiIcon(otherItem.modelData?.signalStrength ?? 0)
                                active: false
                            }

                            Text {
                                Layout.fillWidth: true
                                text: otherItem.modelData?.name ?? ""
                                color: Theme.text
                                opacity: 0.8
                                font {
                                    family: Theme.mainFont
                                    pixelSize: 13
                                }
                                elide: Text.ElideRight
                            }

                            IconImage {
                                visible: (otherItem.modelData?.security ?? 0) !== 0
                                Layout.preferredWidth: 14
                                Layout.preferredHeight: 14
                                source: Quickshell.iconPath("changes-prevent-symbolic")
                            }
                        }
                    }
                }
            }
        }
    }

    WifiPasswordPrompt {
        id: pskPrompt
        property var network: null
        onSubmitted: text => {
            if (network)
                network.connectWithPsk(text);
        }
    }
}