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 { /// /// 流程编辑数据视图 /// public partial class FlowEditViewModel : ObservableObject { /// /// 画布集合 /// [ObservableProperty] private ObservableCollection _canvasTabs = []; /// /// 当前选择的画布 /// [ObservableProperty] private FlowEditorTabModel _selectedTab; private readonly FlowNodeService flowNodeService; public FlowEditViewModel(FlowNodeService flowNodeService) { this.flowNodeService = flowNodeService; flowNodeService.OnCreateFlowCanvasView += OnCreateFlowCanvasView; // 创建了画布 flowNodeService.OnRemoveFlowCanvasView += OnRemoveFlowCanvasView; // 移除了画布 this.PropertyChanged += OnPropertyChanged; } private void OnPropertyChanged(object? value, PropertyChangedEventArgs e) { if (this.SelectedTab is null) return; flowNodeService.CurrentSelectCanvas = this.SelectedTab.Content; } #region 响应环境事件 private void OnCreateFlowCanvasView(FlowCanvasView canvas) { 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 /// /// 进入编辑模式 /// /// public void StartEditingTab(FlowEditorTabModel tab) { if (tab != null) { tab.IsEditing = true; OnPropertyChanged(nameof(CanvasTabs)); // 刷新Tabs集合,以便更新UI } } /// /// 结束编辑,重命名 /// /// /// public void EndEditingTab(FlowEditorTabModel tab, string? newName = null) { if (tab != null) { tab.IsEditing = false; if(tab.Model.Name != newName && !string.IsNullOrWhiteSpace(newName)) tab.Model.Name = newName; // 名称合法时设置新名称 OnPropertyChanged(nameof(CanvasTabs)); // 刷新Tabs集合 } } } }