Files
serein-flow/Workbench/ViewModels/FlowEditViewModel.cs

120 lines
3.4 KiB
C#
Raw Normal View History

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Serein.Workbench.Api;
using Serein.Workbench.Models;
using Serein.Workbench.Node.View;
using Serein.Workbench.Services;
using Serein.Workbench.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Serein.Workbench.ViewModels
{
/// <summary>
/// 流程编辑数据视图
/// </summary>
public partial class FlowEditViewModel : ObservableObject
{
2025-05-27 23:46:06 +08:00
/// <summary>
/// 画布集合
/// </summary>
[ObservableProperty]
private ObservableCollection<FlowEditorTabModel> _canvasTabs = [];
/// <summary>
/// 当前选择的画布
/// </summary>
[ObservableProperty]
private FlowEditorTabModel _selectedTab;
private readonly FlowNodeService flowNodeService;
public FlowEditViewModel(FlowNodeService flowNodeService)
{
this.flowNodeService = flowNodeService;
flowNodeService.OnCreateFlowCanvasView += OnCreateFlowCanvasView; // 创建了画布
flowNodeService.OnRemoveFlowCanvasView += OnRemoveFlowCanvasView; // 移除了画布
//this.PropertyChanged += OnPropertyChanged;
}
partial void OnSelectedTabChanged(FlowEditorTabModel value)
{
flowNodeService.CurrentSelectCanvas = value.Content;
}
#region
private void OnCreateFlowCanvasView(FlowCanvasView canvas)
{
2025-05-27 23:46:06 +08:00
var tab = new FlowEditorTabModel(canvas);
if(canvas is IFlowCanvas flowCanvas)
{
tab.Model = flowCanvas.Model;
}
CanvasTabs.Add(tab);
SelectedTab = tab;
}
private void OnRemoveFlowCanvasView(FlowCanvasView canvas)
{
var tab = CanvasTabs.FirstOrDefault(c => c.Content.Guid.Equals(canvas.Guid));
if (tab is null)
{
return;
}
CanvasTabs.Remove(tab);
if (CanvasTabs.Count > 0 && CanvasTabs[^1] is FlowEditorTabModel c)
{
SelectedTab = c;
}
}
#endregion
/// <summary>
/// 进入编辑模式
/// </summary>
/// <param name="tab"></param>
public void StartEditingTab(FlowEditorTabModel tab)
{
if (tab != null)
{
tab.IsEditing = true;
OnPropertyChanged(nameof(CanvasTabs)); // 刷新Tabs集合以便更新UI
}
}
/// <summary>
/// 结束编辑,重命名
/// </summary>
/// <param name="tab"></param>
/// <param name="newName"></param>
public void EndEditingTab(FlowEditorTabModel tab, string? newName = null)
{
if (tab != null)
{
tab.IsEditing = false;
2025-05-27 23:46:06 +08:00
if(tab.Model.Name != newName && !string.IsNullOrWhiteSpace(newName)) tab.Model.Name = newName; // 名称合法时设置新名称
OnPropertyChanged(nameof(CanvasTabs)); // 刷新Tabs集合
}
}
}
}