Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Add WindowsBatchPropertyMapper to save unnecessary work when initializing views #24417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/Core/src/Handlers/View/ViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IView, IViewHandler>(ElementMapper)
#elif WINDOWS
// Use a custom mapper for Windows which knows how to batch the initial property sets
new WindowsBatchPropertyMapper<IView, IViewHandler>(ElementMapper)
#else
new PropertyMapper<IView, IViewHandler>(ElementHandler.ElementMapper)
#endif
Expand Down
50 changes: 50 additions & 0 deletions src/Core/src/Handlers/View/WindowsBatchPropertyMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;

namespace Microsoft.Maui.Handlers;

#if WINDOWS
class WindowsBatchPropertyMapper<TVirtualView, TViewHandler> : PropertyMapper<TVirtualView, TViewHandler>
where TVirtualView : IElement
where TViewHandler : IElementHandler
{
// During mass property updates, this list of properties will be skipped
public static HashSet<string> 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<string> 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
Loading