Skip to content

feat(controls): Make Durations customizable#1679

Open
dkuaf wants to merge 4 commits intolepoco:mainfrom
dkuaf:feature/transitions
Open

feat(controls): Make Durations customizable#1679
dkuaf wants to merge 4 commits intolepoco:mainfrom
dkuaf:feature/transitions

Conversation

@dkuaf
Copy link
Copy Markdown
Contributor

@dkuaf dkuaf commented Apr 2, 2026

Durations on Animations are freezable, this pull request provides a way to make Durations customizable.

Pull request type

Please check the type of change your PR introduces:

  • Update
  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes

What is the current behavior?

Durations on Animation has hard coded values for all controls

Issue Number: #1586
fixes #1586

What is the new behavior?

Durations can be of three types: SlowDuration, NormalDuration and FastDuration. These can all be set when merging ControlsDictionary. Defaults to previous hardcoded values (no breaking change)

<ui:ControlsDictionary SlowDuration="0:0:0.0" NormalDuration="0:0:0.0" FastDuration="0:0:0.0" />

Other information

@dkuaf dkuaf requested a review from pomianowski as a code owner April 2, 2026 11:51
@github-actions github-actions Bot added controls Changes to the appearance or logic of custom controls. styles Topic is related to styles PR Pull request navigation Changes to navigation related controls. dotnet release labels Apr 2, 2026
@dkuaf dkuaf changed the title Make Durations customizable feature(controls): Make Durations customizable Apr 7, 2026
@pomianowski pomianowski changed the title feature(controls): Make Durations customizable feat(controls): Make Durations customizable Apr 23, 2026
@pomianowski pomianowski added the help_wanted Extra attention is needed label Apr 23, 2026
@dkuaf
Copy link
Copy Markdown
Contributor Author

dkuaf commented Apr 24, 2026

@pomianowski yo! do you think this feature should be added?

if yes, i was thinking about this and i think just 3 static TimeSpan properties should be fine instead of AnimationDurationExtension.cs

/// <summary>
/// A longer duration intended for prominent or decorative transitions.
/// </summary>
SlowDuration,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's drop the Duration from all, it's already an AnimationDuration.
I also think we can also structure this differently to avoid the Dictionary and ControlsDictionary changes.
Something like

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Markup;

namespace Wpf.Ui.Animations;

/// <summary>
/// A XAML markup extension that resolves an <see cref="AnimationDuration" /> key
/// to a concrete <see cref="Duration" /> value at parse time.
/// This allows Storyboard animations to reference configurable durations
/// without requiring <c>DynamicResource</c> (which cannot be frozen).
/// </summary>
/// <example>
/// <code lang="xml">
/// &lt;!-- Positional syntax --&gt;
/// Duration="{markup:AnimationDuration Slow}"
/// 
/// &lt;!-- Named property syntax --&gt;
/// Duration="{markup:AnimationDuration Duration=Slow}"
/// </code>
/// </example>
public class AnimationDurationExtension : MarkupExtension
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AnimationDurationExtension" /> class.
    /// </summary>
    public AnimationDurationExtension() { }

    /// <summary>
    /// Initializes a new instance of the <see cref="AnimationDurationExtension" /> class.
    /// </summary>
    public AnimationDurationExtension(AnimationDurationType animationDurationType)
    {
        Duration = AnimationDuration.FromType(animationDurationType);
    }

    /// <summary>
    /// Gets or sets the <see cref="AnimationDuration" /> key to resolve.
    /// </summary>
    public AnimationDuration Duration { get; set; }

    /// <summary>
    /// Returns the <see cref="System.Windows.Duration" /> associated with the current
    /// <see cref="Duration" /> key, including any user-defined override.
    /// </summary>
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Duration.Duration;
    }
}

public struct AnimationDuration
{
    private static readonly AnimationDuration[] _durations =
    [
        new() { Duration = TimeSpan.FromMilliseconds(333), Type = AnimationDurationType.Slow },
        new() { Duration = TimeSpan.FromMilliseconds(167), Type = AnimationDurationType.Normal },
        new() { Duration = TimeSpan.FromMilliseconds(80), Type = AnimationDurationType.Fast }
    ];

    public Duration Duration { get; set; }

    public AnimationDurationType Type { get; set; }

    public static AnimationDuration Normal
    {
        get => _durations[1];
        set => _durations[1] = value;
    }

    public static AnimationDuration Fast
    {
        get => _durations[2];
        set => _durations[2] = value;
    }

    public static AnimationDuration Slow
    {
        get => _durations[0];
        set => _durations[0] = value;
    }

    public static AnimationDuration FromType(AnimationDurationType type) => _durations[(int)type];
}

/// <summary>
/// Defines the duration types available for control animations.
/// </summary>
public enum AnimationDurationType
{
    /// <summary>
    /// A longer duration intended for prominent or decorative transitions.
    /// </summary>
    Slow,

    /// <summary>
    /// The standard duration for most interactive feedback animations.
    /// </summary>
    Normal,

    /// <summary>
    /// A shorter duration for rapid state changes.
    /// </summary>
    Fast,
}

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

Labels

controls Changes to the appearance or logic of custom controls. dotnet help_wanted Extra attention is needed navigation Changes to navigation related controls. PR Pull request release styles Topic is related to styles

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a new ResourceDictionary for Variables

3 participants