}
}
+ // TODO: the selection code is almost identical to the UiList selection code -> refactor
+ public int[] getSelection() {
+ ListFuncs ui = ListFuncs.getInstance();
+ try {
+ MemorySegment sel = (MemorySegment)ui.list_get_selection_allocated.invoke(valuePtr);
+ int count = (int)ui.list_selection_get_count.invoke(sel);
+ int[] selection = new int[count];
+ MemorySegment rows = (MemorySegment)ui.list_selection_get_rows.invoke(sel);
+ rows = rows.reinterpret(count * ValueLayout.JAVA_INT.byteSize());
+ for(int i=0; i<count; i++) {
+ selection[i] = (int)rows.getAtIndex(ValueLayout.JAVA_INT, i);
+ }
+ ui.list_selection_free.invoke(sel);
+ return selection;
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public int getSelectedIndex() {
+ int[] selection = getSelection();
+ if(selection.length == 0) {
+ return -1;
+ }
+ 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 int size() {
return list.size();
}