Browse Source

Finished implementing WindowingApi

flabbet 1 year ago
parent
commit
1ae174cba0

+ 23 - 4
src/PixiEditor.Extensions.Wasm/Api/Window/PopupWindow.cs

@@ -35,10 +35,29 @@ public class PopupWindow : IPopupWindow
         return showDialogTask;
     }
 
-    public double Width { get; set; }
-    public double Height { get; set; }
-    public bool CanResize { get; set; }
-    public bool CanMinimize { get; set; }
+    public double Width
+    {
+        get => Interop.get_window_width(windowHandle);
+        set => Interop.set_window_width(windowHandle, value);
+    }
+
+    public double Height
+    {
+        get => Interop.get_window_height(windowHandle);
+        set => Interop.set_window_height(windowHandle, value);
+    }
+
+    public bool CanResize
+    {
+        get => Interop.get_window_resizable(windowHandle);
+        set => Interop.set_window_resizable(windowHandle, value);
+    }
+
+    public bool CanMinimize
+    {
+        get => Interop.get_window_minimizable(windowHandle);
+        set => Interop.set_window_minimizable(windowHandle, value);
+    }
     
     Task<bool?> IPopupWindow.ShowDialog()
     {

+ 25 - 0
src/PixiEditor.Extensions.Wasm/Interop.Windowing.cs

@@ -13,6 +13,31 @@ internal static partial class Interop
 
     [MethodImpl(MethodImplOptions.InternalCall)]
     internal static extern string get_window_title(int windowHandle);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern double get_window_width(int windowHandle);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern void set_window_width(int windowHandle, double width);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern double get_window_height(int windowHandle);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern void set_window_height(int windowHandle, double height);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern bool get_window_resizable(int windowHandle);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern void set_window_resizable(int windowHandle, bool resizable);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern bool get_window_minimizable(int windowHandle);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    internal static extern void set_window_minimizable(int windowHandle, bool minimizable);
+    
 
     [MethodImpl(MethodImplOptions.InternalCall)]
     internal static extern void show_window(int windowHandle);

+ 1 - 0
src/PixiEditor.Extensions.Wasm/native/interop.c

@@ -1,6 +1,7 @@
 #include <assert.h>
 #include <driver.h>
 #include <string.h>
+#include <stdbool.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/appdomain.h>

+ 56 - 0
src/PixiEditor.Extensions.WasmRuntime/Api/WindowingApi.cs

@@ -52,4 +52,60 @@ internal class WindowingApi : ApiGroupHandler
         var window = NativeObjectManager.GetObject<PopupWindow>(handle);
         window.Close();
     }
+    
+    [ApiFunction("get_window_width")]
+    public double GetWindowWidth(int handle)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        return window.Width;
+    }
+    
+    [ApiFunction("set_window_width")]
+    public void SetWindowWidth(int handle, double width)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        window.Width = width;
+    }
+    
+    [ApiFunction("get_window_height")]
+    public double GetWindowHeight(int handle)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        return window.Height;
+    }
+    
+    [ApiFunction("set_window_height")]
+    public void SetWindowHeight(int handle, double height)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        window.Height = height;
+    }
+    
+    [ApiFunction("get_window_resizable")]
+    public bool GetWindowResizable(int handle)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        return window.CanResize;
+    }
+    
+    [ApiFunction("set_window_resizable")]
+    public void SetWindowResizable(int handle, bool resizable)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        window.CanResize = resizable;
+    }
+    
+    [ApiFunction("get_window_minimizable")]
+    public bool GetWindowMinimizable(int handle)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        return window.CanMinimize;
+    }
+    
+    [ApiFunction("set_window_minimizable")]
+    public void SetWindowMinimizable(int handle, bool minimizable)
+    {
+        var window = NativeObjectManager.GetObject<PopupWindow>(handle);
+        window.CanMinimize = minimizable;
+    }
 }

+ 26 - 0
src/PixiEditor.Extensions.WasmRuntime/WasmMemoryUtility.cs

@@ -71,6 +71,32 @@ public class WasmMemoryUtility
         memory.WriteString(ptr, valueWithNullTerminator);
         return ptr;
     }
+    
+    public int WriteDouble(double value)
+    {
+        const int length = 8;
+        var ptr = malloc.Invoke(length);
+        memory.WriteDouble(ptr, value);
+        return ptr;
+    }
+    
+    public double GetDouble(int offset)
+    {
+        return memory.ReadDouble(offset);
+    }
+    
+    public int WriteBoolean(bool value)
+    {
+        const int length = 1;
+        var ptr = malloc.Invoke(length);
+        memory.Write(ptr, value);
+        return ptr;
+    }
+    
+    public bool GetBoolean(int offset)
+    {
+        return memory.Read<bool>(offset);
+    }
 
     public void Free(int address)
     {

+ 3 - 3
src/PixiEditor.WasmApi.Gen/ApiGenerator.cs

@@ -96,8 +96,8 @@ public class ApiGenerator : IIncrementalGenerator
 
         foreach (var argSymbol in arguments)
         {
-            // For some reason, int are passed as is, not as a pointer
-            if (!TypeConversionTable.IsIntType(argSymbol.Type))
+            // For some reason, int, double are passed as is, not as a pointer
+            if (!TypeConversionTable.IsValuePassableType(argSymbol.Type, out _))
             {
                 string lowerType = argSymbol.Type.Name;
                 bool isLengthType = TypeConversionTable.IsLengthType(argSymbol);
@@ -137,7 +137,7 @@ public class ApiGenerator : IIncrementalGenerator
                     string statementString =
                         $"return WasmMemoryUtility.Write{returnType}({returnStatementSyntax.Expression.ToFullString()});";
 
-                    if (TypeConversionTable.IsIntType(method.methodSymbol.ReturnType))
+                    if (TypeConversionTable.IsValuePassableType(method.methodSymbol.ReturnType, out _))
                     {
                         statementString = $"return {returnStatementSyntax.Expression.ToFullString()};";
                     }

+ 17 - 4
src/PixiEditor.WasmApi.Gen/TypeConversionTable.cs

@@ -6,9 +6,9 @@ public static class TypeConversionTable
 {
     public static string[] ConvertTypeToFunctionParams(IParameterSymbol symbol)
     {
-        if(IsIntType(symbol.Type))
+        if(IsValuePassableType(symbol.Type, out string? typeName))
         {
-            return [$"int {symbol.Name}"];
+            return [$"{typeName} {symbol.Name}"];
         }
 
         if(IsLengthType(symbol))
@@ -26,8 +26,21 @@ public static class TypeConversionTable
                || symbol.Type.Name.Equals("span", StringComparison.OrdinalIgnoreCase);
     }
 
-    public static bool IsIntType(ITypeSymbol argSymbol)
+    public static bool IsValuePassableType(ITypeSymbol argSymbol, out string? typeName)
     {
-        return argSymbol.Name.Equals("int32", StringComparison.OrdinalIgnoreCase);
+        if (argSymbol.Name.Equals("int32", StringComparison.OrdinalIgnoreCase))
+        {
+            typeName = "int";
+            return true;
+        }
+
+        if (argSymbol.Name.Equals("double", StringComparison.OrdinalIgnoreCase))
+        {
+            typeName = "double";
+            return true;
+        }
+
+        typeName = null;
+        return false;
     }
 }

+ 6 - 1
src/WasmSampleExtension/SampleExtension.cs

@@ -21,7 +21,12 @@ public class SampleExtension : WasmExtension
                 )
             );
 
-        var showTask = Api.WindowProvider.CreatePopupWindow("WASM SampleExtension", layout).ShowDialog();
+        var window = Api.WindowProvider.CreatePopupWindow("WASM SampleExtension", layout);
+        window.Width = 200;
+        window.Height = 200;
+        window.CanResize = false;
+        window.CanMinimize = false;
+        var showTask = window.ShowDialog();
         showTask.Completed += result =>
         {
             Api.Logger.Log($"Show task completed: {result}");