mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.14k stars 522 forks source link

Size in OnSizeAllocated of SKCanvasView in .NET 9.0 / SkiaSharp 3.0 Preview 2.1 does not match value returned in MeasureOverride #2811

Open janne-hmp opened 1 month ago

janne-hmp commented 1 month ago

Description

I'm not sure if this is a .NET MAUI or SkiaSharp 3.0 problem, so I post this in both repositories: I have created a custom control called CustomLabel inheriting from SKCanvasView (SkiaSharp 3.0 Preview 2.1), which works great in Xamarin.Forms. It is an auto-sizing special label (like normal label) with some extra features compared to normal label. It uses overriding OnMeasure in Xamarin and MeasureOverride in .NET MAUI (.NET 9.0 Preview 2) to set itself to the right size, based on its contents. In .NET MAUI, however, the Size value result from MeasureOverride (which is correctly calculated) does not match what MAUI allocates as size in OnSizeAllocated (which is how the control in fact looks like), making the control way smaller in height than requested, sometimes even 0 (on iOS). The error may depend on the layout, or the fact that there is new MeasureOverride function in MAUI, but I haven't figured out the root cause for the bug. In Xamarin, everything works exactly as intended. Tested on Android 14 and iOS 17.3.1.

Code

https://github.com/hyvanmielenpelit/GnollHack

Expected Behavior

I expected the CustomLabel used in GnollHackM (MAUI) menu titles to draw in full height like it does in GnollHackX (Xamarin).

Actual Behavior

The CustomLabel in GnollHackM (.NET MAUI) is drawn in partial height, making it too small and parts of the text being cut off. MeasureOverride calculates the right value, but OnSizeAllocated gets called with the wrong height (and that's how the label in fact looks in practice). On iOS, the height is even sometimes 0.

Version of SkiaSharp

3.x (Alpha)

Last Known Good Version of SkiaSharp

2.80.x (Deprecated)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Android, iOS

Platform / Operating System Version

Android 14, iOS 17.3.1

Devices

Google Pixel 6a, iPad 11,7

Relevant Screenshots

Screenshot_20240327-234854 Screenshot_20240327-234739

Relevant Log Output

No response

Code of Conduct

janne-hmp commented 1 month ago

There's a workaround to this in the following Maui issue: https://github.com/dotnet/maui/issues/4019#issuecomment-1510654509 . @mattleibow Perhaps this can give a clue how to fix the behavior for SKCanvasView when overriding OnMeasure or using MeasureOverride.