From f6e1efc59dcde3660740e835a802ee0cd11dc31c Mon Sep 17 00:00:00 2001 From: Tom Eugelink Date: Sun, 1 Sep 2024 08:29:51 +0200 Subject: [PATCH] cache the screen the node is on --- .../org/tbee/javafx/scene/layout/MigPane.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/javafx/src/main/java/org/tbee/javafx/scene/layout/MigPane.java b/javafx/src/main/java/org/tbee/javafx/scene/layout/MigPane.java index 88794a3..890f657 100755 --- a/javafx/src/main/java/org/tbee/javafx/scene/layout/MigPane.java +++ b/javafx/src/main/java/org/tbee/javafx/scene/layout/MigPane.java @@ -3,6 +3,7 @@ import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.collections.ListChangeListener; +import javafx.collections.ObservableList; import javafx.geometry.*; import javafx.scene.Group; import javafx.scene.Node; @@ -15,14 +16,12 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Screen; -import javafx.stage.Stage; import javafx.stage.Window; import net.miginfocom.layout.*; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; -import java.util.NoSuchElementException; /** @@ -1007,11 +1006,18 @@ public int getVerticalScreenDPI() { private Screen getScreen() { Window window = node.getScene().getWindow(); - for (Screen screen : Screen.getScreensForRectangle(window.getX(), window.getY(), 1., 1.)) { - return screen; - } - return Screen.getPrimary(); - } + + // Cache of one + int screenCacheKey = (window.getX() + "/" + window.getY()).hashCode(); + if (screenCacheKey != this.screenCacheKey) { + List screens = Screen.getScreensForRectangle(window.getX(), window.getY(), 1., 1.); + screen = (screens.isEmpty() ? Screen.getPrimary() : screens.get(0)); + this.screenCacheKey = screenCacheKey; + } + return screen; + } + private int screenCacheKey = 0; + private Screen screen = null; @Override public float getPixelUnitFactor(boolean isHor) {