Skip to content

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

feat: add system tray bar component (#235)

blamdd064dcee66e
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index 17a894c2..5179db64 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -86,6 +86,7 @@ namespace GlazeWM.Bar
TilingDirectionComponentConfig tdc => new TilingDirectionComponentViewModel(this, tdc),
WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),
WindowTitleComponentConfig wtcc => new WindowTitleComponentViewModel(this, wtcc),
+ SystemTrayComponentConfig stcc => new SystemTrayComponentViewModel(this, stcc),
_ => throw new ArgumentOutOfRangeException(nameof(config)),
});
}
diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xaml
index 70016b0c..1c3f100a 100644
--- a/GlazeWM.Bar/Components/ComponentPortal.xaml
+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml
@@ -31,6 +31,9 @@
<DataTemplate DataType="{x:Type components:WindowTitleComponentViewModel}">
<components:WindowTitleComponent />
</DataTemplate>
+ <DataTemplate DataType="{x:Type components:SystemTrayComponentViewModel}">
+ <components:SystemTrayComponent />
+ </DataTemplate>
<DataTemplate DataType="{x:Type components:TilingDirectionComponentViewModel}">
<components:TilingDirectionComponent />
</DataTemplate>
diff --git a/GlazeWM.Bar/Components/NotifyIcon.xaml b/GlazeWM.Bar/Components/NotifyIcon.xaml
new file mode 100644
index 00000000..9d375b90
--- /dev/null
+++ b/GlazeWM.Bar/Components/NotifyIcon.xaml
@@ -0,0 +1,48 @@
+<UserControl
+ x:Class="GlazeWM.Bar.Components.NotifyIcon"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:local="clr-namespace:GlazeWM.Bar.Components"
+ xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
+ <UserControl.Resources>
+ <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
+ </UserControl.Resources>
+ <Border
+ x:Name="NotifyIconBorder"
+ ToolTip="{Binding Path=TrayIcon.Title}">
+ <Image
+ Margin="1,0"
+ Source="{Binding Path=TrayIcon.Icon, Mode=OneWay}"
+ Focusable="True"
+ Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
+ Name="NotifyIconImage">
+ <i:Interaction.Triggers>
+ <i:EventTrigger EventName="MouseUp">
+ <i:InvokeCommandAction
+ Command="{Binding OnMouseUpCommand}"
+ PassEventArgsToCommand="True" />
+ </i:EventTrigger>
+ <i:EventTrigger EventName="MouseDown">
+ <i:InvokeCommandAction
+ Command="{Binding OnMouseDownCommand}"
+ PassEventArgsToCommand="True" />
+ </i:EventTrigger>
+ <i:EventTrigger EventName="MouseEnter">
+ <i:InvokeCommandAction
+ Command="{Binding OnMouseEnterCommand}"
+ PassEventArgsToCommand="True" />
+ </i:EventTrigger>
+ <i:EventTrigger EventName="MouseLeave">
+ <i:InvokeCommandAction
+ Command="{Binding OnMouseLeaveCommand}"
+ PassEventArgsToCommand="True" />
+ </i:EventTrigger>
+ <i:EventTrigger EventName="MouseMove">
+ <i:InvokeCommandAction
+ Command="{Binding OnMouseMoveCommand}"
+ PassEventArgsToCommand="True" />
+ </i:EventTrigger>
+ </i:Interaction.Triggers>
+ </Image>
+ </Border>
+</UserControl>
diff --git a/GlazeWM.Bar/Components/NotifyIcon.xaml.cs b/GlazeWM.Bar/Components/NotifyIcon.xaml.cs
new file mode 100644
index 00000000..af0280ba
--- /dev/null
+++ b/GlazeWM.Bar/Components/NotifyIcon.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace GlazeWM.Bar.Components
+{
+ /// <summary>
+ /// Interaction logic for NotifyIcon.xaml
+ /// </summary>
+ public partial class NotifyIcon : UserControl
+ {
+ public NotifyIcon()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/NotifyIconViewModel.cs b/GlazeWM.Bar/Components/NotifyIconViewModel.cs
new file mode 100644
index 00000000..35e08d9a
--- /dev/null
+++ b/GlazeWM.Bar/Components/NotifyIconViewModel.cs
@@ -0,0 +1,99 @@
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using GlazeWM.Bar.Common;
+using ManagedShell.Common.Helpers;
+using ManagedShell.Interop;
+
+namespace GlazeWM.Bar.Components
+{
+ public class NotifyIconViewModel : ViewModelBase
+ {
+ public ManagedShell.WindowsTray.NotifyIcon TrayIcon { get; set; }
+ // Hide native tray icons
+ public bool IsVisible => !TrayIcon.Path.Contains("\\Windows\\explorer.exe");
+ public ICommand OnMouseUpCommand =>
+ new RelayCommand<MouseButtonEventArgs>(OnMouseUp);
+ public ICommand OnMouseDownCommand =>
+ new RelayCommand<MouseButtonEventArgs>(OnMouseDown);
+ public ICommand OnMouseEnterCommand => new RelayCommand<MouseEventArgs>(OnMouseEnter);
+ public ICommand OnMouseLeaveCommand => new RelayCommand<MouseEventArgs>(OnMouseLeave);
+ public ICommand OnMouseMoveCommand => new RelayCommand<MouseEventArgs>(OnMouseMove);
+
+ public NotifyIconViewModel(ManagedShell.WindowsTray.NotifyIcon trayIcon)
+ {
+ TrayIcon = trayIcon;
+ }
+
+ private void OnMouseDown(MouseButtonEventArgs e)
+ {
+ e.Handled = true;
+
+ if (!Keyboard.IsKeyDown(Key.LeftAlt))
+ TrayIcon.IconMouseDown(
+ e.ChangedButton,
+ MouseHelper.GetCursorPositionParam(),
+ System.Windows.Forms.SystemInformation.DoubleClickTime
+ );
+ }
+
+ private void OnMouseUp(MouseButtonEventArgs e)
+ {
+ e.Handled = true;
+
+ if (Keyboard.IsKeyDown(Key.LeftAlt))
+ {
+ if (TrayIcon.IsPinned)
+ {
+ TrayIcon.Unpin();
+ return;
+ }
+
+ TrayIcon.Pin();
+ return;
+ }
+
+ TrayIcon.IconMouseUp(
+ e.ChangedButton,
+ MouseHelper.GetCursorPositionParam(),
+ System.Windows.Forms.SystemInformation.DoubleClickTime
+ );
+ }
+
+ private void OnMouseEnter(MouseEventArgs e)
+ {
+ e.Handled = true;
+
+ // Update icon position for `Shell_NotifyIconGetRect`.
+ var eventSource = e.Source as Image;
+ var location = eventSource.PointToScreen(new Point(0, 0));
+ var dpiScale = PresentationSource
+ .FromVisual(eventSource)
+ .CompositionTarget
+ .TransformToDevice
+ .M11;
+
+ TrayIcon.Placement = new NativeMethods.Rect
+ {
+ Top = (int)location.Y,
+ Left = (int)location.X,
+ Bottom = (int)(eventSource.ActualHeight * dpiScale),
+ Right = (int)(eventSource.ActualWidth * dpiScale)
+ };
+
+ TrayIcon.IconMouseEnter(MouseHelper.GetCursorPositionParam());
+ }
+
+ private void OnMouseLeave(MouseEventArgs e)
+ {
+ e.Handled = true;
+ TrayIcon.IconMouseLeave(MouseHelper.GetCursorPositionParam());
+ }
+
+ private void OnMouseMove(MouseEventArgs e)
+ {
+ e.Handled = true;
+ TrayIcon.IconMouseMove(MouseHelper.GetCursorPositionParam());
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/SystemTrayComponent.xaml b/GlazeWM.Bar/Components/SystemTrayComponent.xaml
new file mode 100644
index 00000000..810dd293
--- /dev/null
+++ b/GlazeWM.Bar/Components/SystemTrayComponent.xaml
@@ -0,0 +1,61 @@
+<UserControl
+ x:Class="GlazeWM.Bar.Components.SystemTrayComponent"
+ 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">
+ <UserControl.Resources>
+ <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
+ </UserControl.Resources>
+ <StackPanel Orientation="Horizontal">
+ <Button
+ Background="{Binding Background}"
+ Foreground="{Binding Foreground}"
+ FontFamily="{Binding FontFamily}"
+ Content="{Binding ExpandCollapseText}"
+ FontWeight="{Binding FontWeight}"
+ FontSize="{Binding FontSize}"
+ Command="{Binding ToggleShowAllIconsCommand}"
+ BorderThickness="0"
+ Cursor="Hand" />
+ <Border
+ x:Name="NotifyIconBorder"
+ ToolTip="{Binding Path=TrayIcon.Title}"
+ Visibility="{Binding Path=IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}">
+ <ItemsControl
+ Focusable="False"
+ ItemsSource="{Binding UnpinnedTrayIcons}"
+ HorizontalAlignment="Center"
+ VerticalAlignment="Center">
+ <ItemsControl.ItemsPanel>
+ <ItemsPanelTemplate>
+ <WrapPanel Orientation="Horizontal" />
+ </ItemsPanelTemplate>
+ </ItemsControl.ItemsPanel>
+ <ItemsControl.ItemTemplate>
+ <DataTemplate>
+ <local:NotifyIcon Cursor="Hand" />
+ </DataTemplate>
+ </ItemsControl.ItemTemplate>
+ </ItemsControl>
+ </Border>
+ <ItemsControl
+ Focusable="False"
+ ItemsSource="{Binding PinnedTrayIcons}"
+ HorizontalAlignment="Center"
+ VerticalAlignment="Center">
+ <ItemsControl.ItemsPanel>
+ <ItemsPanelTemplate>
+ <WrapPanel Orientation="Horizontal" />
+ </ItemsPanelTemplate>
+ </ItemsControl.ItemsPanel>
+ <ItemsControl.ItemTemplate>
+ <DataTemplate>
+ <local:NotifyIcon Cursor="Hand" />
+ </DataTemplate>
+ </ItemsControl.ItemTemplate>
+ </ItemsControl>
+ </StackPanel>
+</UserControl>
diff --git a/GlazeWM.Bar/Components/SystemTrayComponent.xaml.cs b/GlazeWM.Bar/Components/SystemTrayComponent.xaml.cs
new file mode 100644
index 00000000..08c707ca
--- /dev/null
+++ b/GlazeWM.Bar/Components/SystemTrayComponent.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace GlazeWM.Bar.Components
+{
+ /// <summary>
+ /// Interaction logic for SystemTrayComponent.xaml
+ /// </summary>
+ public partial class SystemTrayComponent : UserControl
+ {
+ public SystemTrayComponent()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/SystemTrayComponentViewModel.cs b/GlazeWM.Bar/Components/SystemTrayComponentViewModel.cs
new file mode 100644
index 00000000..76dac039
--- /dev/null
+++ b/GlazeWM.Bar/Components/SystemTrayComponentViewModel.cs
@@ -0,0 +1,70 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Windows.Input;
+using GlazeWM.Bar.Common;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Infrastructure;
+using ManagedShell;
+
+namespace GlazeWM.Bar.Components
+{
+ public class SystemTrayComponentViewModel : ComponentViewModel
+ {
+ private readonly SystemTrayComponentConfig _config;
+
+ private readonly ShellManager _shellManager =
+ ServiceLocator.GetRequiredService<ShellManager>();
+
+ public ICommand ToggleShowAllIconsCommand => new RelayCommand(ToggleShowAllIcons);
+
+ private bool _isExpanded = true;
+ public bool IsExpanded
+ {
+ get => _isExpanded;
+ set
+ {
+ _isExpanded = value;
+ OnPropertyChanged(nameof(IsExpanded));
+ }
+ }
+
+ public string ExpandCollapseText => IsExpanded
+ ? _config.LabelCollapseText
+ : _config.LabelExpandText;
+
+ public ObservableCollection<NotifyIconViewModel> PinnedTrayIcons =>
+ new(_pinnedTrayIcons);
+ public ObservableCollection<NotifyIconViewModel> UnpinnedTrayIcons =>
+ new(_unpinnedTrayIcons);
+
+ private IEnumerable<NotifyIconViewModel> _pinnedTrayIcons =>
+ _shellManager.NotificationArea.PinnedIcons
+ .Cast<ManagedShell.WindowsTray.NotifyIcon>()
+ .Select(trayIcon => new NotifyIconViewModel(trayIcon));
+
+ private IEnumerable<NotifyIconViewModel> _unpinnedTrayIcons =>
+ _shellManager.NotificationArea.UnpinnedIcons
+ .Cast<ManagedShell.WindowsTray.NotifyIcon>()
+ .Select(trayIcon => new NotifyIconViewModel(trayIcon));
+
+ public SystemTrayComponentViewModel(
+ BarViewModel parentViewModel,
+ SystemTrayComponentConfig config) : base(parentViewModel, config)
+ {
+ _config = config;
+
+ // Subscribe to collection changes of pinned/unpinned tray icons.
+ _shellManager.NotificationArea.UnpinnedIcons.CollectionChanged +=
+ (_, _) => OnPropertyChanged(nameof(UnpinnedTrayIcons));
+ _shellManager.NotificationArea.PinnedIcons.CollectionChanged +=
+ (_, _) => OnPropertyChanged(nameof(PinnedTrayIcons));
+ }
+
+ public void ToggleShowAllIcons()
+ {
+ IsExpanded = !IsExpanded;
+ OnPropertyChanged(nameof(ExpandCollapseText));
+ }
+ }
+}
diff --git a/GlazeWM.Bar/DependencyInjection.cs b/GlazeWM.Bar/DependencyInjection.cs
index d69bb194..13c58ecf 100644
--- a/GlazeWM.Bar/DependencyInjection.cs
+++ b/GlazeWM.Bar/DependencyInjection.cs
@@ -1,3 +1,4 @@
+using ManagedShell;
using Microsoft.Extensions.DependencyInjection;
namespace GlazeWM.Bar
@@ -7,6 +8,7 @@ namespace GlazeWM.Bar
public static IServiceCollection AddBarServices(this IServiceCollection services)
{
services.AddSingleton<BarService>();
+ services.AddSingleton<ShellManager>();
return services;
}
diff --git a/GlazeWM.Bar/GlazeWM.Bar.csproj b/GlazeWM.Bar/GlazeWM.Bar.csproj
index cf12d9f6..5b57b80d 100644
--- a/GlazeWM.Bar/GlazeWM.Bar.csproj
+++ b/GlazeWM.Bar/GlazeWM.Bar.csproj
@@ -11,4 +11,8 @@
<ProjectReference Include="..\GlazeWM.Domain\GlazeWM.Domain.csproj" />
<ProjectReference Include="..\GlazeWM.Infrastructure\GlazeWM.Infrastructure.csproj" />
</ItemGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
+ </ItemGroup>
</Project>
diff --git a/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj b/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
index aa57abf6..9e7b6a65 100644
--- a/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
+++ b/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
@@ -5,7 +5,7 @@
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<DebugType>embedded</DebugType>
- <ApplicationIcon>icon.ico</ApplicationIcon>
+ <ApplicationIcon>Resources\icon.ico</ApplicationIcon>
<AssemblyName>GlazeWM</AssemblyName>
</PropertyGroup>
@@ -16,15 +16,16 @@
</ItemGroup>
<ItemGroup>
- <None Remove="icon.ico" />
- <None Remove="sample-config.yaml" />
+ <None Remove="Resources\icon.ico" />
+ <None Remove="Resources\sample-config.yaml" />
</ItemGroup>
<ItemGroup>
- <EmbeddedResource Include="icon.ico">
+ <EmbeddedResource Include="Resources\icon.ico">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</EmbeddedResource>
- <EmbeddedResource Include="sample-config.yaml">
+ <Resource Include="Resources\MaterialIcons-Regular.ttf" />
+ <EmbeddedResource Include="Resources\sample-config.yaml">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</EmbeddedResource>
</ItemGroup>
diff --git a/GlazeWM.Bootstrapper/Resources/MaterialIcons-Regular.ttf b/GlazeWM.Bootstrapper/Resources/MaterialIcons-Regular.ttf
new file mode 100644
index 00000000..9d09b0fe
Binary files /dev/null and b/GlazeWM.Bootstrapper/Resources/MaterialIcons-Regular.ttf differ
diff --git a/GlazeWM.Bootstrapper/icon.ico b/GlazeWM.Bootstrapper/Resources/icon.ico
similarity index 100%
rename from GlazeWM.Bootstrapper/icon.ico
rename to GlazeWM.Bootstrapper/Resources/icon.ico
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/Resources/sample-config.yaml
similarity index 99%
rename from GlazeWM.Bootstrapper/sample-config.yaml
rename to GlazeWM.Bootstrapper/Resources/sample-config.yaml
index 9372e113..fa142fc7 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/Resources/sample-config.yaml
@@ -27,6 +27,7 @@ bar:
components_center:
- type: "window title"
components_right:
+ - type: "system tray"
- type: "tiling direction"
label_horizontal: "⮂"
label_vertical: "⮁"
diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.cs
index 8deaa959..1ec0aaa6 100644
--- a/GlazeWM.Bootstrapper/Startup.cs
+++ b/GlazeWM.Bootstrapper/Startup.cs
@@ -66,7 +66,7 @@ namespace GlazeWM.Bootstrapper
var systemTrayIconConfig = new SystemTrayIconConfig
{
HoverText = "GlazeWM",
- IconResourceName = "GlazeWM.Bootstrapper.icon.ico",
+ IconResourceName = "GlazeWM.Bootstrapper.Resources.icon.ico",
Actions = new Dictionary<string, Action>
{
{ "Reload config", () => _bus.Invoke(new ReloadUserConfigCommand()) },
diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
index 71d7e530..a6e7ca96 100644
--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
@@ -48,6 +48,11 @@ namespace GlazeWM.Domain.UserConfigs
jsonObject.RootElement.ToString(),
options
),
+ "system tray" =>
+ JsonSerializer.Deserialize<SystemTrayComponentConfig>(
+ jsonObject.RootElement.ToString(),
+ options
+ ),
"workspaces" =>
JsonSerializer.Deserialize<WorkspacesComponentConfig>(
jsonObject.RootElement.ToString(),
diff --git a/GlazeWM.Domain/UserConfigs/SystemTrayComponentConfig.cs b/GlazeWM.Domain/UserConfigs/SystemTrayComponentConfig.cs
new file mode 100644
index 00000000..5f39df9a
--- /dev/null
+++ b/GlazeWM.Domain/UserConfigs/SystemTrayComponentConfig.cs
@@ -0,0 +1,23 @@
+namespace GlazeWM.Domain.UserConfigs
+{
+ public class SystemTrayComponentConfig : BarComponentConfig
+ {
+ /// <summary>
+ /// Expands to show both pinned and unppined icons.
+ /// </summary>
+ public string LabelExpandText { get; set; } = "";
+
+ /// <summary>
+ /// Collapse to show only pinned icons.
+ /// </summary>
+ public string LabelCollapseText { get; set; } = "";
+
+ /// <summary>
+ /// Sets default icon font if one isn't specified.
+ /// </summary>
+ public SystemTrayComponentConfig()
+ {
+ FontFamily = "pack://application:,,,/Resources/#Material Icons";
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
index b3e989be..5d2c032a 100644
--- a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
+++ b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
@@ -18,5 +18,6 @@
<PackageReference Include="YamlDotNet" Version="8.1.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0" />
+ <PackageReference Include="ManagedShell" Version="0.0.198" />
</ItemGroup>
</Project>