More wifi fixes

This commit is contained in:
Ly-sec 2025-08-15 16:13:14 +02:00
parent 474bade65c
commit 04f1287690
2 changed files with 405 additions and 206 deletions

View file

@ -41,7 +41,9 @@ NLoader {
// Also handle visibility changes from external sources
onVisibleChanged: {
if (!visible && wifiMenuRect.opacityValue > 0) {
if (visible && Settings.data.network.wifiEnabled) {
network.refreshNetworks()
} else if (wifiMenuRect.opacityValue > 0) {
// Start hide animation
wifiMenuRect.scaleValue = 0.8
wifiMenuRect.opacityValue = 0.0
@ -65,6 +67,22 @@ NLoader {
WlrLayershell.keyboardFocus: WlrKeyboardFocus.OnDemand
Network {
id: network
}
// Timer to refresh networks when WiFi is enabled while menu is open
Timer {
id: wifiEnableRefreshTimer
interval: 3000 // Wait 3 seconds for WiFi to be fully ready
repeat: false
onTriggered: {
if (Settings.data.network.wifiEnabled && wifiPanel.visible) {
network.refreshNetworks()
}
}
}
Rectangle {
id: wifiMenuRect
color: Colors.mSurface
@ -135,14 +153,19 @@ NLoader {
value: Settings.data.network.wifiEnabled
onToggled: function (value) {
Settings.data.network.wifiEnabled = value
// TBC: This should be done in a service
Quickshell.execDetached(["nmcli", "radio", "wifi", Settings.data.network.wifiEnabled ? "on" : "off"])
network.setWifiEnabled(value)
// If enabling WiFi while menu is open, refresh after a delay
if (value) {
wifiEnableRefreshTimer.start()
}
}
}
NIconButton {
icon: "refresh"
sizeMultiplier: 0.8
enabled: Settings.data.network.wifiEnabled && !network.isLoading
onClicked: {
network.refreshNetworks()
}
@ -159,10 +182,65 @@ NLoader {
NDivider {}
ListView {
id: networkList
Item {
Layout.fillWidth: true
Layout.fillHeight: true
// Loading indicator
ColumnLayout {
anchors.centerIn: parent
visible: Settings.data.network.wifiEnabled && network.isLoading
spacing: Style.marginMedium * scaling
NBusyIndicator {
running: network.isLoading
color: Colors.mPrimary
size: Style.baseWidgetSize * scaling
Layout.alignment: Qt.AlignHCenter
}
NText {
text: "Scanning for networks..."
font.pointSize: Style.fontSizeNormal * scaling
color: Colors.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter
}
}
// WiFi disabled message
ColumnLayout {
anchors.centerIn: parent
visible: !Settings.data.network.wifiEnabled
spacing: Style.marginMedium * scaling
NText {
text: "wifi_off"
font.family: "Material Symbols Outlined"
font.pointSize: Style.fontSizeXXL * scaling
color: Colors.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter
}
NText {
text: "WiFi is disabled"
font.pointSize: Style.fontSizeLarge * scaling
color: Colors.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter
}
NText {
text: "Enable WiFi to see available networks"
font.pointSize: Style.fontSizeNormal * scaling
color: Colors.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter
}
}
// Network list
ListView {
id: networkList
anchors.fill: parent
visible: Settings.data.network.wifiEnabled && !network.isLoading
model: Object.values(network.networks)
spacing: Style.marginMedium * scaling
clip: true
@ -389,3 +467,4 @@ NLoader {
}
}
}
}

View file

@ -1,4 +1,5 @@
import QtQuick
import Quickshell
import Quickshell.Io
QtObject {
@ -10,6 +11,8 @@ QtObject {
property string connectStatusSsid: ""
property string connectError: ""
property string detectedInterface: ""
property string lastConnectedNetwork: ""
property bool isLoading: false
function signalIcon(signal) {
if (signal >= 80)
@ -28,9 +31,29 @@ QtObject {
}
function refreshNetworks() {
isLoading = true
existingNetwork.running = true
}
function setWifiEnabled(enabled) {
if (enabled) {
// Enable WiFi radio
isLoading = true
enableWifiProcess.running = true
} else {
// Store the currently connected network before disabling
for (const ssid in networks) {
if (networks[ssid].connected) {
lastConnectedNetwork = ssid
break
}
}
// Disable WiFi radio
disableWifiProcess.running = true
}
}
function connectNetwork(ssid, security) {
pendingConnect = {
"ssid": ssid,
@ -87,7 +110,7 @@ QtObject {
property int refreshInterval: 25000
// Only refresh when we have an active connection
// Only refresh when we have an active connection and WiFi is enabled
property bool hasActiveConnection: {
for (const net in networks) {
if (networks[net].connected) {
@ -99,18 +122,111 @@ QtObject {
property Timer refreshTimer: Timer {
interval: root.refreshInterval
// Only run timer when we're connected to a network
running: root.hasActiveConnection
// Only run timer when we're connected to a network and WiFi is enabled
running: root.hasActiveConnection && Settings.data.network.wifiEnabled
repeat: true
onTriggered: root.refreshNetworks()
}
// Force a refresh when menu is opened
function onMenuOpened() {
if (Settings.data.network.wifiEnabled) {
refreshNetworks()
}
}
function onMenuClosed() {// No need to do anything special on close
function onMenuClosed() {
// No need to do anything special on close
}
// Process to enable WiFi radio
property Process enableWifiProcess: Process {
id: enableWifiProcess
running: false
command: ["nmcli", "radio", "wifi", "on"]
onRunningChanged: {
if (!running) {
// Wait a moment for the radio to be enabled, then refresh networks
enableWifiDelayTimer.start()
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text.trim() !== "") {
console.warn("Error enabling WiFi:", text)
}
}
}
}
// Timer to delay network refresh after enabling WiFi
property Timer enableWifiDelayTimer: Timer {
id: enableWifiDelayTimer
interval: 2000 // Wait 2 seconds for radio to be ready
repeat: false
onTriggered: {
// Force refresh networks multiple times to ensure UI updates
root.refreshNetworks()
// Try to auto-reconnect to the last connected network if it exists
if (lastConnectedNetwork) {
autoReconnectTimer.start()
}
// Set up additional refresh to ensure UI is populated
postEnableRefreshTimer.start()
}
}
// Additional timer to ensure networks are populated after enabling
property Timer postEnableRefreshTimer: Timer {
id: postEnableRefreshTimer
interval: 1000
repeat: false
onTriggered: {
root.refreshNetworks()
}
}
// Timer to attempt auto-reconnection to the last connected network
property Timer autoReconnectTimer: Timer {
id: autoReconnectTimer
interval: 3000 // Wait 3 seconds after scan for networks to be available
repeat: false
onTriggered: {
if (lastConnectedNetwork && networks[lastConnectedNetwork]) {
const network = networks[lastConnectedNetwork]
if (network.existing && !network.connected) {
upConnectionProcess.profileName = lastConnectedNetwork
upConnectionProcess.running = true
}
}
}
}
// Process to disable WiFi radio
property Process disableWifiProcess: Process {
id: disableWifiProcess
running: false
command: ["nmcli", "radio", "wifi", "off"]
onRunningChanged: {
if (!running) {
// Clear networks when WiFi is disabled
root.networks = ({})
root.connectingSsid = ""
root.connectStatus = ""
root.connectStatusSsid = ""
root.connectError = ""
root.isLoading = false
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text.trim() !== "") {
console.warn("Error disabling WiFi:", text)
}
}
}
}
property Process disconnectProfileProcess: Process {
@ -211,11 +327,10 @@ QtObject {
}
root.networks = networksMap
root.isLoading = false
scanProcess.existingNetwork = {}
}
}
}
property Process connectProcess: Process {
@ -237,6 +352,7 @@ QtObject {
root.connectStatus = "success"
root.connectStatusSsid = connectProcess.ssid
root.connectError = ""
root.lastConnectedNetwork = connectProcess.ssid
root.refreshNetworks()
}
}
@ -324,8 +440,9 @@ QtObject {
onStreamFinished: {
root.connectingSsid = ""
root.connectStatus = "success"
root.connectStatusSsid = root.pendingConnect ? root.pendingConnect.ssid : ""
root.connectStatusSsid = root.pendingConnect ? root.pendingConnect.ssid : profileName
root.connectError = ""
root.lastConnectedNetwork = profileName
root.pendingConnect = null
root.refreshNetworks()
}
@ -334,7 +451,7 @@ QtObject {
onStreamFinished: {
root.connectingSsid = ""
root.connectStatus = "error"
root.connectStatusSsid = root.pendingConnect ? root.pendingConnect.ssid : ""
root.connectStatusSsid = root.pendingConnect ? root.pendingConnect.ssid : profileName
root.connectError = text
root.pendingConnect = null
}
@ -342,6 +459,9 @@ QtObject {
}
Component.onCompleted: {
// Only refresh networks if WiFi is enabled
if (Settings.data.network.wifiEnabled) {
refreshNetworks()
}
}
}