Skip to content

Commit

Permalink
Fix edit menu crashes in non-editor tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
NotroDev committed Jul 10, 2024
1 parent 844146e commit 9b015a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
32 changes: 20 additions & 12 deletions SkEditor/Controls/MainMenuControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion SkEditor/Utilities/Editor/CustomCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion SkEditor/Utilities/Files/FileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 9b015a8

Please sign in to comment.