From d467948d0be0bf4c6ca18143f0f905d8923c0048 Mon Sep 17 00:00:00 2001 From: tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 2 Apr 2026 14:36:06 +0300 Subject: [PATCH] Only execute commands from code if allowed --- .../Utils/Extensions/CommandExtensions.cs | 15 +++++++++++++++ .../Views/Components/DashboardView.axaml.cs | 5 +++-- .../Views/Controls/HyperLink.axaml.cs | 3 +-- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 DiscordChatExporter.Gui/Utils/Extensions/CommandExtensions.cs diff --git a/DiscordChatExporter.Gui/Utils/Extensions/CommandExtensions.cs b/DiscordChatExporter.Gui/Utils/Extensions/CommandExtensions.cs new file mode 100644 index 00000000..fc0008cb --- /dev/null +++ b/DiscordChatExporter.Gui/Utils/Extensions/CommandExtensions.cs @@ -0,0 +1,15 @@ +using System.Windows.Input; + +namespace DiscordChatExporter.Gui.Utils.Extensions; + +internal static class CommandExtensions +{ + extension(ICommand command) + { + public void ExecuteIfCan(object? parameter = null) + { + if (command.CanExecute(parameter)) + command.Execute(parameter); + } + } +} diff --git a/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs b/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs index 709bd542..6155a55d 100644 --- a/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs +++ b/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs @@ -4,6 +4,7 @@ using Avalonia.Input; using Avalonia.Interactivity; using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Gui.Framework; +using DiscordChatExporter.Gui.Utils.Extensions; using DiscordChatExporter.Gui.ViewModels.Components; namespace DiscordChatExporter.Gui.Views.Components; @@ -18,7 +19,7 @@ public partial class DashboardView : UserControl private void AvailableGuildsListBox_OnSelectionChanged( object? sender, SelectionChangedEventArgs args - ) => DataContext.PullChannelsCommand.Execute(null); + ) => DataContext.PullChannelsCommand.ExecuteIfCan(null); private void AvailableChannelsTreeView_OnSelectionChanged( object? sender, @@ -40,6 +41,6 @@ public partial class DashboardView : UserControl if (DataContext.SelectedChannels.Count != 1) return; - DataContext.ExportCommand.Execute(null); + DataContext.ExportCommand.ExecuteIfCan(null); } } diff --git a/DiscordChatExporter.Gui/Views/Controls/HyperLink.axaml.cs b/DiscordChatExporter.Gui/Views/Controls/HyperLink.axaml.cs index 479303de..72ed47a8 100644 --- a/DiscordChatExporter.Gui/Views/Controls/HyperLink.axaml.cs +++ b/DiscordChatExporter.Gui/Views/Controls/HyperLink.axaml.cs @@ -54,8 +54,7 @@ public partial class HyperLink : UserControl { if (Command is not null) { - if (Command.CanExecute(CommandParameter)) - Command.Execute(CommandParameter); + Command.ExecuteIfCan(CommandParameter); } else if (!string.IsNullOrWhiteSpace(Url)) {