More window center fixes

This commit is contained in:
crschnick 2024-05-17 14:45:51 +00:00
parent ff9305da3c
commit b63f899a86

View file

@ -7,7 +7,6 @@ import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.InputHelper; import io.xpipe.app.util.InputHelper;
import io.xpipe.app.util.ThreadHelper; import io.xpipe.app.util.ThreadHelper;
import io.xpipe.core.process.OsType; import io.xpipe.core.process.OsType;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.css.PseudoClass; import javafx.css.PseudoClass;
@ -23,12 +22,16 @@ import javafx.scene.layout.Pane;
import javafx.scene.layout.Region; import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.stage.*; import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
@ -41,19 +44,26 @@ public class AppWindowHelper {
return; return;
} }
var xSet = new AtomicBoolean();
stage.xProperty().addListener((observable, oldValue, newValue) -> { stage.xProperty().addListener((observable, oldValue, newValue) -> {
var n = newValue.doubleValue(); var n = newValue.doubleValue();
var o = oldValue.doubleValue(); var o = oldValue.doubleValue();
if (stage.isShowing() && areNumbersValid(o, n) && n == 0.0 && o != 0.0 && Math.abs(n - o) > 100) { if (stage.isShowing() && areNumbersValid(o, n)) {
if (!xSet.getAndSet(true) && !stage.isMaximized() && n <= 0.0 && o > 0.0 && Math.abs(n - o) > 100) {
stage.setX(o); stage.setX(o);
} }
}
}); });
var ySet = new AtomicBoolean();
stage.yProperty().addListener((observable, oldValue, newValue) -> { stage.yProperty().addListener((observable, oldValue, newValue) -> {
var n = newValue.doubleValue(); var n = newValue.doubleValue();
var o = oldValue.doubleValue(); var o = oldValue.doubleValue();
if (stage.isShowing() && areNumbersValid(o, n) && n == 0.0 && o != 0.0 && Math.abs(n - o) > 20) { if (stage.isShowing() && areNumbersValid(o, n)) {
if (!ySet.getAndSet(true) && !stage.isMaximized() && n <= 0.0 && o > 0.0 && Math.abs(n - o) > 20) {
stage.setY(o); stage.setY(o);
} }
}
}); });
} }
@ -172,7 +182,7 @@ public class AppWindowHelper {
Alert a = AppWindowHelper.createEmptyAlert(); Alert a = AppWindowHelper.createEmptyAlert();
AppFont.normal(a.getDialogPane()); AppFont.normal(a.getDialogPane());
var s = (Stage) a.getDialogPane().getScene().getWindow(); var s = (Stage) a.getDialogPane().getScene().getWindow();
fixInvalidStagePosition(new Stage()); fixInvalidStagePosition(s);
s.setOnShown(event -> { s.setOnShown(event -> {
Platform.runLater(() -> { Platform.runLater(() -> {
clampWindow(s).ifPresent(rectangle2D -> { clampWindow(s).ifPresent(rectangle2D -> {