]> uap-core.de Git - rssreader.git/commitdiff
add widget methods for enabling/disabling and controlling visibility
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 25 Sep 2025 14:58:50 +0000 (16:58 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 25 Sep 2025 14:58:50 +0000 (16:58 +0200)
ui-java/src/main/java/de/unixwork/ui/AbstractWidgetBuilder.java
ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java
ui-java/src/main/java/de/unixwork/ui/UiWidget.java

index bdff20533269b7c597821abc6f88c474090f8a67..60bdc73ad406a22b9fe3429740cf0b0fb8ddf8e7 100644 (file)
@@ -16,7 +16,7 @@ public abstract class AbstractWidgetBuilder {
         UiWidget widget = null;
         try (Arena arena = Arena.ofConfined()) {
             MemorySegment args = createArgs(arena);
-            widget = new UiWidget((MemorySegment) widgetConstructor.invoke(obj.ptr, args));
+            widget = new UiWidget(obj, (MemorySegment) widgetConstructor.invoke(obj.ptr, args));
             freeArgs();
         } catch (Throwable e) {
             throw new RuntimeException(e);
index d53107fd0759db8df77496adfe7245df284ca2a8..4cbc59ff58737cedfa880c0afeb2cee397df89fb 100644 (file)
@@ -80,6 +80,14 @@ public class ToolkitFuncs {
     public MethodHandle textstyle_set_color;
     public MethodHandle textstyle_enable_color;
 
+    public MemorySegment ui_set_visible_addr;
+    public MemorySegment ui_set_enabled_addr;
+
+    public MethodHandle set_visible;
+    public MethodHandle set_enabled;
+    public MethodHandle set_widget_groups2;
+    public MethodHandle set_widget_visibility_states;
+
     public MethodHandle call_mainthread;
 
     public MethodHandle ui_malloc;
@@ -103,6 +111,7 @@ public class ToolkitFuncs {
         FunctionDescriptor sigm_ml = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG);
         FunctionDescriptor sigm_mi = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
         FunctionDescriptor sigv_mb = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_BOOLEAN);
+        FunctionDescriptor sigv_mi = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
         FunctionDescriptor sigv_m = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
         FunctionDescriptor sigm_l = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG);
         FunctionDescriptor sigv_miii = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT);
@@ -110,6 +119,8 @@ public class ToolkitFuncs {
         FunctionDescriptor sigv_md = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_DOUBLE);
         FunctionDescriptor sigd_m = FunctionDescriptor.of(ValueLayout.JAVA_DOUBLE, ValueLayout.ADDRESS);
         FunctionDescriptor sigv_mdd = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_DOUBLE, ValueLayout.JAVA_DOUBLE);
+        FunctionDescriptor sigv_mmmi = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
+        FunctionDescriptor sigv_mmmmi = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
 
         MemorySegment object_get_context_addr = lib.find("ui_object_get_context").orElseThrow();
 
@@ -185,6 +196,11 @@ public class ToolkitFuncs {
         MemorySegment textstyle_set_color_addr = lib.find("ui_textstyle_set_color").orElseThrow();
         MemorySegment textstyle_enable_color_addr = lib.find("ui_textstyle_enable_color").orElseThrow();
 
+        ui_set_visible_addr = lib.find("ui_set_visible").orElseThrow();
+        ui_set_enabled_addr = lib.find("ui_set_enabled").orElseThrow();
+        MemorySegment ui_widget_set_groups2_addr = lib.find("ui_widget_set_groups2").orElseThrow();
+        MemorySegment ui_widget_set_visibility_states_addr = lib.find("ui_widget_set_visibility_states").orElseThrow();
+
         MemorySegment call_mainthread_addr = lib.find("ui_call_mainthread").orElseThrow();
 
         MemorySegment ui_malloc_addr = lib.find("ui_malloc").orElseThrow();
@@ -272,6 +288,11 @@ public class ToolkitFuncs {
         textstyle_set_color = linker.downcallHandle(textstyle_set_color_addr, sigv_miii);
         textstyle_enable_color = linker.downcallHandle(textstyle_enable_color_addr, sigv_mb);
 
+        set_enabled = linker.downcallHandle(ui_set_enabled_addr, sigv_mi);
+        set_visible = linker.downcallHandle(ui_set_visible_addr, sigv_mi);
+        set_widget_groups2 = linker.downcallHandle(ui_widget_set_groups2_addr, sigv_mmmmi);
+        set_widget_visibility_states = linker.downcallHandle(ui_widget_set_visibility_states_addr, sigv_mmmi);
+
         call_mainthread = linker.downcallHandle(call_mainthread_addr, sigv_mm);
 
         getappdir = linker.downcallHandle(getappdir_addr, sigm);
index 84ab107bbf47e39e25469c94311be03922dfe0fd..f97385e15ecb7fb09e0c01f9b2ad2e45fae04bcc 100644 (file)
@@ -1,15 +1,52 @@
 package de.unixwork.ui;
 
+import java.lang.foreign.Arena;
 import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
 
 public class UiWidget {
     private MemorySegment widget;
+    private UiObject obj;
 
-    public UiWidget(MemorySegment widget) {
+    public UiWidget(UiObject obj, MemorySegment widget) {
+        this.obj = obj;
         this.widget = widget;
     }
 
     public MemorySegment getWidgetPtr() {
         return widget;
     }
+
+    public UiObject getToplevelObject() {
+        return obj;
+    }
+
+    public void setEnabled(boolean enabled) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try {
+            ui.set_enabled.invoke(widget, enabled ? 1 : 0);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void setVisible(boolean visible) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try {
+            ui.set_visible.invoke(widget, visible ? 1 : 0);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void setEnabledStates(int... states) {
+        ToolkitFuncs ui = ToolkitFuncs.getInstance();
+        try (Arena arena = Arena.ofConfined()) {
+            MemorySegment statesArray = arena.allocate(ValueLayout.JAVA_INT, states.length);
+            MemorySegment.copy(states, 0, statesArray, ValueLayout.JAVA_INT, 0, states.length);
+            ui.set_widget_groups2.invoke(obj.getCtx(), widget, MemorySegment.NULL, statesArray, states.length);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
 }