Skip to content

Commit

Permalink
fix(effectiveViewport): Make sure to raise the event when initializin…
Browse files Browse the repository at this point in the history
…g multiple elements with EVP handlers

And in tests make sure to subscribe to EVPChanged in Loading intead of Loaded (was failing on UWP)

(cherry picked from commit 2ee3b50)
  • Loading branch information
dr1rrb authored and mergify-bot committed Nov 1, 2021
1 parent d05bbc8 commit 73d782d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,12 @@ public EVPTreeListener(FrameworkElement root, FrameworkElement leaf)
_root = root;
_leaf = leaf;

leaf.Loaded += ElementLoaded;
leaf.Loading += ElementLoading;
}

private void ElementLoaded(object sender, RoutedEventArgs e)
private void ElementLoading(FrameworkElement sender, object args)
{
_leaf.Loaded -= ElementLoaded;
_leaf.Loading -= ElementLoading;

Subscribe(sender);

Expand Down Expand Up @@ -1155,7 +1155,7 @@ public EVPListener Of<T>()

public void Dispose()
{
_leaf.Loaded -= ElementLoaded;
_leaf.Loading -= ElementLoading;
foreach (var listener in _listeners.Values)
{
listener.Dispose();
Expand Down
10 changes: 9 additions & 1 deletion src/Uno.UI/UI/Xaml/FrameworkElement.EffectiveViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ partial class FrameworkElement : IFrameworkElement_EffectiveViewport
{
private static readonly RoutedEventHandler ReconfigureViewportPropagationOnLoad = (snd, e) => ((_This)snd).ReconfigureViewportPropagation();
private event TypedEventHandler<_This, EffectiveViewportChangedEventArgs>? _effectiveViewportChanged;
private bool _hasNewHandler;
private int _childrenInterestedInViewportUpdates;
private IDisposable? _parentViewportUpdatesSubscription;
private ViewportInfo _parentViewport = ViewportInfo.Empty; // WARNING: Stored in parent's coordinates space, use GetParentViewport()
Expand All @@ -45,6 +46,7 @@ public event TypedEventHandler<_This, EffectiveViewportChangedEventArgs> Effecti
{
add
{
_hasNewHandler = true;
_effectiveViewportChanged += value;
ReconfigureViewportPropagation(isInternal: true);
}
Expand Down Expand Up @@ -321,8 +323,14 @@ private void PropagateEffectiveViewportChange(
+ $"| reason: {caller} "
+ $"| children: {_childrenInterestedInViewportUpdates}");

if (!isInternal && viewportUpdated) // We don't want to raise the event when we are only initializing the tree due to a new event handler
if (viewportUpdated
&& (
!isInternal // We don't want to raise the event when we are only initializing the tree due to a new event handler somewhere in sub tree
|| _hasNewHandler // but if we have a new local handler, we do need to raise the event!
))
{
_hasNewHandler = false;

// Note: The event only notify about the parentViewport (expressed in local coordinate space!),
// the "local effective viewport" is used only by our children.
_effectiveViewportChanged?.Invoke(this, new EffectiveViewportChangedEventArgs(parentViewport.Effective));
Expand Down

0 comments on commit 73d782d

Please sign in to comment.