Browse Source

Added command debug window

CPKreuz 2 years ago
parent
commit
f27c899abf

+ 27 - 0
src/PixiEditor/Helpers/Converters/EmptyStringFillerConverter.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Helpers.Converters;
+internal class EmptyStringFillerConverter : MarkupConverter
+{
+    public string NullText { get; set; } = "[null]";
+
+    public string EmptyText { get; set; } = "[empty]";
+
+    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+    {
+        return value switch
+        {
+            string s => s.Length switch
+            {
+                0 => EmptyText,
+                _ => s
+            },
+            _ => NullText
+        };
+    }
+}

+ 7 - 0
src/PixiEditor/ViewModels/SubViewModels/Main/DebugViewModel.cs

@@ -9,6 +9,7 @@ using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.Models.Commands.Templates.Parsers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.UserPreferences;
+using PixiEditor.Views.Dialogs;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main;
 
@@ -137,6 +138,12 @@ internal class DebugViewModel : SubViewModel<ViewModelMain>
         IPreferences.Current.UpdateLocalPreference(PreferencesConstants.RecentlyOpened, Array.Empty<object>());
     }
 
+    [Command.Debug("PixiEditor.Debug.OpenCommandDebugWindow", "Open command debug window", "Open command debug window")]
+    public void OpenCommandDebugWindow()
+    {
+        new CommandDebugPopup().Show();
+    }
+
     [Command.Debug("PixiEditor.Debug.OpenInstallDirectory", "Open Installation Directory", "Open Installation Directory", IconPath = "Folder.png")]
     public static void OpenInstallLocation()
     {

+ 72 - 0
src/PixiEditor/Views/Dialogs/CommandDebugPopup.xaml

@@ -0,0 +1,72 @@
+<Window x:Class="PixiEditor.Views.Dialogs.CommandDebugPopup"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:local="clr-namespace:PixiEditor.Views.Dialogs"
+        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
+        xmlns:behaviours="clr-namespace:PixiEditor.Helpers.Behaviours"
+        xmlns:command="clr-namespace:PixiEditor.Models.Commands"
+        xmlns:cmds="clr-namespace:PixiEditor.Models.Commands.Commands" xmlns:usercontrols="clr-namespace:PixiEditor.Views.UserControls" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
+        WindowStyle="None"
+        mc:Ignorable="d"
+        x:Name="uc"
+        Foreground="White"
+        Title="CommandDebugPopup" Height="450" Width="800">
+
+    <WindowChrome.WindowChrome>
+        <WindowChrome CaptionHeight="32" GlassFrameThickness="0.1"
+                      ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
+    </WindowChrome.WindowChrome>
+
+    <DockPanel Background="{StaticResource AccentColor}" Focusable="True">
+        <b:Interaction.Behaviors>
+            <behaviours:ClearFocusOnClickBehavior />
+        </b:Interaction.Behaviors>
+
+        <local:DialogTitleBar DockPanel.Dock="Top" />
+
+        <Grid>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="Auto" />
+                <RowDefinition />
+            </Grid.RowDefinitions>
+
+            <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
+                <ItemsControl ItemsSource="{Binding Commands, ElementName=uc}" Margin="5,0,0,5">
+                    <ItemsControl.ItemTemplate>
+                        <DataTemplate>
+                            <Border BorderThickness="0,0,0,1" BorderBrush="{StaticResource BrighterAccentColor}">
+                                <Grid Margin="0,5,0,5">
+                                    <Grid.ColumnDefinitions>
+                                        <ColumnDefinition Width="35"/>
+                                        <ColumnDefinition/>
+                                        <ColumnDefinition/>
+                                        <ColumnDefinition/>
+                                    </Grid.ColumnDefinitions>
+                                    <Grid.RowDefinitions>
+                                        <RowDefinition/>
+                                        <RowDefinition/>
+                                        <RowDefinition/>
+                                    </Grid.RowDefinitions>
+
+                                    <Image Grid.RowSpan="3" Source="{Binding Image}" Margin="0,0,5,0"/>
+
+                                    <TextBlock Text="{Binding Command.InternalName}" Grid.Column="1"/>
+                                    <TextBlock Text="{Binding Command.DisplayName, Converter={converters:EmptyStringFillerConverter EmptyText='[internal]', NullText='[internal]'}}" Grid.Column="2" />
+                                    <TextBlock Text="{Binding Command.Description, Converter={converters:EmptyStringFillerConverter}}" Grid.Column="3" />
+
+                                    <usercontrols:PrependTextBlock Prepend="Default Shortcut: '" Text="{Binding Command.DefaultShortcut}" Append="'"  Grid.Row="1" Grid.Column="1"/>
+                                    <usercontrols:PrependTextBlock Prepend="Current Shortcut: '" Text="{Binding Command.Shortcut}" Append="'" Grid.Row="1" Grid.Column="2"/>
+                                    <usercontrols:PrependTextBlock Prepend="Is Debug: '" Text="{Binding Command.IsDebug}" Append="'" Grid.Row="1" Grid.Column="3"/>
+
+                                    <ContentControl Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Content="{Binding Comments}"/>
+                                </Grid>
+                            </Border>
+                        </DataTemplate>
+                    </ItemsControl.ItemTemplate>
+                </ItemsControl>
+            </ScrollViewer>
+        </Grid>
+    </DockPanel>
+</Window>

+ 101 - 0
src/PixiEditor/Views/Dialogs/CommandDebugPopup.xaml.cs

@@ -0,0 +1,101 @@
+using System.Collections.ObjectModel;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Media;
+using PixiEditor.Models.Commands;
+using PixiEditor.Models.Commands.Commands;
+using PixiEditor.Models.Commands.Evaluators;
+using PixiEditor.Models.DataHolders;
+
+namespace PixiEditor.Views.Dialogs;
+
+public partial class CommandDebugPopup : Window
+{
+    private static Brush infoBrush = new SolidColorBrush(Color.FromRgb(129, 143, 156));
+
+    private static Brush warningBrush = new SolidColorBrush(Color.FromRgb(222, 130, 55));
+
+    private static Brush errorBrush = new SolidColorBrush(Color.FromRgb(230, 34, 57));
+
+    public static readonly DependencyProperty CommandsProperty =
+        DependencyProperty.Register(nameof(Commands), typeof(IEnumerable<CommandDebug>), typeof(CommandDebugPopup));
+
+    internal IEnumerable<CommandDebug> Commands
+    {
+        get => (IEnumerable<CommandDebug>)GetValue(CommandsProperty);
+        set => SetValue(CommandsProperty, value);
+    }
+
+    public CommandDebugPopup()
+    {
+        var debugCommands = new List<CommandDebug>();
+
+        foreach (var command in CommandController.Current.Commands)
+        {
+            var comments = new TextBlock();
+
+            var image = command.IconEvaluator.CallEvaluate(command, null);
+
+            foreach (var inline in AnalyzeCommand(command, image))
+            {
+                comments.Inlines.Add(inline);
+            }
+
+            debugCommands.Add(new CommandDebug(command, comments, image));
+        }
+
+        Commands = debugCommands;
+
+        InitializeComponent();
+        //ItemsControl.ItemsSource = CommandController.Current.Commands;
+    }
+
+    private IEnumerable<Inline> AnalyzeCommand(Command command, ImageSource image)
+    {
+        if (image == null && command.IconEvaluator == IconEvaluator.Default)
+        {
+            if (string.IsNullOrWhiteSpace(command.IconPath))
+            {
+                yield return Info("Default evaluator has not found a image (No icon path provided)");
+            }
+            else
+            {
+                yield return Error("Default evaluator has not found a image at icon path!");
+            }
+        }
+
+        if (command.IconEvaluator != IconEvaluator.Default)
+        {
+            yield return Info("Uses custom icon evaluator");
+        }
+
+        if (!string.IsNullOrWhiteSpace(command.IconPath))
+        {
+            yield return Info($"Has custom icon path: '{command.IconPath}'");
+        }
+
+        Run Info(string text) => new Run(text) { Foreground = infoBrush };
+
+        Run Warning(string text) => new Run(text) { Foreground = warningBrush };
+
+        Run Error(string text) => new Run(text) { Foreground = errorBrush };
+    }
+
+    internal class CommandDebug
+    {
+        public Command Command { get; }
+
+        public TextBlock Comments { get; }
+
+        public ImageSource Image { get; }
+
+        public CommandDebug(Command command, TextBlock comments, ImageSource image)
+        {
+            Command = command;
+            Comments = comments;
+            Image = image;
+        }
+    }
+}
+