Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitati
<NuGetAuditMode>all</NuGetAuditMode>

<!-- MAUI Specific -->
<MauiPackageVersion>10.0.10</MauiPackageVersion>
<MauiPackageVersion>10.0.20</MauiPackageVersion>
<MauiStrictXamlCompilation>true</MauiStrictXamlCompilation>
<SkipValidateMauiImplicitPackageReferences>true</SkipValidateMauiImplicitPackageReferences>
<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CommunityToolkit.Maui.Core;

static class BaseAnimationDefaults
{
public const uint Length = 250u;
public static Easing Easing { get; } = Easing.Linear;
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,45 @@ public static object CreateDefaultCustomDuration(global::Microsoft.Maui.Controls

await VerifySourceGeneratorAsync(source, expectedGenerated);
}

[Fact]
public async Task GenerateBindableProperty_GenericClassExample_GeneratesCorrectCode()
{
const string source =
/* language=C#-test */
//lang=csharp
$$"""
using CommunityToolkit.Maui;
using Microsoft.Maui.Controls;

namespace {{defaultTestNamespace}};

public partial class {{defaultTestClassName}}<T> : View
{
[BindablePropertyAttribute]
public partial string Text { get; set; }
}
""";

const string expectedGenerated =
/* language=C#-test */
//lang=csharp
$$"""
// <auto-generated>
// See: CommunityToolkit.Maui.SourceGenerators.Internal.BindablePropertyAttributeSourceGenerator
#pragma warning disable
#nullable enable
namespace {{defaultTestNamespace}};
public partial class {{defaultTestClassName}}<T>
{
/// <summary>
/// Backing BindableProperty for the <see cref = "Text"/> property.
/// </summary>
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}<T>), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
public partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
}
""";

await VerifySourceGeneratorAsync(source, expectedGenerated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static string GenerateSource(SemanticValues value)
? classNameWithGenerics
: string.Concat(value.ClassInformation.ContainingTypes, ".", classNameWithGenerics);

var fileStaticClassName = $"__{classNameWithGenerics}BindablePropertyInitHelpers";
var fileStaticClassName = $"__{value.ClassInformation.ClassName}BindablePropertyInitHelpers";

foreach (var info in value.BindableProperties)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CommunityToolkit.Maui.Animations;
using CommunityToolkit.Maui.Core;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Animations;

public class BaseAnimationTests
{
[Fact]
public void BaseAnimationT_EnsureDefaults()
{
// Arrange
BaseAnimation animation = new FadeAnimation();

// Act // Assert
Assert.Equal(BaseAnimationDefaults.Easing, animation.Easing);
Assert.Equal(BaseAnimationDefaults.Length, animation.Length);
}

[Fact]
public void BaseAnimation_EnsureDefaults()
{
// Arrange
BaseAnimation<VisualElement> animation = new FadeAnimation();

// Act // Assert
Assert.Equal(BaseAnimationDefaults.Easing, animation.Easing);
Assert.Equal(BaseAnimationDefaults.Length, animation.Length);
}

}
43 changes: 14 additions & 29 deletions src/CommunityToolkit.Maui/Animations/BaseAnimation.shared.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CommunityToolkit.Maui.Animations;
using CommunityToolkit.Maui.Core;

namespace CommunityToolkit.Maui.Animations;
/// <summary>
/// Abstract class for animation types to inherit.
/// </summary>
Expand All @@ -7,54 +9,37 @@
/// Initialize BaseAnimation
/// </remarks>
/// <param name="defaultLength">The default time, in milliseconds, over which to animate the transition</param>
public abstract class BaseAnimation<TAnimatable>(uint defaultLength = 250u) : BindableObject where TAnimatable : IAnimatable
public abstract partial class BaseAnimation<TAnimatable>(uint defaultLength = BaseAnimationDefaults.Length) : BindableObject where TAnimatable : IAnimatable
{
readonly uint defaultLength = defaultLength;

/// <summary>
/// Backing BindableProperty for the <see cref="Length"/> property.
/// </summary>
public static readonly BindableProperty LengthProperty =
BindableProperty.Create(nameof(Length), typeof(uint), typeof(BaseAnimation<TAnimatable>), 250u,
BindingMode.OneWay, defaultValueCreator: bindable => ((BaseAnimation<TAnimatable>)bindable).defaultLength);

/// <summary>
/// Backing BindableProperty for the <see cref="Easing"/> property.
/// </summary>
public static readonly BindableProperty EasingProperty =
BindableProperty.Create(nameof(Easing), typeof(Easing), typeof(BaseAnimation<TAnimatable>), Easing.Linear, BindingMode.OneWay);


/// <summary>
/// The time, in milliseconds, over which to animate the transition.
/// Gets or sets the time, in milliseconds, over which to animate the transition.
/// </summary>
public uint Length
{
get => (uint)GetValue(LengthProperty);
set => SetValue(LengthProperty, value);
}
[BindableProperty(DefaultBindingMode = BindingMode.OneWay, DefaultValueCreatorMethodName = nameof(LengthCreateDefaultValue))]
public partial uint Length { get; set; }

/// <summary>
/// The easing function to use for the animation
/// Gets or sets the easing function to use for the animation
/// </summary>
public Easing Easing
{
get => (Easing)GetValue(EasingProperty);
set => SetValue(EasingProperty, value);
}
[BindableProperty(DefaultBindingMode = BindingMode.OneWay)]
public partial Easing Easing { get; set; } = BaseAnimationDefaults.Easing;

/// <summary>
/// Performs the animation on the View
/// </summary>
/// <param name="view">The view to perform the animation on.</param>
/// <param name="token"> <see cref="CancellationToken"/>.</param>
public abstract Task Animate(TAnimatable view, CancellationToken token = default);

static object LengthCreateDefaultValue(BindableObject bindable) => ((BaseAnimation<TAnimatable>)bindable).defaultLength;
}

/// <inheritdoc/>
/// <summary>
/// Initialize BaseAnimation
/// </summary>
/// <param name="defaultLength">The default time, in milliseconds, over which to animate the transition</param>
public abstract class BaseAnimation(uint defaultLength = 250u) : BaseAnimation<VisualElement>(defaultLength)
public abstract class BaseAnimation(uint defaultLength = BaseAnimationDefaults.Length) : BaseAnimation<VisualElement>(defaultLength)
{
}
20 changes: 3 additions & 17 deletions src/CommunityToolkit.Maui/Animations/FadeAnimation.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@
public partial class FadeAnimation() : BaseAnimation(300)
{
/// <summary>
/// Backing BindableProperty for the <see cref="Opacity"/> property.
/// Gets or sets the opacity to fade to before returning to the elements current <see cref="VisualElement.Opacity"/>.
/// </summary>
public static readonly BindableProperty OpacityProperty =
BindableProperty.Create(
nameof(Opacity),
typeof(double),
typeof(FadeAnimation),
0.3,
BindingMode.TwoWay);

/// <summary>
/// Gets or sets the opacity to fade to before returning to the elements current Opacity.
/// </summary>
public double Opacity
{
get => (double)GetValue(OpacityProperty);
set => SetValue(OpacityProperty, value);
}
[BindableProperty(DefaultBindingMode = BindingMode.TwoWay)]
public partial double Opacity { get; set; } = 0.3;

/// <inheritdoc />
public override async Task Animate(VisualElement view, CancellationToken token = default)
Expand Down
Loading