From 373f9a1f9cf6b35f3b8bba2fc3b93d02d3413452 Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Wed, 13 Aug 2025 10:17:58 -0400 Subject: [PATCH] Code Quality: Properly dispose events in layout pages --- .../Views/Layouts/DetailsLayoutPage.xaml.cs | 13 ++++++++++--- src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs | 14 +++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs b/src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs index 8ec82e44f150..aba8740b9d4c 100644 --- a/src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs +++ b/src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs @@ -37,6 +37,8 @@ public sealed partial class DetailsLayoutPage : BaseGroupableLayoutPage /// private uint currentIconSize; + private RectangleSelection? _rectangleSelection; + // Properties protected override ListViewBase ListViewBase => FileList; @@ -77,8 +79,8 @@ public DetailsLayoutPage() : base() { InitializeComponent(); DataContext = this; - var selectionRectangle = RectangleSelection.Create(FileList, SelectionRectangle, FileList_SelectionChanged); - selectionRectangle.SelectionEnded += SelectionRectangle_SelectionEnded; + _rectangleSelection = RectangleSelection.Create(FileList, SelectionRectangle, FileList_SelectionChanged); + _rectangleSelection.SelectionEnded += SelectionRectangle_SelectionEnded; UpdateSortOptionsCommand = new RelayCommand(x => { @@ -202,6 +204,11 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) FolderSettings.SortOptionPreferenceUpdated -= FolderSettings_SortOptionPreferenceUpdated; ParentShellPageInstance.ShellViewModel.PageTypeUpdated -= FilesystemViewModel_PageTypeUpdated; UserSettingsService.LayoutSettingsService.PropertyChanged -= LayoutSettingsService_PropertyChanged; + if (_rectangleSelection is not null) + { + _rectangleSelection.SelectionEnded -= SelectionRectangle_SelectionEnded; + _rectangleSelection = null; + } } private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e) @@ -1053,4 +1060,4 @@ private static GitProperties GetEnabledGitProperties(ColumnsViewModel columnsVie }; } } -} +} \ No newline at end of file diff --git a/src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs b/src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs index a62cddccbf4a..1c16bacedfc4 100644 --- a/src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs +++ b/src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs @@ -31,6 +31,8 @@ public sealed partial class GridLayoutPage : BaseGroupableLayoutPage private volatile bool shouldSetVerticalScrollMode; + private RectangleSelection? _rectangleSelection; + // Properties public ScrollViewer? ContentScroller { get; private set; } @@ -149,8 +151,8 @@ public GridLayoutPage() : base() InitializeComponent(); DataContext = this; - var selectionRectangle = RectangleSelection.Create(ListViewBase, SelectionRectangle, FileList_SelectionChanged); - selectionRectangle.SelectionEnded += SelectionRectangle_SelectionEnded; + _rectangleSelection = RectangleSelection.Create(ListViewBase, SelectionRectangle, FileList_SelectionChanged); + _rectangleSelection.SelectionEnded += SelectionRectangle_SelectionEnded; } // Methods @@ -222,6 +224,12 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) FolderSettings.LayoutModeChangeRequested -= FolderSettings_LayoutModeChangeRequested; UserSettingsService.LayoutSettingsService.PropertyChanged -= LayoutSettingsService_PropertyChanged; + + if (_rectangleSelection is not null) + { + _rectangleSelection.SelectionEnded -= SelectionRectangle_SelectionEnded; + _rectangleSelection = null; + } } private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e) @@ -827,4 +835,4 @@ private void UpdateCheckboxVisibility(object sender, bool isPointerOver) } } } -} +} \ No newline at end of file