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

[Android] invalidate shadows on parent's size change #24561

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24034.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 24034, "Shadow is not updating on change of parent control", PlatformAffected.All)]
public partial class Issue24034 : ContentPage
{
public Issue24034()
{
InitializeComponent();
}

private void Button_Clicked(object sender, EventArgs e)
{
GridView.IsVisible = !GridView.IsVisible;
}
}
45 changes: 45 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24034.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue24034">
<Grid RowDefinitions="300,Auto" Margin="10" ColumnSpacing="0" Padding="5,0" ColumnDefinitions="*,*,Auto">

<Border
Grid.Column="0"
HeightRequest="150"
Grid.Row="0"
StrokeShape="RoundRectangle 16,16,16,16"
Margin="5,0">
<Border.Shadow>
<Shadow Brush="Red"
Offset="2,10"
Radius="{OnPlatform Android=9, iOS=12}"
Opacity="1" />
</Border.Shadow>
<BoxView BackgroundColor="Green"/>
</Border>

<Border
Grid.Column="1" Grid.Row="0"
HeightRequest="150"
StrokeShape="RoundRectangle 16,16,16,16"
Margin="5,0">
<Border.Shadow>
<Shadow Brush="Red"
Offset="2,10"
Radius="{OnPlatform Android=9, iOS=12}"
Opacity="1" />
</Border.Shadow>

<BoxView BackgroundColor="Green"/>
</Border>

<Grid x:Name="GridView" Margin="5,0"
Padding="0, 5, 0, 0"
Grid.Column="2">
<BoxView WidthRequest="100" Height="100"/>
</Grid>

<Button Grid.Row="1" Grid.ColumnSpan="3" AutomationId="button" Text="Click" Clicked="Button_Clicked"/>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue24034(TestDevice device) : _IssuesUITest(device)
{
public override string Issue => "Shadow is not updating on change of parent control";

[Test]
[Category(UITestCategories.Border)]
public void ShadowShouldUpdate()
{
App.WaitForElement("button");
App.Click("button");

VerifyScreenshot();
}
}
12 changes: 11 additions & 1 deletion src/Core/src/Platform/Android/WrapperView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public partial class WrapperView : PlatformWrapperView
Android.Graphics.Paint _shadowPaint;
bool _invalidateShadow;

int _currentViewWidth;
int _currentViewHeight;

AView _borderView;

public bool InputTransparent { get; set; }
Expand Down Expand Up @@ -148,11 +151,13 @@ protected override void DrawShadow(Canvas canvas, int viewWidth, int viewHeight)
Graphics.Color solidColor = null;

// If need to redraw shadow
if (_invalidateShadow)
if (_invalidateShadow || DidViewSizeChange(viewWidth, viewHeight))
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
{
// If bounds is zero
if (viewHeight != 0 && viewWidth != 0)
{
_currentViewWidth = viewWidth;
_currentViewHeight = viewHeight;
var bitmapHeight = viewHeight + MaximumRadius;
var bitmapWidth = viewWidth + MaximumRadius;

Expand Down Expand Up @@ -246,6 +251,11 @@ protected override void DrawShadow(Canvas canvas, int viewWidth, int viewHeight)
canvas.DrawBitmap(_shadowBitmap, 0.0F, 0.0F, _shadowPaint);
}

bool DidViewSizeChange(int viewWidth, int viewHeight)
{
return _currentViewWidth != viewWidth || _currentViewHeight != viewHeight;
}

void ClearShadowResources()
{
_shadowCanvas?.Dispose();
Expand Down