GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add volume bar component (#277)
blam289eeb4c2be1
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.csindex 11ba4983..36aa1280 100644--- a/GlazeWM.Bar/BarViewModel.cs+++ b/GlazeWM.Bar/BarViewModel.cs@@ -100,6 +100,7 @@ namespace GlazeWM.BarTilingDirectionComponentConfig tdc => new TilingDirectionComponentViewModel(this, tdc),WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),WindowTitleComponentConfig wtcc => new WindowTitleComponentViewModel(this, wtcc),+ VolumeComponentConfig vcc => new VolumeComponentViewModel(this, vcc),SystemTrayComponentConfig stcc => new SystemTrayComponentViewModel(this, stcc),_ => throw new ArgumentOutOfRangeException(nameof(config)),});diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xamlindex e616991a..7cca2484 100644--- a/GlazeWM.Bar/Components/ComponentPortal.xaml+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml@@ -43,6 +43,9 @@<DataTemplate DataType="{x:Type components:WorkspacesComponentViewModel}"><components:WorkspacesComponent Padding="{Binding Padding}" Background="{Binding Background}" /></DataTemplate>+ <DataTemplate DataType="{x:Type components:VolumeComponentViewModel}">+ <components:VolumeComponent />+ </DataTemplate></ContentPresenter.Resources></ContentPresenter></Grid>diff --git a/GlazeWM.Bar/Components/VolumeComponent.xaml b/GlazeWM.Bar/Components/VolumeComponent.xamlnew file mode 100644index 00000000..3149b4a6--- /dev/null+++ b/GlazeWM.Bar/Components/VolumeComponent.xaml@@ -0,0 +1,16 @@+<UserControl+ x:Class="GlazeWM.Bar.Components.VolumeComponent"+ 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">+ <TextBlock+ VerticalAlignment="Center"+ Foreground="{Binding Foreground}"+ FontFamily="{Binding FontFamily}"+ FontWeight="{Binding FontWeight}"+ FontSize="{Binding FontSize}"+ Text="{Binding FormattedText}" />+</UserControl>diff --git a/GlazeWM.Bar/Components/VolumeComponent.xaml.cs b/GlazeWM.Bar/Components/VolumeComponent.xaml.csnew file mode 100644index 00000000..1fccd698--- /dev/null+++ b/GlazeWM.Bar/Components/VolumeComponent.xaml.cs@@ -0,0 +1,15 @@+using System.Windows.Controls;++namespace GlazeWM.Bar.Components+{+ /// <summary>+ /// Interaction logic for WindowTitleComponent.xaml+ /// </summary>+ public partial class VolumeComponent : UserControl+ {+ public VolumeComponent()+ {+ InitializeComponent();+ }+ }+}diff --git a/GlazeWM.Bar/Components/VolumeComponentViewModel.cs b/GlazeWM.Bar/Components/VolumeComponentViewModel.csnew file mode 100644index 00000000..7453a009--- /dev/null+++ b/GlazeWM.Bar/Components/VolumeComponentViewModel.cs@@ -0,0 +1,51 @@+using System.Diagnostics;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Infrastructure;+using GlazeWM.Infrastructure.WindowsApi;++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+ {+ get => _formattedText;+ set+ {+ _formattedText = value;+ OnPropertyChanged(nameof(FormattedText));+ }+ }+ public VolumeComponentViewModel(+ BarViewModel parentViewModel,+ VolumeComponentConfig config) : base(parentViewModel, config)+ {+ var initVolume = _sysVolume.GetVolumeInformation();+ FormattedText = GetVolumeIcon(initVolume) + initVolume.Volume.ToString("00");++ _sysVolume.VolumeChanged += (_, volumeInfo) =>+ {+ FormattedText = GetVolumeIcon(volumeInfo) + volumeInfo.Volume.ToString("00");+ Debug.WriteLine(FormattedText);+ };+ }++ private string GetVolumeIcon(VolumeInformation vol)+ {+ if (vol.Muted)+ return _config.LabelVolumeMute;++ if (vol.Volume < 10)+ return _config.LabelVolumeLow;+ else if (vol.Volume < 50)+ return _config.LabelVolumeMed;+ else if (vol.Volume < 100)+ return _config.LabelVolumeHigh;++ return _config.LabelVolumeLow;+ }+ }+}diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.csindex 0f229ece..fccbf726 100644--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs@@ -43,6 +43,11 @@ namespace GlazeWM.Domain.UserConfigsjsonObject.RootElement.ToString(),options),+ "volume" =>+ JsonSerializer.Deserialize<VolumeComponentConfig>(+ jsonObject.RootElement.ToString(),+ options+ ),"tiling direction" =>JsonSerializer.Deserialize<TilingDirectionComponentConfig>(jsonObject.RootElement.ToString(),diff --git a/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs b/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.csnew file mode 100644index 00000000..7597e070--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/VolumeComponentConfig.cs@@ -0,0 +1,30 @@+namespace GlazeWM.Domain.UserConfigs+{+ public class VolumeComponentConfig : BarComponentConfig+ {+ /// <summary>+ /// Icon for low volume.+ /// </summary>+ public string LabelVolumeLow { get; set; } = "";+ /// <summary>+ /// Icon for medium volume.+ /// </summary>+ public string LabelVolumeMed { get; set; } = "";+ /// <summary>+ /// Icon for high volume.+ /// </summary>+ public string LabelVolumeHigh { get; set; } = "";+ /// <summary>+ /// Icon for volume mute.+ /// </summary>+ public string LabelVolumeMute { get; set; } = "";+ /// <summary>+ /// Sets default icon font if one isn't specified.+ /// </summary>+ public VolumeComponentConfig()+ {+ FontFamily = "pack://application:,,,/Resources/#Material Icons";+ }+ }++}diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.csindex df4bd55e..b4930a55 100644--- a/GlazeWM.Infrastructure/DependencyInjection.cs+++ b/GlazeWM.Infrastructure/DependencyInjection.cs@@ -17,6 +17,8 @@ namespace GlazeWM.Infrastructureservices.AddSingleton<JsonService>();services.AddSingleton<YamlService>();+ services.AddSingleton<SystemVolumeInformation>();+services.AddSingleton<ICommandHandler<ExitApplicationCommand>, ExitApplicationHandler>();services.AddSingleton<ICommandHandler<HandleFatalExceptionCommand>, HandleFatalExceptionHandler>();services.AddSingleton<ICommandHandler<NoopCommand>, NoopHandler>();diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.csindex 70ba7918..e9990ec9 100755--- a/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs+++ b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs@@ -11,7 +11,7 @@ namespace GlazeWM.Infrastructure.WindowsApiprivate IMMDevice _defaultDevice;private IAudioEndpointVolume _endPointVolume;- public event EventHandler<VolumeChangedEventArgs> VolumeChanged;+ public event EventHandler<VolumeInformation> VolumeChanged;public SystemVolumeInformation(){@@ -22,12 +22,20 @@ namespace GlazeWM.Infrastructure.WindowsApi_endPointVolume = epVol as IAudioEndpointVolume;_endPointVolume.RegisterControlChangeNotify(this);- VolumeChanged?.Invoke(this, new VolumeChangedEventArgs+ VolumeChanged?.Invoke(this, new VolumeInformation{- Volume = _endPointVolume.GetMasterVolumeLevelScalar(),+ Volume = (int)(_endPointVolume.GetMasterVolumeLevelScalar() * 100),Muted = _endPointVolume.GetMute()});}+ public VolumeInformation GetVolumeInformation()+ {+ return new()+ {+ Volume = (int)(_endPointVolume.GetMasterVolumeLevelScalar() * 100),+ Muted = _endPointVolume.GetMute()+ };+ }public HRESULT OnNotify(IntPtr pNotify){@@ -43,9 +51,9 @@ namespace GlazeWM.Infrastructure.WindowsApichannelVolumes[i] = Marshal.PtrToStructure<float>(firstChannelVolumesMember);}- VolumeChanged?.Invoke(this, new VolumeChangedEventArgs+ VolumeChanged?.Invoke(this, new VolumeInformation{- Volume = notificationData.fMasterVolume,+ Volume = (int)(notificationData.fMasterVolume * 100),Muted = notificationData.bMuted});diff --git a/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs b/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.csindex 22d0c7d4..b804b25e 100755--- a/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs+++ b/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs@@ -1,8 +1,8 @@namespace GlazeWM.Infrastructure.WindowsApi{- public class VolumeChangedEventArgs+ public class VolumeInformation{- public float Volume { get; set; }+ public int Volume { get; set; }public bool Muted { get; set; }}}