diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs index 901bb229d1b..a152d03c03b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs @@ -4,7 +4,7 @@ // // Description: -// AdornerPresentationContext knows that annotation comonents are wrapped +// AdornerPresentationContext knows that annotation components are wrapped // in an AnnotationAdorner and hosted in the AdornerLayer. Note, implementation-wise // a new PresentationContext is created for every annotation component. Executing // operations on a presentation context for a different annotation component @@ -19,17 +19,16 @@ using System.Windows.Annotations; using System.Windows.Documents; using System.Windows.Media; -using System.Collections; namespace MS.Internal.Annotations.Component { /// - /// AdornerPresentationContext knows that annotation comonents are wrapped in an AnnotationAdorner and hosted in the AdornerLayer. + /// AdornerPresentationContext knows that annotation components are wrapped in an AnnotationAdorner and hosted in the AdornerLayer. /// Note, implementation-wise a new PresentationContext is created for every annotation component. Executing operations on a presentation context /// for a different annotation component (located in the same adorner layer) works, but is slower than using the presentation context stored in the /// annotation component. /// - internal class AdornerPresentationContext : PresentationContext + internal sealed class AdornerPresentationContext : PresentationContext { #region Constructors @@ -100,22 +99,14 @@ internal static void HostComponent(AdornerLayer adornerLayer, IAnnotationCompone /// BringToTop method. This will move the component to the top of its priority group. If there are other /// components with higher priority they will still be on top of that component. If more than /// one component type have the same ZLevel that means they all can stay on top of each other. - /// Setting IAnnotationComponent.ZOrder must be invoked only by the PrezentationContext + /// Setting IAnnotationComponent.ZOrder must be invoked only by the PresentationContext /// when the Z-order changes. It can not be set by application in v1. internal static void SetTypeZLevel(Type type, int level) { Invariant.Assert(level >= 0, "level is < 0"); - Invariant.Assert(type != null, "type is null"); - if (_ZLevel.ContainsKey(type)) - { - _ZLevel[type] = level; - } - else - { - _ZLevel.Add(type, level); - } + s_ZLevel[type] = level; } /// @@ -128,9 +119,9 @@ internal static void SetTypeZLevel(Type type, int level) /// max Z-order value for this level internal static void SetZLevelRange(int level, int min, int max) { - if (_ZRanges[level] == null) + if (!s_ZRanges.ContainsKey(level)) { - _ZRanges.Add(level, new ZRange(min, max)); + s_ZRanges.Add(level, new ZRange(min, max)); } } @@ -512,12 +503,9 @@ private AnnotationAdorner GetAnnotationAdorner(IAnnotationComponent component) /// ZLevel private static int GetComponentLevel(IAnnotationComponent component) { - int level = 0; Type type = component.GetType(); - if (_ZLevel.ContainsKey(type)) - level = (int)_ZLevel[type]; - return level; + return s_ZLevel.TryGetValue(type, out int value) ? value : 0; } /// @@ -531,18 +519,21 @@ private static int GetComponentLevel(IAnnotationComponent component) private static int ComponentToAdorner(int zOrder, int level) { int res = zOrder; - ZRange range = (ZRange)_ZRanges[level]; - if (range != null) + + if (s_ZRanges.TryGetValue(level, out ZRange range)) { //adjust the Z-order (shift it with the minimal value for this range) //that way the component does need to know the range for its type that is // set by the application. It always sets the z-order as it starts from 0 res += range.Min; + if (res < range.Min) res = range.Min; + if (res > range.Max) res = range.Max; } + return res; } @@ -559,19 +550,17 @@ private static int ComponentToAdorner(int zOrder, int level) /// /// The adornerLayer which contains the annotation component. Basically what the presentation hides. /// - private AdornerLayer _adornerLayer; + private readonly AdornerLayer _adornerLayer; /// - /// The hashtable holds the priority level for each Component type as defined by the application + /// The dictionary holds the priority level for each Component type as defined by the application /// - private static Hashtable _ZLevel = new Hashtable(); + private static readonly Dictionary s_ZLevel = new(); /// /// The ZRanges for the ZLevels. /// - private static Hashtable _ZRanges = new Hashtable(); - - + private static readonly Dictionary s_ZRanges = new(); #endregion Private Fields @@ -581,40 +570,28 @@ private static int ComponentToAdorner(int zOrder, int level) /// This is to control the relationships with TextSelection which lives in the same /// AdornerLayer. Will be removed when more flexible Z-ordering mechanism is available /// - private class ZRange + private readonly struct ZRange { public ZRange(int min, int max) { - //exchange values if needed + // Swap values if needed if (min > max) { - int temp = min; + int num = min; min = max; - max = temp; + max = num; } - _min = min; - _max = max; - } - public int Min - { - get - { - return _min; - } - } - public int Max - { - get - { - return _max; - } + Min = min; + Max = max; } - private int _min, _max; + public int Min { get; } + public int Max { get; } + } - #endregion Internal classes + #endregion Private classes } }