GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(bar): change bar window and view model to be compatible with DI
Lars Berger42a6f3bf4a60
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.csindex 532925c4..e9de941b 100644--- a/GlazeWM.Bar/BarViewModel.cs+++ b/GlazeWM.Bar/BarViewModel.cs@@ -3,10 +3,14 @@ using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;+using System.Reactive.Linq;using System.Windows.Threading;+using GlazeWM.Domain.Containers.Events;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Workspaces;+using GlazeWM.Domain.Workspaces.Events;+using GlazeWM.Infrastructure.Bussing;namespace GlazeWM.Bar{@@ -24,29 +28,38 @@ namespace GlazeWM.Barpublic List<BarComponentConfig> ComponentsLeft { get; set; }public List<BarComponentConfig> ComponentsCenter { get; set; }public List<BarComponentConfig> ComponentsRight { get; set; }- private readonly Dispatcher _dispatcher;- private readonly Monitor _monitor;- private readonly BarConfig _barConfig;+ public Dispatcher Dispatcher { get; set; }+ public Monitor Monitor { get; set; }+ private readonly Bus _bus;+ private readonly UserConfigService _userConfigService;- public BarViewModel(Dispatcher dispatcher, Monitor monitor, BarConfig barConfig)+ public BarViewModel(Bus bus, UserConfigService userConfigService){- _dispatcher = dispatcher;- _monitor = monitor;- _barConfig = barConfig;+ _bus = bus;+ _userConfigService = userConfigService;}public void InitializeState(){- Background = _barConfig.Background;- FontFamily = _barConfig.FontFamily;- FontSize = _barConfig.FontSize;- BorderColor = _barConfig.BorderColor;- BorderWidth = ShorthandToXamlProperty(_barConfig.BorderWidth);- Padding = ShorthandToXamlProperty(_barConfig.Padding);- Opacity = _barConfig.Opacity;- ComponentsLeft = _barConfig.ComponentsLeft;- ComponentsCenter = _barConfig.ComponentsCenter;- ComponentsRight = _barConfig.ComponentsRight;+ var barConfig = _userConfigService.UserConfig.Bar;+ Background = barConfig.Background;+ FontFamily = barConfig.FontFamily;+ FontSize = barConfig.FontSize;+ BorderColor = barConfig.BorderColor;+ BorderWidth = ShorthandToXamlProperty(barConfig.BorderWidth);+ Padding = ShorthandToXamlProperty(barConfig.Padding);+ Opacity = barConfig.Opacity;+ ComponentsLeft = barConfig.ComponentsLeft;+ ComponentsCenter = barConfig.ComponentsCenter;+ ComponentsRight = barConfig.ComponentsRight;++ var workspaceAttachedEvent = _bus.Events.Where(@event => @event is WorkspaceAttachedEvent);+ var workspaceDetachedEvent = _bus.Events.Where(@event => @event is WorkspaceDetachedEvent);+ var focusChangedEvent = _bus.Events.Where(@event => @event is FocusChangedEvent);++ // Refresh contents of items source.+ Observable.Merge(workspaceAttachedEvent, workspaceDetachedEvent, focusChangedEvent)+ .Subscribe(_observer => UpdateWorkspaces());UpdateWorkspaces();}@@ -72,11 +85,11 @@ namespace GlazeWM.Barpublic void UpdateWorkspaces(){- _dispatcher.Invoke(() =>+ Dispatcher.Invoke(() =>{Workspaces.Clear();- foreach (var workspace in _monitor.Children)+ foreach (var workspace in Monitor.Children)Workspaces.Add(workspace as Workspace);});}diff --git a/GlazeWM.Bar/MainWindow.xaml.cs b/GlazeWM.Bar/MainWindow.xaml.csindex 89dc47f9..3ec432d2 100644--- a/GlazeWM.Bar/MainWindow.xaml.cs+++ b/GlazeWM.Bar/MainWindow.xaml.cs@@ -1,16 +1,8 @@-using GlazeWM.Domain.Monitors;-using GlazeWM.Domain.Workspaces;-using GlazeWM.Domain.Workspaces.Commands;-using GlazeWM.Domain.Workspaces.Events;-using GlazeWM.Infrastructure.Bussing;-using System.Collections.ObjectModel;-using System.Reactive.Linq;-using System;+using System;using System.Windows;-using System.Windows.Controls;-using GlazeWM.Domain.Containers.Events;-using GlazeWM.Domain.UserConfigs;using System.Windows.Interop;+using GlazeWM.Domain.Monitors;+using GlazeWM.Domain.UserConfigs;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Bar@@ -20,39 +12,29 @@ namespace GlazeWM.Bar/// </summary>public partial class MainWindow : Window{- private Monitor _monitor { get; }- private Bus _bus { get; }- private WorkspaceService _workspaceService { get; }private UserConfigService _userConfigService { get; }- private ObservableCollection<Workspace> _workspaces = new ObservableCollection<Workspace>();+ private BarViewModel _barViewModel { get; }+ private Monitor _monitor => _barViewModel.Monitor;- public MainWindow(Monitor monitor, WorkspaceService workspaceService, Bus bus, UserConfigService userConfigService)+ public MainWindow(UserConfigService userConfigService, BarViewModel barViewModel){- _monitor = monitor;- _bus = bus;- _workspaceService = workspaceService;_userConfigService = userConfigService;+ _barViewModel = barViewModel;InitializeComponent();- SourceInitialized += MainWindow_SourceInitialized;-- var barConfig = _userConfigService.UserConfig.Bar;- var viewModel = new BarViewModel(Dispatcher, monitor, barConfig);- viewModel.InitializeState();- DataContext = viewModel;-- var workspaceAttachedEvent = _bus.Events.Where(@event => @event is WorkspaceAttachedEvent);- var workspaceDetachedEvent = _bus.Events.Where(@event => @event is WorkspaceDetachedEvent);- var focusChangedEvent = _bus.Events.Where(@event => @event is FocusChangedEvent);+ }- // Refresh contents of items source.- Observable.Merge(workspaceAttachedEvent, workspaceDetachedEvent, focusChangedEvent)- .Subscribe(_observer => viewModel.UpdateWorkspaces());+ public void BindToMonitor(Monitor monitor)+ {+ throw new NotImplementedException();}- private void MainWindow_SourceInitialized(object sender, EventArgs e)+ protected override void OnSourceInitialized(EventArgs e){- PositionWindow();+ base.OnSourceInitialized(e);++ var windowHandle = new WindowInteropHelper(this).Handle;+ PositionWindow(windowHandle);}/// <summary>@@ -60,25 +42,16 @@ namespace GlazeWM.Bar/// 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()+ private void PositionWindow(IntPtr windowHandle){- 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);+ var barHeight = _userConfigService.UserConfig.Bar.Height;+ var scaledBarHeight = Convert.ToInt32(barHeight * _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;- var clickedWorkspace = button.DataContext as Workspace;-- _bus.Invoke(new FocusWorkspaceCommand(clickedWorkspace.Name));+ MoveWindow(windowHandle, _monitor.X + 1, _monitor.Y, _monitor.Width - 1, scaledBarHeight, false);+ MoveWindow(windowHandle, _monitor.X, _monitor.Y, _monitor.Width, scaledBarHeight, true);}}}