using XFEWorkflow.Abstractions; namespace XFEWorkflow.Views; public sealed partial class MainPage { private bool documentLayout; private ColumnDefinition? documentPaletteColumn; private RowDefinition? documentOutputRow; private Grid? documentWorkspace; private double PaletteWidth => documentPaletteColumn?.ActualWidth ?? (documentLayout ? PaletteSplitter.PreferredSize : PaletteColumn.ActualWidth); private double EditorWorkspaceWidth => documentWorkspace?.ActualWidth ?? (EditorLayout.ActualWidth + (documentLayout ? PaletteWidth + 8 : 0)); private double OutputHeight => documentOutputRow?.ActualHeight ?? OutputPanelRow.ActualHeight; private double OutputWorkspaceHeight => documentWorkspace is { } workspace ? workspace.ActualHeight - EditorStatus.ActualHeight - 44 : WorkspacePanels.ActualHeight; internal async Task OpenDocumentAsync(WorkflowDefinition workflow) { initialized = true; LegacyHeader.Visibility = WorkflowLibraryExpander.Visibility = LegacyCommands.Visibility = Visibility.Collapsed; WorkflowPageLayout.RowSpacing = 0; documentLayout = true; DocumentBackground.Visibility = Visibility.Visible; EditorLayout.Children.Remove(PalettePanel); EditorLayout.Children.Remove(PaletteSplitter); PaletteColumn.Width = EditorLayout.ColumnDefinitions[1].Width = new(0); WorkspacePanels.Children.Remove(OutputPanel); WorkspacePanels.Children.Remove(OutputSplitter); WorkflowPageLayout.Children.Remove(EditorStatus); WorkspacePanels.RowDefinitions[1].Height = OutputPanelRow.Height = new(0); EditorSurface.BorderThickness = new(0); EditorSurface.CornerRadius = new(0); PropertyPanelSurface.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent); await ViewModel.LoadAsync(); ViewModel.SelectedWorkflow = workflow; ready.TrySetResult(); ActivateDocument(); } internal void AttachDocumentPanels(Grid workspace, ColumnDefinition width, RowDefinition outputHeight) { documentWorkspace = workspace; documentPaletteColumn = width; documentOutputRow = outputHeight; Grid.SetRowSpan(PalettePanel, 4); Grid.SetRowSpan(PaletteSplitter, 4); workspace.Children.Add(PalettePanel); workspace.Children.Add(PaletteSplitter); Grid.SetColumn(OutputSplitter, 2); Grid.SetColumn(OutputPanel, 2); Grid.SetColumn(EditorStatus, 2); Grid.SetRow(EditorStatus, 3); workspace.Children.Add(OutputSplitter); workspace.Children.Add(OutputPanel); workspace.Children.Add(EditorStatus); workspace.SizeChanged += DocumentWorkspace_SizeChanged; PaletteSplitter.ReloadPreference(); OutputSplitter.ReloadPreference(); if (outputCollapsed) SetOutputHeight(42); ApplyPanelSizes(); } internal void DetachDocumentPanels() { if (documentWorkspace is { } workspace) { workspace.SizeChanged -= DocumentWorkspace_SizeChanged; workspace.Children.Remove(PalettePanel); workspace.Children.Remove(PaletteSplitter); workspace.Children.Remove(OutputSplitter); workspace.Children.Remove(OutputPanel); workspace.Children.Remove(EditorStatus); } documentPaletteColumn = null; documentOutputRow = null; documentWorkspace = null; } private void DocumentWorkspace_SizeChanged(object sender, SizeChangedEventArgs e) => ApplyPanelSizes(); private void SetPaletteWidth(double value) { if (documentPaletteColumn is { } column) column.Width = new(value); else if (!documentLayout) PaletteColumn.Width = new(value); } private void SetOutputHeight(double value) { if (documentOutputRow is { } row) row.Height = new(value); else if (!documentLayout) OutputPanelRow.Height = new(value); } internal void ActivateDocument() { if (disposed) return; Current = this; runUpdateTimer.Start(); ViewModel.IsRunning = services.Runs.IsRunning(ViewModel.SelectedWorkflow?.Id ?? ""); ApplyDebugState(services.Runs.DebugState(ViewModel.SelectedWorkflow?.Id ?? "")); CodeEditor.IsAnalysisEnabled = CodeEditor.Visibility == Visibility.Visible; } internal void DeactivateDocument() { runUpdateTimer.Stop(); CodeEditor.IsAnalysisEnabled = false; } internal async Task CloseDocumentAsync() { await ViewModel.SaveAsync(); disposed = true; runUpdateTimer.Stop(); runUpdates.Select(null); services.Scheduler.Changed -= schedulerChanged; services.Plugins.Changed -= pluginsChanged; services.Runs.RunChanged -= runChanged; CodeEditor.IsAnalysisEnabled = false; await CodeEditor.AnalysisTask; CodeEditor.Dispose(); if (ReferenceEquals(Current, this)) Current = null; } internal void NavigateToNode(string? nodeId, int line = 0, int column = 0) { if (nodeId is not null) LocateNode(nodeId); if (line > 0) CodeEditor.GoTo(line, column); } internal Task SaveDocumentAsync() { ValidateValueEditors(); return ViewModel.SaveAsync(); } internal async Task ExecuteEditorCommandAsync(string command) { switch (command) { case "save": await ViewModel.SaveAsync(); break; case "validate": Validate_Click(this, new()); break; case "run": case "stepOver": case "stepInto": case "pause": await RunOrDebugAsync(command); break; case "breakpoint": CanvasEditor.ToggleBreakpoint(); break; case "stop": await ViewModel.StopAsync(); break; case "undo": CanvasEditor.Undo(); break; case "redo": CanvasEditor.Redo(); break; case "copy": CanvasEditor.Copy(); break; case "paste": await CanvasEditor.PasteAsync(); break; case "delete": CanvasEditor.Delete(); break; case "selectAll": CanvasEditor.SelectAll(); break; case "search": PaletteSearch.Focus(FocusState.Keyboard); PaletteSearch.SelectAll(); break; case "properties": Properties_Click(this, new()); break; case "output": ToggleOutput_Click(this, new()); break; case "center": CenterCanvas_Click(this, new()); break; case "layout": ResetLayout_Click(this, new()); break; case "settings": WorkflowSettings_Click(this, new()); break; case "export": Export_Click(this, new()); break; case "node": await SaveAsNodeAsync(); break; case "rename": if (ViewModel.SelectedWorkflow is not { } workflow) break; var name = new TextBox { Header = "工作流名称", Text = workflow.Name, MinWidth = 320 }; var dialog = new ContentDialog { Title = "重命名", Content = name, PrimaryButtonText = "保存", CloseButtonText = "取消", XamlRoot = XamlRoot }; dialog.PrimaryButtonClick += (_, e) => e.Cancel = string.IsNullOrWhiteSpace(name.Text); if (await dialog.ShowAsync() == ContentDialogResult.Primary) { WorkflowName.Text = name.Text; await RenameWorkflowAsync(); } break; } } }