瀏覽代碼

Fixed stateful element and added svg support for Image

Krzysztof Krysiński 2 月之前
父節點
當前提交
304b1dd455

+ 2 - 2
src/PixiEditor.Extensions.Sdk/Api/FlyUI/StatefulElement.cs

@@ -4,7 +4,7 @@ using PixiEditor.Extensions.Sdk.Attributes;
 
 namespace PixiEditor.Extensions.Sdk.Api.FlyUI;
 
-[ControlTypeId("StatefulElement")]
+[ControlTypeId("StatefulContainer")]
 public abstract class StatefulElement<TState> : LayoutElement, IStatefulElement<ControlDefinition, TState> where TState : IState<ControlDefinition>
 {
     private TState state;
@@ -36,7 +36,7 @@ public abstract class StatefulElement<TState> : LayoutElement, IStatefulElement<
     protected override ControlDefinition CreateControl()
     {
         ControlDefinition controlDefinition = State.Build().BuildNative();
-        ControlDefinition statefulContainer = new ControlDefinition(UniqueId, GetType());
+        ControlDefinition statefulContainer = new ControlDefinition(UniqueId, typeof(StatefulElement<>));
         statefulContainer.Children.Add(controlDefinition);
 
         return statefulContainer;

+ 9 - 1
src/PixiEditor.Extensions/FlyUI/Converters/PathToBitmapConverter.cs → src/PixiEditor.Extensions/FlyUI/Converters/PathToImgSourceConverter.cs

@@ -1,10 +1,11 @@
 using System.Globalization;
 using Avalonia.Data.Converters;
 using Avalonia.Media.Imaging;
+using Avalonia.Svg.Skia;
 
 namespace PixiEditor.Extensions.FlyUI.Converters;
 
-public class PathToBitmapConverter : IValueConverter
+public class PathToImgSourceConverter : IValueConverter
 {
     public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
     {
@@ -12,9 +13,16 @@ public class PathToBitmapConverter : IValueConverter
         {
             if (File.Exists(path))
             {
+                bool isSvg = path.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);
+                if (isSvg)
+                {
+                    return new SvgImage { Source = SvgSource.Load(path) };
+                }
+
                 return new Bitmap(path);
             }
         }
+
         return null;
     }
 

+ 1 - 1
src/PixiEditor.Extensions/FlyUI/ElementMap.cs

@@ -24,7 +24,7 @@ public class ElementMap
 
     public void AddElementsFromAssembly(Assembly assembly)
     {
-        var layoutElementTypes = assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ILayoutElement<Control>)));
+        var layoutElementTypes = assembly.GetTypes().Where(x => x is { IsAbstract: false, IsInterface: false } && x.GetInterfaces().Contains(typeof(ILayoutElement<Control>)));
         foreach (var type in layoutElementTypes)
         {
             controlMap.Add(type.Name, type); // TODO: Extension unique name prefix?

+ 17 - 15
src/PixiEditor.Extensions/FlyUI/Elements/Image.cs

@@ -5,6 +5,7 @@ using Avalonia.Data;
 using Avalonia.Layout;
 using Avalonia.Media;
 using Avalonia.Media.Imaging;
+using Avalonia.Svg.Skia;
 using PixiEditor.Extensions.CommonApi.FlyUI.Properties;
 using PixiEditor.Extensions.Extensions;
 using PixiEditor.Extensions.FlyUI.Converters;
@@ -19,8 +20,9 @@ public class Image : LayoutElement
     private double _height = -1;
     private FillMode _fillMode = FillMode.Uniform;
     private FilterQuality _filterQuality = FilterQuality.None;
-    private Avalonia.Controls.Image _image = null!;
-    
+    private Avalonia.Controls.Image? _image = null;
+    private SvgImage? _svgImage = null;
+
     public string Source { get => _source; set => SetField(ref _source, value); }
     public double Width { get => _width; set => SetField(ref _width, value); }
     public double Height { get => _height; set => SetField(ref _height, value); }
@@ -37,43 +39,43 @@ public class Image : LayoutElement
             }
         }
     }
-    
+
     protected override Control CreateNativeControl()
     {
         _image = new();
-        
+
         Binding sourceBinding = new()
         {
             Source = this,
             Path = nameof(Source),
-            Converter = new PathToBitmapConverter(),
+            Converter = new PathToImgSourceConverter(),
         };
-        
+
         Binding widthBinding = new()
         {
             Source = this,
             Path = nameof(Width),
         };
-        
+
         Binding heightBinding = new()
         {
             Source = this,
             Path = nameof(Height),
         };
-        
+
         Binding fillModeBinding = new()
         {
             Source = this,
             Path = nameof(FillMode),
             Converter = new EnumToEnumConverter<FillMode, Stretch>()
         };
-        
+
         _image.Bind(Avalonia.Controls.Image.SourceProperty, sourceBinding);
         _image.Bind(Layoutable.WidthProperty, widthBinding);
         _image.Bind(Layoutable.HeightProperty, heightBinding);
         _image.Bind(Avalonia.Controls.Image.StretchProperty, fillModeBinding);
         RenderOptions.SetBitmapInterpolationMode(_image, (BitmapInterpolationMode)(byte)FilterQuality);
-        
+
         return _image;
     }
 
@@ -81,10 +83,10 @@ public class Image : LayoutElement
     protected override IEnumerable<object> GetControlProperties()
     {
         yield return Source;
-        
+
         yield return Width;
         yield return Height;
-        
+
         yield return FillMode;
         yield return FilterQuality;
     }
@@ -93,13 +95,13 @@ public class Image : LayoutElement
     {
         var valuesList = values.ToList();
         Source = (string)valuesList.ElementAtOrDefault(0);
-        
+
         Width = (double)valuesList.ElementAtOrDefault(1, double.NaN);
         Height = (double)valuesList.ElementAtOrDefault(2, double.NaN);
-        
+
         Width = Width < 0 ? double.NaN : Width;
         Height = Height < 0 ? double.NaN : Height;
-        
+
         FillMode = (FillMode)valuesList.ElementAtOrDefault(3, FillMode.Uniform);
         FilterQuality = (FilterQuality)valuesList.ElementAtOrDefault(4, FilterQuality.Unspecified);
     }

+ 1 - 0
src/PixiEditor.Extensions/PixiEditor.Extensions.csproj

@@ -15,6 +15,7 @@
     <ItemGroup>
       <PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
       <PackageReference Include="Avalonia.Remote.Protocol" Version="$(AvaloniaVersion)" />
+      <PackageReference Include="Svg.Controls.Skia.Avalonia" Version="11.3.0.1" />
       <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
       <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
       <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />