2
0
Эх сурвалжийг харах

Merge pull request #1169 from PixiEditor/fixes/conflicting-tool-shortcuts

Fixed shape shortcuts switching to vector on any toolset
Krzysztof Krysiński 3 өдөр өмнө
parent
commit
d8e511c9b9

+ 2 - 0
src/PixiEditor/Models/Commands/Attributes/Commands/ToolAttribute.cs

@@ -10,6 +10,8 @@ internal partial class Command
         public Key Transient { get; set; }
         public bool TransientImmediate { get; set; } = false;
 
+        public string? CommonToolType { get; set; }
+
         public ToolAttribute() : base(null, null, null)
         {
         }

+ 36 - 1
src/PixiEditor/ViewModels/SubViewModels/ToolsViewModel.cs

@@ -2,6 +2,7 @@
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Linq;
+using System.Reflection;
 using Avalonia.Input;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.ChangeableDocument;
@@ -180,14 +181,36 @@ internal class ToolsViewModel : SubViewModel<ViewModelMain>, IToolsHandler
     {
         ActiveTool?.OnToolDeselected(false);
         ActiveToolSet = toolSetHandler;
+        if (ActiveTool != null && !ActiveToolSet.Tools.Contains(ActiveTool))
+        {
+            TrySelectCommonToolInNewToolSet();
+        }
         ActiveToolSet.ApplyToolSetSettings();
         UpdateEnabledState();
 
+
         ActiveTool?.OnToolSelected(false);
 
         OnPropertyChanged(nameof(NonSelectedToolSets));
     }
 
+    private void TrySelectCommonToolInNewToolSet()
+    {
+        var commonTool = ActiveToolSet.Tools.FirstOrDefault(tool =>
+        {
+            var attr = tool.GetType().GetCustomAttribute<Command.ToolAttribute>();
+            if (attr is null) return false;
+
+            return ActiveTool?.GetType().GetCustomAttribute<Command.ToolAttribute>()?.CommonToolType ==
+                   attr.CommonToolType;
+        });
+
+        if (commonTool is not null)
+        {
+            SetActiveTool(commonTool, false);
+        }
+    }
+
     [Command.Basic("PixiEditor.Tools.ToggleSelectionTinting", "TOGGLE_TINTING_SELECTION", "TOGGLE_TINTING_SELECTION_DESCRIPTIVE", AnalyticsTrack = true)]
     public void ToggleTintSelection() => SelectionTintingEnabled = !SelectionTintingEnabled;
 
@@ -389,7 +412,7 @@ internal class ToolsViewModel : SubViewModel<ViewModelMain>, IToolsHandler
         if (foundTool == null)
         {
             foundTool = allTools.FirstOrDefault(x => x.GetType().IsAssignableFrom(toolType));
-            if(foundTool == null)
+            if(foundTool == null || SimilarToolInActiveToolSetExists(toolType))
                 return;
 
             var toolset = AllToolSets.FirstOrDefault(x => x.Tools.Contains(foundTool));
@@ -426,6 +449,18 @@ internal class ToolsViewModel : SubViewModel<ViewModelMain>, IToolsHandler
         }
     }
 
+    private bool SimilarToolInActiveToolSetExists(Type toolType)
+    {
+        Command.ToolAttribute attr = toolType.GetCustomAttribute<Command.ToolAttribute>();
+        if (attr is null) return false;
+
+        return ActiveToolSet.Tools.Any(tool =>
+        {
+            var toolAttr = tool.GetType().GetCustomAttribute<Command.ToolAttribute>();
+            return toolAttr is not null && toolAttr.CommonToolType == attr.CommonToolType;
+        });
+    }
+
     public void HandleToolRepeatShortcutDown()
     {
         if (ActiveTool == null) return;

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/RasterEllipseToolViewModel.cs

@@ -10,7 +10,7 @@ using PixiEditor.UI.Common.Localization;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.C)]
+[Command.Tool(Key = Key.C, CommonToolType = "Ellipse")]
 internal class RasterEllipseToolViewModel : ShapeTool, IRasterEllipseToolHandler
 {
     private string defaultActionDisplay = "ELLIPSE_TOOL_ACTION_DISPLAY_DEFAULT";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/RasterLineToolViewModel.cs

@@ -12,7 +12,7 @@ using PixiEditor.ViewModels.Tools.ToolSettings.Toolbars;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.L)]
+[Command.Tool(Key = Key.L, CommonToolType = "Line")]
 internal class RasterLineToolViewModel : ShapeTool, ILineToolHandler
 {
     private string defaultActionDisplay = "LINE_TOOL_ACTION_DISPLAY_DEFAULT";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/RasterRectangleToolViewModel.cs

@@ -11,7 +11,7 @@ using PixiEditor.ViewModels.Tools.ToolSettings.Toolbars;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.R)]
+[Command.Tool(Key = Key.R, CommonToolType = "Rectangle")]
 internal class RasterRectangleToolViewModel : ShapeTool, IRasterRectangleToolHandler
 {
     private string defaultActionDisplay = "RECTANGLE_TOOL_ACTION_DISPLAY_DEFAULT";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/VectorEllipseToolViewModel.cs

@@ -12,7 +12,7 @@ using PixiEditor.UI.Common.Localization;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.C)]
+[Command.Tool(Key = Key.C, CommonToolType = "Ellipse")]
 internal class VectorEllipseToolViewModel : ShapeTool, IVectorEllipseToolHandler
 {
     public const string NewLayerKey = "NEW_ELLIPSE_LAYER_NAME";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/VectorLineToolViewModel.cs

@@ -16,7 +16,7 @@ using PixiEditor.ViewModels.Tools.ToolSettings.Toolbars;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.L)]
+[Command.Tool(Key = Key.L, CommonToolType = "Line")]
 internal class VectorLineToolViewModel : ShapeTool, IVectorLineToolHandler
 {
     public const string NewLayerKey = "NEW_LINE_LAYER_NAME";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/VectorRectangleToolViewModel.cs

@@ -13,7 +13,7 @@ using PixiEditor.ViewModels.Tools.ToolSettings.Toolbars;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.R)]
+[Command.Tool(Key = Key.R, CommonToolType = "Rectangle")]
 internal class VectorRectangleToolViewModel : ShapeTool, IVectorRectangleToolHandler
 {
     public const string NewLayerKey = "NEW_RECTANGLE_LAYER_NAME";