From 73cfeca8a98267a9c4bc104f7acf3099769c0075 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 30 May 2024 17:30:54 +0800 Subject: [PATCH] fix: memory leak caused by animation --- src/ViewModels/PopupHost.cs | 8 +-- src/Views/Launcher.axaml | 20 ++------ src/Views/LoadingIcon.axaml.cs | 4 +- src/Views/PopupRunningStatus.axaml | 31 ++++++++++++ src/Views/PopupRunningStatus.axaml.cs | 70 +++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 24 deletions(-) create mode 100644 src/Views/PopupRunningStatus.axaml create mode 100644 src/Views/PopupRunningStatus.axaml.cs diff --git a/src/ViewModels/PopupHost.cs b/src/ViewModels/PopupHost.cs index b6351fcb..1a4c8bb2 100644 --- a/src/ViewModels/PopupHost.cs +++ b/src/ViewModels/PopupHost.cs @@ -52,17 +52,13 @@ namespace SourceGit.ViewModels if (task != null) { var finished = await task; + _popup.InProgress = false; if (finished) - { Popup = null; - } - else - { - _popup.InProgress = false; - } } else { + _popup.InProgress = false; Popup = null; } } diff --git a/src/Views/Launcher.axaml b/src/Views/Launcher.axaml index 05eca820..a9202d99 100644 --- a/src/Views/Launcher.axaml +++ b/src/Views/Launcher.axaml @@ -317,23 +317,9 @@ - - - - - - - - - + diff --git a/src/Views/LoadingIcon.axaml.cs b/src/Views/LoadingIcon.axaml.cs index c7c6fd14..5a7e0014 100644 --- a/src/Views/LoadingIcon.axaml.cs +++ b/src/Views/LoadingIcon.axaml.cs @@ -17,7 +17,9 @@ namespace SourceGit.Views protected override void OnLoaded(RoutedEventArgs e) { base.OnLoaded(e); - StartAnim(); + + if (IsVisible) + StartAnim(); } protected override void OnUnloaded(RoutedEventArgs e) diff --git a/src/Views/PopupRunningStatus.axaml b/src/Views/PopupRunningStatus.axaml new file mode 100644 index 00000000..c2759715 --- /dev/null +++ b/src/Views/PopupRunningStatus.axaml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + diff --git a/src/Views/PopupRunningStatus.axaml.cs b/src/Views/PopupRunningStatus.axaml.cs new file mode 100644 index 00000000..e9a77a04 --- /dev/null +++ b/src/Views/PopupRunningStatus.axaml.cs @@ -0,0 +1,70 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Shapes; +using Avalonia.Interactivity; +using Avalonia.Media; + +namespace SourceGit.Views +{ + public partial class PopupRunningStatus : UserControl + { + public static readonly StyledProperty DescriptionProperty = + AvaloniaProperty.Register(nameof(Description)); + + public string Description + { + get => GetValue(DescriptionProperty); + set => SetValue(DescriptionProperty, value); + } + + public PopupRunningStatus() + { + InitializeComponent(); + } + + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + + if (IsVisible) + StartAnim(); + } + + protected override void OnUnloaded(RoutedEventArgs e) + { + StopAnim(); + base.OnUnloaded(e); + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == IsVisibleProperty) + { + if (IsVisible) + StartAnim(); + else + StopAnim(); + } + } + + private void StartAnim() + { + icon.Content = new Path() + { + Data = this.FindResource("Icons.Loading") as StreamGeometry, + Classes = { "rotating" }, + }; + progressBar.IsIndeterminate = true; + } + + private void StopAnim() + { + if (icon.Content is Path path) + path.Classes.Clear(); + icon.Content = null; + progressBar.IsIndeterminate = false; + } + } +}