[Proposal] TextColorTo extension for ITextStyle
See original GitHub issueTextColorTo extension for ITextStyle
- Proposed
- Prototype
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation
Link to Discussion
Extend ColorAnimationExtensions with ‘TextColorTo’ for ITextStyle
Summary
Extend the ColorAnimationExtenions
class with a TextColorTo
extension method on the ITextStyle
interface, allowing developers to animate the color transition of the TextColor
property of controls implementing ITextStyle
.
Motivation
Currently the ColorAnimationExtensions
class has 1 method BackgroundColorTo
which helps animating the color transition of a VisualElement
’s BackgroundColor
. Almost the exact same code can be re-used for this proposed TextColorTo
extension method. Effort is low, value is pretty descent as ITextStyle
is implemented by several controls.
Detailed Design
public static Task<bool> TextColorTo(this ITextStyle element, Color color, uint rate = 16u, uint length = 250u, Easing? easing = null)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(color);
element.TextColor ??= Colors.Transparent;
var animationCompletionSource = new TaskCompletionSource<bool>();
try
{
new Animation
{
{ 0, 1, GetRedTransformAnimation(element, color.Red) },
{ 0, 1, GetGreenTransformAnimation(element, color.Green) },
{ 0, 1, GetBlueTransformAnimation(element, color.Blue) },
{ 0, 1, GetAlphaTransformAnimation(element, color.Alpha) },
}
.Commit(element, nameof(TextColorTo), rate, length, easing, (d, b) => animationCompletionSource.SetResult(true));
}
catch (ArgumentException aex)
{
animationCompletionSource.SetResult(false);
}
return animationCompletionSource.Task;
}
static Animation GetRedTransformAnimation(ITextStyle element, float targetRed) =>
new(v => element.TextColor = element.TextColor.WithRed(v), element.TextColor.Red, targetRed);
static Animation GetGreenTransformAnimation(ITextStyle element, float targetGreen) =>
new(v => element.TextColor= element.TextColor.WithGreen(v), element.TextColor.Green, targetGreen);
static Animation GetBlueTransformAnimation(ITextStyle element, float targetBlue) =>
new(v => element.TextColor = element.TextColor.WithBlue(v), element.TextColor.Blue, targetBlue);
static Animation GetAlphaTransformAnimation(ITextStyle element, float targetAlpha) =>
new(v => element.TextColor = element.TextColor.WithAlpha(v), element.TextColor.Alpha, targetAlpha);
Usage Syntax
XAML Usage
N/A
C# Usage
var label = new Label();
await label.TextColorTo(Colors.Red, 16, 1500, Easing.SinIn);
var button = new TextButton();
await button.TextColorTo(Colors.Green, 16, 1500);
var entry = new Entry();
await entry.TextColorTo(Colors.Pink);
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
TextColorToGenerator.cs
The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your ......
Read more >ColorAnimationExtensions - .NET MAUI Community Toolkit
The ColorAnimationExtensions provide a series of extension methods that support animating the Color related properties of a VisualElement.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’re totally right, @PieEatingNinjas!
I decided that the best way to implement this is to use source generators. This allows us to generate a
TextColorTo
method for each class that implementsITextStyle
. And it ensures that any new classes created by the .NET MAUI team that implementITextStyle
will also be supported.I just opened the PR and I’d love for you to give it a review! https://github.com/CommunityToolkit/Maui/pull/224
Approved!
Thanks @PieEatingNinjas! Assigned ✅