Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Mar 2, 2024
1 parent 8924a58 commit 84bba50
Show file tree
Hide file tree
Showing 70 changed files with 320 additions and 565 deletions.
30 changes: 15 additions & 15 deletions samples/Svg.Skia.Converter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,77 @@ internal class Program
{
private static async Task<int> Main(string[] args)
{
var optionInputFiles = new Option(new[] { "--inputFiles", "-f" }, "The relative or absolute path to the input files")
var optionInputFiles = new Option(["--inputFiles", "-f"], "The relative or absolute path to the input files")
{
Argument = new Argument<FileInfo[]?>(getDefaultValue: () => null)
};

var optionInputDirectory = new Option(new[] { "--inputDirectory", "-d" }, "The relative or absolute path to the input directory")
var optionInputDirectory = new Option(["--inputDirectory", "-d"], "The relative or absolute path to the input directory")
{
Argument = new Argument<DirectoryInfo?>(getDefaultValue: () => null)
};

var optionOutputDirectory = new Option(new[] { "--outputDirectory", "-o" }, "The relative or absolute path to the output directory")
var optionOutputDirectory = new Option(["--outputDirectory", "-o"], "The relative or absolute path to the output directory")
{
Argument = new Argument<DirectoryInfo?>(getDefaultValue: () => null)
};

var optionOutputFiles = new Option(new[] { "--outputFiles" }, "The relative or absolute path to the output files")
var optionOutputFiles = new Option(["--outputFiles"], "The relative or absolute path to the output files")
{
Argument = new Argument<FileInfo[]?>(getDefaultValue: () => null)
};

var optionPattern = new Option(new[] { "--pattern", "-p" }, "The search string to match against the names of files in the input directory")
var optionPattern = new Option(["--pattern", "-p"], "The search string to match against the names of files in the input directory")
{
Argument = new Argument<string?>(getDefaultValue: () => null)
};

var optionFormat = new Option(new[] { "--format" }, "The output image format")
var optionFormat = new Option(["--format"], "The output image format")
{
Argument = new Argument<string>(getDefaultValue: () => "png")
};

var optionQuality = new Option(new[] { "--quality", "-q" }, "The output image quality")
var optionQuality = new Option(["--quality", "-q"], "The output image quality")
{
Argument = new Argument<int>(getDefaultValue: () => 100)
};

var optionBackground = new Option(new[] { "--background", "-b" }, "The output image background")
var optionBackground = new Option(["--background", "-b"], "The output image background")
{
Argument = new Argument<string>(getDefaultValue: () => "#00000000")
};

var optionScale = new Option(new[] { "--scale", "-s" }, "The output image horizontal and vertical scaling factor")
var optionScale = new Option(["--scale", "-s"], "The output image horizontal and vertical scaling factor")
{
Argument = new Argument<float>(getDefaultValue: () => 1f)
};

var optionScaleX = new Option(new[] { "--scaleX", "-sx" }, "The output image horizontal scaling factor")
var optionScaleX = new Option(["--scaleX", "-sx"], "The output image horizontal scaling factor")
{
Argument = new Argument<float>(getDefaultValue: () => 1f)
};

var optionScaleY = new Option(new[] { "--scaleY", "-sy" }, "The output image vertical scaling factor")
var optionScaleY = new Option(["--scaleY", "-sy"], "The output image vertical scaling factor")
{
Argument = new Argument<float>(getDefaultValue: () => 1f)
};

var optionSystemLanguage = new Option(new[] { "--systemLanguage" }, "The system language name as defined in BCP 47")
var optionSystemLanguage = new Option(["--systemLanguage"], "The system language name as defined in BCP 47")
{
Argument = new Argument<string?>(getDefaultValue: () => null)
};

var optionQuiet = new Option(new[] { "--quiet" }, "Set verbosity level to quiet")
var optionQuiet = new Option(["--quiet"], "Set verbosity level to quiet")
{
Argument = new Argument<bool>()
};

var optionLoadConfig = new Option(new[] { "--load-config", "-c" }, "The relative or absolute path to the config file")
var optionLoadConfig = new Option(["--load-config", "-c"], "The relative or absolute path to the config file")
{
Argument = new Argument<FileInfo?>(getDefaultValue: () => null)
};

var optionSaveConfig = new Option(new[] { "--save-config" }, "The relative or absolute path to the config file")
var optionSaveConfig = new Option(["--save-config"], "The relative or absolute path to the config file")
{
Argument = new Argument<FileInfo?>(getDefaultValue: () => null)
};
Expand Down
24 changes: 12 additions & 12 deletions samples/TestApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ public ReadOnlyObservableCollection<FileItemViewModel>? FilteredItems

private List<FilePickerFileType> GetConfigurationFileTypes()
{
return new List<FilePickerFileType>
{
return
[
StorageService.Json,
StorageService.All
};
];
}

private static List<FilePickerFileType> GetExportFileTypes()
{
return new List<FilePickerFileType>
{
return
[
StorageService.ImagePng,
StorageService.ImageJpg,
StorageService.CSharp,
StorageService.Pdf,
StorageService.Xps,
StorageService.All
};
];
}

public MainWindowViewModel()
{
_items = new ObservableCollection<FileItemViewModel>();
_items = [];

var queryFilter = this.WhenValueChanged(t => t.ItemQuery)
.Throttle(TimeSpan.FromMilliseconds(100))
Expand Down Expand Up @@ -193,11 +193,11 @@ private async Task AddItemExecute()
var dlg = new OpenFileDialog

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'

Check warning on line 193 in samples/TestApp/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'OpenFileDialog' is obsolete: 'Use Window.StorageProvider API or TopLevel.StorageProvider API'
{
AllowMultiple = true,
Filters = new List<FileDialogFilter>
{
new() {Name = "Svg Files (*.svg;*.svgz)", Extensions = new List<string> {"svg", "svgz"}},
new() {Name = "All Files (*.*)", Extensions = new List<string> {"*"}}
}
Filters =
[
new() {Name = "Svg Files (*.svg;*.svgz)", Extensions = ["svg", "svgz"]},
new() {Name = "All Files (*.*)", Extensions = ["*"]}
]
};
var result = await dlg.ShowAsync(window);
if (result is { })
Expand Down
7 changes: 2 additions & 5 deletions samples/svgc/ImageSharpAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public ShimSkiaSharp.SKImage LoadImage(Stream stream)
{
if (text is null || string.IsNullOrEmpty(text))
{
return new List<Svg.Model.TypefaceSpan>();
return [];
}

// TODO:
// Font fallback and text advancing code should be generated along with canvas commands instead.
// Otherwise, some package reference hacking may be needed.
return new List<Svg.Model.TypefaceSpan>
{
new(text, text.Length * paintPreferredTypeface.TextSize, paintPreferredTypeface.Typeface)
};
return [new(text, text.Length * paintPreferredTypeface.TextSize, paintPreferredTypeface.Typeface)];
}
}
10 changes: 5 additions & 5 deletions samples/svgc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,35 @@ static async Task<int> Main(string[] args)
Description = "Converts a svg file to a C# code."
};

var optionInputFile = new Option(new[] { "--inputFile", "-i" }, "The relative or absolute path to the input file")
var optionInputFile = new Option(["--inputFile", "-i"], "The relative or absolute path to the input file")
{
IsRequired = false,
Argument = new Argument<System.IO.FileInfo?>(getDefaultValue: () => null)
};
rootCommand.AddOption(optionInputFile);

var optionOutputFile = new Option(new[] { "--outputFile", "-o" }, "The relative or absolute path to the output file")
var optionOutputFile = new Option(["--outputFile", "-o"], "The relative or absolute path to the output file")
{
IsRequired = false,
Argument = new Argument<System.IO.FileInfo?>(getDefaultValue: () => null)
};
rootCommand.AddOption(optionOutputFile);

var optionJsonFile = new Option(new[] { "--jsonFile", "-j" }, "The relative or absolute path to the json file")
var optionJsonFile = new Option(["--jsonFile", "-j"], "The relative or absolute path to the json file")
{
IsRequired = false,
Argument = new Argument<System.IO.FileInfo?>(getDefaultValue: () => null)
};
rootCommand.AddOption(optionJsonFile);

var optionNamespace = new Option(new[] { "--namespace", "-n" }, "The generated C# namespace name")
var optionNamespace = new Option(["--namespace", "-n"], "The generated C# namespace name")
{
IsRequired = false,
Argument = new Argument<string>(getDefaultValue: () => "Svg")
};
rootCommand.AddOption(optionNamespace);

var optionClass = new Option(new[] { "--class", "-c" }, "The generated C# class name")
var optionClass = new Option(["--class", "-c"], "The generated C# class name")
{
IsRequired = false,
Argument = new Argument<string>(getDefaultValue: () => "Generated")
Expand Down
23 changes: 7 additions & 16 deletions src/Avalonia.Controls.Skia/SKBitmapDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@

namespace Avalonia.Controls.Skia;

public class SKBitmapDrawOperation : ICustomDrawOperation
public class SKBitmapDrawOperation(Rect bounds, SKBitmap? bitmap) : ICustomDrawOperation
{
private readonly SKBitmap? _bitmap;
private readonly Rect _bounds;

public SKBitmapDrawOperation(Rect bounds, SKBitmap? bitmap)
{
_bitmap = bitmap;
_bounds = bounds;
}

public void Dispose()
{
}

public Rect Bounds => _bounds;
public Rect Bounds => bounds;

public bool HitTest(Point p) => _bounds.Contains(p);
public bool HitTest(Point p) => bounds.Contains(p);

public bool Equals(ICustomDrawOperation? other) => false;

Expand All @@ -36,12 +27,12 @@ public void Render(ImmediateDrawingContext context)
}
using var lease = leaseFeature.Lease();
var canvas = lease?.SkCanvas;
if (canvas is { } && _bitmap is { })
if (canvas is { } && bitmap is { })
{
canvas.DrawBitmap(
_bitmap,
SKRect.Create(0, 0, _bitmap.Width, _bitmap.Height),
SKRect.Create((float)_bounds.Left, (float)_bounds.Top, (float)_bounds.Width, (float)_bounds.Height));
bitmap,
SKRect.Create(0, 0, bitmap.Width, bitmap.Height),
SKRect.Create((float)bounds.Left, (float)bounds.Top, (float)bounds.Width, (float)bounds.Height));
}
}
}
21 changes: 5 additions & 16 deletions src/Avalonia.Controls.Skia/SKPathDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,15 @@

namespace Avalonia.Controls.Skia;

public class SKPathDrawOperation : ICustomDrawOperation
public class SKPathDrawOperation(Rect bounds, SKPath? path, SKPaint? paint) : ICustomDrawOperation
{
private readonly SKPath? _path;
private readonly SKPaint? _paint;
private readonly Rect _bounds;

public SKPathDrawOperation(Rect bounds, SKPath? path, SKPaint? paint)
{
_path = path;
_paint = paint;
_bounds = bounds;
}

public void Dispose()
{
}

public Rect Bounds => _bounds;
public Rect Bounds => bounds;

public bool HitTest(Point p) => _bounds.Contains(p);
public bool HitTest(Point p) => bounds.Contains(p);

public bool Equals(ICustomDrawOperation? other) => false;

Expand All @@ -38,11 +27,11 @@ public void Render(ImmediateDrawingContext context)
}
using var lease = leaseFeature.Lease();
var canvas = lease?.SkCanvas;
if (canvas is null || _path is null)
if (canvas is null || path is null)
{
return;
}

canvas.DrawPath(_path, _paint);
canvas.DrawPath(path, paint);
}
}
19 changes: 5 additions & 14 deletions src/Avalonia.Controls.Skia/SKPictureDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@

namespace Avalonia.Controls.Skia;

public class SKPictureDrawOperation : ICustomDrawOperation
public class SKPictureDrawOperation(Rect bounds, SKPicture? picture) : ICustomDrawOperation
{
private readonly SKPicture? _picture;
private readonly Rect _bounds;

public SKPictureDrawOperation(Rect bounds, SKPicture? picture)
{
_picture = picture;
_bounds = bounds;
}

public void Dispose()
{
}

public Rect Bounds => _bounds;
public Rect Bounds => bounds;

public bool HitTest(Point p) => _bounds.Contains(p);
public bool HitTest(Point p) => bounds.Contains(p);

public bool Equals(ICustomDrawOperation? other) => false;

Expand All @@ -36,13 +27,13 @@ public void Render(ImmediateDrawingContext context)
}
using var lease = leaseFeature.Lease();
var canvas = lease?.SkCanvas;
if (canvas is null || _picture is null)
if (canvas is null || picture is null)
{
return;
}

canvas.Save();
canvas.DrawPicture(_picture);
canvas.DrawPicture(picture);
canvas.Restore();
}
}
5 changes: 0 additions & 5 deletions src/Avalonia.Svg.Skia/IsExternalInit.cs

This file was deleted.

16 changes: 4 additions & 12 deletions src/Avalonia.Svg.Skia/SvgCustomDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@

namespace Avalonia.Svg.Skia;

public class SvgCustomDrawOperation : ICustomDrawOperation
public class SvgCustomDrawOperation(Rect bounds, SKSvg? svg) : ICustomDrawOperation
{
private readonly SKSvg? _svg;

public SvgCustomDrawOperation(Rect bounds, SKSvg? svg)
{
_svg = svg;
Bounds = bounds;
}

public void Dispose()
{
}

public Rect Bounds { get; }
public Rect Bounds { get; } = bounds;

public bool HitTest(Point p) => false;

public bool Equals(ICustomDrawOperation? other) => false;

public void Render(ImmediateDrawingContext context)
{
if (_svg?.Picture is null)
if (svg?.Picture is null)
{
return;
}
Expand All @@ -46,7 +38,7 @@ public void Render(ImmediateDrawingContext context)
}

canvas.Save();
canvas.DrawPicture(_svg.Picture);
canvas.DrawPicture(svg.Picture);
canvas.Restore();
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.Svg/AvaloniaPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class AvaloniaPicture : IDisposable

private AvaloniaPicture()
{
_commands = new List<DrawCommand>();
_commands = [];
}

private static void RecordPathCommand(DrawPathCanvasCommand drawPathCanvasCommand, List<DrawCommand> commands)
Expand Down
Loading

0 comments on commit 84bba50

Please sign in to comment.