GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(bar): progress on multi-threaded update to workspaces by injecting dispatcher
Lars8293dbf7922d
diff --git a/LarsWM.Bar/BarViewModel.cs b/LarsWM.Bar/BarViewModel.csindex f879a27f..ab89b0cc 100644--- a/LarsWM.Bar/BarViewModel.cs+++ b/LarsWM.Bar/BarViewModel.cs@@ -1,6 +1,8 @@+using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;+using System.Windows.Threading;using LarsWM.Domain.Workspaces;namespace LarsWM.Bar@@ -8,42 +10,39 @@ namespace LarsWM.Barpublic class BarViewModel : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;+ private readonly Dispatcher _dispatcher;private ObservableCollection<Workspace> _workspaces = new ObservableCollection<Workspace>();public ObservableCollection<Workspace> Workspaces{- get { return _workspaces; }+ get { return this._workspaces; }set{- _workspaces = value;- OnPropertyChanged("Workspaces");+ this._workspaces = value;+ this.OnPropertyChanged("Workspaces");}}- public void AddWorkspace(Workspace workspace)+ public BarViewModel(Dispatcher dispatcher){- this.Workspaces.Add(workspace);- OnPropertyChanged("Workspaces");+ this._dispatcher = dispatcher;}- public void ClearWorkspaces()+ public void SetWorkspaces(IEnumerable<Workspace> workspaces){- this.Workspaces.Clear();- OnPropertyChanged("Workspaces");- }-- public void SetWorkspaces(List<Workspace> workspaces)- {- workspaces.Clear();+ this._dispatcher.BeginInvoke(new Action(() =>+ {+ var newWorkspaces = new ObservableCollection<Workspace>();- foreach (var workspace in workspaces)- workspaces.Add(workspace);+ foreach (var workspace in workspaces)+ newWorkspaces.Add(workspace);- OnPropertyChanged("Workspaces");+ this.Workspaces = newWorkspaces;+ }));}private void OnPropertyChanged(string propertyName){- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));+ this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}}diff --git a/LarsWM.Bar/CommandHandlers/LaunchBarOnMonitorHandler.cs b/LarsWM.Bar/CommandHandlers/LaunchBarOnMonitorHandler.csindex 801cac69..e3ef48dd 100644--- a/LarsWM.Bar/CommandHandlers/LaunchBarOnMonitorHandler.cs+++ b/LarsWM.Bar/CommandHandlers/LaunchBarOnMonitorHandler.cs@@ -29,7 +29,7 @@ namespace LarsWM.Bar.CommandHandlers{var bar = new MainWindow(command.Monitor, _workspaceService, _bus);bar.Show();- Application.Run();+ System.Windows.Threading.Dispatcher.Run();});thread.Name = "LarsWMBar";diff --git a/LarsWM.Bar/MainWindow.xaml b/LarsWM.Bar/MainWindow.xamlindex 44c1cef5..7797a65d 100644--- a/LarsWM.Bar/MainWindow.xaml+++ b/LarsWM.Bar/MainWindow.xaml@@ -29,7 +29,8 @@<ItemsControlGrid.Column="1"Grid.Row="1"- x:Name="WorkspaceItems">+ x:Name="WorkspaceItems"+ ItemsSource="{Binding Workspaces}"><ItemsControl.ItemsPanel><ItemsPanelTemplate><UniformGriddiff --git a/LarsWM.Bar/MainWindow.xaml.cs b/LarsWM.Bar/MainWindow.xaml.csindex 96a9c8fd..489d2570 100644--- a/LarsWM.Bar/MainWindow.xaml.cs+++ b/LarsWM.Bar/MainWindow.xaml.cs@@ -3,15 +3,11 @@ using LarsWM.Domain.Workspaces;using LarsWM.Domain.Workspaces.Commands;using LarsWM.Domain.Workspaces.Events;using LarsWM.Infrastructure.Bussing;-using System.Collections.ObjectModel;using System.Reactive.Linq;using System;using System.Windows;using System.Windows.Controls;-using System.Windows.Data;-using System.ComponentModel;-using System.Collections.Generic;-using System.Diagnostics;+using System.Linq;namespace LarsWM.Bar{@@ -22,15 +18,14 @@ namespace LarsWM.Bar{private Bus _bus { get; }private WorkspaceService _workspaceService { get; }- private static object _lock = new object();public MainWindow(Monitor monitor, WorkspaceService workspaceService, Bus bus){+ InitializeComponent();+_bus = bus;_workspaceService = workspaceService;- InitializeComponent();-// TODO: Bind padding, bg color, button bg color and font from user config.this.Top = monitor.Y;this.Left = monitor.X;@@ -38,24 +33,25 @@ namespace LarsWM.Bar// TODO: Change height to be set in XAML.this.Height = 50;- var workspaces = new ObservableCollection<Workspace>();- BindingOperations.EnableCollectionSynchronization(workspaces, _lock);-- foreach (var workspace in monitor.Children)- workspaces.Add(workspace as Workspace);+ this.DataContext = new BarViewModel(Dispatcher);- this.WorkspaceItems.ItemsSource = workspaces;+ // Initialise view model with the workspaces of the current monitor.+ this.UpdateViewModel(monitor);_bus.Events.Where(@event => @event is WorkspaceAttachedEvent).Subscribe(observer =>{- // Refresh contents of `workspaces` collection.- (this.WorkspaceItems.ItemsSource as ObservableCollection<Workspace>).Clear();-- foreach (var workspace in monitor.Children)- (this.WorkspaceItems.ItemsSource as ObservableCollection<Workspace>).Add(workspace as Workspace);+ // Refresh contents of view model.+ this.UpdateViewModel(monitor);});}+ private void UpdateViewModel(Monitor monitor)+ {++ var workspaces = monitor.Children.Select(workspace => workspace as Workspace);+ (this.DataContext as BarViewModel).SetWorkspaces(workspaces);+ }+private void OnWorkspaceButtonClick(object sender, RoutedEventArgs e){var button = sender as Button;