Browse Source

Unsubscribe from stuff in DevTools MainViewModel.

Steven Kirk 5 years ago
parent
commit
89c361f6a4
1 changed files with 14 additions and 9 deletions
  1. 14 9
      src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs

+ 14 - 9
src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs

@@ -1,4 +1,5 @@
 using System;
+using System.ComponentModel;
 using Avalonia.Controls;
 using Avalonia.Diagnostics.Models;
 using Avalonia.Input;
@@ -12,6 +13,7 @@ namespace Avalonia.Diagnostics.ViewModels
         private readonly TreePageViewModel _logicalTree;
         private readonly TreePageViewModel _visualTree;
         private readonly EventsPageViewModel _events;
+        private readonly IDisposable _pointerOverSubscription;
         private ViewModelBase _content;
         private int _selectedTab;
         private string _focusedControl;
@@ -25,16 +27,9 @@ namespace Avalonia.Diagnostics.ViewModels
             _events = new EventsPageViewModel(root);
 
             UpdateFocusedControl();
-            KeyboardDevice.Instance.PropertyChanged += (s, e) =>
-            {
-                if (e.PropertyName == nameof(KeyboardDevice.Instance.FocusedElement))
-                {
-                    UpdateFocusedControl();
-                }
-            };
-
+            KeyboardDevice.Instance.PropertyChanged += KeyboardPropertyChanged;
             SelectedTab = 0;
-            root.GetObservable(TopLevel.PointerOverElementProperty)
+            _pointerOverSubscription = root.GetObservable(TopLevel.PointerOverElementProperty)
                 .Subscribe(x => PointerOverElement = x?.GetType().Name);
             Console = new ConsoleViewModel(UpdateConsoleContext);
         }
@@ -129,6 +124,8 @@ namespace Avalonia.Diagnostics.ViewModels
 
         public void Dispose()
         {
+            KeyboardDevice.Instance.PropertyChanged -= KeyboardPropertyChanged;
+            _pointerOverSubscription.Dispose();
             _logicalTree.Dispose();
             _visualTree.Dispose();
         }
@@ -137,5 +134,13 @@ namespace Avalonia.Diagnostics.ViewModels
         {
             FocusedControl = KeyboardDevice.Instance.FocusedElement?.GetType().Name;
         }
+
+        private void KeyboardPropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            if (e.PropertyName == nameof(KeyboardDevice.Instance.FocusedElement))
+            {
+                UpdateFocusedControl();
+            }
+        }
     }
 }