]> uap-core.de Git - rssreader.git/commitdiff
add methods for changing a UiList selection
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 30 Aug 2025 08:22:23 +0000 (10:22 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 30 Aug 2025 08:22:23 +0000 (10:22 +0200)
ui-java/src/main/java/de/unixwork/ui/ListFuncs.java
ui-java/src/main/java/de/unixwork/ui/UiList.java

index e91eb731c4ea509ce3099c81ab33a81fe950518f..ca9cb08ac97e2d1a28efaae26568f36043292fbf 100644 (file)
@@ -22,6 +22,8 @@ public class ListFuncs {
     public MethodHandle list_get_selection_allocated;
     public MethodHandle list_selection_get_count;
     public MethodHandle list_selection_get_rows;
+    public MethodHandle list_set_selected_indices;
+    public MethodHandle list_setselection;
     public MethodHandle list_selection_free;
 
     public MethodHandle srclist_new;
@@ -42,6 +44,7 @@ public class ListFuncs {
         FunctionDescriptor sigm_m = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
         FunctionDescriptor sigv_mm = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
         FunctionDescriptor sigv_mim = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.ADDRESS);
+        FunctionDescriptor sigv_mmi = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
         FunctionDescriptor sigv_mi = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
         FunctionDescriptor sigv_m = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
         FunctionDescriptor sigi_m = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS);
@@ -62,6 +65,8 @@ public class ListFuncs {
         MemorySegment ui_list_get_selection_allocated_addr = lib.find("ui_list_get_selection_allocated").orElseThrow();
         MemorySegment ui_list_selection_get_count_addr = lib.find("ui_list_selection_get_count").orElseThrow();
         MemorySegment ui_list_selection_get_rows_addr = lib.find("ui_list_selection_get_rows").orElseThrow();
+        MemorySegment ui_list_set_selected_indices_addr = lib.find("ui_list_set_selected_indices").orElseThrow();
+        MemorySegment ui_list_setselection_addr = lib.find("ui_list_setselection").orElseThrow();
         MemorySegment ui_list_selection_free_addr = lib.find("ui_list_selection_free").orElseThrow();
 
         MemorySegment ui_srclist_new_addr = lib.find("ui_srclist_new").orElseThrow();
@@ -92,6 +97,8 @@ public class ListFuncs {
         list_get_selection_allocated = linker.downcallHandle(ui_list_get_selection_allocated_addr, sigm_m);
         list_selection_get_count = linker.downcallHandle(ui_list_selection_get_count_addr, sigi_m);
         list_selection_get_rows = linker.downcallHandle(ui_list_selection_get_rows_addr, sigm_m);
+        list_set_selected_indices = linker.downcallHandle(ui_list_set_selected_indices_addr, sigv_mmi);
+        list_setselection = linker.downcallHandle(ui_list_setselection_addr, sigv_mi);
         list_selection_free = linker.downcallHandle(ui_list_selection_free_addr, sigv_m);
 
         srclist_new = linker.downcallHandle(ui_srclist_new_addr, sigm_mm);
index 104d7f0fec8594a39dbe99489e629bc797b58f4c..223c0ee2004462f147e26476ad48602f06a598c8 100644 (file)
@@ -101,6 +101,25 @@ public class UiList<T> extends ArrayList<T> {
         return selection[0];
     }
 
+    public void setSelectedIndex(int index) {
+        ListFuncs ui = ListFuncs.getInstance();
+        try {
+            ui.list_setselection.invoke(valuePtr, index);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void setSelection(int[] selection) {
+        ListFuncs ui = ListFuncs.getInstance();
+        try (Arena arena = Arena.ofConfined()) {
+            MemorySegment rows = arena.allocateFrom(ValueLayout.JAVA_INT, selection);
+            ui.list_set_selected_indices.invoke(valuePtr, rows, selection.length);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     public T getSelected() {
         int index = getSelectedIndex();
         if(index == -1) {