From 9b015a80319c30fb4e575f11c28929ed8e91c34f Mon Sep 17 00:00:00 2001 From: NotroDev Date: Wed, 10 Jul 2024 09:57:41 +0200 Subject: [PATCH] Fix edit menu crashes in non-editor tabs --- SkEditor/Controls/MainMenuControl.axaml.cs | 32 ++++++++++++------- .../Utilities/Editor/CustomCommandsHandler.cs | 6 +++- SkEditor/Utilities/Files/FileBuilder.cs | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/SkEditor/Controls/MainMenuControl.axaml.cs b/SkEditor/Controls/MainMenuControl.axaml.cs index 1955652..73a1a69 100644 --- a/SkEditor/Controls/MainMenuControl.axaml.cs +++ b/SkEditor/Controls/MainMenuControl.axaml.cs @@ -1,6 +1,7 @@ using Avalonia.Controls; using CommunityToolkit.Mvvm.Input; using FluentAvalonia.UI.Controls; +using FluentAvalonia.UI.Windowing; using SkEditor.API; using SkEditor.Controls.Docs; using SkEditor.Utilities; @@ -12,6 +13,7 @@ using SkEditor.Views.Generators; using SkEditor.Views.Generators.Gui; using SkEditor.Views.Settings; +using System; namespace SkEditor.Controls; public partial class MainMenuControl : UserControl @@ -41,29 +43,35 @@ private void AssignCommands() MenuItemCloseAllLeft.Command = new RelayCommand(FileCloser.CloseAllToTheLeft); MenuItemCloseAllRight.Command = new RelayCommand(FileCloser.CloseAllToTheRight); - MenuItemCopy.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Copy()); - MenuItemPaste.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Paste()); - MenuItemCut.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Cut()); - MenuItemUndo.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Undo()); - MenuItemRedo.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Redo()); - MenuItemDelete.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor.Delete()); - MenuItemGoToLine.Command = new RelayCommand(() => SkEditorAPI.Windows.ShowWindow(new GoToLine())); + MenuItemCopy.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Copy()); + MenuItemPaste.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Paste()); + MenuItemCut.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Cut()); + MenuItemUndo.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Undo()); + MenuItemRedo.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Redo()); + MenuItemDelete.Command = new RelayCommand(() => SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.Delete()); + MenuItemGoToLine.Command = new RelayCommand(() => ShowDialogIfEditorIsOpen(new GoToLineWindow())); MenuItemTrimWhitespaces.Command = new RelayCommand(() => CustomCommandsHandler.OnTrimWhitespacesCommandExecuted(SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.TextArea)); - MenuItemDuplicate.Command = new RelayCommand(() => CustomCommandsHandler.OnDuplicateCommandExecuted(SkEditorAPI.Files.GetCurrentOpenedFile().Editor.TextArea)); - MenuItemComment.Command = new RelayCommand(() => CustomCommandsHandler.OnCommentCommandExecuted(SkEditorAPI.Files.GetCurrentOpenedFile().Editor.TextArea)); + MenuItemDuplicate.Command = new RelayCommand(() => CustomCommandsHandler.OnDuplicateCommandExecuted(SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.TextArea)); + MenuItemComment.Command = new RelayCommand(() => CustomCommandsHandler.OnCommentCommandExecuted(SkEditorAPI.Files.GetCurrentOpenedFile().Editor?.TextArea)); MenuItemRefreshSyntax.Command = new RelayCommand(async () => await SyntaxLoader.RefreshSyntaxAsync()); MenuItemSettings.Command = new RelayCommand(() => new SettingsWindow().ShowDialog(SkEditorAPI.Windows.GetMainWindow())); - MenuItemGenerateGui.Command = new RelayCommand(() => new GuiGenerator().ShowDialog(SkEditorAPI.Windows.GetMainWindow())); - MenuItemGenerateCommand.Command = new RelayCommand(() => new CommandGenerator().ShowDialog(SkEditorAPI.Windows.GetMainWindow())); - MenuItemRefactor.Command = new RelayCommand(() => new RefactorWindow().ShowDialog(SkEditorAPI.Windows.GetMainWindow())); + MenuItemGenerateGui.Command = new RelayCommand(() => ShowDialogIfEditorIsOpen(new GuiGenerator())); + MenuItemGenerateCommand.Command = new RelayCommand(() => ShowDialogIfEditorIsOpen(new CommandGenerator())); + MenuItemRefactor.Command = new RelayCommand(() => ShowDialogIfEditorIsOpen(new RefactorWindow())); MenuItemMarketplace.Command = new RelayCommand(() => new MarketplaceWindow().ShowDialog(SkEditorAPI.Windows.GetMainWindow())); MenuItemDocs.Command = new RelayCommand(AddDocsTab); } + private static void ShowDialogIfEditorIsOpen(AppWindow window) + { + if (SkEditorAPI.Files.GetCurrentOpenedFile().IsEditor) + window.ShowDialog(SkEditorAPI.Windows.GetMainWindow()); + } + public static void AddDocsTab() { FluentIcons.Avalonia.Fluent.SymbolIconSource icon = new() diff --git a/SkEditor/Utilities/Editor/CustomCommandsHandler.cs b/SkEditor/Utilities/Editor/CustomCommandsHandler.cs index 3afe4c8..a064ac7 100644 --- a/SkEditor/Utilities/Editor/CustomCommandsHandler.cs +++ b/SkEditor/Utilities/Editor/CustomCommandsHandler.cs @@ -2,6 +2,7 @@ using AvaloniaEdit.Document; using AvaloniaEdit.Editing; using SkEditor.API; +using SkEditor.Utilities.Files; using SkEditor.Utilities.Parser; using SkEditor.Views; using System; @@ -12,7 +13,10 @@ public class CustomCommandsHandler { public static void OnCommentCommandExecuted(object target) { - TextEditor editor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor; + OpenedFile file = SkEditorAPI.Files.GetCurrentOpenedFile(); + if (!file.IsEditor) return; + + TextEditor editor = file.Editor; var document = editor.Document; var selectionStart = editor.SelectionStart; diff --git a/SkEditor/Utilities/Files/FileBuilder.cs b/SkEditor/Utilities/Files/FileBuilder.cs index 91878c0..27d95a2 100644 --- a/SkEditor/Utilities/Files/FileBuilder.cs +++ b/SkEditor/Utilities/Files/FileBuilder.cs @@ -255,7 +255,7 @@ public static MenuFlyout GetContextMenu(TextEditor editor) new { Header = "MenuHeaderRedo", Command = new RelayCommand(() => editor.Redo()), Icon = Symbol.Redo }, new { Header = "MenuHeaderDuplicate", Command = new RelayCommand(() => CustomCommandsHandler.OnDuplicateCommandExecuted(editor.TextArea)), Icon = Symbol.Copy }, new { Header = "MenuHeaderComment", Command = new RelayCommand(() => CustomCommandsHandler.OnCommentCommandExecuted(editor.TextArea)), Icon = Symbol.Comment }, - new { Header = "MenuHeaderGoToLine", Command = new RelayCommand(() => SkEditorAPI.Windows.ShowWindow(new GoToLine())), Icon = Symbol.Find }, + new { Header = "MenuHeaderGoToLine", Command = new RelayCommand(() => SkEditorAPI.Windows.ShowWindow(new GoToLineWindow())), Icon = Symbol.Find }, new { Header = "MenuHeaderTrimWhitespaces", Command = new RelayCommand(() => CustomCommandsHandler.OnTrimWhitespacesCommandExecuted(editor.TextArea)), Icon = Symbol.Remove }, new { Header = "MenuHeaderDelete", Command = new RelayCommand(editor.Delete), Icon = Symbol.Delete }, new { Header = "MenuHeaderRefactor", Command = new RelayCommand(() => CustomCommandsHandler.OnRefactorCommandExecuted(editor)), Icon = Symbol.Rename },