|
| 1 | +using CommunityToolkit.WinUI.Helpers; |
| 2 | + |
| 3 | +namespace CommunityToolkit.WinUI; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// A class which represents a <see cref="FrameworkElement"/> that decorates a <see cref="UIElement"/>. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// An adorner is a custom element which is bound to a specific <see cref="UIElement"/> and can |
| 10 | +/// provide additional visual cues to the user. Adorners are rendered in an |
| 11 | +/// <see cref="AdornerLayer"/>, a special layer that is on top of the adorned element or a collection |
| 12 | +/// of adorned elements. Rendering of an adorner is independent of the UIElement it is bound to. An |
| 13 | +/// adorner is typically positioned relative to the element it is bound to based on the upper-left |
| 14 | +/// coordinate origin of the adorned element. |
| 15 | +/// |
| 16 | +/// Note: The parent of an <see cref="Adorner"/> is always an <see cref="AdornerLayer"/> and not the element being adorned. |
| 17 | +/// </remarks> |
| 18 | +public partial class Adorner : ContentControl |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Gets the element being adorned by this <see cref="Adorner"/>. |
| 22 | + /// </summary> |
| 23 | + public UIElement? AdornedElement |
| 24 | + { |
| 25 | + get; |
| 26 | + internal set |
| 27 | + { |
| 28 | + var oldvalue = field; |
| 29 | + field = value; |
| 30 | + OnAdornedElementChanged(oldvalue, value); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private void OnAdornedElementChanged(UIElement? oldvalue, UIElement? newvalue) |
| 35 | + { |
| 36 | + if (oldvalue is not null |
| 37 | + && oldvalue is FrameworkElement oldfe) |
| 38 | + { |
| 39 | + // TODO: Should we explicitly detach the WEL here? |
| 40 | + } |
| 41 | + |
| 42 | + if (newvalue is not null |
| 43 | + && newvalue is FrameworkElement newfe) |
| 44 | + { |
| 45 | + // Track changes to the AdornedElement's size |
| 46 | + var weakPropertyChangedListenerSize = new WeakEventListener<Adorner, object, SizeChangedEventArgs>(this) |
| 47 | + { |
| 48 | + OnEventAction = static (instance, source, eventArgs) => instance.OnSizeChanged(source, eventArgs), |
| 49 | + OnDetachAction = (weakEventListener) => newfe.SizeChanged -= weakEventListener.OnEvent // Use Local References Only |
| 50 | + }; |
| 51 | + newfe.SizeChanged += weakPropertyChangedListenerSize.OnEvent; |
| 52 | + |
| 53 | + // Track changes to the AdornedElement's layout |
| 54 | + // Note: This is pretty spammy, thinking we don't need this? |
| 55 | + /*var weakPropertyChangedListenerLayout = new WeakEventListener<Adorner, object?, object>(this) |
| 56 | + { |
| 57 | + OnEventAction = static (instance, source, eventArgs) => instance.OnLayoutUpdated(source, eventArgs), |
| 58 | + OnDetachAction = (weakEventListener) => newfe.LayoutUpdated -= weakEventListener.OnEvent // Use Local References Only |
| 59 | + }; |
| 60 | + newfe.LayoutUpdated += weakPropertyChangedListenerLayout.OnEvent;*/ |
| 61 | + |
| 62 | + // Initial size & layout update |
| 63 | + OnSizeChanged(null, null!); |
| 64 | + OnLayoutUpdated(null, null!); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void OnSizeChanged(object? sender, SizeChangedEventArgs e) |
| 69 | + { |
| 70 | + if (AdornedElement is null) return; |
| 71 | + |
| 72 | + Width = AdornedElement.ActualSize.X; |
| 73 | + Height = AdornedElement.ActualSize.Y; |
| 74 | + } |
| 75 | + |
| 76 | + internal void OnLayoutUpdated(object? sender, object e) |
| 77 | + { |
| 78 | + // Note: Also called by the parent AdornerLayer when its size changes |
| 79 | + if (AdornerLayer is not null |
| 80 | + && AdornedElement is not null) |
| 81 | + { |
| 82 | + var coord = AdornerLayer.CoordinatesTo(AdornedElement); |
| 83 | + |
| 84 | + Canvas.SetLeft(this, coord.X); |
| 85 | + Canvas.SetTop(this, coord.Y); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + internal AdornerLayer? AdornerLayer { get; set; } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Constructs a new instance of <see cref="Adorner"/>. |
| 93 | + /// </summary> |
| 94 | + public Adorner() |
| 95 | + { |
| 96 | + this.DefaultStyleKey = typeof(Adorner); |
| 97 | + } |
| 98 | +} |
0 commit comments