]> uap-core.de Git - rssreader.git/commitdiff
add UiText and UiDouble types
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Jul 2025 16:36:45 +0000 (18:36 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Jul 2025 16:36:45 +0000 (18:36 +0200)
ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java
ui-java/src/main/java/de/unixwork/ui/UiDouble.java [new file with mode: 0644]
ui-java/src/main/java/de/unixwork/ui/UiInteger.java
ui-java/src/main/java/de/unixwork/ui/UiText.java [new file with mode: 0644]
ui-java/src/test/java/de/unixwork/ui/demo/Main.java

index f858b75a06fc37e9c9a3ff0642629b116c10db3f..3df12f43880a37cbdd940d2cb5fa529916905e7b 100644 (file)
@@ -48,7 +48,7 @@ public class ToolkitFuncs {
         // void* func(void*)
         FunctionDescriptor sigm_m = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
         FunctionDescriptor sigi_m = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS);
-        FunctionDescriptor sigm_mm = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
+        FunctionDescriptor sigm_mm = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS);
         FunctionDescriptor sigv_mm = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
         FunctionDescriptor sigv_m = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
         FunctionDescriptor sigm_l = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG);
diff --git a/ui-java/src/main/java/de/unixwork/ui/UiDouble.java b/ui-java/src/main/java/de/unixwork/ui/UiDouble.java
new file mode 100644 (file)
index 0000000..7b5b790
--- /dev/null
@@ -0,0 +1,42 @@
+package de.unixwork.ui;
+
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemorySegment;
+
+public class UiDouble {
+    protected MemorySegment valuePtr;
+
+    protected UiDouble(Context ctx, String name) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+
+        MemorySegment nameCStr = MemorySegment.NULL;
+        try (Arena arena = Arena.ofConfined()) {
+            if(name != null) {
+                nameCStr = arena.allocateFrom(name);
+            }
+
+            valuePtr = (MemorySegment) ui.double_new.invoke(ctx.getCtx(), nameCStr);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public double doubleValue() {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try {
+            Double value = (Double) ui.double_get.invoke(valuePtr);
+            return value.doubleValue();
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void setDoubleValue(double value) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try (Arena arena = Arena.ofConfined()) {
+            ui.int_set.invoke(valuePtr, value);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
index e3b89e686cdd31b2e3fd42fb9ea12acdb72e207f..c6d5a843cf569cecc43aeee94d438c59f6991c0a 100644 (file)
@@ -25,7 +25,7 @@ public class UiInteger {
         ToolkitFuncs ui = ToolkitFuncs.getInstance();
         try {
             Integer value = (Integer) ui.int_get.invoke(valuePtr);
-            return intValue();
+            return value.intValue();
         } catch (Throwable e) {
             throw new RuntimeException(e);
         }
diff --git a/ui-java/src/main/java/de/unixwork/ui/UiText.java b/ui-java/src/main/java/de/unixwork/ui/UiText.java
new file mode 100644 (file)
index 0000000..834ad5f
--- /dev/null
@@ -0,0 +1,46 @@
+package de.unixwork.ui;
+
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemorySegment;
+
+public class UiText {
+    protected MemorySegment valuePtr;
+
+    protected UiText(Context ctx, String name) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+
+        MemorySegment nameCStr = MemorySegment.NULL;
+        try (Arena arena = Arena.ofConfined()) {
+            if(name != null) {
+                nameCStr = arena.allocateFrom(name);
+            }
+
+            valuePtr = (MemorySegment) ui.text_new.invoke(ctx.getCtx(), nameCStr);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public String toString() {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try {
+            MemorySegment cstr = (MemorySegment) ui.text_get.invoke(valuePtr);
+            if (cstr != null && cstr.address() != 0) {
+                return cstr.getString(0);
+            }
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+        return null;
+    }
+
+    public void setText(String string) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try (Arena arena = Arena.ofConfined()) {
+            MemorySegment cstr = arena.allocateFrom(string);
+            ui.text_set.invoke(valuePtr, cstr);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
index b044a5f2b23015106c5a087d9b52a7332346ad49..a8d0918b1ee9bbeb1b38e103cd2734b94c1f25bf 100644 (file)
@@ -4,6 +4,15 @@ import de.unixwork.ui.*;
 
 import static java.awt.SystemColor.window;
 
+class MyDocument extends Document {
+    @Var(name = "string")
+    UiString string;
+
+    MyDocument() {
+
+    }
+}
+
 public class Main implements Application{
     public void startup() {
         UiObject window = UiObject.createWindow("Test Window");
@@ -13,6 +22,8 @@ public class Main implements Application{
         mylist.add("Test3");
         mylist.add("Test4");
 
+        MyDocument doc = new MyDocument();
+
         Button.button(window).label("Click Me").onClick(event -> {
             System.out.println("Clicked");
         }).create();