List box memory usage improvements [stage]

This commit is contained in:
crschnick 2025-03-18 07:30:36 +00:00
parent 516cfced4a
commit e4b01ccf0b
2 changed files with 32 additions and 13 deletions

View file

@ -26,6 +26,7 @@ import javafx.scene.layout.VBox;
import lombok.Setter;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
@ -60,10 +61,26 @@ public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
vbox.setFocusTraversable(false);
var scroll = new ScrollPane(vbox);
refresh(scroll, vbox, shown, all, cache, false, false);
shown.addListener((ListChangeListener<? super T>) (c) -> {
refresh(scroll, vbox, c.getList(), all, cache, true, true);
var l = (ListChangeListener<? super T>) (c) -> {
Platform.runLater(() -> {
refresh(scroll, vbox, c.getList(), all, cache, true);
});
};
scroll.sceneProperty().subscribe(scene -> {
if (scene != null) {
shown.addListener(l);
} else {
shown.removeListener(l);
}
});
scroll.sceneProperty().addListener((observableValue, oldScene, newScene) -> {
// Apply changes made since init and scene assign
if (oldScene == null && newScene != null) {
refresh(scroll, vbox, shown, all, cache, true);
}
if (oldScene != null && newScene == null) {
cache.clear();
}
});
if (scrollBar) {
@ -228,7 +245,6 @@ public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
List<? extends T> shown,
List<? extends T> all,
Map<T, Region> cache,
boolean asynchronous,
boolean refreshVisibilities) {
Runnable update = () -> {
synchronized (cache) {
@ -237,7 +253,9 @@ public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
set.addAll(shown);
set.addAll(all);
// Clear cache of unused values
cache.keySet().removeIf(t -> !set.contains(t));
cache.keySet().removeIf(t -> {
return !set.contains(t);
});
}
// Create copy to reduce chances of concurrent modification
@ -245,6 +263,12 @@ public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
var newShown = shownCopy.stream()
.map(v -> {
if (!cache.containsKey(v)) {
// System.out.println("cache miss: " + v);
if (scroll.getScene() == null) {
// System.out.println("Ignored " + v);
return null;
}
var comp = compFunction.apply(v);
if (comp != null) {
var r = comp.createRegion();
@ -280,11 +304,6 @@ public class ListBoxViewComp<T> extends Comp<CompStructure<ScrollPane>> {
updateVisibilities(scroll, listView);
}
};
if (asynchronous) {
Platform.runLater(update);
} else {
PlatformThread.runLaterIfNeeded(update);
}
update.run();
}
}

View file

@ -1 +1 @@
15.7-2
15.7-3