153 lines
4.3 KiB
C#
153 lines
4.3 KiB
C#
using PLCSharp.Core.Prism;
|
|
using PLCSharp.Models;
|
|
using Prism.Commands;
|
|
using Prism.Dialogs;
|
|
using Prism.Events;
|
|
using Prism.Ioc;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace PLCSharp.VVMs.Workflows
|
|
{
|
|
/// <summary>
|
|
/// 工作流配置视图模型
|
|
/// </summary>
|
|
public class WorkflowConfigViewModel : DialogAwareBase
|
|
{
|
|
/// <summary>
|
|
/// 工作流配置视图模型
|
|
/// </summary>
|
|
public WorkflowConfigViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
|
|
|
|
{
|
|
GlobalModel = container.Resolve<GlobalModel>();
|
|
_dialogService = dialogService;
|
|
}
|
|
|
|
private readonly IDialogService _dialogService;
|
|
|
|
private AsyncDelegateCommand _Save;
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
public AsyncDelegateCommand Save =>
|
|
_Save ??= new AsyncDelegateCommand(ExecuteSaveAsync);
|
|
|
|
private async Task ExecuteSaveAsync()
|
|
{
|
|
await CustomTextEditor.FormatCodeAsync();
|
|
GlobalModel.WorkflowsModel.Manage.Execute("Save");
|
|
}
|
|
|
|
private AsyncDelegateCommand _Compile;
|
|
|
|
/// <summary>
|
|
/// 编译
|
|
/// </summary>
|
|
public AsyncDelegateCommand Compile =>
|
|
_Compile ??= new AsyncDelegateCommand(ExecuteCompileAsync);
|
|
|
|
private async Task ExecuteCompileAsync()
|
|
{
|
|
SelectedFlowTask.CompiledTime = DateTime.Now;
|
|
SelectedFlowTask.Flow.Step = 0;
|
|
await CustomTextEditor.FormatCodeAsync();
|
|
if (GlobalModel.SaveProject()) SelectedFlowTask.Compile.Execute();
|
|
}
|
|
|
|
private Workflow _SelectedFlowTask;
|
|
|
|
/// <summary>
|
|
/// SelectedFlowTask
|
|
/// </summary>
|
|
public Workflow SelectedFlowTask
|
|
{
|
|
get { return _SelectedFlowTask; }
|
|
set { SetProperty(ref _SelectedFlowTask, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 快捷代码片段列表
|
|
/// </summary>
|
|
public ObservableCollection<SnippetItem> SnippetItems { get; set; } = [];
|
|
|
|
/// <summary>
|
|
/// 打开对话框后要执行的
|
|
/// </summary>
|
|
/// <param name="parameters">parameters</param>
|
|
public override void OnDialogOpened(IDialogParameters parameters)
|
|
{
|
|
SelectedFlowTask = parameters.GetValue<Workflow>("SelectedTask");
|
|
LoadSnippets();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从 Script 目录加载 .spt 文件
|
|
/// </summary>
|
|
private void LoadSnippets()
|
|
{
|
|
SnippetItems.Clear();
|
|
foreach (var item in ScriptManager.LoadAll())
|
|
{
|
|
SnippetItems.Add(item);
|
|
}
|
|
}
|
|
|
|
private DelegateCommand _ManageScript;
|
|
|
|
/// <summary>
|
|
/// 打开脚本管理对话框,关闭后刷新快捷按钮列表
|
|
/// </summary>
|
|
public DelegateCommand ManageScript =>
|
|
_ManageScript ??= new DelegateCommand(ExecuteManageScript);
|
|
|
|
private void ExecuteManageScript()
|
|
{
|
|
_dialogService.Show("ScriptManageDialog", null, r =>
|
|
{
|
|
LoadSnippets();
|
|
});
|
|
}
|
|
|
|
private DelegateCommand _Run;
|
|
|
|
/// <summary>
|
|
/// 运行
|
|
/// </summary>
|
|
public DelegateCommand Run =>
|
|
_Run ??= new DelegateCommand(ExecuteRun);
|
|
|
|
private void ExecuteRun()
|
|
{
|
|
if (SelectedFlowTask.AutomaticExecution == false)
|
|
{
|
|
SelectedFlowTask.Run(GlobalModel);
|
|
}
|
|
else
|
|
{
|
|
GlobalModel.SendErrDialog("自动运行中,不能手动运行!"); ;
|
|
}
|
|
}
|
|
|
|
private readonly GlobalModel GlobalModel;
|
|
|
|
private DelegateCommand<string> _FastEntry;
|
|
|
|
/// <summary>
|
|
/// FastEntry
|
|
/// </summary>
|
|
public DelegateCommand<string> FastEntry =>
|
|
_FastEntry ??= new DelegateCommand<string>(ExecuteFastEntry);
|
|
|
|
private void ExecuteFastEntry(string content)
|
|
{
|
|
if (!string.IsNullOrEmpty(content))
|
|
CustomTextEditor.Insert(content);
|
|
}
|
|
|
|
/// <summary>
|
|
/// CustomTextEditor
|
|
/// </summary>
|
|
public CustomTextEditor CustomTextEditor { get; set; }
|
|
}
|
|
} |