Wifi: more clean ups and improvements

This commit is contained in:
LemmyCook 2025-09-06 00:36:29 -04:00
parent a57bfeba31
commit 2398961473
2 changed files with 181 additions and 116 deletions

View file

@ -51,7 +51,7 @@ NPanel {
id: wifiSwitch id: wifiSwitch
checked: Settings.data.network.wifiEnabled checked: Settings.data.network.wifiEnabled
onToggled: checked => NetworkService.setWifiEnabled(checked) onToggled: checked => NetworkService.setWifiEnabled(checked)
baseSize: Style.baseWidgetSize * 0.7 * scaling baseSize: Style.baseWidgetSize * 0.65 * scaling
} }
NIconButton { NIconButton {
@ -122,9 +122,11 @@ NPanel {
ColumnLayout { ColumnLayout {
visible: !Settings.data.network.wifiEnabled visible: !Settings.data.network.wifiEnabled
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginM * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NIcon { NIcon {
text: "wifi_off" text: "wifi_off"
@ -141,22 +143,27 @@ NPanel {
} }
NText { NText {
text: "Enable Wi-Fi to see available networks" text: "Enable Wi-Fi to see available networks."
font.pointSize: Style.fontSizeS * scaling font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant color: Color.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
// Scanning state // Scanning state
ColumnLayout { ColumnLayout {
visible: Settings.data.network.wifiEnabled && NetworkService.scanning && Object.keys(NetworkService.networks).length === 0 visible: Settings.data.network.wifiEnabled && NetworkService.scanning && Object.keys(
NetworkService.networks).length === 0
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginL * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NBusyIndicator { NBusyIndicator {
running: true running: true
@ -172,12 +179,15 @@ NPanel {
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
// Networks list container // Networks list container
ScrollView { ScrollView {
visible: Settings.data.network.wifiEnabled && (!NetworkService.scanning || Object.keys(NetworkService.networks).length > 0) visible: Settings.data.network.wifiEnabled && (!NetworkService.scanning || Object.keys(
NetworkService.networks).length > 0)
anchors.fill: parent anchors.fill: parent
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ScrollBar.vertical.policy: ScrollBar.AsNeeded ScrollBar.vertical.policy: ScrollBar.AsNeeded
@ -190,11 +200,13 @@ NPanel {
// Network list // Network list
Repeater { Repeater {
model: { model: {
if (!Settings.data.network.wifiEnabled) return [] if (!Settings.data.network.wifiEnabled)
return []
const nets = Object.values(NetworkService.networks) const nets = Object.values(NetworkService.networks)
return nets.sort((a, b) => { return nets.sort((a, b) => {
if (a.connected !== b.connected) return b.connected - a.connected if (a.connected !== b.connected)
return b.connected - a.connected
return b.signal - a.signal return b.signal - a.signal
}) })
} }
@ -203,7 +215,8 @@ NPanel {
Layout.fillWidth: true Layout.fillWidth: true
implicitHeight: netColumn.implicitHeight + (Style.marginM * scaling * 2) implicitHeight: netColumn.implicitHeight + (Style.marginM * scaling * 2)
radius: Style.radiusM * scaling radius: Style.radiusM * scaling
color: modelData.connected ? Qt.rgba(Color.mPrimary.r, Color.mPrimary.g, Color.mPrimary.b, 0.05) : Color.mSurface color: modelData.connected ? Qt.rgba(Color.mPrimary.r, Color.mPrimary.g, Color.mPrimary.b,
0.05) : Color.mSurface
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
border.color: modelData.connected ? Color.mPrimary : Color.mOutline border.color: modelData.connected ? Color.mPrimary : Color.mOutline
@ -263,7 +276,6 @@ NPanel {
Layout.preferredWidth: Style.marginXXS * scaling Layout.preferredWidth: Style.marginXXS * scaling
} }
Rectangle { Rectangle {
visible: modelData.connected visible: modelData.connected
color: Color.mPrimary color: Color.mPrimary
@ -302,7 +314,7 @@ NPanel {
// Action area // Action area
RowLayout { RowLayout {
spacing: Style.marginXS * scaling spacing: Style.marginS * scaling
NBusyIndicator { NBusyIndicator {
visible: NetworkService.connectingTo === modelData.ssid visible: NetworkService.connectingTo === modelData.ssid
@ -311,11 +323,23 @@ NPanel {
size: Style.baseWidgetSize * 0.5 * scaling size: Style.baseWidgetSize * 0.5 * scaling
} }
NIconButton {
visible: (modelData.existing || modelData.cached) && !modelData.connected
&& NetworkService.connectingTo !== modelData.ssid
icon: "delete"
tooltipText: "Forget network"
sizeRatio: 0.7
onClicked: expandedSsid = expandedSsid === modelData.ssid ? "" : modelData.ssid
}
NButton { NButton {
visible: !modelData.connected && NetworkService.connectingTo !== modelData.ssid && passwordSsid !== modelData.ssid visible: !modelData.connected && NetworkService.connectingTo !== modelData.ssid
&& passwordSsid !== modelData.ssid
text: { text: {
if (modelData.existing || modelData.cached) return "Connect" if (modelData.existing || modelData.cached)
if (!NetworkService.isSecured(modelData.security)) return "Connect" return "Connect"
if (!NetworkService.isSecured(modelData.security))
return "Connect"
return "Password" return "Password"
} }
outlined: !hovered outlined: !hovered
@ -339,13 +363,6 @@ NPanel {
backgroundColor: Color.mError backgroundColor: Color.mError
onClicked: NetworkService.disconnect(modelData.ssid) onClicked: NetworkService.disconnect(modelData.ssid)
} }
NIconButton {
visible: (modelData.existing || modelData.cached) && !modelData.connected && NetworkService.connectingTo !== modelData.ssid
icon: "more_vert"
sizeRatio: 0.7
onClicked: expandedSsid = expandedSsid === modelData.ssid ? "" : modelData.ssid
}
} }
} }
@ -353,21 +370,24 @@ NPanel {
Rectangle { Rectangle {
visible: passwordSsid === modelData.ssid visible: passwordSsid === modelData.ssid
Layout.fillWidth: true Layout.fillWidth: true
height: 40 * scaling height: passwordRow.implicitHeight + Style.marginS * scaling * 2
color: Color.mSurfaceVariant color: Color.mSurfaceVariant
border.color: Color.mOutline
border.width: Math.max(1, Style.borderS * scaling)
radius: Style.radiusS * scaling radius: Style.radiusS * scaling
RowLayout { RowLayout {
id: passwordRow
anchors.fill: parent anchors.fill: parent
anchors.margins: Style.marginXS * scaling anchors.margins: Style.marginS * scaling
spacing: Style.marginXS * scaling spacing: Style.marginM * scaling
Rectangle { Rectangle {
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true
radius: Style.radiusXS * scaling radius: Style.radiusXS * scaling
color: Color.mSurface color: Color.mSurface
border.color: pwdInput.activeFocus ? Color.mPrimary : Color.mOutline border.color: pwdInput.activeFocus ? Color.mSecondary : Color.mOutline
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
TextInput { TextInput {
@ -382,7 +402,10 @@ NPanel {
echoMode: TextInput.Password echoMode: TextInput.Password
selectByMouse: true selectByMouse: true
focus: visible focus: visible
passwordCharacter: "●"
onTextChanged: passwordInput = text onTextChanged: passwordInput = text
onVisibleChanged: if (visible)
forceActiveFocus()
onAccepted: { onAccepted: {
if (text) { if (text) {
NetworkService.connect(passwordSsid, text) NetworkService.connect(passwordSsid, text)
@ -405,6 +428,7 @@ NPanel {
text: "Connect" text: "Connect"
fontSize: Style.fontSizeXXS * scaling fontSize: Style.fontSizeXXS * scaling
enabled: passwordInput.length > 0 enabled: passwordInput.length > 0
outlined: true
onClicked: { onClicked: {
NetworkService.connect(passwordSsid, passwordInput) NetworkService.connect(passwordSsid, passwordInput)
passwordSsid = "" passwordSsid = ""
@ -414,7 +438,7 @@ NPanel {
NIconButton { NIconButton {
icon: "close" icon: "close"
sizeRatio: 0.6 sizeRatio: 0.8
onClicked: { onClicked: {
passwordSsid = "" passwordSsid = ""
passwordInput = "" passwordInput = ""
@ -423,24 +447,26 @@ NPanel {
} }
} }
// Options menu // Forget network
Rectangle { Rectangle {
visible: expandedSsid === modelData.ssid visible: expandedSsid === modelData.ssid
Layout.fillWidth: true Layout.fillWidth: true
height: forgetRow.implicitHeight + Style.marginS * 2 height: forgetRow.implicitHeight + Style.marginS * 2 * scaling
color: Color.mSurfaceVariant color: Color.mSurfaceVariant
radius: Style.radiusS * scaling radius: Style.radiusS * scaling
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
border.color: Color.mError border.color: Color.mOutline
RowLayout { RowLayout {
id: forgetRow id: forgetRow
anchors.fill: parent anchors.fill: parent
anchors.margins: Style.marginS * scaling anchors.margins: Style.marginS * scaling
spacing: Style.marginM * scaling
RowLayout {
NIcon { NIcon {
text: "delete_outline" text: "delete_outline"
font.pointSize: Style.fontSizeM * scaling font.pointSize: Style.fontSizeL * scaling
color: Color.mError color: Color.mError
} }
@ -450,11 +476,14 @@ NPanel {
color: Color.mError color: Color.mError
Layout.fillWidth: true Layout.fillWidth: true
} }
}
NButton { NButton {
id: forgetButton
text: "Forget" text: "Forget"
fontSize: Style.fontSizeXXS * scaling fontSize: Style.fontSizeXXS * scaling
backgroundColor: Color.mError backgroundColor: Color.mError
outlined: forgetButton.hovered ? false : true
onClicked: { onClicked: {
NetworkService.forget(modelData.ssid) NetworkService.forget(modelData.ssid)
expandedSsid = "" expandedSsid = ""
@ -463,7 +492,7 @@ NPanel {
NIconButton { NIconButton {
icon: "close" icon: "close"
sizeRatio: 0.6 sizeRatio: 0.8
onClicked: expandedSsid = "" onClicked: expandedSsid = ""
} }
} }
@ -476,11 +505,14 @@ NPanel {
// Empty state when no networks // Empty state when no networks
ColumnLayout { ColumnLayout {
visible: Settings.data.network.wifiEnabled && !NetworkService.scanning && Object.keys(NetworkService.networks).length === 0 visible: Settings.data.network.wifiEnabled && !NetworkService.scanning && Object.keys(
NetworkService.networks).length === 0
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginL * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NIcon { NIcon {
text: "wifi_find" text: "wifi_find"
@ -503,7 +535,9 @@ NPanel {
onClicked: NetworkService.scan() onClicked: NetworkService.scan()
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
} }
} }

View file

@ -14,6 +14,7 @@ Singleton {
property bool connecting: false property bool connecting: false
property string connectingTo: "" property string connectingTo: ""
property string lastError: "" property string lastError: ""
property bool ethernet: false
// Persistent cache // Persistent cache
property string cacheFile: Settings.cacheDir + "network.json" property string cacheFile: Settings.cacheDir + "network.json"
@ -85,15 +86,18 @@ Singleton {
} }
function scan() { function scan() {
if (scanning) return if (scanning)
return
scanning = true scanning = true
lastError = "" lastError = ""
scanProcess.running = true scanProcess.running = true
ethernetStateProcess.running = true
} }
function connect(ssid, password = "") { function connect(ssid, password = "") {
if (connecting) return if (connecting)
return
connecting = true connecting = true
connectingTo = ssid connectingTo = ssid
@ -137,10 +141,14 @@ Singleton {
// Helper functions // Helper functions
function signalIcon(signal) { function signalIcon(signal) {
if (signal >= 80) return "network_wifi" if (signal >= 80)
if (signal >= 60) return "network_wifi_3_bar" return "network_wifi"
if (signal >= 40) return "network_wifi_2_bar" if (signal >= 60)
if (signal >= 20) return "network_wifi_1_bar" return "network_wifi_3_bar"
if (signal >= 40)
return "network_wifi_2_bar"
if (signal >= 20)
return "network_wifi_1_bar"
return "signal_wifi_0_bar" return "signal_wifi_0_bar"
} }
@ -149,6 +157,23 @@ Singleton {
} }
// Processes // Processes
Process {
id: ethernetStateProcess
running: false
command: ["nmcli", "-t", "-f", "DEVICE,TYPE,STATE", "device"]
stdout: StdioCollector {
onStreamFinished: {
root.ethernet = text.split("\n").some(line => {
const parts = line.split(":")
return parts[1] === "ethernet" && parts[2] === "connected"
})
Logger.log("Network", "Ethernet connected:", root.ethernet)
}
}
}
Process { Process {
id: wifiStateProcess id: wifiStateProcess
running: false running: false
@ -157,6 +182,7 @@ Singleton {
stdout: StdioCollector { stdout: StdioCollector {
onStreamFinished: { onStreamFinished: {
const enabled = text.trim() === "enabled" const enabled = text.trim() === "enabled"
Logger.log("Network", "Wifi enabled:", enabled)
if (Settings.data.network.wifiEnabled !== enabled) { if (Settings.data.network.wifiEnabled !== enabled) {
Settings.data.network.wifiEnabled = enabled Settings.data.network.wifiEnabled = enabled
} }
@ -175,6 +201,7 @@ Singleton {
if (action === "on") { if (action === "on") {
// Clear networks immediately and start delayed scan // Clear networks immediately and start delayed scan
root.networks = ({}) root.networks = ({})
delayedScanTimer.interval = 8000
delayedScanTimer.restart() delayedScanTimer.restart()
} else { } else {
root.networks = ({}) root.networks = ({})
@ -226,18 +253,20 @@ Singleton {
for (const line of lines) { for (const line of lines) {
const parts = line.split("|") const parts = line.split("|")
if (parts.length < 5) continue if (parts.length < 5)
continue
const ssid = parts[0] const ssid = parts[0]
if (!ssid || ssid.trim() === "") continue if (!ssid || ssid.trim() === "")
continue
const network = { const network = {
ssid: ssid, "ssid": ssid,
security: parts[1] || "--", "security": parts[1] || "--",
signal: parseInt(parts[2]) || 0, "signal": parseInt(parts[2]) || 0,
connected: parts[3] === "*", "connected": parts[3] === "*",
existing: parts[4] === "true", "existing": parts[4] === "true",
cached: ssid in cacheAdapter.knownNetworks "cached": ssid in cacheAdapter.knownNetworks
} }
// Track connected network // Track connected network
@ -254,6 +283,7 @@ Singleton {
root.networks = nets root.networks = nets
root.scanning = false root.scanning = false
Logger.log("Network", "Discovered", Object.keys(root.networks).length, "Wi-Fi networks")
} }
} }
@ -296,8 +326,8 @@ Singleton {
// Success - update cache // Success - update cache
let known = cacheAdapter.knownNetworks let known = cacheAdapter.knownNetworks
known[connectProcess.ssid] = { known[connectProcess.ssid] = {
profileName: connectProcess.ssid, "profileName": connectProcess.ssid,
lastConnected: Date.now() "lastConnected": Date.now()
} }
cacheAdapter.knownNetworks = known cacheAdapter.knownNetworks = known
cacheAdapter.lastConnected = connectProcess.ssid cacheAdapter.lastConnected = connectProcess.ssid
@ -322,6 +352,7 @@ Singleton {
// Parse common errors // Parse common errors
if (text.includes("Secrets were required") || text.includes("no secrets provided")) { if (text.includes("Secrets were required") || text.includes("no secrets provided")) {
root.lastError = "Incorrect password" root.lastError = "Incorrect password"
forget(connectProcess.ssid)
} else if (text.includes("No network with SSID")) { } else if (text.includes("No network with SSID")) {
root.lastError = "Network not found" root.lastError = "Network not found"
} else if (text.includes("Timeout")) { } else if (text.includes("Timeout")) {