mirror of
https://onedev.site.tesses.net/crosslang/crosslangdevstudio
synced 2026-02-08 09:15:45 +00:00
136 lines
3.7 KiB
C#
136 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Markup.Xaml.Templates;
|
|
using Avalonia.Media;
|
|
using Avalonia.Platform.Storage;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using CrossLangDevStudio.Messages;
|
|
using CrossLangDevStudio.Views;
|
|
|
|
namespace CrossLangDevStudio.ViewModels;
|
|
|
|
public partial class NewProjectDialogViewModel : ViewModelBase
|
|
{
|
|
public ObservableCollection<CrossLangTemplate> CrossLangTemplates { get; set; } = new ObservableCollection<CrossLangTemplate>();
|
|
bool updated = false;
|
|
string _name = "";
|
|
public string Name
|
|
{
|
|
get
|
|
{
|
|
return _name;
|
|
}
|
|
set
|
|
{
|
|
updated = true;
|
|
SetProperty(ref _name, value, nameof(Name));
|
|
}
|
|
}
|
|
|
|
private void SelectProjectType(string name)
|
|
{
|
|
|
|
|
|
if (!updated)
|
|
{
|
|
var newName = name.Replace(" ", "");
|
|
int i;
|
|
for (i = 1; i < int.MaxValue; i++)
|
|
{
|
|
string name2 = $"{newName}{i}";
|
|
if (!Directory.Exists(Path.Combine(ParentDirectory, name2)))
|
|
{
|
|
Name = name2;
|
|
updated = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[ObservableProperty]
|
|
private string _parentDirectory = CrossLangShell.Settings.ProjectDirectory;
|
|
CrossLangTemplate? _template = null;
|
|
public CrossLangTemplate? SelectedTemplate
|
|
{
|
|
get
|
|
{
|
|
return _template;
|
|
}
|
|
set
|
|
{
|
|
SetProperty(ref _template, value, nameof(SelectedTemplate));
|
|
if (value is not null)
|
|
{
|
|
SelectProjectType(value.Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
public NewProjectDialogViewModel()
|
|
{
|
|
foreach (var item in Directory.EnumerateFiles(Path.Combine(CrossLangShell.CrossLangConfigDirectory, "Templates"), "*.crvm"))
|
|
{
|
|
CrossLangFile file = new CrossLangFile();
|
|
try
|
|
{
|
|
file.Load(item);
|
|
CrossLangTemplates.Add(new CrossLangTemplate(Path.GetFileNameWithoutExtension(item), file));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
}
|
|
}
|
|
}
|
|
[RelayCommand]
|
|
public async Task BrowseAsync()
|
|
{
|
|
var window = await WeakReferenceMessenger.Default.Send<GetWindowMessage>();
|
|
|
|
|
|
var res = await window.StorageProvider.OpenFolderPickerAsync(new Avalonia.Platform.Storage.FolderPickerOpenOptions() { Title = "Pick Project Parent Folder" });
|
|
if (res.Count != 0)
|
|
{
|
|
string? path = res[0].TryGetLocalPath();
|
|
if(!string.IsNullOrWhiteSpace(path))
|
|
this.ParentDirectory = path;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void OK()
|
|
{
|
|
if (_template is not null && !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(ParentDirectory))
|
|
{
|
|
var path = Path.Combine(ParentDirectory, Name);
|
|
Directory.CreateDirectory(path);
|
|
if (CrossLangShell.CreateProject(_template.TemplateName, path))
|
|
{
|
|
WeakReferenceMessenger.Default.Send(new NewProjectCloseMessage(path));
|
|
}
|
|
|
|
}
|
|
}
|
|
[RelayCommand]
|
|
private void Cancel()
|
|
{
|
|
WeakReferenceMessenger.Default.Send(new NewProjectCloseMessage(null));
|
|
}
|
|
}
|
|
|
|
public partial class CrossLangTemplate(string name,CrossLangFile file) : ObservableObject
|
|
{
|
|
|
|
[ObservableProperty]
|
|
private string _name = file.GetPrettyName();
|
|
[ObservableProperty]
|
|
private IImage _icon = file.GetIcon(64);
|
|
|
|
public string TemplateName = name;
|
|
|
|
} |