Skip to content

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

feat(bar): revert to using `ServiceLocator` for dependencies of window and view models

Lars Bergerb8de662e4a18
diff --git a/GlazeWM.Bar/BarManagerService.cs b/GlazeWM.Bar/BarManagerService.cs
index e7ff226f..1b47fb59 100644
--- a/GlazeWM.Bar/BarManagerService.cs
+++ b/GlazeWM.Bar/BarManagerService.cs
@@ -35,17 +35,15 @@ namespace GlazeWM.Bar
{
application.Dispatcher.Invoke(() =>
{
- using (var scope = ServiceLocator.Provider.CreateScope())
+ var barViewModel = new BarViewModel()
{
- var barViewModel = scope.ServiceProvider.GetRequiredService<BarViewModel>();
- barViewModel.Monitor = (@event as MonitorAddedEvent).AddedMonitor;
- barViewModel.Dispatcher = application.Dispatcher;
- barViewModel.InitializeState();
+ Monitor = (@event as MonitorAddedEvent).AddedMonitor,
+ Dispatcher = application.Dispatcher,
+ };
+ barViewModel.InitializeState();
- var barWindow = scope.ServiceProvider.GetRequiredService<MainWindow>();
- barWindow.DataContext = barViewModel;
- barWindow.Show();
- }
+ var barWindow = new MainWindow(barViewModel);
+ barWindow.Show();
});
});
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index 4a81ba62..06f8a6d1 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -4,12 +4,15 @@ using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Threading;
+using GlazeWM.Bar.Common;
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;
using GlazeWM.Infrastructure.Bussing;
+using Microsoft.Extensions.DependencyInjection;
namespace GlazeWM.Bar
{
@@ -28,13 +31,11 @@ namespace GlazeWM.Bar
public List<BarComponentConfig> ComponentsRight { get; set; }
public Dispatcher Dispatcher { get; set; }
public Monitor Monitor { get; set; }
- private readonly Bus _bus;
- private readonly UserConfigService _userConfigService;
+ private Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();
+ private UserConfigService _userConfigService = ServiceLocator.Provider.GetRequiredService<UserConfigService>();
- public BarViewModel(Bus bus, UserConfigService userConfigService)
+ public BarViewModel()
{
- _bus = bus;
- _userConfigService = userConfigService;
}
public void InitializeState()
diff --git a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
index 5489b37b..d5f4bfb6 100644
--- a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
@@ -1,40 +1,24 @@
using System.Collections.ObjectModel;
-using System.ComponentModel;
using System.Windows.Threading;
using GlazeWM.Bar.Common;
using GlazeWM.Domain.Monitors;
using GlazeWM.Domain.Workspaces;
+using GlazeWM.Infrastructure;
+using Microsoft.Extensions.DependencyInjection;
namespace GlazeWM.Bar.Components
{
public class WorkspacesComponentViewModel : ViewModelBase
{
- public ObservableCollection<Workspace> Workspaces { get; set; } = new ObservableCollection<Workspace>();
+ private WorkspaceService _workspaceService = ServiceLocator.Provider.GetRequiredService<WorkspaceService>();
private readonly BarViewModel _barViewModel;
- private readonly WorkspaceService _workspaceService;
+ public ObservableCollection<Workspace> Workspaces => _barViewModel.Workspaces;
private Dispatcher _dispatcher => _barViewModel.Dispatcher;
private Monitor _monitor => _barViewModel.Monitor;
- public WorkspacesComponentViewModel(BarViewModel barViewModel, WorkspaceService workspaceService)
+ public WorkspacesComponentViewModel(BarViewModel barViewModel)
{
_barViewModel = barViewModel;
- _workspaceService = workspaceService;
- }
-
- public void InitializeState()
- {
- UpdateWorkspaces();
- }
-
- public void UpdateWorkspaces()
- {
- _dispatcher.Invoke(() =>
- {
- Workspaces.Clear();
-
- foreach (var workspace in _monitor.Children)
- Workspaces.Add(workspace as Workspace);
- });
}
}
}
diff --git a/GlazeWM.Bar/MainWindow.xaml.cs b/GlazeWM.Bar/MainWindow.xaml.cs
index 3ec432d2..706c38dc 100644
--- a/GlazeWM.Bar/MainWindow.xaml.cs
+++ b/GlazeWM.Bar/MainWindow.xaml.cs
@@ -3,6 +3,8 @@ using System.Windows;
using System.Windows.Interop;
using GlazeWM.Domain.Monitors;
using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Infrastructure;
+using Microsoft.Extensions.DependencyInjection;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
namespace GlazeWM.Bar
@@ -12,23 +14,18 @@ namespace GlazeWM.Bar
/// </summary>
public partial class MainWindow : Window
{
- private UserConfigService _userConfigService { get; }
+ private UserConfigService _userConfigService = ServiceLocator.Provider.GetRequiredService<UserConfigService>();
private BarViewModel _barViewModel { get; }
private Monitor _monitor => _barViewModel.Monitor;
- public MainWindow(UserConfigService userConfigService, BarViewModel barViewModel)
+ public MainWindow(BarViewModel barViewModel)
{
- _userConfigService = userConfigService;
_barViewModel = barViewModel;
+ DataContext = barViewModel;
InitializeComponent();
}
- public void BindToMonitor(Monitor monitor)
- {
- throw new NotImplementedException();
- }
-
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);