network finished
This commit is contained in:
96
Bar/Network.qml
Normal file
96
Bar/Network.qml
Normal file
@@ -0,0 +1,96 @@
|
||||
pragma ComponentBehavior: Bound
|
||||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import Quickshell
|
||||
import Quickshell.Hyprland
|
||||
|
||||
import "../Color.js" as Colors
|
||||
import "../Components/"
|
||||
import "../Services/"
|
||||
|
||||
Container {
|
||||
id: root
|
||||
|
||||
required property HyprlandMonitor monitor
|
||||
property bool opened: false
|
||||
|
||||
boxColor: Colors.red
|
||||
boxHeight: 25
|
||||
boxWidth: 33
|
||||
defaultItem: icon
|
||||
exclusiveMonitor: root.monitor
|
||||
exclusiveToScreen: true
|
||||
forceHidden: !NetworkManager.isConnectedAndWifi()
|
||||
|
||||
states: [
|
||||
State {
|
||||
name: "closed"
|
||||
when: root.hovered == false && root.opened == false
|
||||
|
||||
PropertyChanges {
|
||||
root.boxHeight: 25
|
||||
root.boxRadius: 9
|
||||
root.boxWidth: 33
|
||||
}
|
||||
},
|
||||
State {
|
||||
name: "hovered"
|
||||
when: root.hovered == true && root.opened == false
|
||||
|
||||
PropertyChanges {
|
||||
root.boxHeight: 28
|
||||
root.boxWidth: 35
|
||||
}
|
||||
},
|
||||
State {
|
||||
name: "opened"
|
||||
when: root.opened == true && root.hovered == true
|
||||
|
||||
PropertyChanges {
|
||||
root.boxHeight: 28
|
||||
root.boxRadius: 12
|
||||
root.boxWidth: 350
|
||||
}
|
||||
|
||||
StateChangeScript {
|
||||
script: {
|
||||
stack.replace(wifiInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
hover.onHoveredChanged: {
|
||||
if (hover.hovered == false) {
|
||||
root.opened = false;
|
||||
if (root.state != "icon") {
|
||||
root.stack.replace(icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
tap.onTapped: {
|
||||
root.opened = true;
|
||||
}
|
||||
|
||||
Component {
|
||||
id: icon
|
||||
|
||||
StyledText {
|
||||
horizontalAlignment: Qt.AlignCenter
|
||||
propo: true
|
||||
text: NetworkManager.getNetworkDetails(NetworkManager.defaultAdapter)["icon"]
|
||||
verticalAlignment: Qt.AlignVCenter
|
||||
}
|
||||
}
|
||||
|
||||
Component {
|
||||
id: wifiInfo
|
||||
|
||||
StyledText {
|
||||
horizontalAlignment: Qt.AlignCenter
|
||||
propo: true
|
||||
text: NetworkManager.getWifiText()
|
||||
verticalAlignment: Qt.AlignVCenter
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,20 +8,70 @@ Singleton {
|
||||
|
||||
property NetworkDevice defaultAdapter: Networking.devices.values[0]
|
||||
|
||||
function getConnectivity() {
|
||||
if (Networking.connectivity == NetworkConnectivity.Full) {
|
||||
return 0;
|
||||
// if ethernet hide ts (return 2)
|
||||
} else if (Networking.connectivity == NetworkConnectivity.Limited || Networking.connectivity
|
||||
== NetworkConnectivity.Portal) {
|
||||
return 1;
|
||||
} else {
|
||||
return 2;
|
||||
function getConnectedNetworks(networksArr) {
|
||||
let connectedNetworks = [];
|
||||
|
||||
for (let network of networksArr) {
|
||||
if (network.state == ConnectionState.Connected) {
|
||||
connectedNetworks.push(network);
|
||||
}
|
||||
}
|
||||
return connectedNetworks;
|
||||
}
|
||||
|
||||
function getIcon() {
|
||||
let state = getConnectivity();
|
||||
let iconArr = [""];
|
||||
function getNetworkDetails(adapter) {
|
||||
let networksArr = adapter.networks.values;
|
||||
let connectedNetworks = getConnectedNetworks(networksArr);
|
||||
let deviceWifi = isDeviceWifi(adapter);
|
||||
|
||||
let primaryNetwork = connectedNetworks[0];
|
||||
|
||||
let networkName = primaryNetwork.name;
|
||||
let networkStrength = primaryNetwork.signalStrength;
|
||||
|
||||
let networkIcon = getWifiIcon(networkStrength);
|
||||
|
||||
let outputDict = {
|
||||
"adapterName": adapter.name,
|
||||
"adapterConnected": adapter.connected,
|
||||
"adapterIsWifi": deviceWifi,
|
||||
"networkName": networkName,
|
||||
"networkStrength": networkStrength,
|
||||
"icon": networkIcon
|
||||
};
|
||||
return outputDict;
|
||||
}
|
||||
|
||||
function getWifiIcon(strength) {
|
||||
let iconArr = ["", "", "", ""];
|
||||
let strengthIndex = Math.ceil(strength * iconArr.length);
|
||||
return iconArr[strengthIndex - 1];
|
||||
}
|
||||
|
||||
function isConnectedAndWifi() {
|
||||
let isConnected = defaultAdapter.connected
|
||||
let isWifi = isDeviceWifi(defaultAdapter)
|
||||
return isConnected && isWifi
|
||||
}
|
||||
|
||||
function getWifiText() {
|
||||
let info = getNetworkDetails(defaultAdapter);
|
||||
|
||||
let adapter = info["adapterName"];
|
||||
let name = info["networkName"];
|
||||
|
||||
let finalString = adapter + " - " + name;
|
||||
return finalString;
|
||||
}
|
||||
|
||||
function isDeviceWifi(adapter) {
|
||||
if (adapter.type == DeviceType.Wifi) {
|
||||
return true;
|
||||
} else if (adapter.type == DeviceType.Wired) {
|
||||
return false;
|
||||
} else {
|
||||
console.log(adapter.type);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user