Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting custom Animator in property to Setter #6077

Closed

Conversation

wieslawsoltes
Copy link
Collaborator

@wieslawsoltes wieslawsoltes commented Jun 15, 2021

What does the pull request do?

Allows setting custom Animator in style setter. This allows granular control of animator per property setter in styles useful for implementing custom geometry animators etc.

Example usage:

<Style Selector="Path.animation">
    <Style.Animations>
      <Animation Duration="0:0:1" IterationCount="Infinite">
        <KeyFrame Cue="0%">
          <Setter Property="Data" Value="M150 0 L75 200 L225 200 Z" Animator="{x:Type CustomGeometryAnimator}"/>
        </KeyFrame>
        <KeyFrame Cue="100%">
          <Setter Property="Data" Value="M 100 100 L 300 100 L 200 300 z" Animator="{x:Type CustomGeometryAnimator}"/>
        </KeyFrame>
      </Animation>
    </Style.Animations>
</Style>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
  <TextBlock.Styles>
    <Style Selector="TextBlock">
      <Style.Animations>
        <Animation Duration="0:0:1" IterationCount="Infinite">
          <KeyFrame Cue="0%">
            <Setter Property="Text" Value="" Animator="{x:Type pages:CustomStringAnimator}"/>
          </KeyFrame>
          <KeyFrame Cue="100%">
            <Setter Property="Text" Value="0123456789" Animator="{x:Type pages:CustomStringAnimator}"/>
          </KeyFrame>
        </Animation>
      </Style.Animations>
    </Style>
  </TextBlock.Styles>
</TextBlock>

What is the current behavior?

You can only register global animators.

What is the updated/expected behavior with this PR?

You can set custom Animator in style setter.

How was the solution implemented (if it's not obvious)?

Added Animator property to Setter class and IAnimationSetter interface.

Checklist

Breaking changes

  • Animator property has been added to IAnimationSetter interface.
  • Animator property has been added to Setter class as required by IAnimationSetter interface contract.

Obsoletions / Deprecations

Fixed issues

@wieslawsoltes wieslawsoltes marked this pull request as ready for review June 15, 2021 20:11
@wieslawsoltes wieslawsoltes requested a review from a team June 15, 2021 20:11
jkoritzinsky
jkoritzinsky previously approved these changes Jun 15, 2021
@maxkatz6
Copy link
Member

maxkatz6 commented Jun 15, 2021

Wouldn't this API be confusing outside of animations?
Especially in your example there is no any animations:

<Style Selector="Path">
  <Setter Property="Data" Value="" Animator="{x:Type CustomGeometryAnimator}" />
</Style>

Maybe it's possible to use attached property "Animation.Animator" and explicit "IAnimationSetter.Type Animator" implementation to read value from that attached property?

@wieslawsoltes
Copy link
Collaborator Author

Wouldn't this API be confusing outside of animations?
Especially in your example there is no any animations:

<Style Selector="Path">
  <Setter Property="Data" Value="" Animator="{x:Type CustomGeometryAnimator}" />
</Style>

Maybe it's possible to use attached property "Animation.Animator" and explicit "IAnimationSetter.Type Animator" implementation to read value from that attached property?

That is something I have considered and it would be better api solution, but I was just bit worried about performance of attached properties.

@wieslawsoltes
Copy link
Collaborator Author

wieslawsoltes commented Jun 16, 2021

Wouldn't this API be confusing outside of animations?
Especially in your example there is no any animations:

<Style Selector="Path">
  <Setter Property="Data" Value="" Animator="{x:Type CustomGeometryAnimator}" />
</Style>

Maybe it's possible to use attached property "Animation.Animator" and explicit "IAnimationSetter.Type Animator" implementation to read value from that attached property?

@maxkatz6 The issue is that Setter is not IAvaloniaObject so we can not set attached property Animation.Animator on setter itself.

@grokys
Copy link
Member

grokys commented Jun 16, 2021

Can attached properties work without it being an AvaloniaObject i wonder? IIRC in XAML all you need is a GetX/SetX method for something to be considered an attached property, not sure if the XAML compiler works like that.

@wieslawsoltes
Copy link
Collaborator Author

Can attached properties work without it being an AvaloniaObject i wonder? IIRC in XAML all you need is a GetX/SetX method for something to be considered an attached property, not sure if the XAML compiler works like that.

Seems to be working

https://github.com/wieslawsoltes/Avalonia/tree/feature/SetterAnimatorAttachedPropertyHack

https://github.com/wieslawsoltes/Avalonia/blob/84dd710fc184c322a59f4f8a650d5573f2ba18de/src/Avalonia.Animation/Animation.cs#L197-L211

https://github.com/wieslawsoltes/Avalonia/blob/84dd710fc184c322a59f4f8a650d5573f2ba18de/samples/RenderDemo/Pages/CustomAnimatorPage.xaml#L14

@wieslawsoltes
Copy link
Collaborator Author

Open draft PR with attached property solution #6082

@wieslawsoltes
Copy link
Collaborator Author

Better solution without breaking changes in #6082

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants