From 0b848c8bac705cd950474c8df877d2e04037b608 Mon Sep 17 00:00:00 2001 From: chinboy Date: Fri, 28 Feb 2025 13:40:52 +0800 Subject: [PATCH] Refactor ScopeAwareAdornerLayer --- .../Primitives/ScopeAwareAdornerLayer.cs | 11 +--------- .../VisualLayerManagerReflectionExtensions.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/AtomUI.Controls/Utils/VisualLayerManagerReflectionExtensions.cs diff --git a/src/AtomUI.Controls/Primitives/ScopeAwareAdornerLayer.cs b/src/AtomUI.Controls/Primitives/ScopeAwareAdornerLayer.cs index 7f2ff51eb..c36549424 100644 --- a/src/AtomUI.Controls/Primitives/ScopeAwareAdornerLayer.cs +++ b/src/AtomUI.Controls/Primitives/ScopeAwareAdornerLayer.cs @@ -45,16 +45,12 @@ public class ScopeAwareAdornerLayer : Canvas private static readonly AttachedProperty SavedAdornerLayerProperty = AvaloniaProperty.RegisterAttached("SavedAdornerLayer"); - private static readonly MethodInfo AddLayerMethodInfo; - #endregion static ScopeAwareAdornerLayer() { AdornedElementProperty.Changed.Subscribe(HandleAdornedElementChanged); AdornerProperty.Changed.Subscribe(HandleAdornerChanged); - AddLayerMethodInfo = - typeof(VisualLayerManager).GetMethod("AddLayer", BindingFlags.Instance | BindingFlags.NonPublic)!; } public ScopeAwareAdornerLayer() @@ -333,11 +329,6 @@ public class ScopeAwareAdornerLayer : Canvas } } - private static void AddLayerToVisualLayerManager(VisualLayerManager visualLayerManager, Control layer, int zindex) - { - AddLayerMethodInfo.Invoke(visualLayerManager, [layer, zindex]); - } - private static ScopeAwareAdornerLayer InjectLayer(Layoutable layerHost) { var layer = FindAdornerLayer(layerHost); @@ -356,7 +347,7 @@ public class ScopeAwareAdornerLayer : Canvas layer.LayerHost = layerHost; if (layerHost is VisualLayerManager visualLayerManager) { - AddLayerToVisualLayerManager(visualLayerManager, layer, visualLayerManager.ZIndex); + visualLayerManager.AddLayer(layer, visualLayerManager.ZIndex); } else if (layerHost is ScrollContentPresenter scrollContentPresenter) { diff --git a/src/AtomUI.Controls/Utils/VisualLayerManagerReflectionExtensions.cs b/src/AtomUI.Controls/Utils/VisualLayerManagerReflectionExtensions.cs new file mode 100644 index 000000000..f82613827 --- /dev/null +++ b/src/AtomUI.Controls/Utils/VisualLayerManagerReflectionExtensions.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using AtomUI.Reflection; +using Avalonia.Controls; +using Avalonia.Controls.Primitives; + +namespace AtomUI.Controls.Utils; + +internal static class VisualLayerManagerReflectionExtensions +{ + #region 反射信息定义 + + private static readonly Lazy AddLayerMethodInfo = new Lazy(() => + typeof(VisualLayerManager).GetMethodInfoOrThrow("AddLayer", + BindingFlags.Instance | BindingFlags.NonPublic)); + + #endregion + + public static void AddLayer(this VisualLayerManager visualLayerManager, Control layer, int zindex) + { + AddLayerMethodInfo.Value.Invoke(visualLayerManager, [layer, zindex]); + } +} \ No newline at end of file