Skip to content
Closed
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
56 changes: 56 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue31182.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 31182, "GraphicsView draws at half size after canvas.ResetState()", PlatformAffected.Android)]
public class Issue31182 : TestContentPage
{
protected override void Init()
{
var someView = new SomeView
{
WidthRequest = 200,
HeightRequest = 200,
AutomationId = "GraphicsView31182"
};

var label = new Label
{
Text = "Waiting",
AutomationId = "StatusLabel31182"
};

someView.Loaded += (s, e) =>
{
Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(100), () =>
{
MainThread.BeginInvokeOnMainThread(() =>
{
someView.Invalidate();
label.Text = "Invalidated";
});
});
};

Content = new VerticalStackLayout
{
WidthRequest = 200,
Background = Colors.Red,
Children =
{
someView,
label
}
};
}

class SomeView : GraphicsView, IDrawable
{
public SomeView() { Drawable = this; }

public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.ResetState();
canvas.FillColor = Colors.Yellow;
canvas.FillRectangle(dirtyRect);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue31182 : _IssuesUITest
{
public Issue31182(TestDevice testDevice) : base(testDevice) { }

public override string Issue => "GraphicsView draws at half size after canvas.ResetState()";

[Test]
[Category(UITestCategories.GraphicsView)]
public void GraphicsViewShouldDrawAtFullSize()
{
App.WaitForElement("StatusLabel31182", timeout: TimeSpan.FromSeconds(5));
// Wait for the label text to change after GraphicsView invalidation
App.WaitForTextToBePresentInElement("StatusLabel31182", "Invalidated");
VerifyScreenshot(retryTimeout: TimeSpan.FromSeconds(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public PlatformGraphicsView(Context context, IAttributeSet attrs, IDrawable draw
{
_scale = Resources.DisplayMetrics.Density;
_canvas = new PlatformCanvas(context);
_scalingCanvas = new ScalingCanvas(_canvas);
_scalingCanvas = new ScalingCanvas(_canvas, _scale);
Drawable = drawable;
}

public PlatformGraphicsView(Context context, IDrawable drawable = null) : base(context)
{
_scale = Resources.DisplayMetrics.Density;
_canvas = new PlatformCanvas(context);
_scalingCanvas = new ScalingCanvas(_canvas);
_scalingCanvas = new ScalingCanvas(_canvas, _scale);
Drawable = drawable;
}

Expand Down Expand Up @@ -67,7 +67,6 @@ public override void Draw(Canvas androidCanvas)
}

_scalingCanvas.ResetState();
_scalingCanvas.Scale(_scale, _scale);
//Since we are using a scaling canvas, we need to scale the rectangle
dirtyRect.Height /= _scale;
dirtyRect.Width /= _scale;
Expand Down
17 changes: 13 additions & 4 deletions src/Graphics/src/Graphics/ScalingCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ScalingCanvas : ICanvas, IBlurrableCanvas
private readonly Stack<float> _scaleYStack = new Stack<float>();
private float _scaleX = 1f;
private float _scaleY = 1f;
private float _initialScaleX = 1f;
private float _initialScaleY = 1f;

/// <summary>
/// Initializes a new instance of the <see cref="ScalingCanvas"/> class.
Expand All @@ -29,6 +31,12 @@ public ScalingCanvas(ICanvas wrapped)
_blurrableCanvas = _canvas as IBlurrableCanvas;
}

internal ScalingCanvas(ICanvas wrapped, float scale) : this(wrapped)
{
_initialScaleX = scale;
_initialScaleY = scale;
}

/// <inheritdoc/>
public float DisplayScale
{
Expand Down Expand Up @@ -271,8 +279,9 @@ public void ResetState()
_canvas.ResetState();
_scaleXStack.Clear();
_scaleYStack.Clear();
_scaleX = 1;
_scaleY = 1;
_scaleX = _initialScaleX;
_scaleY = _initialScaleY;
_canvas.Scale(_scaleX, _scaleY);
}

public bool RestoreState()
Expand All @@ -285,8 +294,8 @@ public bool RestoreState()
}
else
{
_scaleX = 1;
_scaleY = 1;
_scaleX = _initialScaleX;
_scaleY = _initialScaleY;
}

return restored;
Expand Down
Loading