Skip to content

Commit

Permalink
[Android] Render Border without set a Stroke (#7247)
Browse files Browse the repository at this point in the history
* Render Border even without Stroke on Android

* Added device test
  • Loading branch information
jsuarezruiz committed May 18, 2022
1 parent efac4b0 commit b8727a4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
34 changes: 29 additions & 5 deletions src/Core/src/Graphics/MauiDrawable.Android.cs
Expand Up @@ -164,17 +164,20 @@ public void SetBorderBrush(GPaint? paint)
if (paint is SolidPaint solidPaint)
SetBorderBrush(solidPaint);

if (paint is LinearGradientPaint linearGradientPaint)
else if (paint is LinearGradientPaint linearGradientPaint)
SetBorderBrush(linearGradientPaint);

if (paint is RadialGradientPaint radialGradientPaint)
else if (paint is RadialGradientPaint radialGradientPaint)
SetBorderBrush(radialGradientPaint);

if (paint is ImagePaint imagePaint)
else if (paint is ImagePaint imagePaint)
SetBorderBrush(imagePaint);

if (paint is PatternPaint patternPaint)
else if (paint is PatternPaint patternPaint)
SetBorderBrush(patternPaint);

else
SetEmptyBorderBrush();
}

public void SetBorderBrush(SolidPaint solidPaint)
Expand Down Expand Up @@ -218,6 +221,27 @@ public void SetBorderBrush(RadialGradientPaint radialGradientPaint)
InvalidateSelf();
}

public void SetEmptyBorderBrush()
{
_invalidatePath = true;

if (_backgroundColor != null)
{
_borderColor = _backgroundColor.Value;
_stroke = null;
}
else
{
_borderColor = null;

if (_background != null)
SetBorderBrush(_background);
}

InitializeBorderIfNeeded();
InvalidateSelf();
}

public void SetBorderBrush(ImagePaint imagePaint)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -473,7 +497,7 @@ bool HasBorder()
{
InitializeBorderIfNeeded();

return _shape != null && (_stroke != null || _borderColor != null);
return _shape != null;
}

void InitializeBorderIfNeeded()
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/StrokeExtensions.cs
Expand Up @@ -129,7 +129,7 @@ public static void InvalidateBorderStrokeBounds(this AView platformView)

internal static void UpdateMauiDrawable(this AView platformView, IBorderStroke border)
{
bool hasBorder = border.Shape != null && border.Stroke != null;
bool hasBorder = border.Shape != null;

if (!hasBorder)
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/ViewExtensions.cs
Expand Up @@ -160,7 +160,7 @@ public static void SetWindowBackground(this AView view)

public static void UpdateBackground(this ContentViewGroup platformView, IBorderStroke border)
{
bool hasBorder = border.Shape != null && border.Stroke != null;
bool hasBorder = border.Shape != null;

if (hasBorder)
platformView.UpdateBorderStroke(border);
Expand Down
@@ -1,12 +1,34 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.DeviceTests.Stubs;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class BorderHandlerTests
{
[Theory(DisplayName = "Border render without Stroke")]
[InlineData(0xFF0000)]
[InlineData(0x00FF00)]
[InlineData(0x0000FF)]
public async Task BorderRenderWithoutStroke(uint color)
{
var expected = Color.FromUint(color);

var border = new BorderStub()
{
Content = new LabelStub { Text = "Without Stroke", TextColor = Colors.White },
Shape = new RoundRectangleShapeStub { CornerRadius = new CornerRadius(12) },
Background = new SolidPaintStub(expected),
Stroke = null,
StrokeThickness = 2,
Height = 100,
Width = 300
};

await ValidateHasColor(border, expected);
}

ContentViewGroup GetNativeBorder(BorderHandler borderHandler) =>
borderHandler.PlatformView;

Expand All @@ -20,4 +42,4 @@ Task ValidateHasColor(IBorderView border, Color color, Action action = null)
});
}
}
}
}

0 comments on commit b8727a4

Please sign in to comment.