Skip to content
Merged
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
5 changes: 3 additions & 2 deletions binding/SkiaSharp/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,10 +1078,11 @@ public void Dispose ()
/// </summary>
public void Restore ()
{
if (canvas != null) {
// canvas can be GC-ed before us
if (canvas != null && canvas.Handle != IntPtr.Zero) {
canvas.RestoreToCount (saveCount);
canvas = null;
}
canvas = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@ protected override void DisconnectHandler(SKCanvasView platformView)

public static void OnInvalidateSurface(SKCanvasViewHandler handler, ISKCanvasView canvasView, object? args)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.SetNeedsDisplay();
}

public static void MapIgnorePixelScaling(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.IgnorePixelScaling = canvasView.IgnorePixelScaling;
}

public static void MapEnableTouchEvents(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler?.PlatformView == null)
return;

handler.touchProxy?.UpdateEnableTouchEvents(handler.PlatformView, canvasView.EnableTouchEvents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,25 @@ protected override void DisconnectHandler(SKCanvasView platformView)

public static void OnInvalidateSurface(SKCanvasViewHandler handler, ISKCanvasView canvasView, object? args)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.Invalidate();
}

public static void MapIgnorePixelScaling(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.IgnorePixelScaling = canvasView.IgnorePixelScaling;
}

public static void MapEnableTouchEvents(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler?.PlatformView == null)
return;

handler.touchHandler ??= new SKTouchHandler(
args => canvasView.OnTouch(args),
(x, y) => handler.OnGetScaledCoord(x, y));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ protected override void DisconnectHandler(SKXamlCanvas platformView)

public static void OnInvalidateSurface(SKCanvasViewHandler handler, ISKCanvasView canvasView, object? args)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.Invalidate();
}

public static void MapIgnorePixelScaling(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.IgnorePixelScaling = canvasView.IgnorePixelScaling;
}

public static void MapEnableTouchEvents(SKCanvasViewHandler handler, ISKCanvasView canvasView)
{
if (handler.PlatformView == null)
if (handler?.PlatformView == null)
return;

handler.touchHandler ??= new SKTouchHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ protected override void DisconnectHandler(SKGLTextureView platformView)

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (handler?.PlatformView == null)
return;

if (handler.PlatformView.RenderMode == Rendermode.WhenDirty)
handler.PlatformView.RequestRender();
}

public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view)
{
if (handler.PlatformView is not MauiSKGLTextureView pv)
if (handler?.PlatformView is not MauiSKGLTextureView pv)
return;

pv.IgnorePixelScaling = view.IgnorePixelScaling;
Expand All @@ -57,13 +60,19 @@ public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view

public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.RenderMode = view.HasRenderLoop
? Rendermode.Continuously
: Rendermode.WhenDirty;
}

public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.touchHandler ??= new SKTouchHandler(
args => view.OnTouch(args),
(x, y) => handler.OnGetScaledCoord(x, y));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ protected override void DisconnectHandler(SKMetalView platformView)

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (handler?.PlatformView == null)
return;

if (handler.PlatformView.Paused && handler.PlatformView.EnableSetNeedsDisplay)
handler.PlatformView.SetNeedsDisplay();
}

public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view)
{
if (handler.PlatformView is MauiSKMetalView pv)
if (handler?.PlatformView is MauiSKMetalView pv)
{
pv.IgnorePixelScaling = view.IgnorePixelScaling;
handler.PlatformView.SetNeedsDisplay();
Expand All @@ -57,12 +60,18 @@ public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view

public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.Paused = !view.HasRenderLoop;
handler.PlatformView.EnableSetNeedsDisplay = !view.HasRenderLoop;
}

public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.touchProxy?.UpdateEnableTouchEvents(handler.PlatformView, view.EnableTouchEvents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ protected override void DisconnectHandler(SKSwapChainPanel platformView)

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (handler?.PlatformView == null)
return;

if (!handler.PlatformView.EnableRenderLoop)
handler.PlatformView.Invalidate();
}

public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view)
{
if (handler.PlatformView is not MauiSKSwapChainPanel pv)
if (handler?.PlatformView is not MauiSKSwapChainPanel pv)
return;

pv.IgnorePixelScaling = view.IgnorePixelScaling;
Expand All @@ -48,12 +51,15 @@ public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view

public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.PlatformView.EnableRenderLoop = view.HasRenderLoop;
}

public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view)
{
if (handler.PlatformView == null)
if (handler?.PlatformView == null)
return;

handler.touchHandler ??= new SKTouchHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ protected override void DisconnectHandler(SKGLView platformView)

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (handler?.PlatformView == null)
Comment on lines 54 to +56
Copy link

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This null-check pattern is repeated across multiple handler methods; consider extracting a shared guard or helper method to reduce duplication and improve maintainability.

Suggested change
public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (handler?.PlatformView == null)
private static bool IsHandlerValid(SKGLViewHandler? handler) =>
handler?.PlatformView != null;
public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
if (!IsHandlerValid(handler))

Copilot uses AI. Check for mistakes.
return;

handler.renderLoopManager?.RequestDisplay();
}

public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view)
{
if (handler.PlatformView is MauiSKGLView pv)
if (handler?.PlatformView is MauiSKGLView pv)
{
pv.IgnorePixelScaling = view.IgnorePixelScaling;
handler.renderLoopManager?.RequestDisplay();
Expand All @@ -67,6 +70,9 @@ public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view

public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

if (view.HasRenderLoop)
handler.renderLoopManager?.RequestRenderLoop();
else
Expand All @@ -75,6 +81,9 @@ public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)

public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view)
{
if (handler?.PlatformView == null)
return;

handler.touchProxy?.UpdateEnableTouchEvents(handler.PlatformView, view.EnableTouchEvents);
}

Expand Down
4 changes: 2 additions & 2 deletions source/SkiaSharp.Views/SkiaSharp.Views.WinUI/SKXamlCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ private void OnUnloaded(object sender, RoutedEventArgs e)
public void Invalidate()
{
#if WINDOWS
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, DoInvalidate);
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, DoInvalidate);
#else
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, DoInvalidate);
Dispatcher?.RunAsync(CoreDispatcherPriority.Normal, DoInvalidate);
#endif
}

Expand Down