From f52260d846a3fb8a96145de9d1c4af5486e94b47 Mon Sep 17 00:00:00 2001 From: 3meraldK Date: Tue, 11 Jun 2024 03:30:25 +0200 Subject: [PATCH] Save all files --- SkEditor/Controls/MainMenuControl.axaml | 5 ++ SkEditor/Controls/MainMenuControl.axaml.cs | 5 +- SkEditor/Languages/English.xaml | 1 + .../Editor/TextEditorEventHandler.cs | 2 +- SkEditor/Utilities/Files/FileHandler.cs | 47 +++++++++++++++---- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/SkEditor/Controls/MainMenuControl.axaml b/SkEditor/Controls/MainMenuControl.axaml index 911af87d..3c85e126 100644 --- a/SkEditor/Controls/MainMenuControl.axaml +++ b/SkEditor/Controls/MainMenuControl.axaml @@ -38,6 +38,11 @@ + + + + + diff --git a/SkEditor/Controls/MainMenuControl.axaml.cs b/SkEditor/Controls/MainMenuControl.axaml.cs index 36977813..c02711be 100644 --- a/SkEditor/Controls/MainMenuControl.axaml.cs +++ b/SkEditor/Controls/MainMenuControl.axaml.cs @@ -31,13 +31,14 @@ private void AssignCommands() MenuItemOpenFolder.Command = new RelayCommand(() => ProjectOpener.OpenProject()); MenuItemSave.Command = new RelayCommand(async () => { - (bool, Exception) success = await FileHandler.SaveFile(); + (bool, Exception) success = await FileHandler.SaveFile(null); if (!success.Item1) { ApiVault.Get().ShowError("For some reason, the file couldn't be saved. If the problem persists, backup the file so you won't lose any changes.\nError: " + success.Item2.Message); } }); - MenuItemSaveAs.Command = new RelayCommand(FileHandler.SaveAsFile); + MenuItemSaveAs.Command = new RelayCommand(FileHandler.SaveAsFileSingle); + MenuItemSaveAll.Command = new RelayCommand(FileHandler.SaveAllFiles); MenuItemPublish.Command = new RelayCommand(() => new PublishWindow().ShowDialog(ApiVault.Get().GetMainWindow())); MenuItemClose.Command = new RelayCommand(FileCloser.CloseCurrentFile); diff --git a/SkEditor/Languages/English.xaml b/SkEditor/Languages/English.xaml index 25d25a22..b2b5ab6d 100644 --- a/SkEditor/Languages/English.xaml +++ b/SkEditor/Languages/English.xaml @@ -28,6 +28,7 @@ Open Save Save as + Save all Publish Close Current file diff --git a/SkEditor/Utilities/Editor/TextEditorEventHandler.cs b/SkEditor/Utilities/Editor/TextEditorEventHandler.cs index 1cc55540..c2f3aaa3 100644 --- a/SkEditor/Utilities/Editor/TextEditorEventHandler.cs +++ b/SkEditor/Utilities/Editor/TextEditorEventHandler.cs @@ -95,7 +95,7 @@ public async static void OnTextChanged(object sender, EventArgs e) if (ApiVault.Get().GetAppConfig().IsAutoSaveEnabled && !string.IsNullOrEmpty(tab.Tag.ToString())) { - await Dispatcher.UIThread.InvokeAsync(FileHandler.SaveFile); + await Dispatcher.UIThread.InvokeAsync(FileHandler.SaveFileSingle); return; } diff --git a/SkEditor/Utilities/Files/FileHandler.cs b/SkEditor/Utilities/Files/FileHandler.cs index 1884122e..662066be 100644 --- a/SkEditor/Utilities/Files/FileHandler.cs +++ b/SkEditor/Utilities/Files/FileHandler.cs @@ -157,19 +157,18 @@ private static bool IsOnlyEmptyFileOpen() item.Header.ToString().Contains(Translation.Get("NewFileNameFormat").Replace("{0}", "")) && !item.Header.ToString().EndsWith('*'); } - - public static async Task<(bool, Exception)> SaveFile() + public static async Task<(bool, Exception)> SaveFile(TabViewItem? tab) { if (!ApiVault.Get().IsFileOpen()) return (true, null); try { - TabViewItem item = ApiVault.Get().GetTabView().SelectedItem as TabViewItem; + TabViewItem item = GetPreferredTabView(tab); string path = item.Tag.ToString(); if (string.IsNullOrEmpty(path)) { - SaveAsFile(); + SaveAsFile(tab); return (true, null); } @@ -188,14 +187,12 @@ private static bool IsOnlyEmptyFileOpen() return (true, null); } - public async static void SaveAsFile() + public async static void SaveAsFile(TabViewItem? tab = null) { if (!ApiVault.Get().IsFileOpen()) return; var topLevel = TopLevel.GetTopLevel(ApiVault.Get().GetMainWindow()); - var tabView = ApiVault.Get().GetTabView(); - - if (tabView.SelectedItem is not TabViewItem item) return; + TabViewItem item = GetPreferredTabView(tab); string header = item.Header.ToString().TrimEnd('*'); string itemTag = item.Tag.ToString(); @@ -238,6 +235,40 @@ public async static void SaveAsFile() AddChangeChecker(absolutePath, item); } + public async static void SaveAllFiles() + { + if (!ApiVault.Get().IsFileOpen()) return; + + var tabView = ApiVault.Get().GetTabView(); + var tabs = tabView.TabItems; + + foreach (TabViewItem tab in tabs) await SaveFile(tab); + } + + private static TabViewItem GetPreferredTabView(TabViewItem? tab) + { + if (tab == null) + { + var tabView = ApiVault.Get().GetTabView(); + if (tabView.SelectedItem is not TabViewItem) return null; + return tabView.SelectedItem as TabViewItem; + } + else + { + return tab; + } + } + + public static void SaveAsFileSingle() + { + SaveAsFile(null); + } + + public async static void SaveFileSingle() + { + await SaveFile(null); + } + public static void SwitchTab(int index) { var tabView = ApiVault.Get().GetTabView();