From cfd433b3286a5a7634e9dd27ecd5a0148ea3f821 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Tue, 29 Aug 2023 07:26:46 +0200 Subject: [PATCH] revert to simple of of bounds check as suggested in https://github.com/cryptomator/cryptomator/pull/3017#discussion_r1264527894 with minor adjustments --- .../ui/mainwindow/ResizeController.java | 49 +++++++------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java b/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java index b136fa55c..76bddd0d8 100644 --- a/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java +++ b/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java @@ -7,7 +7,6 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; import javafx.beans.binding.BooleanBinding; -import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.geometry.Rectangle2D; import javafx.scene.input.MouseEvent; @@ -67,37 +66,7 @@ public class ResizeController implements FxController { return (settings.windowHeight.get() == 0) && (settings.windowWidth.get() == 0) && (settings.windowXPosition.get() == 0) && (settings.windowYPosition.get() == 0); } - private boolean isWithinDisplayBounds() { - // (x1, y1) is the top left corner of the window, (x2, y2) is the bottom right corner - final double slack = 10; - final double width = window.getWidth() - 2 * slack; - final double height = window.getHeight() - 2 * slack; - final double x1 = window.getX() + slack; - final double y1 = window.getY() + slack; - final double x2 = x1 + width; - final double y2 = y1 + height; - - final ObservableList screens = Screen.getScreensForRectangle(x1, y1, width, height); - - // Find the total visible area of the window - double visibleArea = 0; - for (Screen screen : screens) { - Rectangle2D bounds = screen.getVisualBounds(); - - double xOverlap = Math.min(x2, bounds.getMaxX()) - Math.max(x1, bounds.getMinX()); - double yOverlap = Math.min(y2, bounds.getMaxY()) - Math.max(y1, bounds.getMinY()); - - visibleArea += xOverlap * yOverlap; - } - - final double windowArea = width * height; - - // Within bounds if the visible area matches the window area - return visibleArea == windowArea; - } - private void checkDisplayBounds(WindowEvent evt) { - // Minimizing a window in Windows and closing it could result in an out of bounds position at (x, y) = (-32000, -32000) // See https://devblogs.microsoft.com/oldnewthing/20041028-00/?p=37453 // If the position is (-32000, -32000), restore to the last saved position @@ -108,8 +77,9 @@ public class ResizeController implements FxController { window.setHeight(settings.windowHeight.get()); } - if (!isWithinDisplayBounds()) { + if (isOutOfDisplayBounds()) { // If the position is illegal, then the window appears on the main screen in the middle of the window. + LOG.debug("Resetting window position due to insufficient screen overlap"); Rectangle2D primaryScreenBounds = Screen.getPrimary().getBounds(); window.setX((primaryScreenBounds.getWidth() - window.getMinWidth()) / 2); window.setY((primaryScreenBounds.getHeight() - window.getMinHeight()) / 2); @@ -119,6 +89,21 @@ public class ResizeController implements FxController { } } + private boolean isOutOfDisplayBounds() { + final int x = settings.windowXPosition.get(); + final int y = settings.windowYPosition.get(); + final int width = settings.windowWidth.get(); + final int height = settings.windowHeight.get(); + return isRectangleOutOfScreen(x + 20, y, 0, height) // Left pixel column (+20) + || isRectangleOutOfScreen(x + width - 20, y, 0, height) // Right pixel column (-20) + || isRectangleOutOfScreen(x, y + 5, width, 0) // Top pixel row (+5) + || isRectangleOutOfScreen(x, y + height - 20, width, 0); // Bottom pixel row (-20) + } + + private boolean isRectangleOutOfScreen(int x, int y, int width, int height) { + return Screen.getScreensForRectangle(x, y, width, height).isEmpty(); + } + private void startResize(MouseEvent evt) { origX = window.getX(); origY = window.getY();