Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Android, iOS] Fixed issue rendering Rectangle using Radius #11418

Merged
merged 9 commits into from Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 11413" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue11413">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="If a black square with corner radius is rendered inside a red rectangle, the test has passed."/>
<Grid
WidthRequest="100"
HeightRequest="100"
HorizontalOptions="Center"
VerticalOptions="Center"
BackgroundColor="Red">
<Rectangle
RadiusX="10"
RadiusY="10"
Fill="Black"
Aspect="Fill" />
</Grid>
</StackLayout>
</local:TestContentPage>
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Issue(IssueTracker.Github, 11413,
"[Bug] Rectangle shape - Incorrect rendering/crash (depending on platform)",
PlatformAffected.Android)]
public partial class Issue11413 : TestContentPage
{
public Issue11413()
{
#if APP
Device.SetFlags(new List<string> { ExperimentalFlags.ShapesExperimental });
InitializeComponent();
#endif
}

protected override void Init()
{
}
}
}
Expand Up @@ -1440,6 +1440,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11291.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11244.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11272.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11413.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11430.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11247.cs" />
</ItemGroup>
Expand Down Expand Up @@ -1690,6 +1691,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue11120.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue11413.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue11262.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
24 changes: 14 additions & 10 deletions Xamarin.Forms.Platform.Android/Shapes/RectangleRenderer.cs
Expand Up @@ -21,32 +21,36 @@ protected override void OnElementChanged(ElementChangedEventArgs<Rect> args)
}

base.OnElementChanged(args);

if (args.NewElement != null)
{
UpdateRadiusX();
UpdateRadiusY();
}
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);

if (args.PropertyName == Rect.RadiusXProperty.PropertyName)
if (args.IsOneOf(VisualElement.HeightProperty, VisualElement.WidthProperty))
UpdateRadius();
else if (args.PropertyName == Rect.RadiusXProperty.PropertyName)
UpdateRadiusX();
else if (args.PropertyName == Rect.RadiusYProperty.PropertyName)
UpdateRadiusY();
}

void UpdateRadius()
{
UpdateRadiusX();
UpdateRadiusY();
}

void UpdateRadiusX()
{
Control.UpdateRadiusX(Element.RadiusX / Element.WidthRequest);
if (Element.Width > 0)
Control.UpdateRadiusX(Element.RadiusX / Element.Width);
}

void UpdateRadiusY()
{
Control.UpdateRadiusY(Element.RadiusY / Element.HeightRequest);
if (Element.Height > 0)
Control.UpdateRadiusY(Element.RadiusY / Element.Height);
}
}

Expand Down Expand Up @@ -80,4 +84,4 @@ public void UpdateRadiusY(double radiusY)
UpdateShape();
}
}
}
}
28 changes: 16 additions & 12 deletions Xamarin.Forms.Platform.iOS/Shapes/RectangleRenderer.cs
Expand Up @@ -9,7 +9,7 @@ namespace Xamarin.Forms.Platform.iOS
namespace Xamarin.Forms.Platform.MacOS
#endif
{
public class RectangleRenderer : ShapeRenderer<Rect, RectView>
public class RectangleRenderer : ShapeRenderer<Rect, RectangleView>
{
[Internals.Preserve(Conditional = true)]
public RectangleRenderer()
Expand All @@ -21,42 +21,46 @@ protected override void OnElementChanged(ElementChangedEventArgs<Rect> args)
{
if (Control == null)
{
SetNativeControl(new RectView());
SetNativeControl(new RectangleView());
}

base.OnElementChanged(args);

if (args.NewElement != null)
{
UpdateRadiusX();
UpdateRadiusY();
}
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);

if (args.PropertyName == VisualElement.HeightProperty.PropertyName || args.PropertyName == VisualElement.WidthProperty.PropertyName)
UpdateRadius();
if (args.PropertyName == Rect.RadiusXProperty.PropertyName)
UpdateRadiusX();
else if (args.PropertyName == Rect.RadiusYProperty.PropertyName)
UpdateRadiusY();
}

void UpdateRadius()
{
UpdateRadiusX();
UpdateRadiusY();
}

void UpdateRadiusX()
{
Control.UpdateRadiusX(Element.RadiusX / Element.WidthRequest);
if (Element.Width > 0)
Control.UpdateRadiusX(Element.RadiusX / Element.Width);
}

void UpdateRadiusY()
{
Control.UpdateRadiusY(Element.RadiusY / Element.HeightRequest);
if (Element.Height > 0)
Control.UpdateRadiusY(Element.RadiusY / Element.Height);
}
}

public class RectView : ShapeView
public class RectangleView : ShapeView
{
public RectView()
public RectangleView()
{
UpdateShape();
}
Expand Down