fix: memory leak caused by animation

This commit is contained in:
leo 2024-05-30 17:30:54 +08:00
parent 092bf15906
commit 73cfeca8a9
5 changed files with 109 additions and 24 deletions

View file

@ -52,17 +52,13 @@ namespace SourceGit.ViewModels
if (task != null) if (task != null)
{ {
var finished = await task; var finished = await task;
_popup.InProgress = false;
if (finished) if (finished)
{
Popup = null; Popup = null;
}
else
{
_popup.InProgress = false;
}
} }
else else
{ {
_popup.InProgress = false;
Popup = null; Popup = null;
} }
} }

View file

@ -317,23 +317,9 @@
</StackPanel> </StackPanel>
<!-- Running --> <!-- Running -->
<StackPanel Orientation="Vertical" Margin="8" IsVisible="{Binding InProgress}"> <v:PopupRunningStatus Margin="8"
<Rectangle Height="1" Margin="-8,0" HorizontalAlignment="Stretch" Fill="{DynamicResource Brush.Border1}" /> Description="{Binding ProgressDescription}"
<StackPanel Orientation="Horizontal" Margin="0,8"> IsVisible="{Binding InProgress}"/>
<Path Width="12" Height="12" Classes="waiting" Data="{StaticResource Icons.Waiting}" IsVisible="{Binding InProgress}"/>
<TextBlock Margin="6,0,0,0" FontSize="14" FontWeight="Bold" Text="{DynamicResource Text.Running}"/>
</StackPanel>
<TextBlock HorizontalAlignment="Stretch"
TextWrapping="Wrap"
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:FontSizeModifyConverters.Decrease}}"
FontStyle="Italic"
Text="{Binding ProgressDescription}"/>
<ProgressBar Margin="0,8,0,0"
HorizontalAlignment="Stretch"
IsIndeterminate="True"
Background="{DynamicResource Brush.FG2}" Foreground="{DynamicResource Brush.Accent}"
Minimum="0" Maximum="100"/>
</StackPanel>
</StackPanel> </StackPanel>
</Border> </Border>
</DataTemplate> </DataTemplate>

View file

@ -17,7 +17,9 @@ namespace SourceGit.Views
protected override void OnLoaded(RoutedEventArgs e) protected override void OnLoaded(RoutedEventArgs e)
{ {
base.OnLoaded(e); base.OnLoaded(e);
StartAnim();
if (IsVisible)
StartAnim();
} }
protected override void OnUnloaded(RoutedEventArgs e) protected override void OnUnloaded(RoutedEventArgs e)

View file

@ -0,0 +1,31 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.PopupRunningStatus"
x:Name="me">
<StackPanel Orientation="Vertical">
<Rectangle Height="1" Margin="-8,0" HorizontalAlignment="Stretch" Fill="{DynamicResource Brush.Border1}" />
<StackPanel Orientation="Horizontal" Margin="0,8">
<ContentPresenter x:Name="icon" Width="12" Height="12"/>
<TextBlock Margin="6,0,0,0" FontSize="14" FontWeight="Bold" Text="{DynamicResource Text.Running}"/>
</StackPanel>
<TextBlock x:Name="txtDesc"
HorizontalAlignment="Stretch"
TextWrapping="Wrap"
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:FontSizeModifyConverters.Decrease}}"
FontStyle="Italic"
Text="{Binding #me.Description}"/>
<ProgressBar x:Name="progressBar"
Margin="0,8,0,0"
HorizontalAlignment="Stretch"
Background="{DynamicResource Brush.FG2}" Foreground="{DynamicResource Brush.Accent}"
Minimum="0" Maximum="100"/>
</StackPanel>
</UserControl>

View file

@ -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<string> DescriptionProperty =
AvaloniaProperty.Register<PopupRunningStatus, string>(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;
}
}
}