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

Commit 6e93253

Browse files
Fix Android crashes if Bitmap is small, Fix iOS drawing in scrollView (#1421)
1 parent 1be322f commit 6e93253

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
6565

6666
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
6767
{
68+
const int minW = 1;
69+
const int minH = 1;
70+
w = w < minW ? minW : w;
71+
h = h < minH ? minH : h;
6872
base.OnSizeChanged(w, h, oldw, oldh);
6973

7074
canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888!)!;

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
4545

4646
public override void TouchesBegan(NSSet touches, UIEvent? evt)
4747
{
48+
SetParentTouches(false);
49+
4850
Element.Points.CollectionChanged -= OnPointsCollectionChanged;
4951
Element.Points.Clear();
5052
currentPath.RemoveAllPoints();
@@ -65,16 +67,6 @@ public override void TouchesMoved(NSSet touches, UIEvent? evt)
6567
AddPointToPath(currentPoint);
6668
}
6769

68-
void AddPointToPath(CGPoint currentPoint)
69-
{
70-
currentPath.AddLineTo(currentPoint);
71-
SetNeedsDisplay();
72-
Element.Points.CollectionChanged -= OnPointsCollectionChanged;
73-
var point = currentPoint.ToPoint();
74-
Element.Points.Add(point);
75-
Element.Points.CollectionChanged += OnPointsCollectionChanged;
76-
}
77-
7870
public override void TouchesEnded(NSSet touches, UIEvent? evt)
7971
{
8072
UpdatePath();
@@ -86,16 +78,32 @@ public override void TouchesEnded(NSSet touches, UIEvent? evt)
8678

8779
if (Element.ClearOnFinish)
8880
Element.Points.Clear();
81+
82+
SetParentTouches(true);
8983
}
9084

91-
public override void TouchesCancelled(NSSet touches, UIEvent? evt) => InvokeOnMainThread(SetNeedsDisplay);
85+
public override void TouchesCancelled(NSSet touches, UIEvent? evt)
86+
{
87+
InvokeOnMainThread(SetNeedsDisplay);
88+
SetParentTouches(true);
89+
}
9290

9391
public override void Draw(CGRect rect)
9492
{
9593
lineColor!.SetStroke();
9694
currentPath.Stroke();
9795
}
9896

97+
void AddPointToPath(CGPoint currentPoint)
98+
{
99+
currentPath.AddLineTo(currentPoint);
100+
SetNeedsDisplay();
101+
Element.Points.CollectionChanged -= OnPointsCollectionChanged;
102+
var point = currentPoint.ToPoint();
103+
Element.Points.Add(point);
104+
Element.Points.CollectionChanged += OnPointsCollectionChanged;
105+
}
106+
99107
void LoadPoints()
100108
{
101109
var stylusPoints = Element.Points.Select(point => new CGPoint(point.X, point.Y)).ToList();
@@ -211,5 +219,18 @@ protected override void Dispose(bool disposing)
211219

212220
base.Dispose(disposing);
213221
}
222+
223+
void SetParentTouches(bool enabled)
224+
{
225+
var parent = Superview;
226+
227+
while (parent != null)
228+
{
229+
if (parent.GetType() == typeof(ScrollViewRenderer))
230+
((ScrollViewRenderer)parent).ScrollEnabled = enabled;
231+
232+
parent = parent.Superview;
233+
}
234+
}
214235
}
215236
}

0 commit comments

Comments
 (0)