瀏覽代碼

Code factor stuff

CPKreuz 2 年之前
父節點
當前提交
f78dfff975

+ 0 - 1
src/PixiEditor/App.xaml.cs

@@ -50,7 +50,6 @@ internal partial class App : Application
         
         MainWindow = new MainWindow();
         MainWindow.Show();
-
     }
 
     private void AddNativeAssets()

+ 0 - 1
src/PixiEditor/Helpers/ColorHelper.cs

@@ -75,5 +75,4 @@ public class ColorHelper
 
         return result.Count > 0;
     }
-
 }

+ 52 - 45
src/PixiEditor/ViewModels/SubViewModels/Tools/ToolSettings/Toolbars/ToolbarFactory.cs

@@ -16,71 +16,78 @@ internal static class ToolbarFactory
         foreach (var property in toolType.GetProperties())
         {
             var attribute = property.GetCustomAttribute<Settings.SettingsAttribute>();
+            if (attribute == null) continue;
 
-            if (attribute == null)
-                continue;
-            
             var name = attribute.Name ?? property.Name;
+            var label = attribute.LabelKey ?? name;
 
             if (attribute is Settings.InheritedAttribute)
             {
-                var inherited = toolbar.GetSetting(name);
-                
-                if (inherited == null)
-                {
-                    throw new NullReferenceException($"There's no inherited setting '{name}' on inherited toolbar of type '{typeof(TToolbar).FullName}' (Tool: {typeof(T).FullName})");
-                }
-
-                if (inherited.GetSettingType() != property.PropertyType)
-                {
-                    throw new InvalidCastException($"Inherited setting '{name}' does not match property type '{property.PropertyType}' (Tool: {typeof(T).FullName})");
-                }
-
-                if (attribute.Notify != null)
-                {
-                    AddValueChangedHandler(toolType, tool, inherited, attribute);
-                }
-
-                continue;
+                ProcessInheritedSetting(toolType, tool, toolbar, property, attribute, name);
             }
-            
-            var label = attribute.LabelKey ?? name;
-
-            var setting = attribute switch
+            else
             {
-                Settings.BoolAttribute => new BoolSetting(name, (bool)(attribute.DefaultValue ?? false), label),
-                Settings.ColorAttribute => new ColorSetting(name, ((Color)(attribute.DefaultValue ?? Colors.White)).ToColor(), label),
-                Settings.EnumAttribute => GetEnumSetting(property.PropertyType, name, attribute),
-                Settings.FloatAttribute => new FloatSetting(name, (float)(attribute.DefaultValue ?? 0f), attribute.LabelKey),
-                Settings.SizeAttribute => new SizeSetting(name, label),
-                _ => throw new NotImplementedException($"SettingsAttribute of type '{attribute.GetType().FullName}' has not been implemented")
-            };
-            
-            if (setting.GetSettingType() != property.PropertyType)
-            {
-                throw new InvalidCastException($"Setting '{name}' does not match property type '{property.PropertyType}' (Tool: {typeof(T).FullName})");
+                var setting = CreateSetting(property.PropertyType, name, attribute, label);
+                AddValueChangedHandlerIfRequired(toolType, tool, setting, attribute);
+                toolbar.Settings.Add(setting);
             }
+        }
 
-            if (attribute.Notify != null)
-            {
-                AddValueChangedHandler(toolType, tool, setting, attribute);
-            }
+        return toolbar;
+    }
 
-            toolbar.Settings.Add(setting);
+    private static void ProcessInheritedSetting(Type toolType, ToolViewModel tool, Toolbar toolbar,
+        PropertyInfo property, Settings.SettingsAttribute attribute, string name)
+    {
+        var inherited = toolbar.GetSetting(name);
+        if (inherited == null || inherited.GetSettingType() != property.PropertyType)
+        {
+            throw new InvalidOperationException(
+                $"Inherited setting '{name}' does not match property type '{property.PropertyType}' (Tool: {toolType.FullName})");
         }
 
-        return toolbar;
+        AddValueChangedHandlerIfRequired(toolType, tool, inherited, attribute);
     }
 
-    private static void AddValueChangedHandler<T>(Type toolType, T tool, Setting setting, Settings.SettingsAttribute attribute) where T : ToolViewModel
+    private static Setting CreateSetting(Type propertyType, string name, Settings.SettingsAttribute attribute,
+        string label)
+    {
+        return attribute switch
+        {
+            Settings.BoolAttribute => new BoolSetting(name, (bool)(attribute.DefaultValue ?? false), label),
+            Settings.ColorAttribute => new ColorSetting(name,
+                ((Color)(attribute.DefaultValue ?? Colors.White)).ToColor(), label),
+            Settings.EnumAttribute => GetEnumSetting(propertyType, name, attribute),
+            Settings.FloatAttribute => new FloatSetting(name, (float)(attribute.DefaultValue ?? 0f), label),
+            Settings.SizeAttribute => new SizeSetting(name, label),
+            _ => throw new NotImplementedException(
+                $"SettingsAttribute of type '{attribute.GetType().FullName}' has not been implemented")
+        };
+    }
+
+    private static void AddValueChangedHandlerIfRequired(Type toolType, ToolViewModel tool, Setting setting,
+        Settings.SettingsAttribute attribute)
+    {
+        if (attribute.Notify != null)
+        {
+            AddValueChangedHandler(toolType, tool, setting, attribute);
+        }
+    }
+
+
+    private static void AddValueChangedHandler<T>(Type toolType, T tool, Setting setting,
+        Settings.SettingsAttribute attribute) where T : ToolViewModel
     {
         if (attribute.Notify != null)
         {
-            var method = toolType.GetMethod(attribute.Notify, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, Array.Empty<Type>());
+            var method = toolType.GetMethod(attribute.Notify,
+                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static,
+                Array.Empty<Type>());
 
             if (method is null)
             {
-                throw new NullReferenceException($"No method found with the name '{attribute.Notify}' that does not have any parameters");
+                throw new NullReferenceException(
+                    $"No method found with the name '{attribute.Notify}' that does not have any parameters");
             }
 
             setting.ValueChanged += (_, _) => method.Invoke(tool, null);