Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
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
25 changes: 25 additions & 0 deletions samples/XCT.Sample/Pages/TestCases/DrawingViewInExpanderPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<pages:BasePage
x:Class="Xamarin.CommunityToolkit.Sample.Pages.TestCases.DrawingViewInExpanderPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
xmlns:views="http://xamarin.com/schemas/2020/toolkit">

<ContentPage.Content>
<views:Expander>
<views:Expander.Header>
<Label Text="Click to expand" />
</views:Expander.Header>
<views:Expander.Content>
<views:DrawingView
HeightRequest="200"
BackgroundColor="Yellow"
ClearOnFinish="False"
DefaultLineColor="Red" />
</views:Expander.Content>
</views:Expander>

</ContentPage.Content>

</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.TestCases
{
public partial class DrawingViewInExpanderPage
{
public DrawingViewInExpanderPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ protected override IEnumerable<SectionModel> CreateItems() => new[]
typeof(SnackBarActionExceptionPage),
"SnackBar Action Exception",
"Exception in SnackBar's action doesn't crash the app."),

new SectionModel(
typeof(DrawingViewInExpanderPage),
"DrawingView in expander",
"DrawingView in Expander Page"),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Android.Content;
using Android.Graphics;
Expand Down Expand Up @@ -78,20 +79,23 @@ protected override void OnSizeChanged(int w, int h, int oldw, int oldh)

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

canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888!)!;
drawCanvas = new Canvas(canvasBitmap);
LoadLines();
canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888 ?? throw new NullReferenceException("Unable to create Bitmap config"));
if (canvasBitmap is not null)
{
drawCanvas = new Canvas(canvasBitmap);
LoadLines();
}
}

protected override void OnDraw(Canvas? canvas)
{
base.OnDraw(canvas);

if (canvas is not null)
if (canvas is not null && canvasBitmap is not null)
{
Draw(Element.Lines, canvas);

canvas.DrawBitmap(canvasBitmap!, 0, 0, canvasPaint);
canvas.DrawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.DrawPath(drawPath, drawPaint);
}
}
Expand Down Expand Up @@ -196,7 +200,7 @@ void LoadLines()
if (drawCanvas is null)
return;

drawCanvas.DrawColor(Element.BackgroundColor.ToAndroid(), PorterDuff.Mode.Clear!);
drawCanvas.DrawColor(Element.BackgroundColor.ToAndroid());
drawPath.Reset();
var lines = Element.Lines;
if (lines.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
SetNativeControl(vBox);
}

if (e.OldElement != null)
if (e.OldElement != null && area is not null)
{
area!.ExposeEvent -= OnDrawingAreaExposed;
area.ExposeEvent -= OnDrawingAreaExposed;
area.ButtonPressEvent -= OnMousePress;
area.ButtonReleaseEvent -= OnMouseRelease;
area.MotionNotifyEvent -= OnMouseMotion;
}

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

void OnDrawingAreaExposed(object source, ExposeEventArgs args)
{
if (area is null)
{
return;
}

Context ctx;
using (ctx = CairoHelper.Create(area!.GdkWindow))
using (ctx = CairoHelper.Create(area.GdkWindow))
{
ctx.SetSource(new SurfacePattern(surface));
ctx.Paint();
Expand Down Expand Up @@ -111,7 +116,7 @@ void OnMousePress(object source, ButtonPressEventArgs args)
};
previousPoint = point;
isDrawing = true;
area!.QueueDraw();
area?.QueueDraw();
if (!Element.MultiLineMode)
Element.Lines.Clear();
}
Expand All @@ -125,7 +130,7 @@ void OnMouseRelease(object source, ButtonReleaseEventArgs args)
using var ctx = new Context(surface);
DrawPoint(ctx, point);

area!.QueueDraw();
area?.QueueDraw();
if (currentLine != null)
{
Element.Lines.Add(currentLine);
Expand All @@ -146,19 +151,23 @@ void OnMouseMotion(object source, MotionNotifyEventArgs args)
using var ctx = new Context(surface);
DrawPoint(ctx, point);

area!.QueueDraw();
area?.QueueDraw();
}
}

void DrawPoint(Context ctx, PointD pointD)
{
ctx.SetSourceRGBA(currentLine!.LineColor.R, currentLine!.LineColor.G, currentLine!.LineColor.B, currentLine!.LineColor.A);
ctx.LineWidth = currentLine.LineWidth;
if (currentLine is not null)
{
ctx.SetSourceRGBA(currentLine.LineColor.R, currentLine.LineColor.G, currentLine.LineColor.B, currentLine.LineColor.A);
ctx.LineWidth = currentLine.LineWidth;
currentLine.Points.Add(new Point(pointD.X, pointD.Y));
}

ctx.MoveTo(previousPoint);
previousPoint = pointD;
ctx.LineTo(pointD);
ctx.Stroke();
currentLine.Points.Add(new Point(pointD.X, pointD.Y));
ctx.Stroke();
}

void LoadPoints(ImageSurface imageSurface)
Expand All @@ -177,7 +186,7 @@ void LoadPoints(ImageSurface imageSurface)
foreach (var stylusPoint in stylusPoints)
DrawPoint(ctx, stylusPoint);

area!.QueueDraw();
area?.QueueDraw();
}
}
}
Expand All @@ -190,12 +199,16 @@ protected override void Dispose(bool disposing)

if (disposing)
{
area!.ExposeEvent -= OnDrawingAreaExposed;
area.ButtonPressEvent -= OnMousePress;
area.ButtonReleaseEvent -= OnMouseRelease;
area.MotionNotifyEvent -= OnMouseMotion;
area.Dispose();
surface!.Dispose();
if (area is not null)
{
area.ExposeEvent -= OnDrawingAreaExposed;
area.ButtonPressEvent -= OnMousePress;
area.ButtonReleaseEvent -= OnMouseRelease;
area.MotionNotifyEvent -= OnMouseMotion;
area.Dispose();
}

surface?.Dispose();
if (Element != null)
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;
}
Expand All @@ -205,4 +218,4 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Xamarin.CommunityToolkit.UI.Views
/// </summary>
public class DrawingViewRenderer : ViewRenderer<DrawingView, UIView>
{
readonly UIBezierPath currentPath;
bool disposed;
UIBezierPath currentPath;
UIColor? lineColor;
CGPoint previousPoint;
Line? currentLine;
Expand Down Expand Up @@ -61,11 +61,11 @@ public override void TouchesBegan(NSSet touches, UIEvent? evt)
var touch = (UITouch)touches.AnyObject;
previousPoint = touch.PreviousLocationInView(this);
currentPath.MoveTo(previousPoint);
currentLine = new Line()
currentLine = new Line
{
Points = new ObservableCollection<Point>()
Points = new ObservableCollection<Point>
{
new Point(previousPoint.X, previousPoint.Y)
new (previousPoint.X, previousPoint.Y)
}
};

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

public override void Draw(CGRect rect)
{
lineColor!.SetStroke();
lineColor?.SetStroke();
currentPath.Stroke();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Xamarin.CommunityToolkit.UI.Views
/// </summary>
public class DrawingViewRenderer : ViewRenderer<DrawingView, NSView>
{
readonly NSBezierPath currentPath;
bool disposed;
NSBezierPath currentPath;
NSColor? lineColor;
CGPoint previousPoint;
Line? currentLine;
Expand Down Expand Up @@ -58,11 +58,11 @@ public override void MouseDown(NSEvent theEvent)

previousPoint = theEvent.LocationInWindow;
currentPath.MoveTo(previousPoint);
currentLine = new Line()
currentLine = new Line
{
Points = new ObservableCollection<Point>()
Points = new ObservableCollection<Point>
{
new Point(previousPoint.X, previousPoint.Y)
new (previousPoint.X, previousPoint.Y)
}
};

Expand Down Expand Up @@ -94,7 +94,7 @@ public override void MouseDragged(NSEvent theEvent)
public override void DrawRect(CGRect dirtyRect)
{
base.DrawRect(dirtyRect);
lineColor!.SetStroke();
lineColor?.SetStroke();
currentPath.Stroke();
}

Expand Down Expand Up @@ -124,8 +124,8 @@ void UpdatePath(Line line)
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;

var smoothedPoints = line.EnableSmoothedPath
? SmoothedPathWithGranularity(line.Points, line.Granularity)
: new ObservableCollection<Point>(line.Points);
? SmoothedPathWithGranularity(line.Points, line.Granularity)
: new ObservableCollection<Point>(line.Points);

line.Points.Clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
SetNativeControl(canvasView);
}

if (e.OldElement != null)
if (e.OldElement != null && canvasView is not null)
{
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
canvasView.PaintSurface -= OnPaintSurface;
}

if (e.NewElement != null)
if (e.NewElement != null && canvasView is not null)
{
canvasView!.PaintSurface += OnPaintSurface;
canvasView.PaintSurface += OnPaintSurface;
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
Expand All @@ -56,9 +56,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == DrawingView.LinesProperty.PropertyName)
if (e.PropertyName == DrawingView.LinesProperty.PropertyName && canvasView is not null)
{
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
LoadPoints();
canvasView.EvasCanvas.AddEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
}
Expand All @@ -68,10 +68,10 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE

void MouseMove()
{
if (isDrawing)
if (isDrawing && canvasView is not null)
{
var point = canvasView!.EvasCanvas.Pointer;
currentLine!.Points.Add(new Point(point.X, point.Y));
var point = canvasView.EvasCanvas.Pointer;
currentLine?.Points.Add(new Point(point.X, point.Y));
canvasView.Invalidate();
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ void LoadPoints()
if (Element == null)
return;

canvasView!.Invalidate();
canvasView?.Invalidate();
}

void DrawPath(SKCanvas canvas)
Expand Down Expand Up @@ -150,7 +150,11 @@ protected override void Dispose(bool disposing)
if (Element != null)
{
Element.Lines.CollectionChanged -= OnLinesCollectionChanged;
canvasView!.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
}

if (canvasView is not null)
{
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseDown, MouseDown);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseUp, MouseUp);
canvasView.EvasCanvas.DeleteEventAction(EvasObjectCallbackType.MouseMove, MouseMove);
canvasView.PaintSurface -= OnPaintSurface;
Expand Down
Loading