Skip to content

Commit

Permalink
Merge pull request #72 from 3meraldK/save-all-files
Browse files Browse the repository at this point in the history
Save all files
  • Loading branch information
NotroDev authored Jun 11, 2024
2 parents 8fa6a57 + f52260d commit bf85256
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
5 changes: 5 additions & 0 deletions SkEditor/Controls/MainMenuControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<ui:SymbolIcon Symbol="SaveAs"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource MenuHeaderSaveAll}" Name="MenuItemSaveAll" HotKey="Ctrl+Alt+S" InputGesture="Ctrl+Alt+S">
<MenuItem.Icon>
<ui:SymbolIcon Symbol="SaveAs"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource MenuHeaderPublish}" Name="MenuItemPublish" HotKey="Ctrl+Shift+P" InputGesture="Ctrl+Shift+P">
<MenuItem.Icon>
<ui:SymbolIcon Symbol="Share"/>
Expand Down
5 changes: 3 additions & 2 deletions SkEditor/Controls/MainMenuControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions SkEditor/Languages/English.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<system:String x:Key="MenuHeaderOpen">Open</system:String>
<system:String x:Key="MenuHeaderSave">Save</system:String>
<system:String x:Key="MenuHeaderSaveAs">Save as</system:String>
<system:String x:Key="MenuHeaderSaveAll">Save all</system:String>
<system:String x:Key="MenuHeaderPublish">Publish</system:String>
<system:String x:Key="MenuHeaderClose">Close</system:String>
<system:String x:Key="MenuHeaderCloseCurrentFile">Current file</system:String>
Expand Down
2 changes: 1 addition & 1 deletion SkEditor/Utilities/Editor/TextEditorEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
47 changes: 39 additions & 8 deletions SkEditor/Utilities/Files/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bf85256

Please sign in to comment.