Browse Source

Command icon loading

Equbuxu 1 year ago
parent
commit
4f486aa0d8

+ 2 - 2
src/PixiEditor.AvaloniaUI/Models/Commands/Evaluators/Evaluator.cs

@@ -9,10 +9,10 @@ internal abstract class Evaluator<T>
 {
 {
     public string Name { get; init; }
     public string Name { get; init; }
 
 
-    public Func<object, T> Evaluate { private get; init; }
+    public Func<object, T?> Evaluate { private get; init; }
 
 
     /// <param name="command">The command this evaluator corresponds to</param>
     /// <param name="command">The command this evaluator corresponds to</param>
     /// <param name="parameter">The parameter to pass to the Evaluate function</param>
     /// <param name="parameter">The parameter to pass to the Evaluate function</param>
     /// <returns>The value returned by the Evaluate function</returns>
     /// <returns>The value returned by the Evaluate function</returns>
-    public virtual T CallEvaluate(Command command, object parameter) => Evaluate(parameter);
+    public virtual T? CallEvaluate(Command command, object parameter) => Evaluate(parameter);
 }
 }

+ 15 - 30
src/PixiEditor.AvaloniaUI/Models/Commands/Evaluators/IconEvaluator.cs

@@ -1,12 +1,10 @@
-using System.Collections;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
-using System.IO;
 using System.Linq;
 using System.Linq;
 using System.Reflection;
 using System.Reflection;
-using System.Threading.Tasks;
 using Avalonia.Media;
 using Avalonia.Media;
 using Avalonia.Media.Imaging;
 using Avalonia.Media.Imaging;
+using Avalonia.Platform;
 using PixiEditor.AvaloniaUI.Models.Commands.Commands;
 using PixiEditor.AvaloniaUI.Models.Commands.Commands;
 
 
 namespace PixiEditor.AvaloniaUI.Models.Commands.Evaluators;
 namespace PixiEditor.AvaloniaUI.Models.Commands.Evaluators;
@@ -15,7 +13,7 @@ internal class IconEvaluator : Evaluator<IImage>
 {
 {
     public static IconEvaluator Default { get; } = new CommandNameEvaluator();
     public static IconEvaluator Default { get; } = new CommandNameEvaluator();
 
 
-    public override IImage CallEvaluate(Command command, object parameter) =>
+    public override IImage? CallEvaluate(Command command, object parameter) =>
         base.CallEvaluate(command, parameter ?? command);
         base.CallEvaluate(command, parameter ?? command);
 
 
     public static string GetDefaultPath(Command command)
     public static string GetDefaultPath(Command command)
@@ -42,8 +40,6 @@ internal class IconEvaluator : Evaluator<IImage>
             path = $"Images/Commands/{command.InternalName.Replace('.', '/')}.png";
             path = $"Images/Commands/{command.InternalName.Replace('.', '/')}.png";
         }
         }
 
 
-        path = path.ToLower();
-
         if (path.StartsWith("/"))
         if (path.StartsWith("/"))
         {
         {
             path = path[1..];
             path = path[1..];
@@ -55,35 +51,24 @@ internal class IconEvaluator : Evaluator<IImage>
     [DebuggerDisplay("IconEvaluator.Default")]
     [DebuggerDisplay("IconEvaluator.Default")]
     private class CommandNameEvaluator : IconEvaluator
     private class CommandNameEvaluator : IconEvaluator
     {
     {
-        public static string[] resources = GetResourceNames();
-
         public static Dictionary<string, Bitmap> images = new();
         public static Dictionary<string, Bitmap> images = new();
 
 
-        public override IImage CallEvaluate(Command command, object parameter)
+        public override IImage? CallEvaluate(Command command, object parameter)
         {
         {
             string path = GetDefaultPath(command);
             string path = GetDefaultPath(command);
 
 
-            if (resources.Contains(path))
-            {
-                var image = images.GetValueOrDefault(path);
-
-                if (image == null)
-                {
-                    using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"pack://application:,,,/{path}")!;
-                    image = new Bitmap(stream);
-                    images.Add(path, image);
-                }
-
+            var image = images.GetValueOrDefault(path);
+            if (image is not null)
                 return image;
                 return image;
-            }
-
-            return null;
-        }
-
-        private static string[] GetResourceNames()
-        {
-            return App.Current.Resources.Select(entry =>
-                (string)entry.Key).ToArray();
+            
+            Uri uri = new($"avares://{Assembly.GetExecutingAssembly().GetName().Name}/{path}");
+            if (!AssetLoader.Exists(uri))
+                return null;
+            
+            image = new Bitmap(AssetLoader.Open(uri));
+            images.Add(path, image);
+
+            return image;
         }
         }
     }
     }
 }
 }