From 40b8c02bfc75916bc83f6e4621b1e580a81a448b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Tue, 24 Aug 2021 21:56:00 +0200 Subject: [PATCH] Refactor --- .../Generator/GeneratorBase.cs | 0 .../Generator/GeneratorContext.cs | 0 .../Generator/XamlGenerator.cs | 0 .../SvgConverter.cs | 0 SvgConverter/SvgConverter.csproj | 36 ++++ .../CompilerServices/IsExternalInit.cs | 0 .../Model/Containers/Image.cs | 0 .../Model/Containers/Styles.cs | 0 .../Model/Drawing/Drawing.cs | 0 .../Model/Drawing/DrawingGroup.cs | 0 .../Model/Drawing/DrawingImage.cs | 0 .../Model/Drawing/GeometryDrawing.cs | 0 .../Model/Factory.cs | 0 .../Model/Paint/Brush.cs | 0 .../Model/Paint/Dashes.cs | 0 .../Model/Paint/GradientBrush.cs | 0 .../Model/Paint/GradientStop.cs | 0 .../Model/Paint/LinearGradientBrush.cs | 0 .../Model/Paint/Pen.cs | 0 .../Model/Paint/PictureBrush.cs | 0 .../Model/Paint/RadialGradientBrush.cs | 0 .../Model/Paint/SolidColorBrush.cs | 0 .../Paint/TwoPointConicalGradientBrush.cs | 0 .../Model/Resources/Resource.cs | 0 .../Model/Resources/ResourceDictionary.cs | 0 .../SvgModel.csproj | 6 +- .../CompilerServices/IsExternalInit.cs | 6 + SvgToXaml.sln | 8 +- SvgToXaml/App.axaml.cs | 38 ++-- SvgToXaml/SvgToXaml.csproj | 6 +- SvgToXaml/ViewModels/FileItemViewModel.cs | 31 +++- SvgToXaml/ViewModels/MainWindowViewModel.cs | 165 ++++++++---------- SvgToXaml/ViewModels/ProjectViewModel.cs | 50 ++++++ SvgToXaml/ViewModels/SettingsViewModel.cs | 14 +- SvgToXaml/Views/MainView.axaml | 8 +- svgxaml/svgxaml.csproj | 6 +- 36 files changed, 233 insertions(+), 141 deletions(-) rename {SvgToXamlConverter => SvgConverter}/Generator/GeneratorBase.cs (100%) rename {SvgToXamlConverter => SvgConverter}/Generator/GeneratorContext.cs (100%) rename {SvgToXamlConverter => SvgConverter}/Generator/XamlGenerator.cs (100%) rename {SvgToXamlConverter => SvgConverter}/SvgConverter.cs (100%) create mode 100644 SvgConverter/SvgConverter.csproj rename {SvgToXamlConverter => SvgConverter}/System/Runtime/CompilerServices/IsExternalInit.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Containers/Image.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Containers/Styles.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Drawing/Drawing.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Drawing/DrawingGroup.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Drawing/DrawingImage.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Drawing/GeometryDrawing.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Factory.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/Brush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/Dashes.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/GradientBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/GradientStop.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/LinearGradientBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/Pen.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/PictureBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/RadialGradientBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/SolidColorBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Paint/TwoPointConicalGradientBrush.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Resources/Resource.cs (100%) rename {SvgToXamlConverter => SvgModel}/Model/Resources/ResourceDictionary.cs (100%) rename SvgToXamlConverter/SvgToXamlConverter.csproj => SvgModel/SvgModel.csproj (87%) create mode 100644 SvgModel/System/Runtime/CompilerServices/IsExternalInit.cs create mode 100644 SvgToXaml/ViewModels/ProjectViewModel.cs diff --git a/SvgToXamlConverter/Generator/GeneratorBase.cs b/SvgConverter/Generator/GeneratorBase.cs similarity index 100% rename from SvgToXamlConverter/Generator/GeneratorBase.cs rename to SvgConverter/Generator/GeneratorBase.cs diff --git a/SvgToXamlConverter/Generator/GeneratorContext.cs b/SvgConverter/Generator/GeneratorContext.cs similarity index 100% rename from SvgToXamlConverter/Generator/GeneratorContext.cs rename to SvgConverter/Generator/GeneratorContext.cs diff --git a/SvgToXamlConverter/Generator/XamlGenerator.cs b/SvgConverter/Generator/XamlGenerator.cs similarity index 100% rename from SvgToXamlConverter/Generator/XamlGenerator.cs rename to SvgConverter/Generator/XamlGenerator.cs diff --git a/SvgToXamlConverter/SvgConverter.cs b/SvgConverter/SvgConverter.cs similarity index 100% rename from SvgToXamlConverter/SvgConverter.cs rename to SvgConverter/SvgConverter.cs diff --git a/SvgConverter/SvgConverter.csproj b/SvgConverter/SvgConverter.csproj new file mode 100644 index 0000000..56221c0 --- /dev/null +++ b/SvgConverter/SvgConverter.csproj @@ -0,0 +1,36 @@ + + + Library + netstandard2.0 + True + enable + SvgConverter + + + latest + latest + + + 0.10.6 + preview1 + Wiesław Šoltés + Wiesław Šoltés + Copyright © Wiesław Šoltés 2021 + https://github.com/wieslawsoltes/SvgToXaml + + + An Svg to Xaml conveter. + SvgConverter + MIT + svg;xaml;axaml;avalonia;avaloniaui;convert;converter;vector graphics;rendering;2d;graphics;geometry;shapes;skiasharp;skia + + + + + + + + Converter\Model + + + diff --git a/SvgToXamlConverter/System/Runtime/CompilerServices/IsExternalInit.cs b/SvgConverter/System/Runtime/CompilerServices/IsExternalInit.cs similarity index 100% rename from SvgToXamlConverter/System/Runtime/CompilerServices/IsExternalInit.cs rename to SvgConverter/System/Runtime/CompilerServices/IsExternalInit.cs diff --git a/SvgToXamlConverter/Model/Containers/Image.cs b/SvgModel/Model/Containers/Image.cs similarity index 100% rename from SvgToXamlConverter/Model/Containers/Image.cs rename to SvgModel/Model/Containers/Image.cs diff --git a/SvgToXamlConverter/Model/Containers/Styles.cs b/SvgModel/Model/Containers/Styles.cs similarity index 100% rename from SvgToXamlConverter/Model/Containers/Styles.cs rename to SvgModel/Model/Containers/Styles.cs diff --git a/SvgToXamlConverter/Model/Drawing/Drawing.cs b/SvgModel/Model/Drawing/Drawing.cs similarity index 100% rename from SvgToXamlConverter/Model/Drawing/Drawing.cs rename to SvgModel/Model/Drawing/Drawing.cs diff --git a/SvgToXamlConverter/Model/Drawing/DrawingGroup.cs b/SvgModel/Model/Drawing/DrawingGroup.cs similarity index 100% rename from SvgToXamlConverter/Model/Drawing/DrawingGroup.cs rename to SvgModel/Model/Drawing/DrawingGroup.cs diff --git a/SvgToXamlConverter/Model/Drawing/DrawingImage.cs b/SvgModel/Model/Drawing/DrawingImage.cs similarity index 100% rename from SvgToXamlConverter/Model/Drawing/DrawingImage.cs rename to SvgModel/Model/Drawing/DrawingImage.cs diff --git a/SvgToXamlConverter/Model/Drawing/GeometryDrawing.cs b/SvgModel/Model/Drawing/GeometryDrawing.cs similarity index 100% rename from SvgToXamlConverter/Model/Drawing/GeometryDrawing.cs rename to SvgModel/Model/Drawing/GeometryDrawing.cs diff --git a/SvgToXamlConverter/Model/Factory.cs b/SvgModel/Model/Factory.cs similarity index 100% rename from SvgToXamlConverter/Model/Factory.cs rename to SvgModel/Model/Factory.cs diff --git a/SvgToXamlConverter/Model/Paint/Brush.cs b/SvgModel/Model/Paint/Brush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/Brush.cs rename to SvgModel/Model/Paint/Brush.cs diff --git a/SvgToXamlConverter/Model/Paint/Dashes.cs b/SvgModel/Model/Paint/Dashes.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/Dashes.cs rename to SvgModel/Model/Paint/Dashes.cs diff --git a/SvgToXamlConverter/Model/Paint/GradientBrush.cs b/SvgModel/Model/Paint/GradientBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/GradientBrush.cs rename to SvgModel/Model/Paint/GradientBrush.cs diff --git a/SvgToXamlConverter/Model/Paint/GradientStop.cs b/SvgModel/Model/Paint/GradientStop.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/GradientStop.cs rename to SvgModel/Model/Paint/GradientStop.cs diff --git a/SvgToXamlConverter/Model/Paint/LinearGradientBrush.cs b/SvgModel/Model/Paint/LinearGradientBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/LinearGradientBrush.cs rename to SvgModel/Model/Paint/LinearGradientBrush.cs diff --git a/SvgToXamlConverter/Model/Paint/Pen.cs b/SvgModel/Model/Paint/Pen.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/Pen.cs rename to SvgModel/Model/Paint/Pen.cs diff --git a/SvgToXamlConverter/Model/Paint/PictureBrush.cs b/SvgModel/Model/Paint/PictureBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/PictureBrush.cs rename to SvgModel/Model/Paint/PictureBrush.cs diff --git a/SvgToXamlConverter/Model/Paint/RadialGradientBrush.cs b/SvgModel/Model/Paint/RadialGradientBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/RadialGradientBrush.cs rename to SvgModel/Model/Paint/RadialGradientBrush.cs diff --git a/SvgToXamlConverter/Model/Paint/SolidColorBrush.cs b/SvgModel/Model/Paint/SolidColorBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/SolidColorBrush.cs rename to SvgModel/Model/Paint/SolidColorBrush.cs diff --git a/SvgToXamlConverter/Model/Paint/TwoPointConicalGradientBrush.cs b/SvgModel/Model/Paint/TwoPointConicalGradientBrush.cs similarity index 100% rename from SvgToXamlConverter/Model/Paint/TwoPointConicalGradientBrush.cs rename to SvgModel/Model/Paint/TwoPointConicalGradientBrush.cs diff --git a/SvgToXamlConverter/Model/Resources/Resource.cs b/SvgModel/Model/Resources/Resource.cs similarity index 100% rename from SvgToXamlConverter/Model/Resources/Resource.cs rename to SvgModel/Model/Resources/Resource.cs diff --git a/SvgToXamlConverter/Model/Resources/ResourceDictionary.cs b/SvgModel/Model/Resources/ResourceDictionary.cs similarity index 100% rename from SvgToXamlConverter/Model/Resources/ResourceDictionary.cs rename to SvgModel/Model/Resources/ResourceDictionary.cs diff --git a/SvgToXamlConverter/SvgToXamlConverter.csproj b/SvgModel/SvgModel.csproj similarity index 87% rename from SvgToXamlConverter/SvgToXamlConverter.csproj rename to SvgModel/SvgModel.csproj index 9bc4087..a36f632 100644 --- a/SvgToXamlConverter/SvgToXamlConverter.csproj +++ b/SvgModel/SvgModel.csproj @@ -4,7 +4,7 @@ netstandard2.0 True enable - SvgToXamlConverter + SvgModel latest @@ -19,8 +19,8 @@ https://github.com/wieslawsoltes/SvgToXaml - An Svg to Xaml conveter. - SvgToXamlConverter + An Svg drawing model. + SvgModel MIT svg;xaml;axaml;avalonia;avaloniaui;convert;converter;vector graphics;rendering;2d;graphics;geometry;shapes;skiasharp;skia diff --git a/SvgModel/System/Runtime/CompilerServices/IsExternalInit.cs b/SvgModel/System/Runtime/CompilerServices/IsExternalInit.cs new file mode 100644 index 0000000..39dca3a --- /dev/null +++ b/SvgModel/System/Runtime/CompilerServices/IsExternalInit.cs @@ -0,0 +1,6 @@ +#if NET461 || NETSTANDARD +namespace System.Runtime.CompilerServices +{ + internal static class IsExternalInit {} +} +#endif diff --git a/SvgToXaml.sln b/SvgToXaml.sln index 64ed0ae..69d19d7 100644 --- a/SvgToXaml.sln +++ b/SvgToXaml.sln @@ -5,7 +5,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvgToXaml", "SvgToXaml\SvgT EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "svgxaml", "svgxaml\svgxaml.csproj", "{06BE0887-524B-4119-814D-42AE1DCA4C91}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvgToXamlConverter", "SvgToXamlConverter\SvgToXamlConverter.csproj", "{DFCAD52A-92A3-4421-9620-923A9271D016}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvgModel", "SvgModel\SvgModel.csproj", "{DFCAD52A-92A3-4421-9620-923A9271D016}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvgConverter", "SvgConverter\SvgConverter.csproj", "{DC4DBC06-FA81-4658-8718-F0A004CE0C2E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,5 +27,9 @@ Global {DFCAD52A-92A3-4421-9620-923A9271D016}.Debug|Any CPU.Build.0 = Debug|Any CPU {DFCAD52A-92A3-4421-9620-923A9271D016}.Release|Any CPU.ActiveCfg = Release|Any CPU {DFCAD52A-92A3-4421-9620-923A9271D016}.Release|Any CPU.Build.0 = Release|Any CPU + {DC4DBC06-FA81-4658-8718-F0A004CE0C2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC4DBC06-FA81-4658-8718-F0A004CE0C2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC4DBC06-FA81-4658-8718-F0A004CE0C2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC4DBC06-FA81-4658-8718-F0A004CE0C2E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/SvgToXaml/App.axaml.cs b/SvgToXaml/App.axaml.cs index f5267ba..abf8ae1 100644 --- a/SvgToXaml/App.axaml.cs +++ b/SvgToXaml/App.axaml.cs @@ -11,9 +11,7 @@ namespace SvgToXaml { public class App : Application { - private const string SettingsFileName = "settings.json"; - - private const string PathsFileName = "paths.txt"; + private const string ProjectFileName = "project.json"; public override void Initialize() { @@ -35,36 +33,26 @@ namespace SvgToXaml desktop.Startup += (_, _) => { - if (File.Exists(SettingsFileName)) + if (File.Exists(ProjectFileName)) { - var json = File.ReadAllText(SettingsFileName); - var settings = JsonSerializer.Deserialize(json); - if (settings is { }) + var json = File.ReadAllText(ProjectFileName); + var project = JsonSerializer.Deserialize(json); + if (project is { }) { - mainViewModel.Settings = settings; - } - } - - if (File.Exists(PathsFileName)) - { - var paths = File.ReadAllLines(PathsFileName); - if (paths.Length > 0) - { - mainViewModel.Add(paths); + mainViewModel.Project = project; + + foreach (var fileItemViewModel in mainViewModel.Project.Items) + { + mainViewModel.Initialize(fileItemViewModel); + } } } }; desktop.Exit += (_, _) => { - var paths = mainViewModel.Items?.Select(x => x.Path); - if (paths is { }) - { - File.WriteAllLines(PathsFileName, paths); - } - - var json = JsonSerializer.Serialize(mainViewModel.Settings); - File.WriteAllText(SettingsFileName, json); + var json = JsonSerializer.Serialize(mainViewModel.Project); + File.WriteAllText(ProjectFileName, json); }; } diff --git a/SvgToXaml/SvgToXaml.csproj b/SvgToXaml/SvgToXaml.csproj index c34f4f7..307419b 100644 --- a/SvgToXaml/SvgToXaml.csproj +++ b/SvgToXaml/SvgToXaml.csproj @@ -24,13 +24,13 @@ - + Converter\Model - + Converter\Generator - + Converter diff --git a/SvgToXaml/ViewModels/FileItemViewModel.cs b/SvgToXaml/ViewModels/FileItemViewModel.cs index fe0b1fb..a8e72e0 100644 --- a/SvgToXaml/ViewModels/FileItemViewModel.cs +++ b/SvgToXaml/ViewModels/FileItemViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json.Serialization; using System.Threading.Tasks; using System.Windows.Input; using ReactiveUI; @@ -14,44 +15,60 @@ namespace SvgToXaml.ViewModels private SKSvg? _svg; private SkiaSharp.SKPicture? _picture; + [JsonInclude] public string Name { get => _name; private set => this.RaiseAndSetIfChanged(ref _name, value); } + [JsonInclude] public string Path { get => _path; private set => this.RaiseAndSetIfChanged(ref _path, value); } + [JsonIgnore] public SKSvg? Svg { get => _svg; private set => this.RaiseAndSetIfChanged(ref _svg, value); } + [JsonIgnore] public SkiaSharp.SKPicture? Picture { get => _picture; private set => this.RaiseAndSetIfChanged(ref _picture, value); } - public ICommand PreviewCommand { get; } + [JsonIgnore] + public ICommand? PreviewCommand { get; private set; } - public ICommand RemoveCommand { get; } + [JsonIgnore] + public ICommand? RemoveCommand { get; private set; } - public FileItemViewModel(string name, string path, Func preview, Func remove) + [JsonConstructor] + public FileItemViewModel(string name, string path) { _name = name; _path = path; - - PreviewCommand = ReactiveCommand.CreateFromTask(async () => await preview(this)); - - RemoveCommand = ReactiveCommand.Create(async () => await remove(this)); + } + + public FileItemViewModel(string name, string path, Func preview, Func remove) + : this(name, path) + { + Initialize(preview, remove); } + public void Initialize(Func preview, Func remove) + { + PreviewCommand = ReactiveCommand.CreateFromTask(async () => await preview(this)); + + RemoveCommand = ReactiveCommand.Create(async () => await remove(this)); + } + public async Task Load() { if (_isLoading) diff --git a/SvgToXaml/ViewModels/MainWindowViewModel.cs b/SvgToXaml/ViewModels/MainWindowViewModel.cs index 24d9e45..afb472a 100644 --- a/SvgToXaml/ViewModels/MainWindowViewModel.cs +++ b/SvgToXaml/ViewModels/MainWindowViewModel.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; using System.Windows.Input; using Avalonia; @@ -20,56 +20,41 @@ namespace SvgToXaml.ViewModels { public class MainWindowViewModel : ViewModelBase { - private FileItemViewModel? _selectedItem; - private ObservableCollection? _items; - private SettingsViewModel _settings; + private ProjectViewModel _project; - public FileItemViewModel? SelectedItem + [JsonInclude] + public ProjectViewModel Project { - get => _selectedItem; - set => this.RaiseAndSetIfChanged(ref _selectedItem, value); - } - - public ObservableCollection? Items - { - get => _items; - set => this.RaiseAndSetIfChanged(ref _items, value); - } - - public SettingsViewModel Settings - { - get => _settings; - set => this.RaiseAndSetIfChanged(ref _settings, value); + get => _project; + set => this.RaiseAndSetIfChanged(ref _project, value); } + [JsonIgnore] public ICommand ClearCommand { get; } + [JsonIgnore] public ICommand AddCommand { get; } + [JsonIgnore] public ICommand CopySelectedCommand { get; } + [JsonIgnore] public ICommand CopyAllCommand { get; } + [JsonIgnore] public ICommand ExportSelectedCommand { get; } + [JsonIgnore] public ICommand ExportAllCommand { get; } + [JsonIgnore] public ICommand ClipboardCommand { get; } + [JsonConstructor] public MainWindowViewModel() { - _items = new ObservableCollection(); - - _settings = new SettingsViewModel() - { - EnableGenerateImage = true, - EnableGeneratePreview = true, - UseResources = true, - ReuseExistingResources = true, - UseCompatMode = false, - UseBrushTransform = false, - }; - + _project = new ProjectViewModel(); + ClearCommand = ReactiveCommand.Create(Clear); AddCommand = ReactiveCommand.CreateFromTask(async () => await Add()); @@ -85,7 +70,7 @@ namespace SvgToXaml.ViewModels ClipboardCommand = ReactiveCommand.CreateFromTask(async format => await Clipboard(format)); // ReSharper disable once AsyncVoidLambda - this.WhenAnyValue(x => x.SelectedItem).Subscribe(async x => + this.WhenAnyValue(x => x.Project.SelectedItem).Subscribe(async x => { if (x is { }) { @@ -94,13 +79,13 @@ namespace SvgToXaml.ViewModels }); // ReSharper disable once AsyncVoidLambda - this.WhenAnyValue(x => x.Settings.UseCompatMode).Subscribe(async _ => + this.WhenAnyValue(x => x.Project.Settings.UseCompatMode).Subscribe(async _ => { await Reload(); }); // ReSharper disable once AsyncVoidLambda - this.WhenAnyValue(x => x.Settings.UseBrushTransform).Subscribe(async _ => + this.WhenAnyValue(x => x.Project.Settings.UseBrushTransform).Subscribe(async _ => { await Reload(); }); @@ -108,8 +93,8 @@ namespace SvgToXaml.ViewModels private void Clear() { - SelectedItem = null; - _items?.Clear(); + Project.SelectedItem = null; + Project.Items.Clear(); } private async Task Add() @@ -133,12 +118,12 @@ namespace SvgToXaml.ViewModels private async Task CopySelected(string format) { - if (_selectedItem is null || string.IsNullOrWhiteSpace(format)) + if (Project.SelectedItem is null || string.IsNullOrWhiteSpace(format)) { return; } - var xaml = await ToXaml(_selectedItem, _settings.EnableGenerateImage); + var xaml = await ToXaml(Project.SelectedItem, Project.Settings.EnableGenerateImage); await SetClipboard(xaml); } @@ -150,17 +135,14 @@ namespace SvgToXaml.ViewModels return; } - var paths = Items?.Select(x => x.Path).ToList(); - if (paths is { }) - { - var xaml = await ToXamlStyles(paths); - await SetClipboard(xaml); - } + var paths = Project.Items.Select(x => x.Path).ToList(); + var xaml = await ToXamlStyles(paths); + await SetClipboard(xaml); } private async Task ExportSelected(string format) { - if (_selectedItem is null || string.IsNullOrWhiteSpace(format)) + if (Project.SelectedItem is null || string.IsNullOrWhiteSpace(format)) { return; } @@ -169,11 +151,11 @@ namespace SvgToXaml.ViewModels dlg.Filters.Add(new FileDialogFilter() { Name = "AXAML Files (*.axaml)", Extensions = new List { "axaml" } }); dlg.Filters.Add(new FileDialogFilter() { Name = "XAML Files (*.xaml)", Extensions = new List { "xaml" } }); dlg.Filters.Add(new FileDialogFilter() { Name = "All Files (*.*)", Extensions = new List { "*" } }); - dlg.InitialFileName = Path.GetFileNameWithoutExtension(_selectedItem.Path); + dlg.InitialFileName = Path.GetFileNameWithoutExtension(Project.SelectedItem.Path); var result = await dlg.ShowAsync((Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow); if (result is { }) { - var xaml = await ToXaml(_selectedItem, _settings.EnableGenerateImage); + var xaml = await ToXaml(Project.SelectedItem, Project.Settings.EnableGenerateImage); try { @@ -201,8 +183,8 @@ namespace SvgToXaml.ViewModels var result = await dlg.ShowAsync((Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow); if (result is { }) { - var paths = Items?.Select(x => x.Path).ToList(); - if (paths is { }) + var paths = Project.Items.Select(x => x.Path).ToList(); + if (paths.Count > 0) { try { @@ -251,14 +233,14 @@ namespace SvgToXaml.ViewModels var text = await Task.Run(() => { - if (_settings.EnableGenerateImage) + if (Project.Settings.EnableGenerateImage) { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; var xaml = converter.ToXamlImage(skSvg.Model); @@ -268,10 +250,10 @@ namespace SvgToXaml.ViewModels { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; var xaml = converter.ToXamlDrawingGroup(skSvg.Model); @@ -304,9 +286,8 @@ namespace SvgToXaml.ViewModels private async Task Reload() { - var items = _items; - - if (items is null || items.Count == 0) + var items = Project.Items; + if (items.Count == 0) { return; } @@ -319,7 +300,7 @@ namespace SvgToXaml.ViewModels } }); - if (_selectedItem is { } selectedItem) + if (Project.SelectedItem is { } selectedItem) { await selectedItem.Load(); } @@ -340,10 +321,10 @@ namespace SvgToXaml.ViewModels { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; var xaml = converter.ToXamlImage(fileItemViewModel.Svg.Model); @@ -353,10 +334,10 @@ namespace SvgToXaml.ViewModels { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; var xaml = converter.ToXamlDrawingGroup(fileItemViewModel.Svg.Model); @@ -374,13 +355,13 @@ namespace SvgToXaml.ViewModels { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; - var xaml = converter.ToXamlStyles(paths, _settings.EnableGenerateImage, _settings.EnableGeneratePreview); + var xaml = converter.ToXamlStyles(paths, Project.Settings.EnableGenerateImage, Project.Settings.EnableGeneratePreview); return converter.Format(xaml); }); } @@ -411,11 +392,8 @@ namespace SvgToXaml.ViewModels private async Task Add(string path) { - if (_items is { }) - { - var item = await Task.Run(() => new FileItemViewModel(Path.GetFileName(path), path, Preview, Remove)); - _items.Add(item); - } + var item = await Task.Run(() => new FileItemViewModel(Path.GetFileName(path), path, Preview, Remove)); + Project.Items.Add(item); } private async Task Preview(FileItemViewModel item) @@ -436,10 +414,10 @@ namespace SvgToXaml.ViewModels { var converter = new SvgConverter() { - UseCompatMode = _settings.UseCompatMode, - UseBrushTransform = _settings.UseBrushTransform, - ReuseExistingResources = _settings.ReuseExistingResources, - Resources = _settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null + UseCompatMode = Project.Settings.UseCompatMode, + UseBrushTransform = Project.Settings.UseBrushTransform, + ReuseExistingResources = Project.Settings.ReuseExistingResources, + Resources = Project.Settings.UseResources ? new SvgToXamlConverter.ResourceDictionary() : null }; var xaml = converter.ToXamlDrawingGroup(item.Svg.Model); @@ -478,22 +456,21 @@ namespace SvgToXaml.ViewModels { await Task.Run(() => { - if (_items is { }) - { - _items.Remove(item); - } + Project.Items.Remove(item); }); } + public void Initialize(FileItemViewModel item) + { + item.Initialize(Preview, Remove); + } + public void Add(IEnumerable paths) { - if (_items is { }) + foreach (var path in paths) { - foreach (var path in paths) - { - var item = new FileItemViewModel(Path.GetFileName(path), path, Preview, Remove); - _items.Add(item); - } + var item = new FileItemViewModel(Path.GetFileName(path), path, Preview, Remove); + Project.Items.Add(item); } } } diff --git a/SvgToXaml/ViewModels/ProjectViewModel.cs b/SvgToXaml/ViewModels/ProjectViewModel.cs new file mode 100644 index 0000000..7ad2227 --- /dev/null +++ b/SvgToXaml/ViewModels/ProjectViewModel.cs @@ -0,0 +1,50 @@ +using System.Collections.ObjectModel; +using System.Text.Json.Serialization; +using ReactiveUI; + +namespace SvgToXaml.ViewModels +{ + public class ProjectViewModel : ViewModelBase + { + private FileItemViewModel? _selectedItem; + private ObservableCollection _items; + private SettingsViewModel _settings; + + [JsonIgnore] + public FileItemViewModel? SelectedItem + { + get => _selectedItem; + set => this.RaiseAndSetIfChanged(ref _selectedItem, value); + } + + [JsonInclude] + public ObservableCollection Items + { + get => _items; + set => this.RaiseAndSetIfChanged(ref _items, value); + } + + [JsonInclude] + public SettingsViewModel Settings + { + get => _settings; + set => this.RaiseAndSetIfChanged(ref _settings, value); + } + + [JsonConstructor] + public ProjectViewModel() + { + _items = new ObservableCollection(); + + _settings = new SettingsViewModel() + { + EnableGenerateImage = true, + EnableGeneratePreview = true, + UseResources = true, + ReuseExistingResources = true, + UseCompatMode = false, + UseBrushTransform = false, + }; + } + } +} diff --git a/SvgToXaml/ViewModels/SettingsViewModel.cs b/SvgToXaml/ViewModels/SettingsViewModel.cs index 5e35305..c59cc86 100644 --- a/SvgToXaml/ViewModels/SettingsViewModel.cs +++ b/SvgToXaml/ViewModels/SettingsViewModel.cs @@ -1,4 +1,5 @@ -using ReactiveUI; +using System.Text.Json.Serialization; +using ReactiveUI; namespace SvgToXaml.ViewModels { @@ -11,40 +12,51 @@ namespace SvgToXaml.ViewModels private bool _useCompatMode; private bool _useBrushTransform; + [JsonInclude] public bool EnableGenerateImage { get => _enableGenerateImage; set => this.RaiseAndSetIfChanged(ref _enableGenerateImage, value); } + [JsonInclude] public bool EnableGeneratePreview { get => _enableGeneratePreview; set => this.RaiseAndSetIfChanged(ref _enableGeneratePreview, value); } + [JsonInclude] public bool UseResources { get => _useResources; set => this.RaiseAndSetIfChanged(ref _useResources, value); } + [JsonInclude] public bool ReuseExistingResources { get => _reuseExistingResources; set => this.RaiseAndSetIfChanged(ref _reuseExistingResources, value); } + [JsonInclude] public bool UseCompatMode { get => _useCompatMode; set => this.RaiseAndSetIfChanged(ref _useCompatMode, value); } + [JsonInclude] public bool UseBrushTransform { get => _useBrushTransform; set => this.RaiseAndSetIfChanged(ref _useBrushTransform, value); } + + [JsonConstructor] + public SettingsViewModel() + { + } } } diff --git a/SvgToXaml/Views/MainView.axaml b/SvgToXaml/Views/MainView.axaml index 34aafaa..c1ba142 100644 --- a/SvgToXaml/Views/MainView.axaml +++ b/SvgToXaml/Views/MainView.axaml @@ -92,8 +92,8 @@ Opacity="0.3" Height="0.55"/> - @@ -250,7 +250,7 @@ - + - + diff --git a/svgxaml/svgxaml.csproj b/svgxaml/svgxaml.csproj index 3c340d0..66f18b9 100644 --- a/svgxaml/svgxaml.csproj +++ b/svgxaml/svgxaml.csproj @@ -37,13 +37,13 @@ - + Converter\Model - + Converter\Generator - + Converter