Skip to content

Commit

Permalink
Use file-scoped namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Dec 8, 2021
1 parent 9602f6a commit ac7a6eb
Show file tree
Hide file tree
Showing 37 changed files with 2,424 additions and 2,464 deletions.
73 changes: 36 additions & 37 deletions src/SvgToXaml/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,55 @@
using SvgToXaml.ViewModels;
using SvgToXaml.Views;

namespace SvgToXaml
namespace SvgToXaml;

public class App : Application
{
public class App : Application
private const string ProjectFileName = "project.json";

public override void Initialize()
{
private const string ProjectFileName = "project.json";
AvaloniaXamlLoader.Load(this);
}

public override void Initialize()
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
AvaloniaXamlLoader.Load(this);
}
var mainViewModel = new MainWindowViewModel();

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
var mainWindow = new MainWindow
{
var mainViewModel = new MainWindowViewModel();

var mainWindow = new MainWindow
{
DataContext = mainViewModel
};
DataContext = mainViewModel
};

desktop.MainWindow = mainWindow;
desktop.MainWindow = mainWindow;

desktop.Startup += (_, _) =>
desktop.Startup += (_, _) =>
{
if (File.Exists(ProjectFileName))
{
if (File.Exists(ProjectFileName))
var json = File.ReadAllText(ProjectFileName);
var project = JsonSerializer.Deserialize<ProjectViewModel>(json);
if (project is { })
{
var json = File.ReadAllText(ProjectFileName);
var project = JsonSerializer.Deserialize<ProjectViewModel>(json);
if (project is { })
{
mainViewModel.Project = project;
mainViewModel.Project = project;
foreach (var fileItemViewModel in mainViewModel.Project.Items)
{
mainViewModel.Initialize(fileItemViewModel);
}
foreach (var fileItemViewModel in mainViewModel.Project.Items)
{
mainViewModel.Initialize(fileItemViewModel);
}
}
};
}
};

desktop.Exit += (_, _) =>
{
var json = JsonSerializer.Serialize(mainViewModel.Project);
File.WriteAllText(ProjectFileName, json);
};
}

base.OnFrameworkInitializationCompleted();
desktop.Exit += (_, _) =>
{
var json = JsonSerializer.Serialize(mainViewModel.Project);
File.WriteAllText(ProjectFileName, json);
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
65 changes: 32 additions & 33 deletions src/SvgToXaml/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,41 @@
using Avalonia.Xaml.Interactions.Core;
using Avalonia.Xaml.Interactivity;

namespace SvgToXaml
namespace SvgToXaml;

class Program
{
class Program
[STAThread]
public static void Main(string[] args)
{
[STAThread]
public static void Main(string[] args)
{
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}

public static AppBuilder BuildAvaloniaApp()
{
GC.KeepAlive(typeof(SKPictureControl).Assembly);
GC.KeepAlive(typeof(Behavior).Assembly);
GC.KeepAlive(typeof(ComparisonConditionType).Assembly);
public static AppBuilder BuildAvaloniaApp()
{
GC.KeepAlive(typeof(SKPictureControl).Assembly);
GC.KeepAlive(typeof(Behavior).Assembly);
GC.KeepAlive(typeof(ComparisonConditionType).Assembly);

return AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseSkia()
//.UseDirect2D1()
.With(new Win32PlatformOptions()
{
UseDeferredRendering = true,
AllowEglInitialization = true,
UseWindowsUIComposition = true
})
.With(new X11PlatformOptions()
{
UseDeferredRendering = true
})
.With(new AvaloniaNativePlatformOptions()
{
UseDeferredRendering = true
})
.UseReactiveUI();
}
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseSkia()
//.UseDirect2D1()
.With(new Win32PlatformOptions()
{
UseDeferredRendering = true,
AllowEglInitialization = true,
UseWindowsUIComposition = true
})
.With(new X11PlatformOptions()
{
UseDeferredRendering = true
})
.With(new AvaloniaNativePlatformOptions()
{
UseDeferredRendering = true
})
.UseReactiveUI();
}
}
35 changes: 17 additions & 18 deletions src/SvgToXaml/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
using Avalonia.Controls.Templates;
using SvgToXaml.ViewModels;

namespace SvgToXaml
namespace SvgToXaml;

public class ViewLocator : IDataTemplate
{
public class ViewLocator : IDataTemplate
public bool SupportsRecycling => false;

public IControl Build(object data)
{
public bool SupportsRecycling => false;
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

public IControl Build(object data)
if (type != null)
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control) Activator.CreateInstance(type)!;
}
else
{
return new TextBlock {Text = "Not Found: " + name};
}
return (Control) Activator.CreateInstance(type)!;
}

public bool Match(object data)
else
{
return data is ViewModelBase;
return new TextBlock {Text = "Not Found: " + name};
}
}

public bool Match(object data)
{
return data is ViewModelBase;
}
}
151 changes: 75 additions & 76 deletions src/SvgToXaml/ViewModels/FileItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,99 @@
using ReactiveUI;
using Svg.Model;

namespace SvgToXaml.ViewModels
namespace SvgToXaml.ViewModels;

public class FileItemViewModel : ViewModelBase
{
public class FileItemViewModel : ViewModelBase
{
private bool _isLoading;
private string _name;
private string _path;
private SvgViewModel? _svg;
private SkiaSharp.SKPicture? _picture;
private bool _isLoading;
private string _name;
private string _path;
private SvgViewModel? _svg;
private SkiaSharp.SKPicture? _picture;

[JsonInclude]
public string Name
{
get => _name;
private set => this.RaiseAndSetIfChanged(ref _name, value);
}
[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);
}
[JsonInclude]
public string Path
{
get => _path;
private set => this.RaiseAndSetIfChanged(ref _path, value);
}

[JsonIgnore]
public SvgViewModel? Svg
{
get => _svg;
private set => this.RaiseAndSetIfChanged(ref _svg, value);
}
[JsonIgnore]
public SvgViewModel? Svg
{
get => _svg;
private set => this.RaiseAndSetIfChanged(ref _svg, value);
}

[JsonIgnore]
public SkiaSharp.SKPicture? Picture
{
get => _picture;
private set => this.RaiseAndSetIfChanged(ref _picture, value);
}
[JsonIgnore]
public SkiaSharp.SKPicture? Picture
{
get => _picture;
private set => this.RaiseAndSetIfChanged(ref _picture, value);
}

[JsonIgnore]
public ICommand? PreviewCommand { get; private set; }
[JsonIgnore]
public ICommand? PreviewCommand { get; private set; }

[JsonIgnore]
public ICommand? RemoveCommand { get; private set; }
[JsonIgnore]
public ICommand? RemoveCommand { get; private set; }

[JsonConstructor]
public FileItemViewModel(string name, string path)
{
_name = name;
_path = path;
}
[JsonConstructor]
public FileItemViewModel(string name, string path)
{
_name = name;
_path = path;
}

public FileItemViewModel(string name, string path, Func<FileItemViewModel, Task> preview, Func<FileItemViewModel, Task> remove)
: this(name, path)
{
Initialize(preview, remove);
}
public FileItemViewModel(string name, string path, Func<FileItemViewModel, Task> preview, Func<FileItemViewModel, Task> remove)
: this(name, path)
{
Initialize(preview, remove);
}

public void Initialize(Func<FileItemViewModel, Task> preview, Func<FileItemViewModel, Task> remove)
{
PreviewCommand = ReactiveCommand.CreateFromTask(async () => await preview(this));
public void Initialize(Func<FileItemViewModel, Task> preview, Func<FileItemViewModel, Task> remove)
{
PreviewCommand = ReactiveCommand.CreateFromTask(async () => await preview(this));

RemoveCommand = ReactiveCommand.Create(async () => await remove(this));
}
RemoveCommand = ReactiveCommand.Create(async () => await remove(this));
}

public async Task Load(DrawAttributes ignoreAttribute)
public async Task Load(DrawAttributes ignoreAttribute)
{
if (_isLoading)
{
if (_isLoading)
{
return;
}
return;
}

_isLoading = true;
_isLoading = true;

if (Picture is null)
if (Picture is null)
{
await Task.Run(() =>
{
await Task.Run(() =>
{
Svg = new SvgViewModel();
Picture = Svg.Load(Path, ignoreAttribute);
});
}

_isLoading = false;
Svg = new SvgViewModel();
Picture = Svg.Load(Path, ignoreAttribute);
});
}

public void Clean()
_isLoading = false;
}

public void Clean()
{
if (Picture is not null)
{
if (Picture is not null)
{
Picture?.Dispose();
Svg?.Dispose();
Picture = null;
Svg = null;
}
Picture?.Dispose();
Svg?.Dispose();
Picture = null;
Svg = null;
}
}
}
}
Loading

0 comments on commit ac7a6eb

Please sign in to comment.