using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Threading; using Avalonia.VisualTree; using Prism.Dialogs; using SopHelper.Engine.Sop; using SopHelper.Engine.Recipes; using SopHelper.VVMs.RecipeEditor; internal static class RecipeCloseChecks { public static void Run(string root) { var store = new RecipeWorkspace(root); var config = new SopConfig(); config.Sop.Name = "关闭保存验证"; var savedCalls = 0; RecipeEditorViewModel Create(bool isNew) { var model = new RecipeEditorViewModel(); model.OnDialogOpened(new DialogParameters { { "directory", store.RecipesDirectory }, { "config", config }, { "path", isNew ? "" : store.ConfigPath(config.Sop.Name) }, { "new", isNew }, { "onSaved", new Action(_ => savedCalls++) } }); return model; } // 新建默认值、回退到原值及手动保存后的内容,都应直接关闭。 void CloseWithoutPrompt(RecipeEditorViewModel editor) { var cleanWindow = new Window { Content = new RecipeEditor { DataContext = editor } }; cleanWindow.Show(); cleanWindow.Close(); Until(() => !cleanWindow.IsVisible || cleanWindow.OwnedWindows.Any()); Check(!cleanWindow.IsVisible && !cleanWindow.OwnedWindows.Any(), "没有未保存修改时直接关闭"); editor.OnDialogClosed(); } CloseWithoutPrompt(Create(true)); Check(savedCalls == 0 && !File.Exists(store.ConfigPath(config.Sop.Name)), "未编辑的新配方直接关闭且不落盘"); var model = Create(true); model.Recipe.Description = "新配方内容"; var view = new RecipeEditor { DataContext = model }; var window = new Window { Content = view, Width = 1040, Height = 800 }; window.Show(); window.Close(); Until(() => window.OwnedWindows.Any()); var prompt = window.OwnedWindows.Single(); var buttons = prompt.GetVisualDescendants().OfType