From aceb68c6b57bdfb7b88ca4a3fe213c97677a0357 Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Wed, 9 Oct 2024 16:31:03 +0200 Subject: [PATCH] Translate points in display coordinate space gap This contribution fixes the current behaviour of the display coordinate system of eclipse where a point can be manually set to somewhere in the gap between the 2 monitor in the coordinate space (in the points system). This fix translates those points to inside the scaled down space of a monitor providing it the right coordinates in the display coordinate space. contributes to #62 and #127 --- .../org/eclipse/swt/widgets/Display.java | 23 +++++++++++++++---- .../win32/org/eclipse/swt/widgets/Shell.java | 6 +++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index 00205d39e30..836af42d9f2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -5375,14 +5375,25 @@ private boolean setDPIAwareness(int desiredDpiAwareness) { } private Monitor getContainingMonitor(int x, int y) { + return getContainingMonitorOptional(x, y).orElse(getPrimaryMonitor()); +} + +private Optional getContainingMonitorOptional(int x, int y) { Monitor[] monitors = getMonitors(); for (Monitor currentMonitor : monitors) { Rectangle clientArea = currentMonitor.getClientArea(); if (clientArea.contains(x, y)) { - return currentMonitor; + return Optional.of(currentMonitor); } } - return getPrimaryMonitor(); + return Optional.empty(); +} + +Point translatePointIfInDisplayCoordinateGap(int x, int y) { + if(getContainingMonitorOptional(x, y).isEmpty() && getContainingMonitorInPixelsCoordinateOptional(x, y).isPresent()) { + return translateLocationInPointInDisplayCoordinateSystem(x, y); + } + return new Point(x, y); } private Monitor getContainingMonitor(int x, int y, int width, int height) { @@ -5403,14 +5414,18 @@ private Monitor getContainingMonitor(int x, int y, int width, int height) { } private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) { + return getContainingMonitorInPixelsCoordinateOptional(xInPixels, yInPixels).orElse(getPrimaryMonitor()); +} + +private Optional getContainingMonitorInPixelsCoordinateOptional(int xInPixels, int yInPixels) { Monitor[] monitors = getMonitors(); for (Monitor current : monitors) { Rectangle clientArea = getMonitorClientAreaInPixels(current); if (clientArea.contains(xInPixels, yInPixels)) { - return current; + return Optional.of(current); } } - return getPrimaryMonitor(); + return Optional.empty(); } private Rectangle getMonitorClientAreaInPixels(Monitor monitor) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java index 287646dad6a..93635d13e28 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java @@ -1586,7 +1586,8 @@ public void setLocation(Point location) { @Override public void setLocation(int x, int y) { - Point location = display.translateLocationInPixelsInDisplayCoordinateSystem(x, y); + Point translatedPoint = display.translatePointIfInDisplayCoordinateGap(x, y); + Point location = display.translateLocationInPixelsInDisplayCoordinateSystem(translatedPoint.x, translatedPoint.y); setLocationInPixels(location.x, location.y); } @@ -1598,7 +1599,8 @@ public void setBounds(Rectangle rect) { @Override public void setBounds(int x, int y, int width, int height) { - Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(x, y, width, height); + Point translatedPoint = display.translatePointIfInDisplayCoordinateGap(x, y); + Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(translatedPoint.x, translatedPoint.y, width, height); setBoundsInPixels(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height); }