Skip to content

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

feat: update label parsing for volume component (#324)

blam15ca0618aaea
diff --git a/GlazeWM.Bar/Components/VolumeComponent.xaml b/GlazeWM.Bar/Components/VolumeComponent.xaml
index 3149b4a6..64dddba5 100644
--- a/GlazeWM.Bar/Components/VolumeComponent.xaml
+++ b/GlazeWM.Bar/Components/VolumeComponent.xaml
@@ -4,13 +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">
- <TextBlock
- VerticalAlignment="Center"
- Foreground="{Binding Foreground}"
- FontFamily="{Binding FontFamily}"
- FontWeight="{Binding FontWeight}"
- FontSize="{Binding FontSize}"
- Text="{Binding FormattedText}" />
+ <components:LabelComponent DataContext="{Binding Label}" />
</UserControl>
diff --git a/GlazeWM.Bar/Components/VolumeComponentViewModel.cs b/GlazeWM.Bar/Components/VolumeComponentViewModel.cs
index 7453a009..e42cdd71 100644
--- a/GlazeWM.Bar/Components/VolumeComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/VolumeComponentViewModel.cs
@@ -1,4 +1,5 @@
-using System.Diagnostics;
+using System.Collections.Generic;
+using System.Globalization;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Infrastructure;
using GlazeWM.Infrastructure.WindowsApi;
@@ -7,45 +8,62 @@ namespace GlazeWM.Bar.Components
{
public class VolumeComponentViewModel : ComponentViewModel
{
- private VolumeComponentConfig _config => _componentConfig as VolumeComponentConfig;
- private readonly SystemVolumeInformation _sysVolume = ServiceLocator.GetRequiredService<SystemVolumeInformation>();
- private string _formattedText;
- public string FormattedText
+ private readonly VolumeComponentConfig _config;
+
+ private readonly SystemVolumeInformation _sysVolume =
+ ServiceLocator.GetRequiredService<SystemVolumeInformation>();
+
+ private LabelViewModel _label;
+ public LabelViewModel Label
{
- get => _formattedText;
- set
- {
- _formattedText = value;
- OnPropertyChanged(nameof(FormattedText));
- }
+ get => _label;
+ protected set => SetField(ref _label, value);
}
+
public VolumeComponentViewModel(
BarViewModel parentViewModel,
VolumeComponentConfig config) : base(parentViewModel, config)
{
+ _config = config;
+
var initVolume = _sysVolume.GetVolumeInformation();
- FormattedText = GetVolumeIcon(initVolume) + initVolume.Volume.ToString("00");
+ Label = CreateLabel(initVolume);
_sysVolume.VolumeChanged += (_, volumeInfo) =>
{
- FormattedText = GetVolumeIcon(volumeInfo) + volumeInfo.Volume.ToString("00");
- Debug.WriteLine(FormattedText);
+ Label = CreateLabel(volumeInfo);
};
}
- private string GetVolumeIcon(VolumeInformation vol)
+ private string GetVolumeLabel(VolumeInformation volumeInfo)
{
- if (vol.Muted)
- return _config.LabelVolumeMute;
+ if (volumeInfo.Muted)
+ return _config.LabelMute;
+
+ return volumeInfo.Volume switch
+ {
+ > 0 and < 33 => _config.LabelLow,
+ >= 33 and < 66 => _config.LabelMedium,
+ _ => _config.LabelHigh
+ };
+ }
- if (vol.Volume < 10)
- return _config.LabelVolumeLow;
- else if (vol.Volume < 50)
- return _config.LabelVolumeMed;
- else if (vol.Volume < 100)
- return _config.LabelVolumeHigh;
+ public LabelViewModel CreateLabel(VolumeInformation volumeInfo)
+ {
+ return XamlHelper.ParseLabel(
+ GetVolumeLabel(volumeInfo),
+ CreateVariableDict(volumeInfo),
+ this
+ );
+ }
- return _config.LabelVolumeLow;
+ public static Dictionary<string, string> CreateVariableDict(
+ VolumeInformation volumeInfo)
+ {
+ return new()
+ {
+ { "volume_level", volumeInfo.Volume.ToString("0", CultureInfo.InvariantCulture) },
+ };
}
}
}
diff --git a/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs b/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs
index 7597e070..744df85c 100644
--- a/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs
+++ b/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs
@@ -3,28 +3,25 @@ namespace GlazeWM.Domain.UserConfigs
public class VolumeComponentConfig : BarComponentConfig
{
/// <summary>
- /// Icon for low volume.
+ /// Default label.
/// </summary>
- public string LabelVolumeLow { get; set; } = "";
+ public string Label { get; set; } = "{volume_level}%";
/// <summary>
- /// Icon for medium volume.
+ /// Label for low volume.
/// </summary>
- public string LabelVolumeMed { get; set; } = "";
+ public string LabelLow { get; set; } = "<attr ff='pack://application:,,,/Resources/#Material Icons'></attr>{volume_level}%";
/// <summary>
- /// Icon for high volume.
+ /// Label for medium volume.
/// </summary>
- public string LabelVolumeHigh { get; set; } = "";
+ public string LabelMedium { get; set; } = "<attr ff='pack://application:,,,/Resources/#Material Icons'></attr>{volume_level}%";
/// <summary>
- /// Icon for volume mute.
+ /// Label for high volume.
/// </summary>
- public string LabelVolumeMute { get; set; } = "";
+ public string LabelHigh { get; set; } = "<attr ff='pack://application:,,,/Resources/#Material Icons'></attr>{volume_level}%";
/// <summary>
- /// Sets default icon font if one isn't specified.
+ /// Label for volume mute.
/// </summary>
- public VolumeComponentConfig()
- {
- FontFamily = "pack://application:,,,/Resources/#Material Icons";
- }
+ public string LabelMute { get; set; } = "<attr ff='pack://application:,,,/Resources/#Material Icons'></attr>{volume_level}%";
}
}