Skip to content
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,17 +19,16 @@
using System.Windows.Annotations;
using System.Windows.Documents;
using System.Windows.Media;
using System.Collections;

namespace MS.Internal.Annotations.Component
{
/// <summary>
/// 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.
/// </summary>
internal class AdornerPresentationContext : PresentationContext
internal sealed class AdornerPresentationContext : PresentationContext
{
#region Constructors

Expand Down Expand Up @@ -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.</remarks>
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;
}

/// <summary>
Expand All @@ -128,9 +119,9 @@ internal static void SetTypeZLevel(Type type, int level)
/// <param name="max">max Z-order value for this level</param>
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));
}
}

Expand Down Expand Up @@ -512,12 +503,9 @@ private AnnotationAdorner GetAnnotationAdorner(IAnnotationComponent component)
/// <returns>ZLevel</returns>
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;
}

/// <summary>
Expand All @@ -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;
}

Expand All @@ -559,19 +550,17 @@ private static int ComponentToAdorner(int zOrder, int level)
/// <summary>
/// The adornerLayer which contains the annotation component. Basically what the presentation hides.
/// </summary>
private AdornerLayer _adornerLayer;
private readonly AdornerLayer _adornerLayer;

/// <summary>
/// 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
/// </summary>
private static Hashtable _ZLevel = new Hashtable();
private static readonly Dictionary<Type, int> s_ZLevel = new();

/// <summary>
/// The ZRanges for the ZLevels.
/// </summary>
private static Hashtable _ZRanges = new Hashtable();


private static readonly Dictionary<int, ZRange> s_ZRanges = new();

#endregion Private Fields

Expand All @@ -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
/// </summary>
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
}
}