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

Porting Behaviors.Animations #49

Merged
merged 8 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="CommunityToolkit.WinUI.BehaviorsAnimations" />
<PackageReference Include="PolySharp" Version="1.13.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions components/Animations/src/Xaml/AnimationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public async void Start()
}

/// <inheritdoc cref="AnimationBuilder.Start(UIElement)"/>
public async void Start(UIElement element)
public async void Start(UIElement? element)
{
await StartAsync(element);
await StartAsync(element!);
}

/// <inheritdoc cref="AnimationBuilder.Start(UIElement)"/>
Expand Down Expand Up @@ -204,9 +204,9 @@ public void Stop()
/// Cancels the current animation for a target <see cref="UIElement"/> instance.
/// </summary>
/// <param name="element">The target <see cref="UIElement"/> instance to stop the animation for.</param>
public void Stop(UIElement element)
public void Stop(UIElement? element)
{
if (this.cancellationTokenMap.TryGetValue(element, out CancellationTokenSource? value))
if (this.cancellationTokenMap.TryGetValue(element!, out CancellationTokenSource? value))
{
value.Cancel();
}
Expand Down
3 changes: 3 additions & 0 deletions components/BehaviorsAnimations/OpenSolution.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

powershell ..\..\tooling\ProjectHeads\GenerateSingleSampleHeads.ps1 -componentPath %CD% %*
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="MSBuild.Sdk.Extras/3.0.23">
<PropertyGroup>
<ToolkitComponentName>BehaviorsAnimations</ToolkitComponentName>
</PropertyGroup>

<!-- Sets this up as a toolkit component's sample project -->
<Import Project="$(ToolingDirectory)\ToolkitComponent.SampleProject.props" />
<ItemGroup>
<None Remove="Assets\ToolkitIcon.png" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\ToolkitIcon.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
210 changes: 210 additions & 0 deletions components/BehaviorsAnimations/samples/BehaviorsAnimations.md

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions components/BehaviorsAnimations/samples/Dependencies.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
WinUI 2 under UWP uses TargetFramework uap10.0.*
WinUI 3 under WinAppSdk uses TargetFramework net6.0-windows10.*
However, under Uno-powered platforms, both WinUI 2 and 3 can share the same TargetFramework.

MSBuild doesn't play nicely with this out of the box, so we've made it easy for you.

For .NET Standard packages, you can use the Nuget Package Manager in Visual Studio.
For UWP / WinAppSDK / Uno packages, place the package references here.
-->
<Project>
<!-- WinUI 2 / UWP -->
<ItemGroup Condition="'$(IsUwp)' == 'true'">
<!-- <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.2"/> -->
</ItemGroup>

<!-- WinUI 2 / Uno -->
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '2'">
<!-- <PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.11"/> -->
</ItemGroup>

<!-- WinUI 3 / WinAppSdk -->
<ItemGroup Condition="'$(IsWinAppSdk)' == 'true'">
<!-- <PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.2"/> -->
</ItemGroup>

<!-- WinUI 3 / Uno -->
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '3'">
<!-- <PackageReference Include="Uno.CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.100-dev.15.g12261e2626"/> -->
</ItemGroup>
</Project>
64 changes: 64 additions & 0 deletions components/BehaviorsAnimations/samples/InvokeActionsSample.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="BehaviorsAnimationsExperiment.Samples.InvokeActionsActivitySample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ani="using:CommunityToolkit.WinUI.Animations"
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:BehaviorsAnimationsExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Button Width="200"
Height="200"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ani:Explicit.Animations>
<ani:AnimationSet x:Name="MoveAnimation"
IsSequential="True">
<ani:StartAnimationActivity Animation="{Binding ElementName=FadeOutAnimation}" />
<ani:InvokeActionsActivity>
<interactions:ChangePropertyAction PropertyName="Foreground"
TargetObject="{Binding ElementName=MyText}"
Value="Purple" />
<!--<mediaactions:PlaySoundAction Source="Assets/Llama.mp3"/>-->
</ani:InvokeActionsActivity>
<ani:StartAnimationActivity Animation="{Binding ElementName=FadeInAnimation}"
Delay="0:0:2" />
</ani:AnimationSet>
</ani:Explicit.Animations>

<TextBlock x:Name="MyText"
Text="🦙 Text">
<ani:Explicit.Animations>
<ani:AnimationSet x:Name="FadeOutAnimation">
<ani:OpacityAnimation Delay="0"
EasingMode="EaseOut"
EasingType="Linear"
From="1"
To="0"
Duration="0:0:1" />
</ani:AnimationSet>
<ani:AnimationSet x:Name="FadeInAnimation">
<ani:OpacityAnimation Delay="0"
EasingMode="EaseOut"
EasingType="Linear"
From="0"
To="1"
Duration="0:0:1" />
</ani:AnimationSet>
</ani:Explicit.Animations>
</TextBlock>

<interactivity:Interaction.Behaviors>
<interactions:EventTriggerBehavior EventName="Click">
<interactions:ChangePropertyAction PropertyName="Foreground"
TargetObject="{Binding ElementName=MyText}"
Value="White" />
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" />
</interactions:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
</Page>
16 changes: 16 additions & 0 deletions components/BehaviorsAnimations/samples/InvokeActionsSample.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI.Animations;

namespace BehaviorsAnimationsExperiment.Samples;

[ToolkitSample(id: nameof(InvokeActionsActivitySample), "InvokeActionsActivity", description: $"A sample for showing how to create and use a {nameof(StartAnimationActivity)} behavior.")]
public sealed partial class InvokeActionsActivitySample : Page
{
public InvokeActionsActivitySample()
{
this.InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="BehaviorsAnimationsExperiment.Samples.StartAnimationActivitySample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ani="using:CommunityToolkit.WinUI.Animations"
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:BehaviorsAnimationsExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Button Width="200"
Height="200"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ani:Explicit.Animations>
<ani:AnimationSet x:Name="MoveAnimation"
IsSequential="True">
<ani:TranslationAnimation From="0,0,0"
To="0,32,0"
Duration="0:0:3" />
<ani:StartAnimationActivity Animation="{Binding ElementName=FadeOutAnimation}"
Delay="0:0:3" />
<ani:StartAnimationActivity Animation="{Binding ElementName=FadeInAnimation}"
Delay="0:0:3" />
<ani:TranslationAnimation From="0,32,0"
To="0,0,0"
Duration="0:0:1" />
</ani:AnimationSet>
</ani:Explicit.Animations>

<Image Width="100"
Height="100"
Source="ms-appx:///Assets/ToolkitIcon.png">
<ani:Explicit.Animations>
<ani:AnimationSet x:Name="FadeOutAnimation">
<ani:OpacityAnimation Delay="0"
EasingMode="EaseOut"
EasingType="Linear"
From="1"
To="0"
Duration="0:0:1" />
</ani:AnimationSet>
<ani:AnimationSet x:Name="FadeInAnimation">
<ani:OpacityAnimation Delay="0"
EasingMode="EaseOut"
EasingType="Linear"
From="0"
To="1"
Duration="0:0:1" />
</ani:AnimationSet>
</ani:Explicit.Animations>
</Image>

<interactivity:Interaction.Behaviors>
<interactions:EventTriggerBehavior EventName="Click">
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" />
</interactions:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI.Animations;

namespace BehaviorsAnimationsExperiment.Samples;

[ToolkitSample(id: nameof(StartAnimationActivitySample), "StartAnimationActivity", description: $"A sample for showing how to create and use a {nameof(InvokeActionsActivity)} behavior.")]
public sealed partial class StartAnimationActivitySample : Page
{
public StartAnimationActivitySample()
{
this.InitializeComponent();
}
}
13 changes: 13 additions & 0 deletions components/BehaviorsAnimations/src/AdditionalAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;

// These `InternalsVisibleTo` calls are intended to make it easier for
// for any internal code to be testable in all the different test projects
// used with the Labs infrastructure.
[assembly: InternalsVisibleTo("BehaviorsAnimations.Tests.Uwp")]
[assembly: InternalsVisibleTo("BehaviorsAnimations.Tests.WinAppSdk")]
[assembly: InternalsVisibleTo("CommunityToolkit.Tests.Uwp")]
[assembly: InternalsVisibleTo("CommunityToolkit.Tests.WinAppSdk")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI.Animations;
using Microsoft.Xaml.Interactivity;

namespace CommunityToolkit.WinUI.Behaviors;

/// <summary>
/// A custom <see cref="Trigger"/> that fires whenever a linked <see cref="AnimationSet"/> completes.
/// </summary>
public sealed class AnimationCompletedTriggerBehavior : Trigger<AnimationSet>
{
/// <summary>
/// The current <see cref="AnimationSet"/> instance in use.
/// </summary>
private AnimationSet? animationCollection;

/// <inheritdoc/>
protected override void OnAttached()
{
base.OnAttached();

SetResolvedCollection(AssociatedObject);
}

/// <inheritdoc/>
protected override void OnDetaching()
{
base.OnDetaching();

SetResolvedCollection(null);
}

/// <summary>
/// Sets the current <see cref="AnimationSet"/> instance in use.
/// </summary>
/// <param name="animationCollection">The <see cref="AnimationSet"/> instance in use.</param>
private void SetResolvedCollection(AnimationSet? animationCollection)
{
if (this.animationCollection == animationCollection)
{
return;
}

if (this.animationCollection is not null)
{
this.animationCollection.Completed -= AnimationCollection_Completed;
}

this.animationCollection = animationCollection;

if (animationCollection is not null)
{
animationCollection.Completed += AnimationCollection_Completed;
}
}

/// <summary>
/// Invokes the current actions when the linked animations completes.
/// </summary>
/// <param name="sender">The source <see cref="AnimationSet"/> instance.</param>
/// <param name="e">The arguments for the event (unused).</param>
private void AnimationCollection_Completed(object? sender, System.EventArgs e)
{
Interaction.ExecuteActions(sender, Actions, e);
}
}
Loading