enhance: reduce CPU usage

This commit is contained in:
leo 2024-05-30 16:28:21 +08:00
parent b6e087259b
commit 092bf15906
2 changed files with 34 additions and 3 deletions

View file

@ -5,5 +5,4 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:m="using:SourceGit.Models"
x:Class="SourceGit.Views.LoadingIcon">
<Path x:Name="target" Data="{DynamicResource Icons.Loading}"/>
</UserControl>

View file

@ -1,6 +1,8 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Interactivity;
using Avalonia.Media;
namespace SourceGit.Views
{
@ -15,13 +17,43 @@ namespace SourceGit.Views
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
target.Classes.Add("rotating");
StartAnim();
}
protected override void OnUnloaded(RoutedEventArgs e)
{
StopAnim();
base.OnUnloaded(e);
target.Classes.Clear();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsVisibleProperty)
{
if (IsVisible)
StartAnim();
else
StopAnim();
}
}
private void StartAnim()
{
Content = new Path()
{
Data = this.FindResource("Icons.Loading") as StreamGeometry,
Classes = { "rotating" },
};
}
private void StopAnim()
{
if (Content is Path path)
path.Classes.Clear();
Content = null;
}
}
}