Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: update label parsing for network component (#377)

blamdfbfe089c7eb
diff --git a/GlazeWM.Bar/Components/NetworkComponent.xaml b/GlazeWM.Bar/Components/NetworkComponent.xaml
index 491240d9..8d88f465 100644
--- a/GlazeWM.Bar/Components/NetworkComponent.xaml
+++ b/GlazeWM.Bar/Components/NetworkComponent.xaml
@@ -4,15 +4,7 @@
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"
+ xmlns:components="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>
+ <components:LabelComponent DataContext="{Binding Label}" />
</UserControl>
diff --git a/GlazeWM.Bar/Components/NetworkComponentViewModel.cs b/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
index 2ff3c66d..e7c770e3 100644
--- a/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Reactive.Linq;
using GlazeWM.Domain.UserConfigs;
@@ -10,12 +11,16 @@ namespace GlazeWM.Bar.Components
public class NetworkComponentViewModel : ComponentViewModel
{
private NetworkComponentConfig _config => _componentConfig as NetworkComponentConfig;
- public string Text => FormatLabel();
- private string FormatLabel()
+
+ /// <summary>
+ /// Format the current power status with the user's formatting config.
+ /// </summary>
+ private LabelViewModel _label;
+
+ public LabelViewModel Label
{
- if (!NetworkService.PingTest())
- return _config.LabelNoInternet;
- return getNetworkIcon();
+ get => _label;
+ protected set => SetField(ref _label, value);
}
private string getNetworkIcon()
@@ -57,9 +62,25 @@ namespace GlazeWM.Bar.Components
Observable
.Interval(TimeSpan.FromSeconds(10))
.TakeUntil(_parentViewModel.WindowClosing)
- .Subscribe(_ => OnPropertyChanged(nameof(Text)));
+ .Subscribe(_ => Label = CreateLabel());
+
+ NetworkChange.NetworkAddressChanged +=
+ (s, e) => Label = CreateLabel();
+ }
+
+ public LabelViewModel CreateLabel()
+ {
+ string icon;
+ if (!NetworkService.PingTest())
+ icon = _config.LabelNoInternet;
+ else
+ icon = getNetworkIcon();
- NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler((s, e) => OnPropertyChanged(nameof(Text)));
+ return XamlHelper.ParseLabel(
+ icon,
+ null,
+ this
+ );
}
}
}
diff --git a/GlazeWM.Bar/XamlHelper.cs b/GlazeWM.Bar/XamlHelper.cs
index 4427eef4..c0fd405a 100644
--- a/GlazeWM.Bar/XamlHelper.cs
+++ b/GlazeWM.Bar/XamlHelper.cs
@@ -66,9 +66,12 @@ namespace GlazeWM.Bar
{
var labelWithVariables = labelString;
- // Replace variables in label with their corresponding variable.
- foreach (var (key, value) in labelVariables)
- labelWithVariables = labelWithVariables.Replace($"{{{key}}}", value());
+ if (labelVariables is not null)
+ {
+ // Replace variables in label with their corresponding variable.
+ foreach (var (key, value) in labelVariables)
+ labelWithVariables = labelWithVariables.Replace($"{{{key}}}", value());
+ }
// Wrap `labelString` in arbitrary tag to make it valid XML.
var wrappedLabel = $"<Label>{labelWithVariables}</Label>";
diff --git a/README.md b/README.md
index 04554c98..ff457915 100644
--- a/README.md
+++ b/README.md
@@ -332,6 +332,33 @@ Displays the current Memory usage.
refresh_interval_ms: 1000
```
+### Bar Component: Network
+
+Displays the active Network.
+
+```yaml
+- type: "network"
+ label_no_internet: "NC"
+ label_ethernet: "Eth"
+ label_wifi_strength_0: "WiFi: 0%"
+ label_wifi_strength_25: "WiFi: 25%"
+ label_wifi_strength_50: "WiFi: 50%"
+ label_wifi_strength_75: "WiFi: 75%"
+ label_wifi_strength_100: "WiFi: 100%"
+```
+
+### Bar Component: Volume
+
+Displays volume level.
+
+```yaml
+- type: "volume"
+ label_low: "πŸ”Š{volume_level}%"
+ label_medium: "πŸ”Š{volume_level}%"
+ label_high: "πŸ”Š{volume_level}%"
+ label_mute: "πŸ”Š{volume_level}%"
+```
+
### Bar Component: Text File
For displaying any content without a native integrated widget; updates in real time.