]> uap-core.de Git - rssreader.git/commitdiff
keep sourcelist selection when updating it
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 27 Nov 2025 18:54:37 +0000 (19:54 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 27 Nov 2025 18:54:37 +0000 (19:54 +0100)
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt
ui-java/src/main/java/de/unixwork/ui/UiSourceList.java

index cb3c6964d5587a23602d0d05f2fd29a53c0e30a3..5d837bf923dd21cbea0a16e386b40f3f0f2669e3 100644 (file)
@@ -124,4 +124,10 @@ class FeedSourceList : Document() {
 
         }
     }
+
+    fun updateFeeds() {
+        val selection = feeds.selectedIndex
+        feeds.update()
+        feeds.selectedIndex = selection
+    }
 }
\ No newline at end of file
index d01738dc8cb602757066bc98ff892cf385631bec..cd6211bb46f34aeb6c6cbe301aa83209b5111f3c 100644 (file)
@@ -83,7 +83,7 @@ class MainWindow() {
                 } catch (e: Exception) {
                     e.printStackTrace()
                 }
-                sourceList.feeds.update()
+                sourceList.updateFeeds()
             }
         }
         separator()
@@ -247,7 +247,7 @@ class MainWindow() {
                             }
                             feedList.items.selected?.let {
                                 feedList.selectItem(it)
-                                sourceList.feeds.update()
+                                sourceList.updateFeeds()
                             }
                         },
                         getstyle = { elm, col, style ->
index f644889016727451996dac170415cceba49aa8ed..0f17c96e22d27ba9cf02307dccad17bec487fa86 100644 (file)
@@ -99,6 +99,52 @@ public class UiSourceList {
         }
     }
 
+    // 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();
     }