GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(bar): position and size the WPF window manually using WinAPI
Lars Bergerb8af8d002995
diff --git a/LarsWM.Bar/MainWindow.xaml b/LarsWM.Bar/MainWindow.xamlindex a07d5c6b..d67cb123 100644--- a/LarsWM.Bar/MainWindow.xaml+++ b/LarsWM.Bar/MainWindow.xaml@@ -11,6 +11,8 @@Background="#101010"Title="LarsWMBar"ShowInTaskbar="False"+ Width="800"+ Height="50"Topmost="True"><Grid><Grid.ColumnDefinitions>diff --git a/LarsWM.Bar/MainWindow.xaml.cs b/LarsWM.Bar/MainWindow.xaml.csindex 0ab69612..e32eae20 100644--- a/LarsWM.Bar/MainWindow.xaml.cs+++ b/LarsWM.Bar/MainWindow.xaml.cs@@ -10,6 +10,8 @@ using System.Windows;using System.Windows.Controls;using LarsWM.Domain.Containers.Events;using LarsWM.Domain.UserConfigs;+using System.Windows.Interop;+using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;namespace LarsWM.Bar{@@ -18,6 +20,7 @@ namespace LarsWM.Bar/// </summary>public partial class MainWindow : Window{+ private Monitor _monitor { get; }private Bus _bus { get; }private WorkspaceService _workspaceService { get; }private UserConfigService _userConfigService { get; }@@ -25,23 +28,15 @@ namespace LarsWM.Barpublic MainWindow(Monitor monitor, WorkspaceService workspaceService, Bus bus, UserConfigService userConfigService){+ _monitor = monitor;_bus = bus;_workspaceService = workspaceService;_userConfigService = userConfigService;InitializeComponent();-- // TODO: Bind padding, bg color, button bg color and font from user config.- Top = monitor.Y;- Left = monitor.X;- Height = _userConfigService.UserConfig.Bar.Height;-- // WPF automatically changes the size of the bar based on display scale factor. This causes- // the bar width to overflow on scale factors > 100%, so need to adjust accordingly.- Width = Convert.ToInt32(monitor.Width / monitor.ScaleFactor);+ SourceInitialized += MainWindow_SourceInitialized;RefreshState(monitor);-WorkspaceItems.ItemsSource = _workspaces;var workspaceAttachedEvent = _bus.Events.Where(@event => @event is WorkspaceAttachedEvent);@@ -61,6 +56,29 @@ namespace LarsWM.Bar_workspaces.Add(workspace as Workspace);}+ private void MainWindow_SourceInitialized(object sender, EventArgs e)+ {+ PositionWindow();+ }++ /// <summary>+ /// Position and size the WPF window manually using WinAPI. When using `PerMonitorAwareV2` DPI+ /// awareness, positioning the window with WPF bindings is ambiguous and annoying.+ /// Ref: https://github.com/dotnet/wpf/issues/4127#issuecomment-790194817+ /// </summary>+ public void PositionWindow()+ {+ var windowHandle = new WindowInteropHelper(this).Handle;++ // Since window size is set manually, need to scale up height to make window DPI responsive.+ var scaledHeight = Convert.ToInt32(_userConfigService.UserConfig.Bar.Height * _monitor.ScaleFactor);++ // The first move puts it on the correct monitor, which triggers WM_DPICHANGED.+ // The +1/-1 coerces WPF to update Top/Left/Width/Height in the second move.+ MoveWindow(windowHandle, _monitor.X + 1, _monitor.Y, _monitor.Width - 1, scaledHeight, false);+ MoveWindow(windowHandle, _monitor.X, _monitor.Y, _monitor.Width, scaledHeight, true);+ }+private void OnWorkspaceButtonClick(object sender, RoutedEventArgs e){var button = sender as Button;