Rework window centering again

This commit is contained in:
crschnick 2024-05-17 12:23:18 +00:00
parent 59ce98d9db
commit 54e1e6369c
2 changed files with 25 additions and 14 deletions

View file

@ -7,7 +7,6 @@ import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.prefs.CloseBehaviourAlert;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.core.process.OsType;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Rectangle2D;
@ -106,7 +105,8 @@ public class AppMainWindow {
.handle();
}
private void setupListeners(WindowState state) {
private void setupListeners() {
AppWindowHelper.fixInvalidStagePosition(stage);
stage.xProperty().addListener((c, o, n) -> {
if (windowActive.get()) {
onChange();
@ -142,13 +142,6 @@ public class AppMainWindow {
});
stage.setOnShown(event -> {
// On some platforms, e.g. KDE with wayland, the screen size is not known when the window is first shown
// This fixes the alignment in these cases
if (state == null && stage.getX() == 0 && stage.getY() == 0) {
Platform.runLater(() -> {
stage.centerOnScreen();
});
}
stage.requestFocus();
});
@ -250,7 +243,7 @@ public class AppMainWindow {
var state = loadState();
initializeWindow(state);
setupListeners(state);
setupListeners();
windowActive.set(true);
TrackEvent.debug("Window set to active");
}

View file

@ -23,10 +23,7 @@ import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.*;
import java.util.Arrays;
import java.util.List;
@ -39,6 +36,27 @@ import java.util.function.Supplier;
public class AppWindowHelper {
public static void fixInvalidStagePosition(Stage stage) {
if (OsType.getLocal() != OsType.LINUX) {
return;
}
stage.xProperty().addListener((observable, oldValue, newValue) -> {
var n = newValue.doubleValue();
var o = oldValue.doubleValue();
if (stage.isShowing() && areNumbersValid(o, n) && n == 0.0 && o != 0.0 && Math.abs(n - o) > 100) {
stage.setX(o);
}
});
stage.yProperty().addListener((observable, oldValue, newValue) -> {
var n = newValue.doubleValue();
var o = oldValue.doubleValue();
if (stage.isShowing() && areNumbersValid(o, n) && n == 0.0 && o != 0.0 && Math.abs(n - o) > 100) {
stage.setY(o);
}
});
}
public static Node alertContentText(String s) {
var text = new Text(s);
text.setWrappingWidth(450);