GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add wifi/eth bar component (#240)
blama197fd5fdea0
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.csindex 5179db64..c0d561e1 100644--- a/GlazeWM.Bar/BarViewModel.cs+++ b/GlazeWM.Bar/BarViewModel.cs@@ -83,6 +83,7 @@ namespace GlazeWM.BarBindingModeComponentConfig bmc => new BindingModeComponentViewModel(this, bmc),ClockComponentConfig ccc => new ClockComponentViewModel(this, ccc),TextComponentConfig tcc => new TextComponentViewModel(this, tcc),+ NetworkComponentConfig ncc => new NetworkComponentViewModel(this, ncc),TilingDirectionComponentConfig tdc => new TilingDirectionComponentViewModel(this, tdc),WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),WindowTitleComponentConfig wtcc => new WindowTitleComponentViewModel(this, wtcc),diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xamlindex 1c3f100a..247eccaf 100644--- a/GlazeWM.Bar/Components/ComponentPortal.xaml+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml@@ -28,6 +28,9 @@<DataTemplate DataType="{x:Type components:TextComponentViewModel}"><components:TextComponent /></DataTemplate>+ <DataTemplate DataType="{x:Type components:NetworkComponentViewModel}">+ <components:NetworkComponent />+ </DataTemplate><DataTemplate DataType="{x:Type components:WindowTitleComponentViewModel}"><components:WindowTitleComponent /></DataTemplate>diff --git a/GlazeWM.Bar/Components/NetworkComponent.xaml b/GlazeWM.Bar/Components/NetworkComponent.xamlnew file mode 100644index 00000000..491240d9--- /dev/null+++ b/GlazeWM.Bar/Components/NetworkComponent.xaml@@ -0,0 +1,18 @@+<UserControl+ x:Class="GlazeWM.Bar.Components.NetworkComponent"+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"+ xmlns:local="clr-namespace:GlazeWM.Bar.Components"+ mc:Ignorable="d">+ <StackPanel Orientation="Horizontal">+ <TextBlock+ VerticalAlignment="Center"+ FontFamily="{Binding FontFamily}"+ Foreground="{Binding Foreground}"+ FontWeight="{Binding FontWeight}"+ FontSize="{Binding FontSize}"+ Text="{Binding Text}" />+ </StackPanel>+</UserControl>diff --git a/GlazeWM.Bar/Components/NetworkComponent.xaml.cs b/GlazeWM.Bar/Components/NetworkComponent.xaml.csnew file mode 100644index 00000000..d8991165--- /dev/null+++ b/GlazeWM.Bar/Components/NetworkComponent.xaml.cs@@ -0,0 +1,15 @@+using System.Windows.Controls;++namespace GlazeWM.Bar.Components+{+ /// <summary>+ /// Interaction logic for NetworkComponent.xaml+ /// </summary>+ public partial class NetworkComponent : UserControl+ {+ public NetworkComponent()+ {+ InitializeComponent();+ }+ }+}diff --git a/GlazeWM.Bar/Components/NetworkComponentViewModel.cs b/GlazeWM.Bar/Components/NetworkComponentViewModel.csnew file mode 100644index 00000000..ef38b535--- /dev/null+++ b/GlazeWM.Bar/Components/NetworkComponentViewModel.cs@@ -0,0 +1,62 @@+using System;+using System.Net.NetworkInformation;+using System.Reactive.Linq;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Infrastructure.WindowsApi;+using static Vanara.PInvoke.IpHlpApi;++namespace GlazeWM.Bar.Components+{+ public class NetworkComponentViewModel : ComponentViewModel+ {+ private NetworkComponentConfig _config => _componentConfig as NetworkComponentConfig;+ public string Text => FormatLabel();+ private string FormatLabel()+ {+ if (!NetworkService.PingTest())+ return _config.LabelNoInternet;+ return getNetworkIcon();+ }++ private string getNetworkIcon()+ {+ var primaryAdapter = NetworkService.GetPrimaryAdapter();+ switch (primaryAdapter.IfType)+ {+ case IFTYPE.IF_TYPE_ETHERNET_CSMACD:+ case IFTYPE.IF_TYPE_ETHERNET_3MBIT:+ // Primary adapter is using ethernet.+ return _config.LabelEthernet;+ case IFTYPE.IF_TYPE_IEEE80211:+ // Primary adapter is using wifi.+ var rssi = NetworkService.GetWlanRSSI();+ return assignWifiIcon(rssi);+ default:+ return _config.LabelNoInternet;+ }+ }++ private string assignWifiIcon(int signalQuality)+ {+ // Round to nearest multiple and assign icon.+ return ((signalQuality % 25) > (25 / 2) ? (signalQuality / 25) + 1 : signalQuality / 25) switch+ {+ 0 => _config.LabelWifiStrength0,+ 1 => _config.LabelWifiStrength25,+ 2 => _config.LabelWifiStrength50,+ 3 => _config.LabelWifiStrength75,+ 4 => _config.LabelWifiStrength100,+ _ => _config.LabelNoInternet,+ };+ }++ public NetworkComponentViewModel(+ BarViewModel parentViewModel,+ NetworkComponentConfig config) : base(parentViewModel, config)+ {+ Observable.Interval(TimeSpan.FromSeconds(10))+ .Subscribe(_ => OnPropertyChanged(nameof(Text)));+ NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler((s, e) => OnPropertyChanged(nameof(Text)));+ }+ }+}diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.csindex a6e7ca96..0f229ece 100644--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs@@ -38,6 +38,11 @@ namespace GlazeWM.Domain.UserConfigsjsonObject.RootElement.ToString(),options),+ "network" =>+ JsonSerializer.Deserialize<NetworkComponentConfig>(+ jsonObject.RootElement.ToString(),+ options+ ),"tiling direction" =>JsonSerializer.Deserialize<TilingDirectionComponentConfig>(jsonObject.RootElement.ToString(),diff --git a/GlazeWM.Domain/UserConfigs/NetworkComponentConfig.cs b/GlazeWM.Domain/UserConfigs/NetworkComponentConfig.csnew file mode 100644index 00000000..4eb987a2--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/NetworkComponentConfig.cs@@ -0,0 +1,34 @@+namespace GlazeWM.Domain.UserConfigs+{+ public class NetworkComponentConfig : BarComponentConfig+ {+ /// <summary>+ /// Label for wifi at 0% RSSI.+ /// </summary>+ public string LabelWifiStrength0 { get; set; } = " ";+ /// <summary>+ /// Label for wifi at 25% RSSI.+ /// </summary>+ public string LabelWifiStrength25 { get; set; } = " ";+ /// <summary>+ /// Label for wifi at 50% RSSI.+ /// </summary>+ public string LabelWifiStrength50 { get; set; } = " ";+ /// <summary>+ /// Label for wifi at 75% RSSI.+ /// </summary>+ public string LabelWifiStrength75 { get; set; } = " ";+ /// <summary>+ /// Label for wifi at 100% RSSI.+ /// </summary>+ public string LabelWifiStrength100 { get; set; } = " ";+ /// <summary>+ /// Label for ethernet connection.+ /// </summary>+ public string LabelEthernet { get; set; } = " ";+ /// <summary>+ /// Label for connection to the internet.+ /// </summary>+ public string LabelNoInternet { get; set; } = " ";+ }+}diff --git a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csprojindex 5d2c032a..e16698b3 100644--- a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj+++ b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj@@ -15,6 +15,8 @@<PackageReference Include="Vanara.PInvoke.Kernel32" Version="3.4.13" /><PackageReference Include="Vanara.PInvoke.SHCore" Version="3.4.13" /><PackageReference Include="Vanara.PInvoke.User32" Version="3.4.13" />+ <PackageReference Include="Vanara.PInvoke.WlanApi" Version="3.4.12" />+ <PackageReference Include="Vanara.PInvoke.IpHlpApi" Version="3.4.12" /><PackageReference Include="YamlDotNet" Version="8.1.0" /><PackageReference Include="Roslynator.Analyzers" Version="4.2.0" /><PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0" />diff --git a/GlazeWM.Infrastructure/WindowsApi/NetworkService.cs b/GlazeWM.Infrastructure/WindowsApi/NetworkService.csnew file mode 100644index 00000000..c51e7b63--- /dev/null+++ b/GlazeWM.Infrastructure/WindowsApi/NetworkService.cs@@ -0,0 +1,65 @@+using System;+using System.Linq;+using System.Net.NetworkInformation;+using System.Text;+using Vanara.InteropServices;+using static Vanara.PInvoke.IpHlpApi;+using static Vanara.PInvoke.WlanApi;++namespace GlazeWM.Infrastructure.WindowsApi+{+ public static class NetworkService+ {+ public static int GetWlanRSSI()+ {+ var hWlan = WlanOpenHandle();+ WlanEnumInterfaces(hWlan, default, out var list);+ if (list == null || list.dwNumberOfItems < 1)+ return -1;+ var primaryIntfGuid = list.InterfaceInfo[0].InterfaceGuid;++ // Get wireless connection details.+ var getType = CorrespondingTypeAttribute.GetCorrespondingTypes(WLAN_INTF_OPCODE.wlan_intf_opcode_current_connection, CorrespondingAction.Get).FirstOrDefault();+ var interfaceDetails = WlanQueryInterface(hWlan, primaryIntfGuid, WLAN_INTF_OPCODE.wlan_intf_opcode_current_connection, default, out var dataSize, out var data, out _);+ if (interfaceDetails.Failed)+ return -1;++ // Get RSSI signal strength of connection.+ var connectionAttributes = (WLAN_CONNECTION_ATTRIBUTES)data.DangerousGetHandle().Convert(dataSize, getType);+ return (int)connectionAttributes.wlanAssociationAttributes.wlanSignalQuality;+ }++ public static bool PingTest()+ {+ try+ {+ // Use Google DNS servers to check if online.+ var reply = new Ping().Send("8.8.8.8");+ return reply.Status == IPStatus.Success;+ }+ catch (Exception)+ {+ return false;+ }+ }++ public static uint GetPrimaryAdapterID()+ {+ // Using Google DNS as example IP for IP to check against.+ var dwDestAddr = BitConverter.ToUInt32(Encoding.ASCII.GetBytes("8.8.8.8"));+ GetBestInterface(dwDestAddr, out var dwBestIfIndex);+ return dwBestIfIndex;+ }++ public static IP_ADAPTER_ADDRESSES GetPrimaryAdapter()+ {+ // Filter out active tunnels and VPNS.+ return GetAdaptersAddresses(GetAdaptersAddressesFlags.GAA_FLAG_INCLUDE_GATEWAYS).FirstOrDefault(+ r => r.OperStatus == IF_OPER_STATUS.IfOperStatusUp+ && r.TunnelType == TUNNEL_TYPE.TUNNEL_TYPE_NONE+ && r.FirstGatewayAddress != IntPtr.Zero+ && r.IfIndex == GetPrimaryAdapterID()+ );+ }+ }+}