From f30f88261c550bc9cdd35631ad48e5bbef754c0e Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Thu, 27 Jun 2024 09:13:21 +0200 Subject: [PATCH] [win32] Update shell zoom before position change When a Shell is moved to a new position, the new position can be located on a different monitor. If autoScaleOnRuntimeActive is active and the monitor has a different zoom than the previous one, this would lead to a wrong positioning of the shell due to the stored zoom of the shell. Therefor the Shell nativeZoom must update BEFORE the location change will be applied. This commit adds this behaviour on win32. Contributes to #62 and #127 --- .../org/eclipse/swt/widgets/Display.java | 11 ++++++ .../win32/org/eclipse/swt/widgets/Shell.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) 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 78dd10f2f6d..3afb5dd3e6e 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 @@ -5229,4 +5229,15 @@ static String withCrLf (String string) { static boolean isActivateShellOnForceFocus() { return "true".equals(System.getProperty("org.eclipse.swt.internal.activateShellOnForceFocus", "true")); //$NON-NLS-1$ } + +Monitor getContainingMonitor(int x, int y) { + Monitor[] monitors = getMonitors(); + for (Monitor current : monitors) { + Rectangle clientArea = current.getClientArea(); + if (clientArea.contains(x, y)) { + return current; + } + } + return getPrimaryMonitor(); +} } 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 d5c2a929a6e..c8875948449 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 @@ -1571,6 +1571,40 @@ public void setAlpha (int alpha) { } } +private void updateNativeZoomBeforePositionChange(int x, int y) { + if (DPIUtil.isAutoScaleOnRuntimeActive()) { + Monitor monitor = display.getContainingMonitor(x, y); + this.nativeZoom = monitor.zoom; + System.out.println("Update shell zoom (" + handle + ") " + monitor.zoom); + } +} + +@Override +public void setLocation(Point location) { + if (location == null) error (SWT.ERROR_NULL_ARGUMENT); + updateNativeZoomBeforePositionChange(location.x, location.y); + super.setLocation(location); +} + +@Override +public void setLocation(int x, int y) { + updateNativeZoomBeforePositionChange(x, y); + super.setLocation(x, y); +} + +@Override +public void setBounds(Rectangle rect) { + if (rect == null) error (SWT.ERROR_NULL_ARGUMENT); + updateNativeZoomBeforePositionChange(rect.x, rect.y); + super.setBounds(rect); +} + +@Override +public void setBounds(int x, int y, int width, int height) { + updateNativeZoomBeforePositionChange(x, y); + super.setBounds(x, y, width, height); +} + @Override void setBoundsInPixels (int x, int y, int width, int height, int flags, boolean defer) { if (fullScreen) setFullScreen (false);