Skip to content

Commit

Permalink
refactor: Enable nullable in BorderLayerRenderer.Android
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Sep 5, 2023
1 parent 8bf0af2 commit 371ec88
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void buildBorderCornerRadius(
drawable.setShape(new PathShape(borderPath, width, height));

Paint paint = drawable.getPaint();
paing.setAntiAlias(true);
paint.setAntiAlias(true);
paint.setAlpha(strokePaint.getAlpha());
paint.setColor(strokePaint.getColor());
paint.setShader(strokePaint.getShader());
Expand Down
108 changes: 61 additions & 47 deletions src/Uno.UI/UI/Xaml/Controls/Border/BorderLayerRenderer.Android.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
#pragma warning disable 0618 // Used for compatibility with SetBackgroundDrawable and previous API Levels

using System;
Expand All @@ -23,13 +24,13 @@ partial class BorderLayerRenderer
{
private const double __opaqueAlpha = 255;

private LayoutState _currentState;
private LayoutState? _currentState;

private readonly SerialDisposable _layerDisposable = new SerialDisposable();
private static readonly float[] _outerRadiiStore = new float[8];
private static readonly float[] _innerRadiiStore = new float[8];
private static Paint _strokePaint;
private static Paint _fillPaint;
private static Paint? _strokePaint;
private static Paint? _fillPaint;

/// <summary>
/// Updates or creates a sublayer to render a border-like shape.
Expand Down Expand Up @@ -69,7 +70,7 @@ public void UpdateLayer(
_layerDisposable.Disposable = null;
}

Action onImageSet = null;
Action? onImageSet = null;
var disposable = InnerCreateLayers(view, drawArea, background, backgroundSizing, borderThickness, borderBrush, cornerRadius, () => onImageSet?.Invoke());

// Most of the time we immediately dispose the previous layer. In the case where we're using an ImageBrush,
Expand Down Expand Up @@ -145,7 +146,7 @@ private static IDisposable InnerCreateLayers(

using (var backgroundPath = new Path())
{
backgroundPath.AddRoundRect(adjustedArea.ToRectF(), _innerRadiiStore, Path.Direction.Cw);
backgroundPath.AddRoundRect(adjustedArea.ToRectF(), _innerRadiiStore, Path.Direction.Cw!);
//We only need to set a background if the drawArea is non-zero
if (!drawArea.HasZeroArea())
{
Expand Down Expand Up @@ -188,8 +189,8 @@ private static IDisposable InnerCreateLayers(
//Create the path for the outer and inner rectangles that will become our border shape
using var borderPath = new Path();

borderPath.AddRoundRect(drawArea, _outerRadiiStore, Path.Direction.Cw);
borderPath.AddRoundRect(adjustedArea, _innerRadiiStore, Path.Direction.Cw);
borderPath.AddRoundRect(drawArea, _outerRadiiStore, Path.Direction.Cw!);
borderPath.AddRoundRect(adjustedArea, _innerRadiiStore, Path.Direction.Cw!);

var overlay = GetOverlayDrawable(
_strokePaint,
Expand Down Expand Up @@ -286,8 +287,8 @@ private static void SetOverlay(BindableView view, CompositeDisposable disposable
{
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
ExecuteWithNoRelayout(view, v => v.Overlay.Add(overlay));
disposables.Add(() => ExecuteWithNoRelayout(view, v => v.Overlay.Remove(overlay)));
ExecuteWithNoRelayout(view, v => v.Overlay?.Add(overlay));
disposables.Add(() => ExecuteWithNoRelayout(view, v => v.Overlay?.Remove(overlay)));
}
else
{
Expand All @@ -310,11 +311,11 @@ private static void SetOverlay(BindableView view, CompositeDisposable disposable
}
}

private static IDisposable DispatchSetImageBrushAsBackground(BindableView view, ImageBrush background, Windows.Foundation.Rect drawArea, Action onImageSet, Path maskingPath = null)
private static IDisposable DispatchSetImageBrushAsBackground(BindableView view, ImageBrush background, Windows.Foundation.Rect drawArea, Action onImageSet, Path? maskingPath = null)
{
var disposable = new CompositeDisposable();
Dispatch(
view?.Dispatcher,
view.Dispatcher,
async ct =>
{
var bitmapDisposable = await SetImageBrushAsBackground(ct, view, background, drawArea, maskingPath, onImageSet);
Expand All @@ -327,7 +328,7 @@ private static IDisposable DispatchSetImageBrushAsBackground(BindableView view,
}

//Load bitmap from ImageBrush and set it as a bitmapDrawable background on target view
private static async Task<IDisposable> SetImageBrushAsBackground(CancellationToken ct, BindableView view, ImageBrush background, Windows.Foundation.Rect drawArea, Path maskingPath, Action onImageSet)
private static async Task<IDisposable> SetImageBrushAsBackground(CancellationToken ct, BindableView view, ImageBrush background, Windows.Foundation.Rect drawArea, Path? maskingPath, Action onImageSet)
{
var bitmap = await background.GetBitmap(ct, drawArea, maskingPath);

Expand Down Expand Up @@ -364,17 +365,17 @@ private static void ExecuteWithNoRelayout(BindableView target, Action<BindableVi
}
}

private static Drawable GetOverlayDrawable(
private static Drawable? GetOverlayDrawable(
Paint strokePaint,
Thickness physicalBorderThickness,
global::System.Drawing.Size viewSize,
Path borderPath = null)
Path? borderPath = null)
{
if (strokePaint != null)
{
if (borderPath != null)
{
borderPath.SetFillType(Path.FillType.EvenOdd);
borderPath.SetFillType(Path.FillType.EvenOdd!);

var drawable = new PaintDrawable();

Expand All @@ -400,13 +401,15 @@ private static Drawable GetOverlayDrawable(

var lineDrawable = new PaintDrawable();
lineDrawable.Shape = new PathShape(line, viewSize.Width, viewSize.Height);
var paint = lineDrawable.Paint;
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Top;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
if (lineDrawable.Paint is { } paint)
{
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Top;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
}
drawables.Add(lineDrawable);
}
}
Expand All @@ -423,13 +426,15 @@ private static Drawable GetOverlayDrawable(

var lineDrawable = new PaintDrawable();
lineDrawable.Shape = new PathShape(line, viewSize.Width, viewSize.Height);
var paint = lineDrawable.Paint;
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Right;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
if (lineDrawable.Paint is { } paint)
{
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Right;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
}
drawables.Add(lineDrawable);
}
}
Expand All @@ -446,13 +451,15 @@ private static Drawable GetOverlayDrawable(

var lineDrawable = new PaintDrawable();
lineDrawable.Shape = new PathShape(line, viewSize.Width, viewSize.Height);
var paint = lineDrawable.Paint;
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Bottom;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
if (lineDrawable.Paint is { } paint)
{
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Bottom;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
}
drawables.Add(lineDrawable);
}
}
Expand All @@ -469,13 +476,15 @@ private static Drawable GetOverlayDrawable(

var lineDrawable = new PaintDrawable();
lineDrawable.Shape = new PathShape(line, viewSize.Width, viewSize.Height);
var paint = lineDrawable.Paint;
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Left;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
if (lineDrawable.Paint is { } paint)
{
paint.AntiAlias = false;
paint.Color = strokePaint.Color;
paint.SetShader(strokePaint.Shader);
paint.StrokeWidth = (float)physicalBorderThickness.Left;
paint.SetStyle(paintStyleStroke);
paint.Alpha = strokePaint.Alpha;
}
drawables.Add(lineDrawable);
}
}
Expand All @@ -487,8 +496,13 @@ private static Drawable GetOverlayDrawable(
return null;
}

private static IDisposable Dispatch(CoreDispatcher dispatcher, Func<CancellationToken, Task> handler)
private static IDisposable Dispatch(CoreDispatcher? dispatcher, Func<CancellationToken, Task> handler)
{
if (dispatcher is null)
{
return Disposable.Empty;
}

var cd = new CancellationDisposable();

// Execute the non-async part of the loading on the current thread.
Expand All @@ -507,8 +521,8 @@ private class LayoutState : IEquatable<LayoutState>
{
public readonly Windows.Foundation.Rect Area;
public readonly Brush Background;
public readonly ImageSource BackgroundImageSource;
public readonly Uri BackgroundImageSourceUri;
public readonly ImageSource? BackgroundImageSource;
public readonly Uri? BackgroundImageSourceUri;
public readonly Color? BackgroundColor;
public readonly Brush BorderBrush;
public readonly Color? BorderBrushColor;
Expand Down Expand Up @@ -541,7 +555,7 @@ public LayoutState(Windows.Foundation.Rect area, Brush background, Thickness bor
BackgroundFallbackColor = (Background as XamlCompositionBrushBase)?.FallbackColor;
}

public bool Equals(LayoutState other)
public bool Equals(LayoutState? other)
{
return other != null
&& other.Area == Area
Expand Down

0 comments on commit 371ec88

Please sign in to comment.