Skip to content

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

feat(bar): create view model for bar state

Lars Berger1c6e047901c9
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
new file mode 100644
index 00000000..df024159
--- /dev/null
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -0,0 +1,54 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Windows.Threading;
+using GlazeWM.Domain.Monitors;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Domain.Workspaces;
+
+namespace GlazeWM.Bar
+{
+ public class BarViewModel : INotifyPropertyChanged
+ {
+ public event PropertyChangedEventHandler PropertyChanged;
+ private readonly Dispatcher _dispatcher;
+ private readonly Monitor _monitor;
+ private readonly BarConfig _barConfig;
+
+ public BarViewModel(Dispatcher dispatcher, Monitor monitor, BarConfig barConfig)
+ {
+ _dispatcher = dispatcher;
+ _monitor = monitor;
+ _barConfig = barConfig;
+ }
+
+ private ObservableCollection<Workspace> _workspaces = new ObservableCollection<Workspace>();
+ public ObservableCollection<Workspace> Workspaces
+ {
+ get { return _workspaces; }
+ set
+ {
+ _workspaces = value;
+ OnPropertyChanged("Workspaces");
+ }
+ }
+
+ public void SetWorkspaces()
+ {
+ _dispatcher.Invoke(() =>
+ {
+ Workspaces.Clear();
+
+ foreach (var workspace in _monitor.Children)
+ Workspaces.Add(workspace as Workspace);
+
+ OnPropertyChanged("Workspaces");
+ });
+ }
+
+ private void OnPropertyChanged(string propertyName)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+}
diff --git a/GlazeWM.Bar/MainWindow.xaml b/GlazeWM.Bar/MainWindow.xaml
index da6dba4d..ef49117d 100644
--- a/GlazeWM.Bar/MainWindow.xaml
+++ b/GlazeWM.Bar/MainWindow.xaml
@@ -15,7 +15,7 @@
Height="50"
Topmost="True">
<DockPanel Margin="10">
- <ItemsControl x:Name="WorkspaceItems">
+ <ItemsControl ItemsSource="{Binding Workspaces}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid
diff --git a/GlazeWM.Bar/MainWindow.xaml.cs b/GlazeWM.Bar/MainWindow.xaml.cs
index a2ad8a50..96b8f06e 100644
--- a/GlazeWM.Bar/MainWindow.xaml.cs
+++ b/GlazeWM.Bar/MainWindow.xaml.cs
@@ -36,8 +36,10 @@ namespace GlazeWM.Bar
InitializeComponent();
SourceInitialized += MainWindow_SourceInitialized;
- RefreshState(monitor);
- WorkspaceItems.ItemsSource = _workspaces;
+ var barConfig = _userConfigService.UserConfig.Bar;
+ var viewModel = new BarViewModel(Dispatcher, monitor, barConfig);
+ viewModel.SetWorkspaces();
+ DataContext = viewModel;
var workspaceAttachedEvent = _bus.Events.Where(@event => @event is WorkspaceAttachedEvent);
var workspaceDetachedEvent = _bus.Events.Where(@event => @event is WorkspaceDetachedEvent);
@@ -45,15 +47,7 @@ namespace GlazeWM.Bar
// Refresh contents of items source.
Observable.Merge(workspaceAttachedEvent, workspaceDetachedEvent, focusChangedEvent)
- .Subscribe(_observer => Dispatcher.Invoke(() => RefreshState(monitor)));
- }
-
- private void RefreshState(Monitor monitor)
- {
- _workspaces.Clear();
-
- foreach (var workspace in monitor.Children)
- _workspaces.Add(workspace as Workspace);
+ .Subscribe(_observer => viewModel.SetWorkspaces());
}
private void MainWindow_SourceInitialized(object sender, EventArgs e)