Check display bounds onShowing event

This commit is contained in:
Rexbas
2023-07-18 20:08:13 +02:00
parent aedbefc38a
commit 094a7c6a20

View File

@@ -15,6 +15,7 @@ import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
@MainWindow
public class ResizeController implements FxController {
@@ -71,6 +72,9 @@ public class ResizeController implements FxController {
window.setY(settings.windowYPosition.get());
}
}
window.setOnShowing(this::checkDisplayBounds);
savePositionalSettings();
}
@@ -115,6 +119,18 @@ public class ResizeController implements FxController {
return visibleArea == windowArea;
}
private void checkDisplayBounds(WindowEvent evt) {
if (!isWithinDisplayBounds()) {
// If the position is illegal, then the window appears on the main screen in the middle of the window.
Rectangle2D primaryScreenBounds = Screen.getPrimary().getBounds();
window.setX((primaryScreenBounds.getWidth() - window.getMinWidth()) / 2);
window.setY((primaryScreenBounds.getHeight() - window.getMinHeight()) / 2);
window.setWidth(window.getMinWidth());
window.setHeight(window.getMinHeight());
savePositionalSettings();
}
}
private String getMonitorSizes() {
ObservableList<Screen> screens = Screen.getScreens();
StringBuilder sb = new StringBuilder();
@@ -212,5 +228,4 @@ public class ResizeController implements FxController {
public boolean isShowResizingArrows() {
return showResizingArrows.get();
}
}