nixnew/modules/user/modules/quickshell/shell/bar/connections/Network.qml
2026-04-24 01:41:22 -07:00

194 lines
4.7 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Quickshell
import Quickshell.Widgets
import Quickshell.Networking
import qs
import qs.bar
ClickableIcon {
id: root
required property var bar;
readonly property WifiDevice adapter: Networking.devices.values.find(d => d.type === DeviceType.Wifi)
readonly property WiredDevice eth: Networking.devices.values.find(d => d.type === DeviceType.Wired)
readonly property bool connected: adapter.connected
readonly property WifiNetwork activeNetwork: adapter.networks.values.find(network => network.connected)
readonly property string wifiIcon: {
if (root.adapter) {
if (!Networking.wifiEnabled) return "root:/icons/wifi-slash.svg";
if (!root.adapter.connected) return "root:/icons/wifi-x.svg";
return `root:/icons/wifi-${Math.round(root.activeNetwork.signalStrength * 3)}.svg`
}
return "root:/icons/wifi-x.svg";
}
readonly property string ethIcon: {
if (root.eth) {
if (!root.eth.network) return "root:/icons/ethernet-x.svg";
else if (!root.eth.network.connected) return "root:/icons/ethernet-slash.svg";
else return "root:/icons/ethernet.svg";
}
return "root:/icons/ethernet-x.svg";
}
property bool showMenu: false
onPressed: event => {
event.accepted = true;
if (event.button === Qt.RightButton) {
showMenu = !showMenu;
}
}
onClicked: event => {
if (event.button === Qt.LeftButton) {
Networking.wifiEnabled = !Networking.wifiEnabled;
}
}
showPressed: showMenu || (pressedButtons & ~Qt.RightButton)
implicitHeight: width
fillWindowWidth: true
acceptedButtons: Qt.LeftButton | Qt.RightButton
image: {
if (root.eth?.network && root.eth.network.connected) {
return "root:/icons/ethernet.svg";
} else if (Networking.wifiEnabled) {
return root.wifiIcon;
} else if (root.eth?.network) {
return "root:/icons/ethernet-slash.svg";
} else {
return "root:/icons/ethernet-x.svg";
}
}
property var tooltip: TooltipItem {
tooltip: bar.tooltip
owner: root
show: root.containsMouse
Label { text: "Network" }
}
property var rightclickMenu: TooltipItem {
id: rightclickMenu
tooltip: bar.tooltip
owner: root
isMenu: true
show: root.showMenu
onClose: root.showMenu = false
Loader {
width: 400
active: root.showMenu || rightclickMenu.visible
sourceComponent: Column {
spacing: 5
move: Transition {
SmoothedAnimation { property: "y"; velocity: 350 }
}
Loader {
width: parent.width
active: root.eth != null
visible: active
sourceComponent: ColumnLayout {
RowLayout {
Layout.fillWidth: true
ClickableIcon {
image: root.ethIcon
implicitHeight: 40
implicitWidth: height
onClicked: {
const n = root.eth.network;
if (n.connected) n.disconnect();
else n.connect();
}
}
Label {
text: `Ethernet (${root.eth?.name ?? ""})`
}
}
Rectangle {
Layout.fillWidth: true
implicitHeight: 1
color: ShellGlobals.colors.separator
visible: root.adapter != null
}
}
}
Loader {
width: parent.width
active: root.adapter != null
visible: active
sourceComponent: ColumnLayout {
RowLayout {
Layout.fillWidth: true
ClickableIcon {
image: root.wifiIcon
implicitHeight: 40
implicitWidth: height
onClicked: Networking.wifiEnabled = !Networking.wifiEnabled
}
Label {
text: `Wifi (${root.adapter.name})`
}
Item { Layout.fillWidth: true }
ActivityButton {
image: "root:/icons/binoculars.svg"
implicitHeight: 24
implicitWidth: height
onClicked: root.adapter.scannerEnabled = !root.adapter.scannerEnabled
showAction: root.adapter.scannerEnabled
Layout.rightMargin: 4
}
}
Rectangle {
Layout.fillWidth: parent
implicitHeight: 1
visible: root.adapter.networks.values.length > 0
color: ShellGlobals.colors.separator
}
Repeater {
model: ScriptModel {
values: [...root.adapter.networks.values].sort((a, b) => {
if (a.connected && !b.connected) return -1;
if (b.connected && !a.connected) return 1;
if (a.bonded && !b.bonded) return -1;
if (b.bonded && !a.bonded) return 1;
return b.name - a.name;
})
}
delegate: NetworkDelegate {
required property var modelData
Layout.fillWidth: parent
device: root.adapter
network: modelData
width: parent.width
onConnectionAttempted: root.adapter.scannerEnabled = false
}
}
}
}
}
}
}
}