88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
using Microsoft.Win32;
|
|
using Prism.Commands;
|
|
using System.Windows;
|
|
|
|
namespace PLCSharp.Models;
|
|
|
|
public partial class GlobalModel
|
|
{
|
|
internal ProjectDirectory Project { get; }
|
|
public string ProjectLocation => Project.Root ?? "未保存工程";
|
|
private AsyncDelegateCommand<string> projectManage;
|
|
public AsyncDelegateCommand<string> ProjectManage => projectManage ??= new AsyncDelegateCommand<string>(ManageProjectAsync);
|
|
|
|
private static string ChooseFolder(string title)
|
|
{
|
|
var dialog = new OpenFolderDialog { Title = title };
|
|
return dialog.ShowDialog() == true ? dialog.FolderName : null;
|
|
}
|
|
|
|
internal bool SaveProject()
|
|
{
|
|
try
|
|
{
|
|
var root = Project.Root ?? ChooseFolder("选择空文件夹以保存工程");
|
|
if (root == null) return false;
|
|
Project.Save(root);
|
|
RaisePropertyChanged(nameof(ProjectLocation));
|
|
foreach (var recipe in Recipes)
|
|
{
|
|
recipe.Prompt = "";
|
|
foreach (var flow in Project.Flows(recipe.ID))
|
|
if (flow.Prompt == "已修改,请保存") flow.Prompt = "";
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SendErrDialog($"工程保存失败:{ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task ManageProjectAsync(string action)
|
|
{
|
|
if (WorkflowsModel.IsBusy || ModeState.State.Execute)
|
|
{
|
|
SendInfoDialog("请先停止运行并等待配方切换完成。");
|
|
return;
|
|
}
|
|
if (action == "Save")
|
|
{
|
|
SaveProject();
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
if (Project.HasChanges)
|
|
{
|
|
var answer = MessageBox.Show("当前工程有未保存的修改。是否保存?", "切换工程",
|
|
MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
|
|
if (answer == MessageBoxResult.Cancel || (answer == MessageBoxResult.Yes && !SaveProject())) return;
|
|
}
|
|
var folder = ChooseFolder(action == "New" ? "选择空文件夹以新建工程" : "选择包含 project.json 的工程目录");
|
|
if (folder == null) return;
|
|
using var next = action == "New" ? ProjectDirectory.New() : ProjectDirectory.Open(folder);
|
|
if (action == "New") next.Save(folder);
|
|
// Hardware modules own pollers and vendor handles for their whole process lifetime.
|
|
// Start the replacement session after this one has released its single-instance mutex.
|
|
await WorkflowsModel.ChangeRecipeAsync(() =>
|
|
{
|
|
var start = new System.Diagnostics.ProcessStartInfo(Environment.ProcessPath) { UseShellExecute = false };
|
|
if (string.Equals(System.IO.Path.GetFileNameWithoutExtension(Environment.ProcessPath), "dotnet", StringComparison.OrdinalIgnoreCase))
|
|
start.ArgumentList.Add(typeof(App).Assembly.Location);
|
|
start.ArgumentList.Add("--switch-project");
|
|
start.ArgumentList.Add(next.Root);
|
|
System.Diagnostics.Process.Start(start)?.Dispose();
|
|
((App)Application.Current).SwitchingProject = true;
|
|
Application.Current.Shutdown();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SendErrDialog($"工程操作失败:{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|