AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.42k stars 2.2k forks source link

Animation is rendered even when element is not visible #17139

Open SiMet opened 1 week ago

SiMet commented 1 week ago

Describe the bug

Frames are rendered when animation is running and element is not visible.

I'm observe using DevTools and Overlay->FPS that frames are rendered even element is not visible. When checking this sample with SVG Frames are rendered after first display of svg. If we use for example border it's rendered from the beining of app start.

image

To Reproduce

MainView.axaml

<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="clr-namespace:AvaloniaApplication2.ViewModels"
             mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="250"
             x:Class="AvaloniaApplication2.Views.MainView"
             x:DataType="vm:MainViewModel">
    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainViewModel />
    </Design.DataContext>

    <UserControl.Styles>
        <Style Selector="Svg.Rotate">
            <Style.Animations>
                <Animation IterationCount="INFINITE" Duration="0:0:0.5">
                    <KeyFrame Cue="100%">
                        <Setter Property="RotateTransform.Angle" Value="-180"/>
                    </KeyFrame>
                </Animation>
            </Style.Animations>
        </Style>
    </UserControl.Styles>

    <StackPanel>
        <Button Command="{Binding ButtonClick}" Content="ButtonText"/>
        <StackPanel IsVisible="{Binding Animate}">
            <Svg Path="/Assets/avaloniauinet-icon.svg" Classes="Rotate"/>
        </StackPanel>
    </StackPanel>
</UserControl>

MainViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AvaloniaApplication2.ViewModels;

public partial class MainViewModel : ViewModelBase
{
    [ObservableProperty]
    private bool _animate = false;

    [RelayCommand]
    public void ButtonClick()
    {
        Animate = !Animate;
    }
}

Expected behavior

Frames are not rendered when nothing changed

Avalonia version

11.3.1

OS

Windows 10

Additional context

No response

rabbitism commented 1 week ago

Best workaround is to add [IsVisible=True] in every animation related style selector.

workgroupengineering commented 1 week ago

Even though the scenario is different, look at the comment https://github.com/AvaloniaUI/Avalonia/pull/5919#issuecomment-841646221 to understand why.

MaxwellDAssistek commented 1 week ago

Looks like this is one of the causes of https://github.com/AvaloniaUI/Avalonia/issues/17129