Skip to content

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

refactor(bar): move workspaces-related state out of `BarViewModel`

Lars Berger65a5cb1344b0
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index 3b6bc585..0d771f79 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
-using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Threading;
using GlazeWM.Bar.Common;
using GlazeWM.Bar.Components;
-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
@@ -21,11 +16,8 @@ namespace GlazeWM.Bar
{
public Dispatcher Dispatcher { get; set; }
public Monitor Monitor { get; set; }
- private Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();
private UserConfigService _userConfigService = ServiceLocator.Provider.GetRequiredService<UserConfigService>();
private BarConfig _barConfig => _userConfigService.UserConfig.Bar;
- public ObservableCollection<Workspace> Workspaces =>
- new ObservableCollection<Workspace>(Monitor.Children.Cast<Workspace>());
public string Background => _barConfig.Background;
public string FontFamily => _barConfig.FontFamily;
public string FontSize => _barConfig.FontSize;
@@ -45,17 +37,6 @@ namespace GlazeWM.Bar
public BarViewModel()
{
- var workspacesChangedEvent = _bus.Events.Where((@event) =>
- @event is WorkspaceAttachedEvent ||
- @event is WorkspaceDetachedEvent ||
- @event is FocusChangedEvent
- );
-
- // Refresh contents of workspaces collection.
- workspacesChangedEvent.Subscribe((_observer) =>
- {
- Dispatcher.Invoke(() => OnPropertyChanged(nameof(Workspaces)));
- });
}
private List<ComponentViewModel> CreateComponentViewModels(List<BarComponentConfig> componentConfigs)
diff --git a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
index 2e0ad887..111a6e74 100644
--- a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs
@@ -1,22 +1,39 @@
+using System;
using System.Collections.ObjectModel;
+using System.Linq;
+using System.Reactive.Linq;
using System.Windows.Threading;
+using GlazeWM.Domain.Containers.Events;
using GlazeWM.Domain.Monitors;
using GlazeWM.Domain.Workspaces;
+using GlazeWM.Domain.Workspaces.Events;
using GlazeWM.Infrastructure;
+using GlazeWM.Infrastructure.Bussing;
using Microsoft.Extensions.DependencyInjection;
namespace GlazeWM.Bar.Components
{
public class WorkspacesComponentViewModel : ComponentViewModel
{
- private WorkspaceService _workspaceService = ServiceLocator.Provider.GetRequiredService<WorkspaceService>();
- private readonly BarViewModel _barViewModel;
- public ObservableCollection<Workspace> Workspaces => _barViewModel.Workspaces;
- private Dispatcher _dispatcher => _barViewModel.Dispatcher;
- private Monitor _monitor => _barViewModel.Monitor;
+ private Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();
+ private Dispatcher _dispatcher => _parentViewModel.Dispatcher;
+ private Monitor _monitor => _parentViewModel.Monitor;
+ public ObservableCollection<Workspace> Workspaces =>
+ new ObservableCollection<Workspace>(_monitor.Children.Cast<Workspace>());
public WorkspacesComponentViewModel(BarViewModel parentViewModel) : base(parentViewModel)
{
+ var workspacesChangedEvent = _bus.Events.Where((@event) =>
+ @event is WorkspaceAttachedEvent ||
+ @event is WorkspaceDetachedEvent ||
+ @event is FocusChangedEvent
+ );
+
+ // Refresh contents of workspaces collection.
+ workspacesChangedEvent.Subscribe((_observer) =>
+ {
+ _dispatcher.Invoke(() => OnPropertyChanged(nameof(Workspaces)));
+ });
}
}
}