Giter Site home page Giter Site logo

avalonia.listboxanimation.samples's Introduction

Avalonia.ListBoxAnimation.Samples

Simple item selection animation using Avalonia 11

2022-08-31.22-15-47.mp4

To make this kind of animation work you need 3 things -

Indicator visual

The indicator visual needs to be in the control's template, for example for ListBoxItem

 <ControlTemplate>
                <Panel Margin="{TemplateBinding Margin}">
                    <Border
                        Width="3.5"
                        ZIndex="1"
                        Height="{DynamicResource ListBoxItemPipeHeight}"
                        Name="PART_SelectedPipe"
                        CornerRadius="15"
                        ClipToBounds="False"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Background="{DynamicResource SystemControlHighlightListAccentLowBrush}" />
                    <ContentPresenter Name="PART_ContentPresenter"
                                      ZIndex="0"
                                      BorderBrush="{TemplateBinding BorderBrush}"
                                      CornerRadius="{TemplateBinding CornerRadius}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      Content="{TemplateBinding Content}"
                                      FontWeight="Normal"
                                      FontSize="{DynamicResource ControlContentThemeFontSize}"
                                      Padding="{TemplateBinding Padding}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" />
                </Panel>
</ControlTemplate>

The "PART_SelectedPipe" Border is the indicator. Important - you must set ClipToBounds=False on both the control and the indicator, otherwise the animation won't be as cool ;)

Attached property

Create simple AttachedProperty for SelectingItemsControl -

    public static readonly AttachedProperty<bool> EnableSelectionAnimationProperty =
        AvaloniaProperty.RegisterAttached<SelectingItemsControl, bool>("EnableSelectionAnimation",
            typeof(SelectingItemControlExtension));

    static SelectingItemControlExtension()
    {
        EnableSelectionAnimationProperty.Changed.AddClassHandler<Control>(OnEnableSelectionAnimation);
    }

Using this we can know when an item selection change occured and get the controls of the previous and current selection.

Animation

Using the new Composition APIs, given the two controls we can calculate the offset between them -

// Get the composition visuals for all controls
CompositionVisual? pipeVisual = ElementComposition.GetElementVisual(borderPipe);
CompositionVisual? newSelectionVisual = ElementComposition.GetElementVisual(newSelection);
CompositionVisual? oldSelectionVisual = ElementComposition.GetElementVisual(oldSelection);
if (pipeVisual == null || newSelectionVisual == null || oldSelectionVisual == null) return;

// Calculate the offset between old and new selections
Vector3 selectionOffset = oldSelectionVisual.Offset - newSelectionVisual.Offset;
bool isVerticalOffset = selectionOffset.Y != 0;
float offset = isVerticalOffset ? selectionOffset.Y : selectionOffset.X;

Using that information we create a simple Offset animation between the old position and the new position!

// Create new offset animation between old selection position to the current position
Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = "Offset";
offsetAnimation.InsertKeyFrame(0f,
    isVerticalOffset ? pipeVisual.Offset with {Y = offset} : pipeVisual.Offset with {X = offset},
    quadraticEaseIn);
offsetAnimation.InsertKeyFrame(1f, pipeVisual.Offset, quadraticEaseIn);
offsetAnimation.Duration = TimeSpan.FromMilliseconds(250);

avalonia.listboxanimation.samples's People

Contributors

adirh3 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

dbriard

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.