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

Don't propagate VE.Frame changes back down to the Handler #22347

Merged
merged 3 commits into from
May 15, 2024
Merged
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
5 changes: 4 additions & 1 deletion src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
{
base.OnPropertyChanged(propertyName);

Handler?.UpdateValue(propertyName);
UpdateHandlerValue(propertyName);

if (_effects?.Count > 0)
{
Expand All @@ -583,6 +583,9 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
}
}

private protected virtual void UpdateHandlerValue(string property) =>
Handler?.UpdateValue(property);

internal IEnumerable<Element> Descendants() =>
Descendants<Element>();

Expand Down
13 changes: 13 additions & 0 deletions src/Controls/src/Core/VisualElement/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,19 @@ static double EnsurePositive(double value)
return value;
}

private protected override void UpdateHandlerValue(string property)
{
// The HeightProperty and WidthProperty are not designed to propagate back to the handler.
// Instead, we use WidthRequestProperty and HeightRequestProperty to propagate changes to the handler.
// HeightProperty and WidthProperty are readonly and only update when the VisualElement.Frame property is set
// during an arrange pass, which indicates the actual width and height of the platform element.
// Changes to WidthRequestProperty and HeightRequestProperty will propagate to the handler via the `OnRequestChanged` method.
if (this.Batched && (property == HeightProperty.PropertyName || property == WidthProperty.PropertyName))
return;

base.UpdateHandlerValue(property);
}

/// <inheritdoc/>
double IView.Width
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Microsoft.Maui.Animations;

using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.Controls.Core.UnitTests
{
public class BasicVisualElement : VisualElement
{
}

public class BasicVisualElementHandler : ViewHandler<BasicVisualElement, object>
{
public BasicVisualElementHandler(IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper)
{
}

protected override object CreatePlatformView()
{
return new object();
}
}
}
86 changes: 86 additions & 0 deletions src/Controls/tests/Core.UnitTests/VisualElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
using System.Data.Common;
using System.Threading.Tasks;
using Microsoft.Maui.Controls.Shapes;

using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Primitives;
using Xunit;
Expand Down Expand Up @@ -237,5 +242,86 @@ public async Task ShadowDoesNotLeak()

Assert.False(reference.IsAlive, "VisualElement should not be alive!");
}

[Fact]
public void HandlerDoesntPropagateWidthChangesDuringBatchUpdates()
{
bool mapperCalled = false;

var mapper = new PropertyMapper<IView, ViewHandler>(ViewHandler.ViewMapper)
{
[nameof(IView.Height)] = (_,_) => mapperCalled = true,
[nameof(IView.Width)] = (_,_) => mapperCalled = true,
};

var mauiApp1 = MauiApp.CreateBuilder()
.UseMauiApp<ApplicationStub>()
.ConfigureMauiHandlers(handlers => handlers.AddHandler<BasicVisualElement>((services) => new BasicVisualElementHandler(mapper)))
.Build();

var element = new BasicVisualElement();
var platformView = element.ToPlatform(new MauiContext(mauiApp1.Services));

mapperCalled = false;
element.Frame = new Rect(0,0,100,100);
Assert.False(mapperCalled);
}

[Fact]
public void HandlerDoesPropagateWidthChangesWhenUpdatedDuringSizedChanged()
{
bool mapperCalled = false;

var mapper = new PropertyMapper<IView, ViewHandler>(ViewHandler.ViewMapper)
{
[nameof(IView.Height)] = (_,_) => mapperCalled = true,
[nameof(IView.Width)] = (_,_) => mapperCalled = true,
};

var mauiApp1 = MauiApp.CreateBuilder()
.UseMauiApp<ApplicationStub>()
.ConfigureMauiHandlers(handlers => handlers.AddHandler<BasicVisualElement>((services) => new BasicVisualElementHandler(mapper)))
.Build();

var element = new BasicVisualElement();
var platformView = element.ToPlatform(new MauiContext(mauiApp1.Services));

element.SizeChanged += (_,_) => element.HeightRequest = 100;
mapperCalled = false;
element.Frame = new Rect(0,0,100,100);

Assert.True(mapperCalled);
}

[Fact]
public void WidthAndHeightRequestPropagateToHandler()
{
int heightMapperCalled = 0;
int widthMapperCalled = 0;

var mapper = new PropertyMapper<IView, ViewHandler>(ViewHandler.ViewMapper)
{
[nameof(IView.Height)] = (_,_) => heightMapperCalled++,
[nameof(IView.Width)] = (_,_) => widthMapperCalled++,
};

var mauiApp1 = MauiApp.CreateBuilder()
.UseMauiApp<ApplicationStub>()
.ConfigureMauiHandlers(handlers => handlers.AddHandler<BasicVisualElement>((services) => new BasicVisualElementHandler(mapper)))
.Build();

var element = new BasicVisualElement();
var platformView = element.ToPlatform(new MauiContext(mauiApp1.Services));

heightMapperCalled = 0;
widthMapperCalled = 0;
element.WidthRequest = 99;
Assert.Equal(1, heightMapperCalled);
Assert.Equal(1, widthMapperCalled);

element.HeightRequest = 99;
Assert.Equal(2, heightMapperCalled);
Assert.Equal(2, widthMapperCalled);
}
}
}
Loading