From 68a712a6356b70e9152cb58f412e4bd0d978c678 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 06:38:37 +0100 Subject: [PATCH 01/13] Initial attempt at exposing the ability to export the full image size --- .../Pages/Views/DrawingViewPage.xaml.cs | 5 +-- .../ViewModels/Views/DrawingViewViewModel.cs | 2 +- .../Interfaces/IDrawingLine.shared.cs | 5 +-- .../Interfaces/IDrawingView.shared.cs | 19 +++++++++++- .../Views/DrawingView/DrawingLine.shared.cs | 9 ++++-- .../Service/DrawingViewService.android.cs | 6 ++-- .../Service/DrawingViewService.macios.cs | 31 +++++++++++-------- .../Service/DrawingViewService.net.cs | 6 ++-- .../Views/DrawingView/DrawingView.shared.cs | 17 +++++----- 9 files changed, 67 insertions(+), 33 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs index 1dfdf1d52e..ab449a84c3 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs @@ -31,7 +31,7 @@ void LoadPointsButtonClicked(object sender, EventArgs e) async void GetCurrentDrawingViewImageClicked(object sender, EventArgs e) { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - var stream = await DrawingViewControl.GetImageStream(GestureImage.Width, GestureImage.Height, cts.Token); + var stream = await DrawingViewControl.GetImageStream(GestureImage.Width, GestureImage.Height, ImageOutputOption.Lines, cts.Token); GestureImage.Source = ImageSource.FromStream(() => stream); } @@ -51,6 +51,7 @@ async Task DrawImage(IEnumerable lines, CancellationToken token) var stream = await DrawingView.GetImageStream(drawingLines, new Size(points.Max(x => x.X) - points.Min(x => x.X), points.Max(x => x.Y) - points.Min(x => x.Y)), Colors.Gray, + this.DrawingViewControl.Bounds.Size, token); GestureImage.Source = ImageSource.FromStream(() => stream); @@ -79,7 +80,7 @@ async void OnDrawingLineCompleted(object sender, DrawingLineCompletedEventArgs e var height = GetSide(GestureImage.Height); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - var stream = await e.LastDrawingLine.GetImageStream(width, height, Colors.Gray.AsPaint(), cts.Token); + var stream = await e.LastDrawingLine.GetImageStream(width, height, Colors.Gray.AsPaint(), this.DrawingViewControl.Bounds.Size, cts.Token); GestureImage.Source = ImageSource.FromStream(() => stream); } diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs index c5360cb545..97b61b3e58 100644 --- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs @@ -72,7 +72,7 @@ async Task Save(CancellationToken cancellationToken) { try { - await using var stream = await DrawingView.GetImageStream(Lines, new Size(1920, 1080), Brush.Blue, cancellationToken); + await using var stream = await DrawingView.GetImageStream(Lines, new Size(1920, 1080), Brush.Blue, new Size(300, 200), cancellationToken); await Permissions.RequestAsync().WaitAsync(cancellationToken); await Permissions.RequestAsync().WaitAsync(cancellationToken); diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs index 6fde7f4d38..eb293496c9 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs @@ -38,7 +38,8 @@ public interface IDrawingLine /// Desired width of the image that is returned. /// Desired height of the image that is returned. /// Background of the generated image. - /// containing the data of the requested image with data that's currently on the . + /// /// . - ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, CancellationToken token = default); + /// /// containing the data of the requested image with data that's currently on the . + ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, Size? canvaSize = null, CancellationToken token = default); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs index bef0059b4b..1ab4cf1044 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs @@ -45,9 +45,10 @@ public interface IDrawingView : IView /// /// Desired width of the image that is returned. The image will be resized proportionally. /// Desired height of the image that is returned. The image will be resized proportionally. + /// The to determine the bounds and the contents of the resulting image. /// . /// containing the data of the requested image with data that's currently on the . - ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, CancellationToken token = default); + ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, ImageOutputOption imageOutputOption = ImageOutputOption.Lines, CancellationToken token = default); /// /// Clears the that are currently drawn on the . @@ -76,4 +77,20 @@ public interface IDrawingView : IView /// /// Last drawing line void OnDrawingLineCompleted(IDrawingLine lastDrawingLine); +} + +/// +/// Enumeration of the options available when generating an image stream using the DrawingView. +/// +public enum ImageOutputOption +{ + /// + /// Outputs the area covered by the top-left to the bottom-right most points. + /// + Lines, + + /// + /// Outputs the full area displayed within the drawing view. + /// + FullCanvas } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs index bd4c0670f3..80d1c02e9f 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs @@ -46,6 +46,7 @@ public int Granularity /// The desired line width to be used in the generated image. /// The desired color of the line to be used in the generated image. /// Background of the generated image. + /// /// /// containing the data of the requested image with data that's provided through the parameter. public static ValueTask GetImageStream(IEnumerable points, @@ -53,9 +54,10 @@ public static ValueTask GetImageStream(IEnumerable points, float lineWidth, Color strokeColor, Paint background, + Size? canvaSize = null, CancellationToken token = default) { - return DrawingViewService.GetImageStream(points.ToList(), imageSize, lineWidth, strokeColor, background, token); + return DrawingViewService.GetImageStream(points.ToList(), imageSize, lineWidth, strokeColor, background, canvaSize, token); } /// @@ -64,10 +66,11 @@ public static ValueTask GetImageStream(IEnumerable points, /// Desired width of the image that is returned. /// Desired height of the image that is returned. /// Background of the generated image. + /// /// /// containing the data of the requested image with data that's currently on the . - public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, CancellationToken token = default) + public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default) { - return DrawingViewService.GetImageStream(Points.ToList(), new Size(imageSizeWidth, imageSizeHeight), LineWidth, LineColor, background, token); + return DrawingViewService.GetImageStream(Points.ToList(), new Size(imageSizeWidth, imageSizeHeight), LineWidth, LineColor, background, canvasSize, token); } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs index 80357f4a7e..35fa9b4a92 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs @@ -20,8 +20,9 @@ public static class DrawingViewService /// Maximum image size. The image will be resized proportionally. /// Image background /// + /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); @@ -38,9 +39,10 @@ public static ValueTask GetImageStream(IList lines, Size i /// Line Width /// Line color /// Image background + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs index c59c9f7418..71e5a83465 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs @@ -15,13 +15,14 @@ public static class DrawingViewService /// Drawing lines /// Maximum image size. The image will be resized proportionally. /// Image background + /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetUIImageForLines(lines, background); + var image = GetUIImageForLines(lines, background, canvasSize); if (image is null) { return ValueTask.FromResult(Stream.Null); @@ -43,13 +44,14 @@ public static ValueTask GetImageStream(IList lines, Size i /// Line Width /// Line color /// Image background + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetUIImageForPoints(points, lineWidth, strokeColor, background); + var image = GetUIImageForPoints(points, lineWidth, strokeColor, background, canvasSize); if (image is null) { return ValueTask.FromResult(Stream.Null); @@ -57,7 +59,7 @@ public static ValueTask GetImageStream(IList points, Size imageS token.ThrowIfCancellationRequested(); - var imageAsPng = GetMaximumUIImage(image, imageSize.Width, imageSize.Height) + var imageAsPng = GetMaximumUIImage(image, canvasSize?.Width ?? imageSize.Width, canvasSize?.Height ?? imageSize.Height) .AsPNG() ?? throw new InvalidOperationException("Unable to convert image to PNG"); return ValueTask.FromResult(imageAsPng.AsStream()); @@ -66,15 +68,16 @@ public static ValueTask GetImageStream(IList points, Size imageS static UIImage? GetUIImageForPoints(ICollection points, NFloat lineWidth, Color strokeColor, - Paint? background) + Paint? background, + Size? canvasSize = null) { return GetUIImage(points, (context, offset) => { DrawStrokes(context, points.ToList(), lineWidth, strokeColor, offset); - }, background, lineWidth); + }, background, lineWidth, canvasSize); } - static UIImage? GetUIImageForLines(IList lines, in Paint? background) + static UIImage? GetUIImageForLines(IList lines, in Paint? background, Size? canvasSize = null) { var points = lines.SelectMany(x => x.Points).ToList(); var drawingLineWithLargestLineWidth = lines.MaxBy(x => x.LineWidth); @@ -90,16 +93,16 @@ public static ValueTask GetImageStream(IList points, Size imageS { DrawStrokes(context, line.Points, line.LineWidth, line.LineColor, offset); } - }, background, drawingLineWithLargestLineWidth.LineWidth); + }, background, drawingLineWithLargestLineWidth.LineWidth, canvasSize); } - static UIImage? GetUIImage(ICollection points, Action drawStrokes, Paint? background, NFloat maxLineWidth) + static UIImage? GetUIImage(ICollection points, Action drawStrokes, Paint? background, NFloat maxLineWidth, Size? canvasSize) { const int minSize = 1; var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; if (drawingWidth < minSize || drawingHeight < minSize) { @@ -113,7 +116,9 @@ public static ValueTask GetImageStream(IList points, Size imageS DrawBackground(context, background, imageSize); - drawStrokes(context, new Size(minPointX, minPointY)); + var offset = canvasSize is null ? new Size(minPointX, minPointY) : Size.Zero; + + drawStrokes(context, offset); var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs index 2ef5b849f3..47b4815892 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs @@ -13,9 +13,10 @@ public static class DrawingViewService /// Line Width /// Line color /// Image background + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); @@ -27,9 +28,10 @@ public static ValueTask GetImageStream(IList points, Size imageS /// Drawing lines /// Maximum image size. The image will be resized proportionally. /// Image background + /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); diff --git a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs index 5a5c8e3341..ed69f3c122 100644 --- a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs +++ b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs @@ -215,25 +215,28 @@ public Action? DrawAction /// Retrieves a containing an image of the collection of that is provided as a parameter. /// /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. The image will be resized proportionally. + /// The desired dimensions of the generated image. The image will be resized proportionally. /// Background of the generated image. If color is null, default background color is used. + /// The size of the canvas where the lines fit. /// /// containing the data of the requested image with data that's provided through the parameter. public static ValueTask GetImageStream(IEnumerable lines, - Size imageSize, + Size desiredSize, Brush? background, + Size? canvasSize = null, CancellationToken token = default) => - DrawingViewService.GetImageStream(lines.ToList(), imageSize, background, token); + DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, canvasSize, token); /// /// Retrieves a containing an image of the that are currently drawn on the . /// - /// Desired width of the image that is returned. The image will be resized proportionally. - /// Desired height of the image that is returned. The image will be resized proportionally. + /// Desired width of the image that is returned. The image will be resized proportionally. + /// Desired height of the image that is returned. The image will be resized proportionally. + /// The to determine the bounds and the contents of the resulting image. /// /// containing the data of the requested image with data that's currently on the . - public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, CancellationToken token = default) => - DrawingViewService.GetImageStream(Lines.ToList(), new Size(imageSizeWidth, imageSizeHeight), Background, token); + public ValueTask GetImageStream(double desiredWidth, double desiredHeight, ImageOutputOption imageOutputOption = ImageOutputOption.Lines, CancellationToken token = default) => + DrawingViewService.GetImageStream(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background, imageOutputOption == ImageOutputOption.Lines ? null : new Size(this.Width, this.Height), token); /// /// Clears the collection. From 18196814a6dc07f2c4838661b0a5021f3516aa04 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:41:35 +0100 Subject: [PATCH 02/13] Opt for overload methods rather than parameter defaults to avoid breaking changes --- .../Pages/Views/DrawingViewPage.xaml.cs | 2 +- .../Interfaces/IDrawingLine.shared.cs | 25 +++++++-- .../Interfaces/IDrawingView.shared.cs | 19 +++++-- .../Views/DrawingView/DrawingLine.shared.cs | 56 +++++++++++-------- .../Service/DrawingViewService.android.cs | 26 ++++++--- .../Service/DrawingViewService.macios.cs | 18 ++++-- .../Service/DrawingViewService.net.cs | 22 +++++--- .../Service/DrawingViewService.shared.cs | 28 ++++++++++ .../Service/DrawingViewService.tizen.cs | 16 +++++- .../Service/DrawingViewService.windows.cs | 16 +++++- .../Mocks/MockDrawingViewHandler.cs | 2 +- .../Views/DrawingView/DrawingView.shared.cs | 43 +++++++++----- 12 files changed, 197 insertions(+), 76 deletions(-) create mode 100644 src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs index ab449a84c3..e4b1c910e9 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs @@ -31,7 +31,7 @@ void LoadPointsButtonClicked(object sender, EventArgs e) async void GetCurrentDrawingViewImageClicked(object sender, EventArgs e) { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - var stream = await DrawingViewControl.GetImageStream(GestureImage.Width, GestureImage.Height, ImageOutputOption.Lines, cts.Token); + var stream = await DrawingViewControl.GetImageStream(GestureImage.Width, GestureImage.Height, DrawingViewOutputOption.Lines, cts.Token); GestureImage.Source = ImageSource.FromStream(() => stream); } diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs index eb293496c9..4e62a78585 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingLine.shared.cs @@ -35,11 +35,26 @@ public interface IDrawingLine /// /// Retrieves a containing an image of this line, based on the data. /// - /// Desired width of the image that is returned. - /// Desired height of the image that is returned. + /// Desired width of the image that is returned. + /// Desired height of the image that is returned. /// Background of the generated image. - /// /// . - /// /// containing the data of the requested image with data that's currently on the . - ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, Size? canvaSize = null, CancellationToken token = default); + /// containing the data of the requested image with data that's currently on the . + ValueTask GetImageStream(double desiredSizeWidth, double desiredSizeHeight, Paint background, CancellationToken token = default) => + GetImageStream(desiredSizeWidth, desiredSizeHeight, background, null, token); + + /// + /// Retrieves a containing an image of this line, based on the data. + /// + /// Desired width of the image that is returned. + /// Desired height of the image that is returned. + /// Background of the generated image. + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of this line inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// + /// . + /// containing the data of the requested image with data that's currently on the . + ValueTask GetImageStream(double desiredSizeWidth, double desiredSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs index 1ab4cf1044..ed2ed7db8a 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs @@ -43,12 +43,21 @@ public interface IDrawingView : IView /// /// Retrieves a containing an image of the that are currently drawn on the . /// - /// Desired width of the image that is returned. The image will be resized proportionally. - /// Desired height of the image that is returned. The image will be resized proportionally. - /// The to determine the bounds and the contents of the resulting image. + /// Desired width of the image that is returned. The image will be resized proportionally. + /// Desired height of the image that is returned. The image will be resized proportionally. /// . /// containing the data of the requested image with data that's currently on the . - ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, ImageOutputOption imageOutputOption = ImageOutputOption.Lines, CancellationToken token = default); + ValueTask GetImageStream(double desiredWidth, double desiredHeight, CancellationToken token = default) => GetImageStream(desiredWidth, desiredHeight, DrawingViewOutputOption.Lines, token); + + /// + /// Retrieves a containing an image of the that are currently drawn on the . + /// + /// Desired width of the image that is returned. The image will be resized proportionally. + /// Desired height of the image that is returned. The image will be resized proportionally. + /// The to determine the bounds and the contents of the resulting image. + /// . + /// containing the data of the requested image with data that's currently on the . + ValueTask GetImageStream(double desiredWidth, double desiredHeight, DrawingViewOutputOption imageOutputOption, CancellationToken token = default); /// /// Clears the that are currently drawn on the . @@ -82,7 +91,7 @@ public interface IDrawingView : IView /// /// Enumeration of the options available when generating an image stream using the DrawingView. /// -public enum ImageOutputOption +public enum DrawingViewOutputOption { /// /// Outputs the area covered by the top-left to the bottom-right most points. diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs index 80d1c02e9f..85723a197a 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs @@ -42,35 +42,47 @@ public int Granularity /// Retrieves a containing an image of the collection of that is provided as a parameter. /// /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. + /// The desired dimensions of the generated image. /// The desired line width to be used in the generated image. /// The desired color of the line to be used in the generated image. /// Background of the generated image. - /// /// /// containing the data of the requested image with data that's provided through the parameter. - public static ValueTask GetImageStream(IEnumerable points, - Size imageSize, - float lineWidth, - Color strokeColor, - Paint background, - Size? canvaSize = null, - CancellationToken token = default) - { - return DrawingViewService.GetImageStream(points.ToList(), imageSize, lineWidth, strokeColor, background, canvaSize, token); - } - + public static ValueTask GetImageStream( + IEnumerable points, + Size desiredSize, + float lineWidth, + Color strokeColor, + Paint background, + CancellationToken token = default) => + GetImageStream(points, desiredSize, lineWidth, strokeColor, background, null, token); + /// - /// Retrieves a containing an image of this line, based on the data. + /// Retrieves a containing an image of the collection of that is provided as a parameter. /// - /// Desired width of the image that is returned. - /// Desired height of the image that is returned. + /// A collection of that a image is generated from. + /// The desired dimensions of the generated image. + /// The desired line width to be used in the generated image. + /// The desired color of the line to be used in the generated image. /// Background of the generated image. - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// - /// containing the data of the requested image with data that's currently on the . - public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default) - { - return DrawingViewService.GetImageStream(Points.ToList(), new Size(imageSizeWidth, imageSizeHeight), LineWidth, LineColor, background, canvasSize, token); - } + /// containing the data of the requested image with data that's provided through the parameter. + public static ValueTask GetImageStream( + IEnumerable points, + Size desiredSize, + float lineWidth, + Color strokeColor, + Paint background, + Size? canvasSize = null, + CancellationToken token = default) => + DrawingViewService.GetImageStream(points.ToList(), desiredSize, lineWidth, strokeColor, background, canvasSize, token); + + /// + public ValueTask GetImageStream(double desiredSizeWidth, double desiredSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default) => + DrawingViewService.GetImageStream(Points.ToList(), new Size(desiredSizeWidth, desiredSizeHeight), LineWidth, LineColor, background, canvasSize, token); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs index 35fa9b4a92..fd4cb52044 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs @@ -11,44 +11,52 @@ namespace CommunityToolkit.Maui.Core.Views; /// /// Drawing view service /// -public static class DrawingViewService +public static partial class DrawingViewService { /// /// Get image stream from lines /// /// Drawing lines - /// Maximum image size. The image will be resized proportionally. + /// The desired dimensions of the generated image. The image will be resized proportionally. /// Image background /// - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); var image = GetBitmapForLines(lines, background); - return ValueTask.FromResult(GetBitmapStream(image, imageSize)); + return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); } /// /// Get image stream from points /// /// Drawing points - /// Maximum image size. The image will be resized proportionally. + /// The desired dimensions of the generated image. The image will be resized proportionally. /// Line Width /// Line color /// Image background - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); var image = GetBitmapForPoints(points, lineWidth, strokeColor, background); - return ValueTask.FromResult(GetBitmapStream(image, imageSize)); + return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); } static Stream GetBitmapStream(Bitmap? image, Size imageSize) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs index 71e5a83465..b482b902d2 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs @@ -7,7 +7,7 @@ namespace CommunityToolkit.Maui.Core.Views; /// /// Drawing view service /// -public static class DrawingViewService +public static partial class DrawingViewService { /// /// Get image stream from lines @@ -15,10 +15,14 @@ public static class DrawingViewService /// Drawing lines /// Maximum image size. The image will be resized proportionally. /// Image background - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); @@ -44,10 +48,14 @@ public static ValueTask GetImageStream(IList lines, Size i /// Line Width /// Line color /// Image background - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs index 47b4815892..5fcac6b4a3 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs @@ -3,20 +3,24 @@ /// /// Drawing view service /// -public static class DrawingViewService +public static partial class DrawingViewService { /// /// Get image stream from points /// /// Drawing points - /// Maximum image size. The image will be resized proportionally. + /// The desired dimensions of the generated image. The image will be resized proportionally. /// Line Width /// Line color /// Image background - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); @@ -26,12 +30,16 @@ public static ValueTask GetImageStream(IList points, Size imageS /// Get image stream from lines /// /// Drawing lines - /// Maximum image size. The image will be resized proportionally. + /// The desired dimensions of the generated image. The image will be resized proportionally. /// Image background - /// + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize = null, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs new file mode 100644 index 0000000000..ca87ae5517 --- /dev/null +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs @@ -0,0 +1,28 @@ +namespace CommunityToolkit.Maui.Core.Views; + +public static partial class DrawingViewService +{ + /// + /// Get image stream from points + /// + /// Drawing points + /// The desired dimensions of the generated image. The image will be resized proportionally. + /// Line Width + /// Line color + /// Image background + /// + /// Image stream + public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) => + GetImageStream(points, desiredSize, lineWidth, strokeColor, background, null, token); + + /// + /// Get image stream from lines + /// + /// Drawing lines + /// The desired dimensions of the generated image. The image will be resized proportionally. + /// Image background + /// + /// Image stream + public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, CancellationToken token = default) => + GetImageStream(lines, desiredSize, background, null, token); +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs index 2835f4754f..5a4186c67a 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs @@ -6,7 +6,7 @@ namespace CommunityToolkit.Maui.Core.Views; /// /// Drawing view service /// -public static class DrawingViewService +public static partial class DrawingViewService { /// /// Get image stream from points @@ -16,9 +16,14 @@ public static class DrawingViewService /// Line Width /// Line color /// Image background + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static async ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) + public static async ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); @@ -51,9 +56,14 @@ public static async ValueTask GetImageStream(IList points, Size /// Drawing lines /// Maximum image size. The image will be resized proportionally. /// Image background + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static async ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, CancellationToken token = default) + public static async ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index 7ca15dfea6..e7109956b8 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -13,7 +13,7 @@ namespace CommunityToolkit.Maui.Core.Views; /// /// Drawing view service /// -public static class DrawingViewService +public static partial class DrawingViewService { /// /// Get image stream from lines @@ -21,9 +21,14 @@ public static class DrawingViewService /// Drawing lines /// Maximum image size. The image will be resized proportionally. /// Image background + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) { var canvas = GetImageInternal(lines, imageSize, background); @@ -38,9 +43,14 @@ public static ValueTask GetImageStream(IList lines, Size i /// Line Width /// Line color /// Image background + /// + /// The actual size of the canvas being displayed. This is an optional parameter + /// if a value is provided then the contents of the inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { var canvas = GetImageInternal(points, imageSize, lineWidth, strokeColor, background); diff --git a/src/CommunityToolkit.Maui.UnitTests/Mocks/MockDrawingViewHandler.cs b/src/CommunityToolkit.Maui.UnitTests/Mocks/MockDrawingViewHandler.cs index 94405fdead..e8bd360c84 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Mocks/MockDrawingViewHandler.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Mocks/MockDrawingViewHandler.cs @@ -130,7 +130,7 @@ class MockDrawingLine : IDrawingLine public float LineWidth { get; set; } public ObservableCollection Points { get; set; } = []; public bool ShouldSmoothPathWhenDrawn { get; set; } - public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, CancellationToken token) + public ValueTask GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, Size? canvasSize, CancellationToken token) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); diff --git a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs index ed69f3c122..af38034cf8 100644 --- a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs +++ b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs @@ -217,26 +217,39 @@ public Action? DrawAction /// A collection of that a image is generated from. /// The desired dimensions of the generated image. The image will be resized proportionally. /// Background of the generated image. If color is null, default background color is used. - /// The size of the canvas where the lines fit. /// /// containing the data of the requested image with data that's provided through the parameter. - public static ValueTask GetImageStream(IEnumerable lines, - Size desiredSize, - Brush? background, - Size? canvasSize = null, - CancellationToken token = default) => - DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, canvasSize, token); - + public static ValueTask GetImageStream( + IEnumerable lines, + Size desiredSize, + Brush? background, + CancellationToken token = default) => + DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, null, token); + /// - /// Retrieves a containing an image of the that are currently drawn on the . + /// Retrieves a containing an image of the collection of that is provided as a parameter. /// - /// Desired width of the image that is returned. The image will be resized proportionally. - /// Desired height of the image that is returned. The image will be resized proportionally. - /// The to determine the bounds and the contents of the resulting image. + /// A collection of that a image is generated from. + /// The desired dimensions of the generated image. The image will be resized proportionally. + /// Background of the generated image. If color is null, default background color is used. + /// The size of the canvas where the lines fit. /// - /// containing the data of the requested image with data that's currently on the . - public ValueTask GetImageStream(double desiredWidth, double desiredHeight, ImageOutputOption imageOutputOption = ImageOutputOption.Lines, CancellationToken token = default) => - DrawingViewService.GetImageStream(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background, imageOutputOption == ImageOutputOption.Lines ? null : new Size(this.Width, this.Height), token); + /// containing the data of the requested image with data that's provided through the parameter. + public static ValueTask GetImageStream( + IEnumerable lines, + Size desiredSize, + Brush? background, + Size? canvasSize = null, + CancellationToken token = default) => + DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, canvasSize, token); + + /// + public ValueTask GetImageStream(double desiredWidth, double desiredHeight, CancellationToken token = default) => + GetImageStream(desiredWidth, desiredHeight, DrawingViewOutputOption.Lines, token); + + /// + public ValueTask GetImageStream(double desiredWidth, double desiredHeight, DrawingViewOutputOption imageOutputOption, CancellationToken token = default) => + DrawingViewService.GetImageStream(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background, imageOutputOption == DrawingViewOutputOption.Lines ? null : new Size(this.Width, this.Height), token); /// /// Clears the collection. From e33ca704ebf575b1407afbfe369810e8884fdfd7 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:57:36 +0100 Subject: [PATCH 03/13] Android sizing change --- .../Service/DrawingViewService.android.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs index fd4cb52044..a6da2b9280 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs @@ -30,7 +30,7 @@ public static ValueTask GetImageStream(IList lines, Size d { token.ThrowIfCancellationRequested(); - var image = GetBitmapForLines(lines, background); + var image = GetBitmapForLines(lines, background, canvasSize); return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); } @@ -54,7 +54,7 @@ public static ValueTask GetImageStream(IList points, Size desire { token.ThrowIfCancellationRequested(); - var image = GetBitmapForPoints(points, lineWidth, strokeColor, background); + var image = GetBitmapForPoints(points, lineWidth, strokeColor, background, canvasSize); return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); } @@ -85,9 +85,10 @@ static Stream GetBitmapStream(Bitmap? image, Size imageSize) static Bitmap? GetBitmapForPoints(ICollection points, float lineWidth, Color strokeColor, - Paint? background) + Paint? background, + Size? canvasSize) { - var (image, offset) = GetBitmap(points, lineWidth); + var (image, offset) = GetBitmap(points, lineWidth, canvasSize); if (image is null) { return null; @@ -99,11 +100,11 @@ static Stream GetBitmapStream(Bitmap? image, Size imageSize) return image; } - static Bitmap? GetBitmapForLines(IList lines, Paint? background) + static Bitmap? GetBitmapForLines(IList lines, Paint? background, Size? canvasSize) { var points = lines.SelectMany(x => x.Points).ToList(); var maxLineWidth = lines.Select(x => x.LineWidth).Max(); - var (image, offset) = GetBitmap(points, maxLineWidth); + var (image, offset) = GetBitmap(points, maxLineWidth, canvasSize); if (image is null) { return null; @@ -119,7 +120,7 @@ static Stream GetBitmapStream(Bitmap? image, Size imageSize) return image; } - static (Bitmap?, SizeF offset) GetBitmap(ICollection points, float maxLineWidth) + static (Bitmap?, SizeF offset) GetBitmap(ICollection points, float maxLineWidth, Size? canvasSize) { if (points.Count is 0) { @@ -128,8 +129,8 @@ static Stream GetBitmapStream(Bitmap? image, Size imageSize) var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; const int minSize = 1; if (drawingWidth < minSize || drawingHeight < minSize) { @@ -146,8 +147,10 @@ static Stream GetBitmapStream(Bitmap? image, Size imageSize) { return (null, SizeF.Zero); } + + var offset = canvasSize is null ? new SizeF(minPointX, minPointY) : SizeF.Zero; - return (image, new SizeF(minPointX, minPointY)); + return (image, offset); } static void DrawStrokes(Canvas canvas, ICollection points, float lineWidth, Color strokeColor, SizeF offset) From 2b1894d8c33f5c1a4b0b2b2a4737a7974ec649d2 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:02:39 +0100 Subject: [PATCH 04/13] Windows implementation --- .../Service/DrawingViewService.windows.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index e7109956b8..e7509999b1 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -16,10 +16,10 @@ namespace CommunityToolkit.Maui.Core.Views; public static partial class DrawingViewService { /// - /// Get image stream from lines + /// Get image stream from lines /// /// Drawing lines - /// Maximum image size. The image will be resized proportionally. + /// Maximum image size. The image will be resized proportionally. /// Image background /// /// The actual size of the canvas being displayed. This is an optional parameter @@ -28,18 +28,18 @@ public static partial class DrawingViewService /// /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) { - var canvas = GetImageInternal(lines, imageSize, background); + var canvas = GetImageInternal(lines, desiredSize, background, canvasSize); return GetCanvasRenderTargetStream(canvas, token); } /// - /// Get image stream from points + /// Get image stream from points /// /// Drawing points - /// Maximum image size. The image will be resized proportionally. + /// Maximum image size. The image will be resized proportionally. /// Line Width /// Line color /// Image background @@ -50,9 +50,9 @@ public static ValueTask GetImageStream(IList lines, Size i /// /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { - var canvas = GetImageInternal(points, imageSize, lineWidth, strokeColor, background); + var canvas = GetImageInternal(points, desiredSize, lineWidth, strokeColor, background, canvasSize); return GetCanvasRenderTargetStream(canvas, token); } @@ -118,7 +118,7 @@ static void DrawStrokes(CanvasDrawingSession session, session.DrawInk(strokes); } - static (CanvasRenderTarget? offscreen, Size offset) GetCanvasRenderTarget(ICollection points, SizeF size, bool scale, float maxLineWidth) + static (CanvasRenderTarget? offscreen, Size offset) GetCanvasRenderTarget(ICollection points, SizeF size, bool scale, float maxLineWidth, Size? canvasSize) { const int minSize = 1; @@ -129,22 +129,23 @@ static void DrawStrokes(CanvasDrawingSession session, var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; if (drawingWidth < minSize || drawingHeight < minSize) { return (null, new Size(minPointX, minPointY)); } var device = CanvasDevice.GetSharedDevice(); - return (new CanvasRenderTarget(device, scale ? size.Width : drawingWidth, scale ? size.Height : drawingHeight, 96), new Size(minPointX, minPointY)); + var offset = canvasSize is null ? new Size(minPointX, minPointY) : Size.Zero; + return (new CanvasRenderTarget(device, scale ? size.Width : drawingWidth, scale ? size.Height : drawingHeight, 96), offset); } - static CanvasRenderTarget? GetImageInternal(IList lines, Size size, Paint? background, bool scale = false) + static CanvasRenderTarget? GetImageInternal(IList lines, Size size, Paint? background, Size? canvasSize, bool scale = false) { var points = lines.SelectMany(x => x.Points).ToList(); var maxLineWidth = lines.Select(x => x.LineWidth).Max(); - var (offscreen, offset) = GetCanvasRenderTarget(points, size, scale, maxLineWidth); + var (offscreen, offset) = GetCanvasRenderTarget(points, size, scale, maxLineWidth, canvasSize); if (offscreen is null) { return null; From 309268e9c40d5ac82845295643a34c4b0e7e4962 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:11:34 +0100 Subject: [PATCH 05/13] Tizen support --- .../Service/DrawingViewService.tizen.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs index 5a4186c67a..e0a69f4d00 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs @@ -12,7 +12,7 @@ public static partial class DrawingViewService /// Get image stream from points /// /// Drawing points - /// Maximum image size. The image will be resized proportionally. + /// Maximum image size. The image will be resized proportionally. /// Line Width /// Line color /// Image background @@ -23,11 +23,11 @@ public static partial class DrawingViewService /// /// /// Image stream - public static async ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static async ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForPoints(points, lineWidth, strokeColor, background); + var image = GetBitmapForPoints(points, lineWidth, strokeColor, background, canvasSize); if (image is null) { @@ -37,7 +37,7 @@ public static async ValueTask GetImageStream(IList points, Size // Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727 var stream = await Task.Run(() => { - var resized = image.Resize(new SKImageInfo((int)imageSize.Width, (int)imageSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); + var resized = image.Resize(new SKImageInfo((int)desiredSize.Width, (int)desiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); var data = resized.Encode(SKEncodedImageFormat.Png, 100); var stream = new MemoryStream(); @@ -63,11 +63,11 @@ public static async ValueTask GetImageStream(IList points, Size /// /// /// Image stream - public static async ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static async ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForLines(lines, background); + var image = GetBitmapForLines(lines, background, canvasSize); if (image is null) { @@ -77,7 +77,7 @@ public static async ValueTask GetImageStream(IList lines, // Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727 var stream = await Task.Run(() => { - var resized = image.Resize(new SKImageInfo((int)imageSize.Width, (int)imageSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); + var resized = image.Resize(new SKImageInfo((int)desiredSize.Width, (int)desiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); var data = resized.Encode(SKEncodedImageFormat.Png, 100); var stream = new MemoryStream(); @@ -90,7 +90,7 @@ public static async ValueTask GetImageStream(IList lines, return stream; } - static (SKBitmap?, SizeF offset) GetBitmap(in ICollection points, float maxLineWidth) + static (SKBitmap?, SizeF offset) GetBitmap(in ICollection points, float maxLineWidth, Size? canvasSize) { if (points.Count is 0) { @@ -100,8 +100,8 @@ public static async ValueTask GetImageStream(IList lines, const int minSize = 1; var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; if (drawingWidth < minSize || drawingHeight < minSize) { @@ -110,6 +110,8 @@ public static async ValueTask GetImageStream(IList lines, var bitmap = new SKBitmap((int)drawingWidth, (int)drawingHeight, SKColorType.Bgra8888, SKAlphaType.Opaque); + var offset = canvasSize is null ? new Size(minPointX, minPointY) : Size.Zero; + return (bitmap, new SizeF(minPointX, minPointY)); } @@ -136,9 +138,9 @@ public static async ValueTask GetImageStream(IList lines, return image; } - static SKBitmap? GetBitmapForPoints(in ICollection points, in float lineWidth, in Color strokeColor, in Paint? background) + static SKBitmap? GetBitmapForPoints(in ICollection points, in float lineWidth, in Color strokeColor, in Paint? background, in Size? canvasSize) { - var (image, offset) = GetBitmap(points, lineWidth); + var (image, offset) = GetBitmap(points, lineWidth, canvasSize); if (image is null) { return null; From f4667742050cdab002c9b6edee40e06d467be50d Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:06:50 +0100 Subject: [PATCH 06/13] Attempt to fix the unknowns --- .../Views/DrawingView/Service/DrawingViewService.tizen.cs | 4 ++-- .../Views/DrawingView/Service/DrawingViewService.windows.cs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs index e0a69f4d00..e4ff2be63d 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs @@ -115,11 +115,11 @@ public static async ValueTask GetImageStream(IList lines, return (bitmap, new SizeF(minPointX, minPointY)); } - static SKBitmap? GetBitmapForLines(in IList lines, in Paint? background) + static SKBitmap? GetBitmapForLines(in IList lines, in Paint? background, Size? canvasSize) { var points = lines.SelectMany(static x => x.Points).ToList(); var maxLineWidth = lines.Select(x => x.LineWidth).Max(); - var (image, offset) = GetBitmap(points, maxLineWidth); + var (image, offset) = GetBitmap(points, maxLineWidth, canvasSize); if (image is null) { diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index e7509999b1..d5a496065f 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -81,9 +81,10 @@ static async ValueTask GetCanvasRenderTargetStream(CanvasRenderTarget? c float lineWidth, Color lineColor, Paint? background, + Size? canvasSize, bool scale = false) { - var (offscreen, offset) = GetCanvasRenderTarget(points, size, scale, lineWidth); + var (offscreen, offset) = GetCanvasRenderTarget(points, size, scale, lineWidth, canvasSize); if (offscreen is null) { return null; From fa5ee837bd527879207e8b1d250d622f9a4a8a1f Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:20:44 +0100 Subject: [PATCH 07/13] More fixes --- .../Views/DrawingView/Service/DrawingViewService.tizen.cs | 2 +- .../Views/DrawingView/Service/DrawingViewService.windows.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs index e4ff2be63d..ddfbbf66a9 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs @@ -54,7 +54,7 @@ public static async ValueTask GetImageStream(IList points, Size /// Get image stream from lines /// /// Drawing lines - /// Maximum image size. The image will be resized proportionally. + /// Maximum image size. The image will be resized proportionally. /// Image background /// /// The actual size of the canvas being displayed. This is an optional parameter diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index d5a496065f..f461463918 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -130,8 +130,8 @@ static void DrawStrokes(CanvasDrawingSession session, var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = (float)canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = (float)canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; if (drawingWidth < minSize || drawingHeight < minSize) { return (null, new Size(minPointX, minPointY)); From 532c49386fece501b7693f2264cc1a12c593841e Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:34:57 +0100 Subject: [PATCH 08/13] One more time --- .../Views/DrawingView/Service/DrawingViewService.windows.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index f461463918..8d334d0135 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -130,8 +130,8 @@ static void DrawStrokes(CanvasDrawingSession session, var minPointX = points.Min(p => p.X) - maxLineWidth; var minPointY = points.Min(p => p.Y) - maxLineWidth; - var drawingWidth = (float)canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; - var drawingHeight = (float)canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; + var drawingWidth = (float?)canvasSize?.Width ?? points.Max(p => p.X) - minPointX + maxLineWidth; + var drawingHeight = (float?)canvasSize?.Height ?? points.Max(p => p.Y) - minPointY + maxLineWidth; if (drawingWidth < minSize || drawingHeight < minSize) { return (null, new Size(minPointX, minPointY)); From c7c7d30d7566cd1f71bd63ebd7f1e91b357effc1 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 10 Sep 2024 20:53:00 +0100 Subject: [PATCH 09/13] Enable selection of the output option in the sample --- .../Pages/Views/DrawingViewPage.xaml | 24 +++++++++++++------ .../ViewModels/Views/DrawingViewViewModel.cs | 17 ++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml index 857475bc2c..d753c3d3bc 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml @@ -10,7 +10,7 @@ Title="DrawingView"> - + /// Last drawing line void OnDrawingLineCompleted(IDrawingLine lastDrawingLine); -} - -/// -/// Enumeration of the options available when generating an image stream using the DrawingView. -/// -public enum DrawingViewOutputOption -{ - /// - /// Outputs the area covered by the top-left to the bottom-right most points. - /// - Lines, - - /// - /// Outputs the full area displayed within the drawing view. - /// - FullCanvas } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Primitives/DrawingViewOutputOption.shared.cs b/src/CommunityToolkit.Maui.Core/Primitives/DrawingViewOutputOption.shared.cs new file mode 100644 index 0000000000..9b93f1ddcd --- /dev/null +++ b/src/CommunityToolkit.Maui.Core/Primitives/DrawingViewOutputOption.shared.cs @@ -0,0 +1,17 @@ +namespace CommunityToolkit.Maui.Core; + +/// +/// Enumeration of the options available when generating an image stream using the DrawingView. +/// +public enum DrawingViewOutputOption +{ + /// + /// Outputs the area covered by the top-left to the bottom-right most points. + /// + Lines, + + /// + /// Outputs the full area displayed within the drawing view. + /// + FullCanvas +} \ No newline at end of file From 91519aba09dcbb1027fbf7fe80701d64cca61ec1 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:34:15 +0100 Subject: [PATCH 11/13] Tidy up merge --- .../Views/DrawingView/DrawingLine.shared.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs index fe81bd5a58..e7e0148fb1 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs @@ -80,7 +80,7 @@ public static ValueTask GetImageStream( Paint background, Size? canvasSize = null, CancellationToken token = default) => - DrawingViewService.GetImageStream([.. Points], desiredSize, lineWidth, strokeColor, background, canvasSize, token); + DrawingViewService.GetImageStream([.. points], desiredSize, lineWidth, strokeColor, background, canvasSize, token); /// public ValueTask GetImageStream(double desiredSizeWidth, double desiredSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default) => From 13382045bce307bd0fbe008edf69cec2621e19fa Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Mon, 13 Jan 2025 22:03:56 +0000 Subject: [PATCH 12/13] Refactored to make use of some options classes --- .../Pages/Views/DrawingViewPage.xaml.cs | 10 +- .../ViewModels/Views/DrawingViewViewModel.cs | 12 +- .../Interfaces/IDrawingView.shared.cs | 2 +- .../Views/DrawingView/DrawingLine.shared.cs | 43 +------ .../Service/DrawingViewService.android.cs | 33 ++--- .../Service/DrawingViewService.macios.cs | 35 ++---- .../Service/DrawingViewService.net.cs | 24 +--- .../Service/DrawingViewService.shared.cs | 117 ++++++++++++++++-- .../Service/DrawingViewService.tizen.cs | 32 ++--- .../Service/DrawingViewService.windows.cs | 28 +---- .../Views/DrawingView/DrawingView.shared.cs | 40 ++---- 11 files changed, 172 insertions(+), 204 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs index e4b1c910e9..ec589562ca 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml.cs @@ -48,10 +48,12 @@ async Task DrawImage(IEnumerable lines, CancellationToken token) { var drawingLines = lines.ToList(); var points = drawingLines.SelectMany(x => x.Points).ToList(); - var stream = await DrawingView.GetImageStream(drawingLines, - new Size(points.Max(x => x.X) - points.Min(x => x.X), points.Max(x => x.Y) - points.Min(x => x.Y)), - Colors.Gray, - this.DrawingViewControl.Bounds.Size, + var stream = await DrawingView.GetImageStream( + ImageLineOptions.FullCanvas( + drawingLines.OfType().ToList(), + new Size(points.Max(x => x.X) - points.Min(x => x.X), points.Max(x => x.Y) - points.Min(x => x.Y)), + new SolidPaint(Colors.Gray), + this.DrawingViewControl.Bounds.Size), token); GestureImage.Source = ImageSource.FromStream(() => stream); diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs index 7cedb6a7a0..6757a1a443 100644 --- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/DrawingViewViewModel.cs @@ -81,13 +81,11 @@ async Task Save(CancellationToken cancellationToken) { try { - await using var stream = await DrawingView.GetImageStream( - Lines, - new Size(1920, 1080), - Brush.Blue, - // If the user wants to output the full canvas then we need to pass in the canvas dimensions, otherwise null will use the bounds of the current drawing. - SelectedOutputOption == DrawingViewOutputOption.FullCanvas ? new Size(CanvasWidth, CanvasHeight) : null, - cancellationToken); + var options = SelectedOutputOption == DrawingViewOutputOption.Lines + ? ImageLineOptions.JustLines(Lines.ToList(), new Size(1920, 1080), Brush.Blue) + : ImageLineOptions.FullCanvas(Lines.ToList(), new Size(1920, 1080), Brush.Blue, new Size(CanvasWidth, CanvasHeight)); + + await using var stream = await DrawingView.GetImageStream(options, cancellationToken); await Permissions.RequestAsync().WaitAsync(cancellationToken); await Permissions.RequestAsync().WaitAsync(cancellationToken); diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs index 3c23908854..8e249e70d0 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IDrawingView.shared.cs @@ -86,4 +86,4 @@ public interface IDrawingView : IView /// /// Last drawing line void OnDrawingLineCompleted(IDrawingLine lastDrawingLine); -} \ No newline at end of file +} diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs index 78854c483c..2cb20c5d14 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/DrawingLine.shared.cs @@ -39,48 +39,15 @@ public int Granularity /// /// Retrieves a containing an image of the collection of that is provided as a parameter. /// - /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. - /// The desired line width to be used in the generated image. - /// The desired color of the line to be used in the generated image. - /// Background of the generated image. + /// The options controlling how the resulting image is generated. /// - /// containing the data of the requested image with data that's provided through the parameter. + /// containing the data of the requested image with data that's provided through the parameter. public static ValueTask GetImageStream( - IEnumerable points, - Size desiredSize, - float lineWidth, - Color strokeColor, - Paint background, + ImagePointOptions options, CancellationToken token = default) => - GetImageStream(points, desiredSize, lineWidth, strokeColor, background, null, token); - - /// - /// Retrieves a containing an image of the collection of that is provided as a parameter. - /// - /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. - /// The desired line width to be used in the generated image. - /// The desired color of the line to be used in the generated image. - /// Background of the generated image. - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// - /// - /// containing the data of the requested image with data that's provided through the parameter. - public static ValueTask GetImageStream( - IEnumerable points, - Size desiredSize, - float lineWidth, - Color strokeColor, - Paint background, - Size? canvasSize = null, - CancellationToken token = default) => - DrawingViewService.GetImageStream([.. points], desiredSize, lineWidth, strokeColor, background, canvasSize, token); + DrawingViewService.GetImageStream(options, token); /// public ValueTask GetImageStream(double desiredSizeWidth, double desiredSizeHeight, Paint background, Size? canvasSize = null, CancellationToken token = default) => - DrawingViewService.GetImageStream([.. Points], new Size(desiredSizeWidth, desiredSizeHeight), LineWidth, LineColor, background, canvasSize, token); + DrawingViewService.GetImageStream(new ImagePointOptions([.. Points], new Size(desiredSizeWidth, desiredSizeHeight), LineWidth, LineColor, background, canvasSize), token); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs index a6da2b9280..64c15b59da 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.android.cs @@ -16,47 +16,30 @@ public static partial class DrawingViewService /// /// Get image stream from lines /// - /// Drawing lines - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Image background - /// - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// Image stream - public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImageLineOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForLines(lines, background, canvasSize); + var image = GetBitmapForLines(options.Lines, options.Background, options.CanvasSize); - return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); + return ValueTask.FromResult(GetBitmapStream(image, options.DesiredSize)); } /// /// Get image stream from points /// - /// Drawing points - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Line Width - /// Line color - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImagePointOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForPoints(points, lineWidth, strokeColor, background, canvasSize); + var image = GetBitmapForPoints(options.Points, options.LineWidth, options.StrokeColor, options.Background, options.CanvasSize); - return ValueTask.FromResult(GetBitmapStream(image, desiredSize)); + return ValueTask.FromResult(GetBitmapStream(image, options.DesiredSize)); } static Stream GetBitmapStream(Bitmap? image, Size imageSize) diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs index 7546140bff..df7a4d858e 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.macios.cs @@ -11,21 +11,14 @@ public static partial class DrawingViewService /// /// Get image stream from lines /// - /// Drawing lines - /// Maximum image size. The image will be resized proportionally. - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size imageSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImageLineOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetUIImageForLines(lines, background, canvasSize); + var image = GetUIImageForLines(options.Lines, options.Background, options.CanvasSize); if (image is null) { return ValueTask.FromResult(Stream.Null); @@ -33,7 +26,7 @@ public static ValueTask GetImageStream(IList lines, Size i token.ThrowIfCancellationRequested(); - var imageAsPng = GetMaximumUIImage(image, imageSize.Width, imageSize.Height) + var imageAsPng = GetMaximumUIImage(image, options.DesiredSize.Width, options.DesiredSize.Height) .AsPNG() ?? throw new InvalidOperationException("Unable to convert image to PNG"); return ValueTask.FromResult(imageAsPng.AsStream()); @@ -42,23 +35,17 @@ public static ValueTask GetImageStream(IList lines, Size i /// /// Get image stream from points /// - /// Drawing points - /// Image size - /// Line Width - /// Line color - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList points, Size imageSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImagePointOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetUIImageForPoints(points, lineWidth, strokeColor, background, canvasSize); + var imageSize = options.DesiredSize; + var canvasSize = options.CanvasSize; + + var image = GetUIImageForPoints(options.Points, options.LineWidth, options.StrokeColor, options.Background, canvasSize); if (image is null) { return ValueTask.FromResult(Stream.Null); @@ -103,7 +90,7 @@ public static ValueTask GetImageStream(IList points, Size imageS }, background, drawingLineWithLargestLineWidth.LineWidth, canvasSize); } - static UIImage? GetUIImage(ICollection points, Action drawStrokes, Paint? background, NFloat maxLineWidth, Size? canvasSize) + static UIImage? GetUIImage(ICollection points, Action drawStrokes, Paint? background, nfloat maxLineWidth, Size? canvasSize) { const int minSize = 1; var minPointX = points.Min(p => p.X) - maxLineWidth; diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs index 5fcac6b4a3..6db689acbd 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.net.cs @@ -8,19 +8,10 @@ public static partial class DrawingViewService /// /// Get image stream from points /// - /// Drawing points - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Line Width - /// Line color - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImagePointOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); @@ -29,17 +20,10 @@ public static ValueTask GetImageStream(IList points, Size desire /// /// Get image stream from lines /// - /// Drawing lines - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImageLineOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return ValueTask.FromResult(Stream.Null); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs index ca87ae5517..e55715f3eb 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.shared.cs @@ -5,24 +5,117 @@ public static partial class DrawingViewService /// /// Get image stream from points /// - /// Drawing points - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Line Width - /// Line color - /// Image background + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, CancellationToken token = default) => - GetImageStream(points, desiredSize, lineWidth, strokeColor, background, null, token); + public static ValueTask GetImageStream(ImagePointOptions options, CancellationToken token = default) => + GetPlatformImageStream(options, token); /// /// Get image stream from lines /// - /// Drawing lines - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Image background + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, CancellationToken token = default) => - GetImageStream(lines, desiredSize, background, null, token); + public static ValueTask GetImageStream(ImageLineOptions options, CancellationToken token = default) => + GetPlatformImageStream(options, token); +} + +/// +/// Base class set of options used for generating an image from an . +/// +/// The desired dimensions of the generated image. The image will be resized proportionally. +/// The background to apply to the output image. +/// The actual size of the canvas being displayed. +/// if a value is provided then the contents of the drawing inside these dimensions will be included in the output, +/// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. +public abstract class ImageOptions(Size desiredSize, Paint? background, Size? canvasSize) +{ + /// + /// Gets the desired dimensions of the generated image. The image will be resized proportionally. + /// + public Size DesiredSize { get; } = desiredSize; + + /// + /// Gets the background to apply to the output image. + /// + public Paint? Background { get; } = background; + + /// + /// Gets the actual size of the canvas being displayed. + /// if a value is provided then the contents of the drawing inside these dimensions will be included in the output, + /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. + /// + public Size? CanvasSize { get; } = canvasSize; +} + +/// +/// Represents a set of options that controls how a set of points will be output to an image. +/// +/// The points that will result in a single line. +/// The desired dimensions of the generated image. The image will be resized proportionally. +/// The width of the line to render. +/// The of the line to render. +/// The background to apply to the output image. +/// The actual size of the canvas being displayed. +/// if a value is provided then the contents of the drawing inside these dimensions will be included in the output, +/// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. +public class ImagePointOptions(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize) : ImageOptions(desiredSize, background, canvasSize) +{ + /// + /// Gets the points that will result in a single line. + /// + public IList Points { get; } = points; + + /// + /// Gets the width of the line to render. + /// + public float LineWidth { get; } = lineWidth; + + /// + /// Gets the of the line to render. + /// + public Color StrokeColor { get; } = strokeColor; +} + +/// +/// Represents a set of options that controls how a set of lines will be output to an image. +/// +public class ImageLineOptions : ImageOptions +{ + ImageLineOptions(IList lines, Size desiredSize, Paint? background, Size? canvasSize) + : base(desiredSize, background, canvasSize) + { + Lines = lines; + } + + /// + /// Creates an instance of that will result in the output image covering the top-left to the bottom-right most points. + /// + /// The lines that will be rendered in the resulting image. + /// The desired dimensions of the generated image. The image will be resized proportionally. + /// The background to apply to the output image. + /// An instance of . + public static ImageLineOptions JustLines(IList lines, Size desiredSize, Paint? background) + { + return new ImageLineOptions(lines, desiredSize, background, null); + } + + /// + /// Creates an instance of that will result in the contents of the drawing inside the supplied will be included in the output, + /// + /// The lines that will be rendered in the resulting image. + /// The desired dimensions of the generated image. The image will be resized proportionally. + /// The background to apply to the output image. + /// The actual size of the canvas being displayed. + /// An instance of . + public static ImageLineOptions FullCanvas(IList lines, Size desiredSize, Paint? background, Size? canvasSize) + { + return new ImageLineOptions(lines, desiredSize, background, canvasSize); + } + + /// + /// Gets the lines that will be rendered in the resulting image. + /// + public IList Lines { get; } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs index ddfbbf66a9..c78197ba02 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.tizen.cs @@ -11,23 +11,14 @@ public static partial class DrawingViewService /// /// Get image stream from points /// - /// Drawing points - /// Maximum image size. The image will be resized proportionally. - /// Line Width - /// Line color - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static async ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static async ValueTask GetPlatformImageStream(ImagePointOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForPoints(points, lineWidth, strokeColor, background, canvasSize); + var image = GetBitmapForPoints(options.Points, options.LineWidth, options.StrokeColor, options.Background, options.CanvasSize); if (image is null) { @@ -37,7 +28,7 @@ public static async ValueTask GetImageStream(IList points, Size // Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727 var stream = await Task.Run(() => { - var resized = image.Resize(new SKImageInfo((int)desiredSize.Width, (int)desiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); + var resized = image.Resize(new SKImageInfo((int)options.DesiredSize.Width, (int)options.DesiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); var data = resized.Encode(SKEncodedImageFormat.Png, 100); var stream = new MemoryStream(); @@ -53,21 +44,14 @@ public static async ValueTask GetImageStream(IList points, Size /// /// Get image stream from lines /// - /// Drawing lines - /// Maximum image size. The image will be resized proportionally. - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static async ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static async ValueTask GetPlatformImageStream(ImageLineOptions options, CancellationToken token = default) { token.ThrowIfCancellationRequested(); - var image = GetBitmapForLines(lines, background, canvasSize); + var image = GetBitmapForLines(options.Lines, options.Background, options.CanvasSize); if (image is null) { @@ -77,7 +61,7 @@ public static async ValueTask GetImageStream(IList lines, // Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727 var stream = await Task.Run(() => { - var resized = image.Resize(new SKImageInfo((int)desiredSize.Width, (int)desiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); + var resized = image.Resize(new SKImageInfo((int)options.DesiredSize.Width, (int)options.DesiredSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High); var data = resized.Encode(SKEncodedImageFormat.Png, 100); var stream = new MemoryStream(); diff --git a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs index 194c3a9a77..9694e8d2ff 100644 --- a/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs +++ b/src/CommunityToolkit.Maui.Core/Views/DrawingView/Service/DrawingViewService.windows.cs @@ -18,19 +18,12 @@ public static partial class DrawingViewService /// /// Get image stream from lines /// - /// Drawing lines - /// Maximum image size. The image will be resized proportionally. - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList lines, Size desiredSize, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImageLineOptions options, CancellationToken token = default) { - var canvas = GetImageInternal(lines, desiredSize, background, canvasSize); + var canvas = GetImageInternal(options.Lines, options.DesiredSize, options.Background, options.CanvasSize); return GetCanvasRenderTargetStream(canvas, token); } @@ -38,21 +31,12 @@ public static ValueTask GetImageStream(IList lines, Size d /// /// Get image stream from points /// - /// Drawing points - /// Maximum image size. The image will be resized proportionally. - /// Line Width - /// Line color - /// Image background - /// - /// The actual size of the canvas being displayed. This is an optional parameter - /// if a value is provided then the contents of the inside these dimensions will be included in the output, - /// if null is provided then the resulting output will be the area covered by the top-left to the bottom-right most points. - /// + /// The options controlling how the resulting image is generated. /// /// Image stream - public static ValueTask GetImageStream(IList points, Size desiredSize, float lineWidth, Color strokeColor, Paint? background, Size? canvasSize, CancellationToken token = default) + public static ValueTask GetPlatformImageStream(ImagePointOptions options, CancellationToken token = default) { - var canvas = GetImageInternal(points, desiredSize, lineWidth, strokeColor, background, canvasSize); + var canvas = GetImageInternal(options.Points, options.DesiredSize, options.LineWidth, options.StrokeColor, options.Background, options.CanvasSize); return GetCanvasRenderTargetStream(canvas, token); } diff --git a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs index d47a7cbd50..7ce9276499 100644 --- a/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs +++ b/src/CommunityToolkit.Maui/Views/DrawingView/DrawingView.shared.cs @@ -215,42 +215,28 @@ public Action? DrawAction /// /// Retrieves a containing an image of the collection of that is provided as a parameter. /// - /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Background of the generated image. If color is null, default background color is used. + /// The options controlling how the resulting image is generated. /// - /// containing the data of the requested image with data that's provided through the parameter. + /// containing the data of the requested image with data that's provided through the parameter. public static ValueTask GetImageStream( - IEnumerable lines, - Size desiredSize, - Brush? background, + ImageLineOptions options, CancellationToken token = default) => - DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, null, token); - - /// - /// Retrieves a containing an image of the collection of that is provided as a parameter. - /// - /// A collection of that a image is generated from. - /// The desired dimensions of the generated image. The image will be resized proportionally. - /// Background of the generated image. If color is null, default background color is used. - /// The size of the canvas where the lines fit. - /// - /// containing the data of the requested image with data that's provided through the parameter. - public static ValueTask GetImageStream( - IEnumerable lines, - Size desiredSize, - Brush? background, - Size? canvasSize = null, - CancellationToken token = default) => - DrawingViewService.GetImageStream(lines.ToList(), desiredSize, background, canvasSize, token); + DrawingViewService.GetImageStream(options, token); /// public ValueTask GetImageStream(double desiredWidth, double desiredHeight, CancellationToken token = default) => GetImageStream(desiredWidth, desiredHeight, DrawingViewOutputOption.Lines, token); /// - public ValueTask GetImageStream(double desiredWidth, double desiredHeight, DrawingViewOutputOption imageOutputOption, CancellationToken token = default) => - DrawingViewService.GetImageStream(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background, imageOutputOption == DrawingViewOutputOption.Lines ? null : new Size(this.Width, this.Height), token); + public ValueTask GetImageStream(double desiredWidth, double desiredHeight, DrawingViewOutputOption imageOutputOption, CancellationToken token = default) + { + var options = imageOutputOption == DrawingViewOutputOption.Lines + ? ImageLineOptions.JustLines(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background) + : ImageLineOptions.FullCanvas(Lines.ToList(), new Size(desiredWidth, desiredHeight), Background, + new Size(this.Width, this.Height)); + + return DrawingViewService.GetImageStream(options, token); + } /// /// Clears the collection. From 1963fdd8cd2140b4beba2207165f21d6a0dbcc2e Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:26:52 +0000 Subject: [PATCH 13/13] Attempt to fix unit tests --- .../Views/DrawingView/DrawingLineTests.cs | 3 ++- .../Views/DrawingView/DrawingViewTests.cs | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingLineTests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingLineTests.cs index b0c6115154..1d7107f703 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingLineTests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingLineTests.cs @@ -101,7 +101,8 @@ public async Task GetImageStreamReturnsNullStream() [Fact(Timeout = (int)TestDuration.Short)] public async Task GetImageStreamStaticReturnsNullStream() { - var stream = await DrawingLine.GetImageStream(Array.Empty(), new Size(10, 10), 5, Colors.Yellow, Colors.Blue.AsPaint(), CancellationToken.None); + ImagePointOptions pointOptions = new([], new Size(10, 10), 5, Colors.Yellow, Colors.Blue.AsPaint(), null); + var stream = await DrawingLine.GetImageStream(pointOptions, CancellationToken.None); Assert.Equal(Stream.Null, stream); } diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingViewTests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingViewTests.cs index cf4eabfb93..f92ec2f57b 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingViewTests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Views/DrawingView/DrawingViewTests.cs @@ -159,7 +159,9 @@ public async Task GetImageStream_CancellationTokenExpired() // Ensure CancellationToken Expired await Task.Delay(100, CancellationToken.None); - await Assert.ThrowsAsync(async () => await DrawingView.GetImageStream([new DrawingLine()], Size.Zero, Colors.Blue, cts.Token)); + ImageLineOptions options = ImageLineOptions.JustLines([new DrawingLine()], Size.Zero, Colors.Transparent.AsPaint()); + + await Assert.ThrowsAsync(async () => await DrawingView.GetImageStream(options, cts.Token)); } [Fact(Timeout = (int)TestDuration.Short)] @@ -170,9 +172,9 @@ public async Task GetImageStream_CancellationTokenCanceled() // Ensure CancellationToken Expired await cts.CancelAsync(); - await Assert.ThrowsAsync(async () => await DrawingView.GetImageStream([ - new DrawingLine() - ], Size.Zero, Colors.Blue, cts.Token)); + ImageLineOptions options = ImageLineOptions.JustLines([new DrawingLine()], Size.Zero, Colors.Transparent.AsPaint()); + + await Assert.ThrowsAsync(async () => await DrawingView.GetImageStream(options, cts.Token)); } [Fact(Timeout = (int)TestDuration.Short)] @@ -187,9 +189,8 @@ public async Task GetImageStreamReturnsNullStream() [Fact(Timeout = (int)TestDuration.Short)] public async Task GetImageStreamStaticReturnsNullStream() { - var stream = await DrawingView.GetImageStream([ - new DrawingLine() - ], Size.Zero, Colors.Blue, CancellationToken.None); + ImageLineOptions options = ImageLineOptions.JustLines([new DrawingLine()], Size.Zero, Colors.Blue.AsPaint()); + var stream = await DrawingView.GetImageStream(options, CancellationToken.None); stream.Should().BeSameAs(Stream.Null); }