From 90b04a78883088500aac13f97f912cfde177eb0e Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 13 Sep 2024 13:25:39 +0200 Subject: [PATCH 1/5] Convert RepeatBehavior struct to readonly struct as it is immutable --- .../Windows/Media/Animation/RepeatBehavior.cs | 38 +++++++++++-------- .../PresentationCore/ref/PresentationCore.cs | 2 +- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs index 79b0efc44af..19391597175 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs @@ -32,11 +32,11 @@ namespace System.Windows.Media.Animation /// A Forever RepeatBehavior specifies that a Timeline will repeat forever. /// [TypeConverter(typeof(RepeatBehaviorConverter))] - public struct RepeatBehavior : IFormattable + public readonly struct RepeatBehavior : IFormattable { - private double _iterationCount; - private TimeSpan _repeatDuration; - private RepeatBehaviorType _type; + private readonly double _iterationCount; + private readonly TimeSpan _repeatDuration; + private readonly RepeatBehaviorType _type; #region Constructors @@ -76,20 +76,14 @@ public RepeatBehavior(TimeSpan duration) } /// - /// Creates and returns a RepeatBehavior that indicates that a Timeline should repeat its - /// simple duration forever. + /// Private constructor, serves for creation of only. /// - /// A RepeatBehavior that indicates that a Timeline should repeat its simple duration - /// forever. - public static RepeatBehavior Forever + /// Only value is permitted. + private RepeatBehavior(RepeatBehaviorType behaviorType) { - get - { - RepeatBehavior forever = new RepeatBehavior(); - forever._type = RepeatBehaviorType.Forever; + Debug.Assert(behaviorType == RepeatBehaviorType.Forever); - return forever; - } + _type = behaviorType; } #endregion // Constructors @@ -158,6 +152,20 @@ public TimeSpan Duration } } + /// + /// Creates and returns a that indicates that a + /// should repeat its simple duration forever. + /// + /// A that indicates that a + /// should repeat its simple duration forever. + public static RepeatBehavior Forever + { + get + { + return new RepeatBehavior(RepeatBehaviorType.Forever); + } + } + #endregion // Properties #region Methods diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/ref/PresentationCore.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/ref/PresentationCore.cs index e1910f9f670..47494202b44 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/ref/PresentationCore.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/ref/PresentationCore.cs @@ -10119,7 +10119,7 @@ void System.Collections.IList.Insert(int index, object keyFrame) { } void System.Collections.IList.Remove(object keyFrame) { } } [System.ComponentModel.TypeConverterAttribute(typeof(System.Windows.Media.Animation.RepeatBehaviorConverter))] - public partial struct RepeatBehavior : System.IFormattable + public readonly partial struct RepeatBehavior : System.IFormattable { public RepeatBehavior(double count) { throw null; } public RepeatBehavior(System.TimeSpan duration) { throw null; } From 5a89a018984d1c3dffd51384570c8de220f7ceaf Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 13 Sep 2024 13:27:17 +0200 Subject: [PATCH 2/5] Simplify Equals override --- .../System/Windows/Media/Animation/RepeatBehavior.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs index 19391597175..389ff0af9e2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs @@ -175,16 +175,9 @@ public static RepeatBehavior Forever /// /// /// true if value is a RepeatBehavior and is equal to this instance; otherwise false. - public override bool Equals(Object value) + public override bool Equals(object value) { - if (value is RepeatBehavior) - { - return this.Equals((RepeatBehavior)value); - } - else - { - return false; - } + return value is RepeatBehavior behavior && Equals(behavior); } /// From 0d67178e9273504c0c90764d29d98b4db2931088 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 13 Sep 2024 13:53:33 +0200 Subject: [PATCH 3/5] Use nameof(count) and nameof(duration) in throw methods --- .../System/Windows/Media/Animation/RepeatBehavior.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs index 389ff0af9e2..912c82e63e2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs @@ -46,12 +46,8 @@ namespace System.Windows.Media.Animation /// The number of iterations specified by this RepeatBehavior. public RepeatBehavior(double count) { - if ( Double.IsInfinity(count) - || double.IsNaN(count) - || count < 0.0) - { - throw new ArgumentOutOfRangeException("count", SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count)); - } + if (double.IsInfinity(count) || double.IsNaN(count) || count < 0.0) + throw new ArgumentOutOfRangeException(nameof(count), SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count)); _repeatDuration = new TimeSpan(0); _iterationCount = count; @@ -66,9 +62,7 @@ public RepeatBehavior(double count) public RepeatBehavior(TimeSpan duration) { if (duration < new TimeSpan(0)) - { - throw new ArgumentOutOfRangeException("duration", SR.Format(SR.Timing_RepeatBehaviorInvalidRepeatDuration, duration)); - } + throw new ArgumentOutOfRangeException(nameof(duration), SR.Format(SR.Timing_RepeatBehaviorInvalidRepeatDuration, duration)); _iterationCount = 0.0; _repeatDuration = duration; From 5d9b5cde229b8318612434c5a23e5cbfacd1de4c Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 13 Sep 2024 14:08:23 +0200 Subject: [PATCH 4/5] Simplify Count/Duration property and remove unnecessary warning suppression --- .../Windows/Media/Animation/RepeatBehavior.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs index 912c82e63e2..b87da02019c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs @@ -4,16 +4,11 @@ // -// Allow suppression of certain presharp messages -#pragma warning disable 1634, 1691 - -using MS.Internal; - using System.ComponentModel; using System.Diagnostics; using System.Text; -using SR=MS.Internal.PresentationCore.SR; +using SR = MS.Internal.PresentationCore.SR; namespace System.Windows.Media.Animation { @@ -117,11 +112,8 @@ public double Count { get { - if (_type != RepeatBehaviorType.IterationCount) - { -#pragma warning suppress 56503 // Suppress presharp warning: Follows a pattern similar to Nullable. + if (!HasCount) throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotIterationCount, this)); - } return _iterationCount; } @@ -136,11 +128,8 @@ public TimeSpan Duration { get { - if (_type != RepeatBehaviorType.RepeatDuration) - { -#pragma warning suppress 56503 // Suppress presharp warning: Follows a pattern similar to Nullable. + if (!HasDuration) throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotRepeatDuration, this)); - } return _repeatDuration; } From bbc9462782512186ce28395d926b3ff322558634 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 13 Sep 2024 14:08:28 +0200 Subject: [PATCH 5/5] Use TimeSpan.Zero over new TimeSpan(0) though it does the same --- .../System/Windows/Media/Animation/RepeatBehavior.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs index b87da02019c..2f95c0746b0 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs @@ -44,7 +44,7 @@ public RepeatBehavior(double count) if (double.IsInfinity(count) || double.IsNaN(count) || count < 0.0) throw new ArgumentOutOfRangeException(nameof(count), SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count)); - _repeatDuration = new TimeSpan(0); + _repeatDuration = TimeSpan.Zero; _iterationCount = count; _type = RepeatBehaviorType.IterationCount; } @@ -56,7 +56,7 @@ public RepeatBehavior(double count) /// A TimeSpan representing the repeat duration specified by this RepeatBehavior. public RepeatBehavior(TimeSpan duration) { - if (duration < new TimeSpan(0)) + if (duration < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(duration), SR.Format(SR.Timing_RepeatBehaviorInvalidRepeatDuration, duration)); _iterationCount = 0.0;