Skip to content

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

fix: dispose bar component timers on reload (#264)

Zinvoke181ea38ab775
diff --git a/GlazeWM.Bar/BarService.cs b/GlazeWM.Bar/BarService.cs
index 75db0706..2350a55a 100644
--- a/GlazeWM.Bar/BarService.cs
+++ b/GlazeWM.Bar/BarService.cs
@@ -115,9 +115,15 @@ namespace GlazeWM.Bar
{
_application.Dispatcher.Invoke(() =>
{
- // Kill the corresponding bar window.
var barWindow = _activeWindowsByDeviceName.GetValueOrDefault(deviceName);
- barWindow?.Close();
+ var barViewModel = barWindow.BarViewModel;
+
+ // Unsubscribe all component observables.
+ barViewModel.WindowClosing.OnNext(true);
+
+ // Dispose view model and close the window.
+ barViewModel.Dispose();
+ barWindow.Close();
});
}
}
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index c0d561e1..11ba4983 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Reactive.Subjects;
using System.Windows.Threading;
using GlazeWM.Bar.Common;
using GlazeWM.Bar.Components;
@@ -11,6 +12,8 @@ namespace GlazeWM.Bar
{
public class BarViewModel : ViewModelBase
{
+ public readonly Subject<bool> WindowClosing = new();
+
public Monitor Monitor { get; }
public Dispatcher Dispatcher { get; }
public BarConfig BarConfig { get; }
@@ -74,6 +77,16 @@ namespace GlazeWM.Bar
return componentViewModels;
}
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ WindowClosing.Dispose();
+ }
+
+ base.Dispose(disposing);
+ }
+
private List<ComponentViewModel> CreateComponentViewModels(
List<BarComponentConfig> componentConfigs)
{
diff --git a/GlazeWM.Bar/Common/ViewModelBase.cs b/GlazeWM.Bar/Common/ViewModelBase.cs
index d0768d57..2f5a937a 100644
--- a/GlazeWM.Bar/Common/ViewModelBase.cs
+++ b/GlazeWM.Bar/Common/ViewModelBase.cs
@@ -1,16 +1,41 @@
+using System;
+using System.Collections.Generic;
using System.ComponentModel;
+using System.Runtime.CompilerServices;
namespace GlazeWM.Bar.Common
{
- public class ViewModelBase : INotifyPropertyChanged
+ public class ViewModelBase : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
+ public void Dispose()
{
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ Dispose(true);
+ GC.SuppressFinalize(this);
}
- public virtual void Dispose() { }
+ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke(this, new(propertyName));
+ }
+
+ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
+ {
+ if (EqualityComparer<T>.Default.Equals(field, value))
+ {
+ return false;
+ }
+
+ field = value;
+
+ OnPropertyChanged(propertyName);
+
+ return true;
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ }
}
}
diff --git a/GlazeWM.Bar/Components/BatteryComponentViewModel.cs b/GlazeWM.Bar/Components/BatteryComponentViewModel.cs
index 523c46a6..bf9aad92 100644
--- a/GlazeWM.Bar/Components/BatteryComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/BatteryComponentViewModel.cs
@@ -46,7 +46,9 @@ namespace GlazeWM.Bar.Components
{
_batteryComponentConfig = config;
- Observable.Interval(TimeSpan.FromSeconds(3))
+ Observable
+ .Interval(TimeSpan.FromSeconds(3))
+ .TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => OnPropertyChanged(nameof(FormattedPowerStatus)));
}
}
diff --git a/GlazeWM.Bar/Components/ClockComponentViewModel.cs b/GlazeWM.Bar/Components/ClockComponentViewModel.cs
index 05b2bcae..5b0c4f05 100644
--- a/GlazeWM.Bar/Components/ClockComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/ClockComponentViewModel.cs
@@ -67,7 +67,9 @@ namespace GlazeWM.Bar.Components
// Update the displayed time every second.
var updateInterval = TimeSpan.FromSeconds(1);
- Observable.Interval(updateInterval)
+ Observable
+ .Interval(updateInterval)
+ .TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => OnPropertyChanged(nameof(FormattedTime)));
}
}
diff --git a/GlazeWM.Bar/Components/NetworkComponentViewModel.cs b/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
index ef38b535..2ff3c66d 100644
--- a/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/NetworkComponentViewModel.cs
@@ -54,8 +54,11 @@ namespace GlazeWM.Bar.Components
BarViewModel parentViewModel,
NetworkComponentConfig config) : base(parentViewModel, config)
{
- Observable.Interval(TimeSpan.FromSeconds(10))
+ Observable
+ .Interval(TimeSpan.FromSeconds(10))
+ .TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => OnPropertyChanged(nameof(Text)));
+
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler((s, e) => OnPropertyChanged(nameof(Text)));
}
}
diff --git a/GlazeWM.Bar/MainWindow.xaml.cs b/GlazeWM.Bar/MainWindow.xaml.cs
index d625fccc..891260b5 100644
--- a/GlazeWM.Bar/MainWindow.xaml.cs
+++ b/GlazeWM.Bar/MainWindow.xaml.cs
@@ -19,13 +19,13 @@ namespace GlazeWM.Bar
public partial class MainWindow : Window
{
private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();
- private BarViewModel _barViewModel { get; }
- private Dispatcher _dispatcher => _barViewModel.Dispatcher;
- private Monitor _monitor => _barViewModel.Monitor;
+ public BarViewModel BarViewModel { get; }
+ private Dispatcher _dispatcher => BarViewModel.Dispatcher;
+ private Monitor _monitor => BarViewModel.Monitor;
public MainWindow(BarViewModel barViewModel)
{
- _barViewModel = barViewModel;
+ BarViewModel = barViewModel;
DataContext = barViewModel;
InitializeComponent();
@@ -62,16 +62,16 @@ namespace GlazeWM.Bar
private void PositionWindow(IntPtr windowHandle)
{
// Since window size is set manually, need to scale up height to make window DPI responsive.
- var barHeight = UnitsHelper.TrimUnits(_barViewModel.BarConfig.Height);
+ var barHeight = UnitsHelper.TrimUnits(BarViewModel.BarConfig.Height);
var scaledBarHeight = Convert.ToInt32(barHeight * _monitor.ScaleFactor);
// Get offset from top of monitor.
- var barOffsetY = _barViewModel.BarConfig.Position == BarPosition.Bottom
+ var barOffsetY = BarViewModel.BarConfig.Position == BarPosition.Bottom
? _monitor.Height - scaledBarHeight
: 0;
- var floatBarOffsetX = UnitsHelper.TrimUnits(_barViewModel.BarConfig.OffsetX);
- var floatBarOffsetY = UnitsHelper.TrimUnits(_barViewModel.BarConfig.OffsetY);
+ var floatBarOffsetX = UnitsHelper.TrimUnits(BarViewModel.BarConfig.OffsetX);
+ var floatBarOffsetY = UnitsHelper.TrimUnits(BarViewModel.BarConfig.OffsetY);
// The first move puts it on the correct monitor, which triggers WM_DPICHANGED.
MoveWindow(