From 8a52032cfd28ec92504515c1e08a2af006c4d2b1 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:35:07 +0200 Subject: [PATCH] Add `WindowsBatchPropertyMapper` --- src/Core/src/Handlers/View/ViewHandler.cs | 3 ++ .../View/WindowsBatchPropertyMapper.cs | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/Core/src/Handlers/View/WindowsBatchPropertyMapper.cs diff --git a/src/Core/src/Handlers/View/ViewHandler.cs b/src/Core/src/Handlers/View/ViewHandler.cs index 0878fb9b6a1c..65790edc2479 100644 --- a/src/Core/src/Handlers/View/ViewHandler.cs +++ b/src/Core/src/Handlers/View/ViewHandler.cs @@ -27,6 +27,9 @@ public abstract partial class ViewHandler : ElementHandler, IViewHandler #if ANDROID // Use a custom mapper for Android which knows how to batch the initial property sets new AndroidBatchPropertyMapper(ElementMapper) +#elif WINDOWS + // Use a custom mapper for Windows which knows how to batch the initial property sets + new WindowsBatchPropertyMapper(ElementMapper) #else new PropertyMapper(ElementHandler.ElementMapper) #endif diff --git a/src/Core/src/Handlers/View/WindowsBatchPropertyMapper.cs b/src/Core/src/Handlers/View/WindowsBatchPropertyMapper.cs new file mode 100644 index 000000000000..1668373bfad5 --- /dev/null +++ b/src/Core/src/Handlers/View/WindowsBatchPropertyMapper.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.Maui.Handlers; + +#if WINDOWS +class WindowsBatchPropertyMapper : PropertyMapper + where TVirtualView : IElement + where TViewHandler : IElementHandler +{ + // During mass property updates, this list of properties will be skipped + public static HashSet SkipList = new(StringComparer.Ordinal) + { + // TranslationX does the work that is necessary to make other properties work. + nameof(IView.TranslationY), + nameof(IView.Scale), + nameof(IView.ScaleX), + nameof(IView.ScaleY), + nameof(IView.Rotation), + nameof(IView.RotationX), + nameof(IView.RotationY), + nameof(IView.AnchorX), + nameof(IView.AnchorY), + }; + + public WindowsBatchPropertyMapper(params IPropertyMapper[] chained) : base(chained) { } + + public override IEnumerable GetKeys() + { + foreach (var key in _mapper.Keys) + { + // When reporting the key list for mass updates up the chain, ignore properties in SkipList. + // These will be handled by ViewHandler.SetVirtualView() instead. + if (SkipList.Contains(key)) + { + continue; + } + + yield return key; + } + + if (Chained is not null) + { + foreach (var chain in Chained) + foreach (var key in chain.GetKeys()) + yield return key; + } + } +} +#endif \ No newline at end of file