Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 5bd4b5b

Browse files
Nullable fixes in DrawingView (#1817)
* Nullable fixes in DrawingView * Update DrawingViewRenderer.gtk.cs
1 parent 2803cb4 commit 5bd4b5b

File tree

9 files changed

+134
-62
lines changed

9 files changed

+134
-62
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.TestCases.DrawingViewInExpanderPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
7+
xmlns:views="http://xamarin.com/schemas/2020/toolkit">
8+
9+
<ContentPage.Content>
10+
<views:Expander>
11+
<views:Expander.Header>
12+
<Label Text="Click to expand" />
13+
</views:Expander.Header>
14+
<views:Expander.Content>
15+
<views:DrawingView
16+
HeightRequest="200"
17+
BackgroundColor="Yellow"
18+
ClearOnFinish="False"
19+
DefaultLineColor="Red" />
20+
</views:Expander.Content>
21+
</views:Expander>
22+
23+
</ContentPage.Content>
24+
25+
</pages:BasePage>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Xamarin.CommunityToolkit.Sample.Pages.TestCases
2+
{
3+
public partial class DrawingViewInExpanderPage
4+
{
5+
public DrawingViewInExpanderPage() => InitializeComponent();
6+
}
7+
}

samples/XCT.Sample/ViewModels/TestCases/TestCasesGalleryViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ protected override IEnumerable<SectionModel> CreateItems() => new[]
4848
typeof(SnackBarActionExceptionPage),
4949
"SnackBar Action Exception",
5050
"Exception in SnackBar's action doesn't crash the app."),
51+
52+
new SectionModel(
53+
typeof(DrawingViewInExpanderPage),
54+
"DrawingView in expander",
55+
"DrawingView in Expander Page"),
5156
};
5257
}
5358
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.android.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using Android.Content;
45
using Android.Graphics;
@@ -78,20 +79,23 @@ protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
7879

7980
base.OnSizeChanged(w, h, oldw, oldh);
8081

81-
canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888!)!;
82-
drawCanvas = new Canvas(canvasBitmap);
83-
LoadLines();
82+
canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888 ?? throw new NullReferenceException("Unable to create Bitmap config"));
83+
if (canvasBitmap is not null)
84+
{
85+
drawCanvas = new Canvas(canvasBitmap);
86+
LoadLines();
87+
}
8488
}
8589

8690
protected override void OnDraw(Canvas? canvas)
8791
{
8892
base.OnDraw(canvas);
8993

90-
if (canvas is not null)
94+
if (canvas is not null && canvasBitmap is not null)
9195
{
9296
Draw(Element.Lines, canvas);
9397

94-
canvas.DrawBitmap(canvasBitmap!, 0, 0, canvasPaint);
98+
canvas.DrawBitmap(canvasBitmap, 0, 0, canvasPaint);
9599
canvas.DrawPath(drawPath, drawPaint);
96100
}
97101
}
@@ -196,7 +200,7 @@ void LoadLines()
196200
if (drawCanvas is null)
197201
return;
198202

199-
drawCanvas.DrawColor(Element.BackgroundColor.ToAndroid(), PorterDuff.Mode.Clear!);
203+
drawCanvas.DrawColor(Element.BackgroundColor.ToAndroid());
200204
drawPath.Reset();
201205
var lines = Element.Lines;
202206
if (lines.Count > 0)

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.gtk.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
5050
SetNativeControl(vBox);
5151
}
5252

53-
if (e.OldElement != null)
53+
if (e.OldElement != null && area is not null)
5454
{
55-
area!.ExposeEvent -= OnDrawingAreaExposed;
55+
area.ExposeEvent -= OnDrawingAreaExposed;
5656
area.ButtonPressEvent -= OnMousePress;
5757
area.ButtonReleaseEvent -= OnMouseRelease;
5858
area.MotionNotifyEvent -= OnMouseMotion;
5959
}
6060

61-
if (e.NewElement != null)
61+
if (e.NewElement != null && area is not null)
6262
{
63-
area!.ExposeEvent += OnDrawingAreaExposed;
63+
area.ExposeEvent += OnDrawingAreaExposed;
6464
area.ButtonPressEvent += OnMousePress;
6565
area.ButtonReleaseEvent += OnMouseRelease;
6666
area.MotionNotifyEvent += OnMouseMotion;
@@ -82,8 +82,13 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
8282

8383
void OnDrawingAreaExposed(object source, ExposeEventArgs args)
8484
{
85+
if (area is null)
86+
{
87+
return;
88+
}
89+
8590
Context ctx;
86-
using (ctx = CairoHelper.Create(area!.GdkWindow))
91+
using (ctx = CairoHelper.Create(area.GdkWindow))
8792
{
8893
ctx.SetSource(new SurfacePattern(surface));
8994
ctx.Paint();
@@ -111,7 +116,7 @@ void OnMousePress(object source, ButtonPressEventArgs args)
111116
};
112117
previousPoint = point;
113118
isDrawing = true;
114-
area!.QueueDraw();
119+
area?.QueueDraw();
115120
if (!Element.MultiLineMode)
116121
Element.Lines.Clear();
117122
}
@@ -125,7 +130,7 @@ void OnMouseRelease(object source, ButtonReleaseEventArgs args)
125130
using var ctx = new Context(surface);
126131
DrawPoint(ctx, point);
127132

128-
area!.QueueDraw();
133+
area?.QueueDraw();
129134
if (currentLine != null)
130135
{
131136
Element.Lines.Add(currentLine);
@@ -146,19 +151,23 @@ void OnMouseMotion(object source, MotionNotifyEventArgs args)
146151
using var ctx = new Context(surface);
147152
DrawPoint(ctx, point);
148153

149-
area!.QueueDraw();
154+
area?.QueueDraw();
150155
}
151156
}
152157

153158
void DrawPoint(Context ctx, PointD pointD)
154159
{
155-
ctx.SetSourceRGBA(currentLine!.LineColor.R, currentLine!.LineColor.G, currentLine!.LineColor.B, currentLine!.LineColor.A);
156-
ctx.LineWidth = currentLine.LineWidth;
160+
if (currentLine is not null)
161+
{
162+
ctx.SetSourceRGBA(currentLine.LineColor.R, currentLine.LineColor.G, currentLine.LineColor.B, currentLine.LineColor.A);
163+
ctx.LineWidth = currentLine.LineWidth;
164+
currentLine.Points.Add(new Point(pointD.X, pointD.Y));
165+
}
166+
157167
ctx.MoveTo(previousPoint);
158168
previousPoint = pointD;
159169
ctx.LineTo(pointD);
160-
ctx.Stroke();
161-
currentLine.Points.Add(new Point(pointD.X, pointD.Y));
170+
ctx.Stroke();
162171
}
163172

164173
void LoadPoints(ImageSurface imageSurface)
@@ -177,7 +186,7 @@ void LoadPoints(ImageSurface imageSurface)
177186
foreach (var stylusPoint in stylusPoints)
178187
DrawPoint(ctx, stylusPoint);
179188

180-
area!.QueueDraw();
189+
area?.QueueDraw();
181190
}
182191
}
183192
}
@@ -190,12 +199,16 @@ protected override void Dispose(bool disposing)
190199

191200
if (disposing)
192201
{
193-
area!.ExposeEvent -= OnDrawingAreaExposed;
194-
area.ButtonPressEvent -= OnMousePress;
195-
area.ButtonReleaseEvent -= OnMouseRelease;
196-
area.MotionNotifyEvent -= OnMouseMotion;
197-
area.Dispose();
198-
surface!.Dispose();
202+
if (area is not null)
203+
{
204+
area.ExposeEvent -= OnDrawingAreaExposed;
205+
area.ButtonPressEvent -= OnMousePress;
206+
area.ButtonReleaseEvent -= OnMouseRelease;
207+
area.MotionNotifyEvent -= OnMouseMotion;
208+
area.Dispose();
209+
}
210+
211+
surface?.Dispose();
199212
if (Element != null)
200213
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;
201214
}
@@ -205,4 +218,4 @@ protected override void Dispose(bool disposing)
205218
base.Dispose(disposing);
206219
}
207220
}
208-
}
221+
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.ios.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace Xamarin.CommunityToolkit.UI.Views
1818
/// </summary>
1919
public class DrawingViewRenderer : ViewRenderer<DrawingView, UIView>
2020
{
21+
readonly UIBezierPath currentPath;
2122
bool disposed;
22-
UIBezierPath currentPath;
2323
UIColor? lineColor;
2424
CGPoint previousPoint;
2525
Line? currentLine;
@@ -61,11 +61,11 @@ public override void TouchesBegan(NSSet touches, UIEvent? evt)
6161
var touch = (UITouch)touches.AnyObject;
6262
previousPoint = touch.PreviousLocationInView(this);
6363
currentPath.MoveTo(previousPoint);
64-
currentLine = new Line()
64+
currentLine = new Line
6565
{
66-
Points = new ObservableCollection<Point>()
66+
Points = new ObservableCollection<Point>
6767
{
68-
new Point(previousPoint.X, previousPoint.Y)
68+
new (previousPoint.X, previousPoint.Y)
6969
}
7070
};
7171

@@ -105,7 +105,7 @@ public override void TouchesCancelled(NSSet touches, UIEvent? evt)
105105

106106
public override void Draw(CGRect rect)
107107
{
108-
lineColor!.SetStroke();
108+
lineColor?.SetStroke();
109109
currentPath.Stroke();
110110
}
111111

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.macos.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace Xamarin.CommunityToolkit.UI.Views
1717
/// </summary>
1818
public class DrawingViewRenderer : ViewRenderer<DrawingView, NSView>
1919
{
20+
readonly NSBezierPath currentPath;
2021
bool disposed;
21-
NSBezierPath currentPath;
2222
NSColor? lineColor;
2323
CGPoint previousPoint;
2424
Line? currentLine;
@@ -58,11 +58,11 @@ public override void MouseDown(NSEvent theEvent)
5858

5959
previousPoint = theEvent.LocationInWindow;
6060
currentPath.MoveTo(previousPoint);
61-
currentLine = new Line()
61+
currentLine = new Line
6262
{
63-
Points = new ObservableCollection<Point>()
63+
Points = new ObservableCollection<Point>
6464
{
65-
new Point(previousPoint.X, previousPoint.Y)
65+
new (previousPoint.X, previousPoint.Y)
6666
}
6767
};
6868

@@ -94,7 +94,7 @@ public override void MouseDragged(NSEvent theEvent)
9494
public override void DrawRect(CGRect dirtyRect)
9595
{
9696
base.DrawRect(dirtyRect);
97-
lineColor!.SetStroke();
97+
lineColor?.SetStroke();
9898
currentPath.Stroke();
9999
}
100100

@@ -124,8 +124,8 @@ void UpdatePath(Line line)
124124
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;
125125

126126
var smoothedPoints = line.EnableSmoothedPath
127-
? SmoothedPathWithGranularity(line.Points, line.Granularity)
128-
: new ObservableCollection<Point>(line.Points);
127+
? SmoothedPathWithGranularity(line.Points, line.Granularity)
128+
: new ObservableCollection<Point>(line.Points);
129129

130130
line.Points.Clear();
131131

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.tizen.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
3636
SetNativeControl(canvasView);
3737
}
3838

39-
if (e.OldElement != null)
39+
if (e.OldElement != null && canvasView is not null)
4040
{
41-
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
41+
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
4242
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
4343
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
4444
canvasView.PaintSurface -= OnPaintSurface;
4545
}
4646

47-
if (e.NewElement != null)
47+
if (e.NewElement != null && canvasView is not null)
4848
{
49-
canvasView!.PaintSurface += OnPaintSurface;
49+
canvasView.PaintSurface += OnPaintSurface;
5050
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
5151
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
5252
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
@@ -56,9 +56,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
5656
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
5757
{
5858
base.OnElementPropertyChanged(sender, e);
59-
if (e.PropertyName == DrawingView.LinesProperty.PropertyName)
59+
if (e.PropertyName == DrawingView.LinesProperty.PropertyName && canvasView is not null)
6060
{
61-
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
61+
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
6262
LoadPoints();
6363
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
6464
}
@@ -68,10 +68,10 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
6868

6969
void MouseMove()
7070
{
71-
if (isDrawing)
71+
if (isDrawing && canvasView is not null)
7272
{
73-
var point = canvasView!.EvasCanvas.Pointer;
74-
currentLine!.Points.Add(new Point(point.X, point.Y));
73+
var point = canvasView.EvasCanvas.Pointer;
74+
currentLine?.Points.Add(new Point(point.X, point.Y));
7575
canvasView.Invalidate();
7676
}
7777
}
@@ -111,7 +111,7 @@ void LoadPoints()
111111
if (Element == null)
112112
return;
113113

114-
canvasView!.Invalidate();
114+
canvasView?.Invalidate();
115115
}
116116

117117
void DrawPath(SKCanvas canvas)
@@ -150,7 +150,11 @@ protected override void Dispose(bool disposing)
150150
if (Element != null)
151151
{
152152
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;
153-
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
153+
}
154+
155+
if (canvasView is not null)
156+
{
157+
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
154158
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
155159
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
156160
canvasView.PaintSurface -= OnPaintSurface;

0 commit comments

Comments
 (0)