Prechádzať zdrojové kódy

Fixed flyui deserialization and made sample

flabbet 1 rok pred
rodič
commit
4562d9cb0f

+ 6 - 0
samples/PixiEditorExtensionSamples.sln

@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample5_Resources", "Sample
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample6_Palettes", "Sample6_Palettes\Sample6_Palettes.csproj", "{6FF1D3AB-358A-4D78-8877-DACC01D34B87}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample7_FlyUI", "Sample7_FlyUI\Sample7_FlyUI.csproj", "{432A224A-8035-47C1-AC41-6715021B3AA3}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -56,6 +58,10 @@ Global
 		{6FF1D3AB-358A-4D78-8877-DACC01D34B87}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{6FF1D3AB-358A-4D78-8877-DACC01D34B87}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{6FF1D3AB-358A-4D78-8877-DACC01D34B87}.Release|Any CPU.Build.0 = Release|Any CPU
+		{432A224A-8035-47C1-AC41-6715021B3AA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{432A224A-8035-47C1-AC41-6715021B3AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{432A224A-8035-47C1-AC41-6715021B3AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{432A224A-8035-47C1-AC41-6715021B3AA3}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(NestedProjects) = preSolution
 		{FD9B4C32-4D2E-410E-BC6B-787779BEB6E2} = {7CC35BC4-829F-4EF4-8EB6-E1D46206E7DC}

+ 0 - 1
samples/Sample3_Preferences/PreferencesSampleExtension.cs

@@ -21,7 +21,6 @@ public class PreferencesSampleExtension : WasmExtension
         Api.Preferences.AddCallback<double>("TestDouble", (name, value) => Api.Logger.Log($"Test double changed to {value}!"));
         Api.Preferences.AddCallback<string>("TestString", (name, value) => Api.Logger.Log($"Test string changed to {value}!"));
         Api.Preferences.AddCallback<bool>("TestBool", (name, value) => Api.Logger.Log($"Test bool changed to {value}!"));
-        Api.Preferences.AddCallback<byte[]>("TestByteArray", (name, value) => Api.Logger.Log($"Test byte array changed to {value}!"));
         
         // Internally this preference will have name "yourCompany.Samples.Preferences:HelloCount".
         int helloCount = Api.Preferences.GetPreference<int>("HelloCount");

+ 12 - 0
samples/Sample7_FlyUI/FlyUISampleExtension.cs

@@ -0,0 +1,12 @@
+using PixiEditor.Extensions.Wasm;
+
+namespace FlyUISample;
+
+public class FlyUiSampleExtension : WasmExtension
+{
+    public override void OnInitialized()
+    {
+        WindowContentElement content = new WindowContentElement();
+        Api.WindowProvider.CreatePopupWindow("Sample Window", content).Show();
+    }
+}

+ 9 - 0
samples/Sample7_FlyUI/Program.cs

@@ -0,0 +1,9 @@
+namespace FlyUISample;
+
+public static class Program
+{
+    public static void Main()
+    {
+        
+    }
+}

+ 42 - 0
samples/Sample7_FlyUI/Sample7_FlyUI.csproj

@@ -0,0 +1,42 @@
+<Project Sdk="Microsoft.NET.Sdk">
+    <PropertyGroup>
+        <TargetFramework>net8.0</TargetFramework>
+        <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
+        <OutputType>Exe</OutputType>
+        <PublishTrimmed>true</PublishTrimmed>
+        <WasmSingleFileBundle>true</WasmSingleFileBundle>
+        <GenerateExtensionPackage>true</GenerateExtensionPackage>
+        <PixiExtOutputPath>..\..\src\PixiEditor.AvaloniaUI.Desktop\bin\Debug\net8.0\win-x64\Extensions</PixiExtOutputPath>
+        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+        <RootNamespace>FlyUISample</RootNamespace>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <None Remove="extension.json" />
+        <Content Include="extension.json">
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </Content>
+    </ItemGroup>
+
+    <ItemGroup>
+        <Content Include="Localization\*">
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </Content>
+    </ItemGroup>
+
+    <ItemGroup>
+        <Content Include="Resources\*">
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </Content>
+    </ItemGroup>
+
+    <!--Below is not required if you use Nuget package, this sample references project directly, so it must be here-->
+    <ItemGroup>
+        <ProjectReference Include="..\..\src\PixiEditor.Extensions.Wasm\PixiEditor.Extensions.Wasm.csproj" />
+    </ItemGroup>
+
+    <!--Below is not required if you use Nuget package, this sample references project directly, so it must be here-->
+    <Import Project="..\..\src\PixiEditor.Extensions.Wasm\build\PixiEditor.Extensions.Wasm.props"/>
+    <Import Project="..\..\src\PixiEditor.Extensions.Wasm\build\PixiEditor.Extensions.Wasm.targets" />
+
+</Project>

+ 20 - 0
samples/Sample7_FlyUI/WindowContentElement.cs

@@ -0,0 +1,20 @@
+using PixiEditor.Extensions.Wasm.Api.FlyUI;
+using PixiEditor.Extensions.Wasm.Api.Localization;
+
+namespace FlyUISample;
+
+public class WindowContentElement : StatelessElement
+{
+    public override CompiledControl BuildNative()
+    {
+        Layout layout = new Layout(body:
+            new Column(
+                new Center(
+                    new Text("Hello there!")
+                ),
+                new Text("This is a sample window content element."))
+        );
+
+        return layout.BuildNative();
+    }
+}

+ 27 - 0
samples/Sample7_FlyUI/extension.json

@@ -0,0 +1,27 @@
+{
+  "displayName": "Sample Extension - FlyUI",
+  "uniqueName": "yourCompany.Samples.FlyUI",
+  "description": "Sample FlyUI extension for PixiEditor",
+  "version": "1.0.0",
+  "author": {
+    "name": "PixiEditor",
+    "email": "[email protected]",
+    "website": "https://pixieditor.net"
+  },
+  "publisher": {
+    "name": "PixiEditor",
+    "email": "[email protected]",
+    "website": "https://pixieditor.net"
+  },
+  "contributors": [
+    {
+      "name": "flabbet",
+      "email": "[email protected]",
+      "website": "https://github.com/flabbet"
+    }
+  ],
+  "license": "MIT",
+  "categories": [
+    "Extension"
+  ]
+}

+ 5 - 0
src/PixiEditor.Extensions.Wasm/Api/FlyUI/Column.cs

@@ -2,6 +2,11 @@
 
 public class Column : MultiChildLayoutElement
 {
+    public Column(params LayoutElement[] children)
+    {
+        Children = new List<LayoutElement>(children);
+    }
+    
     public override CompiledControl BuildNative()
     {
         CompiledControl control = new CompiledControl(UniqueId, "Column");

+ 2 - 1
src/PixiEditor.Extensions.Wasm/Api/FlyUI/CompiledControl.cs

@@ -47,7 +47,7 @@ public class CompiledControl
     private List<byte> Serialize(List<byte> bytes)
     {
         // TODO: Make it more efficient
-
+        
         byte[] uniqueIdBytes = BitConverter.GetBytes(UniqueId);
         bytes.AddRange(uniqueIdBytes);
         byte[] idLengthBytes = BitConverter.GetBytes(ControlTypeId.Length);
@@ -58,6 +58,7 @@ public class CompiledControl
         bytes.AddRange(SerializeProperties());
         bytes.AddRange(BitConverter.GetBytes(Children.Count));
         SerializeChildren(bytes);
+        
         return bytes;
     }
 

+ 5 - 1
src/PixiEditor.Extensions/FlyUI/Elements/LayoutBuilder.cs

@@ -20,7 +20,11 @@ public class LayoutBuilder
     public ILayoutElement<Control> Deserialize(Span<byte> layoutSpan, DuplicateResolutionTactic duplicatedIdTactic)
     {
         int offset = 0;
+        return DeserializeInternal(layoutSpan, duplicatedIdTactic, ref offset);
+    }
 
+    private ILayoutElement<Control> DeserializeInternal(Span<byte> layoutSpan, DuplicateResolutionTactic duplicatedIdTactic, ref int offset)
+    {
         int uniqueId = BitConverter.ToInt32(layoutSpan[offset..(offset + int32Size)]);
         offset += int32Size;
 
@@ -75,7 +79,7 @@ public class LayoutBuilder
         var children = new List<ILayoutElement<Control>>();
         for (int i = 0; i < childrenCount; i++)
         {
-            children.Add(Deserialize(layoutSpan[offset..], duplicatedIdTactic));
+            children.Add(DeserializeInternal(layoutSpan, duplicatedIdTactic, ref offset));
         }
 
         return children;