Skip to content

Commit efaeac0

Browse files
akoch-yattafedejeanne
authored andcommitted
[win32] Forward ZoomChanged to all child widgets
This commit adapt how the DPI change is propagated into the DPI update mechanism. In comes down to the following: Automatic rescaling is not active: It is exactly as in the beginning -> A ZoomChanged event is send for the affected control, wenn the zoom is change Automatic rescaling is active: The update is no longer propagation via a ZoomChanged event that was used by a listener in a Shell to propagate this change into the DPIZoomChangeRegistry, but it is directly propagated into DPIZoomChangeRegistry in the WM_DPICHANGED callback. Additionally, for each widget, that is handled in DPIZoomChangeRegistry for a dpi change a ZoomChanged event is created and sent to the widget. That enables creators of custom widget to hook into the update and refresh the state of the affected widget, e.g. refresh a widget, when it is rendering its content without a layout manager. Contributes to #62 and #131
1 parent 663d109 commit efaeac0

File tree

5 files changed

+29
-39
lines changed

5 files changed

+29
-39
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/GCWin32Tests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.eclipse.swt.*;
2222
import org.eclipse.swt.internal.*;
23-
import org.eclipse.swt.widgets.*;
2423
import org.junit.jupiter.api.*;
2524

2625
class GCWin32Tests extends Win32AutoscaleTestBase {
@@ -43,11 +42,7 @@ public void gcZoomLevelMustChangeOnShellZoomChange() {
4342
assertEquals("GCData must have a zoom level equal to the actual zoom level of the widget/shell", DPIUtil.getNativeDeviceZoom(), (int) gcNativeZoom.join());
4443

4544
int newSWTZoom = zoom * 2;
46-
Event swtEvent = new Event();
47-
swtEvent.type = SWT.ZoomChanged;
48-
swtEvent.widget = shell;
49-
swtEvent.detail = newSWTZoom;
50-
shell.notifyListeners(SWT.ZoomChanged, swtEvent);
45+
changeDPIZoom(newSWTZoom);
5146
isScaled.set(true);
5247
shell.setVisible(false);
5348
shell.setVisible(true);

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.internal;
1515

16-
import org.eclipse.swt.*;
1716
import org.eclipse.swt.widgets.*;
1817
import org.junit.jupiter.api.*;
1918

@@ -42,11 +41,7 @@ public void tearDownTest() {
4241
}
4342

4443
protected void changeDPIZoom (int nativeZoom) {
45-
Event event = new Event();
46-
event.type = SWT.ZoomChanged;
47-
event.widget = shell;
48-
event.detail = nativeZoom;
49-
event.doit = true;
50-
shell.notifyListeners(SWT.ZoomChanged, event);
44+
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom);
45+
DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor);
5146
}
5247
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeRegistry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.*;
1717
import java.util.Map.*;
1818

19+
import org.eclipse.swt.*;
1920
import org.eclipse.swt.widgets.*;
2021

2122
public class DPIZoomChangeRegistry {
@@ -54,6 +55,12 @@ public static void applyChange(Widget widget, int newZoom, float scalingFactor)
5455
handler.handleDPIChange(widget, newZoom, scalingFactor);
5556
}
5657
}
58+
Event event = new Event();
59+
event.type = SWT.ZoomChanged;
60+
event.widget = widget;
61+
event.detail = newZoom;
62+
event.doit = true;
63+
widget.notifyListeners(SWT.ZoomChanged, event);
5764
}
5865

5966
public static void registerHandler(DPIZoomChangeHandler zoomChangeVisitor, Class<? extends Widget> clazzToRegisterFor) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4900,28 +4900,32 @@ LRESULT WM_DESTROY (long wParam, long lParam) {
49004900
LRESULT WM_DPICHANGED (long wParam, long lParam) {
49014901
// Map DPI to Zoom and compare
49024902
int newNativeZoom = DPIUtil.mapDPIToZoom (OS.HIWORD (wParam));
4903-
int oldNativeZoom = getShell().nativeZoom;
4904-
4905-
// Throw the DPI change event if zoom value changes
4906-
if (newNativeZoom != oldNativeZoom) {
4907-
Event event = new Event();
4908-
event.type = SWT.ZoomChanged;
4909-
event.widget = this;
4910-
event.detail = newNativeZoom;
4911-
event.doit = true;
4912-
4913-
if (getDisplay().isRescalingAtRuntime()) {
4903+
if (getDisplay().isRescalingAtRuntime()) {
4904+
int oldNativeZoom = nativeZoom;
4905+
if (newNativeZoom != oldNativeZoom) {
49144906
DPIUtil.setDeviceZoom (newNativeZoom);
4915-
}
49164907

4917-
notifyListeners(SWT.ZoomChanged, event);
4908+
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(newNativeZoom) / DPIUtil.getZoomForAutoscaleProperty(oldNativeZoom);
4909+
DPIZoomChangeRegistry.applyChange(this, newNativeZoom, scalingFactor);
49184910

4919-
if (getDisplay().isRescalingAtRuntime()) {
49204911
RECT rect = new RECT ();
49214912
COM.MoveMemory(rect, lParam, RECT.sizeof);
49224913
this.setBoundsInPixels(rect.left, rect.top, rect.right - rect.left, rect.bottom-rect.top);
4914+
return LRESULT.ZERO;
4915+
}
4916+
} else {
4917+
int newZoom = DPIUtil.getZoomForAutoscaleProperty (newNativeZoom);
4918+
int oldZoom = DPIUtil.getZoomForAutoscaleProperty (nativeZoom);
4919+
if (newZoom != oldZoom) {
4920+
// Throw the DPI change event if zoom value changes
4921+
Event event = new Event();
4922+
event.type = SWT.ZoomChanged;
4923+
event.widget = this;
4924+
event.detail = DPIUtil.getZoomForAutoscaleProperty(newNativeZoom);
4925+
event.doit = true;
4926+
notifyListeners(SWT.ZoomChanged, event);
4927+
return LRESULT.ZERO;
49234928
}
4924-
return LRESULT.ZERO;
49254929
}
49264930
return LRESULT.ONE;
49274931
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,6 @@ public Shell (Display display, int style) {
311311

312312
reskinWidget();
313313
createWidget ();
314-
315-
316-
if (getDisplay().isRescalingAtRuntime()) {
317-
addListener(SWT.ZoomChanged, this::handleZoomEvent);
318-
}
319314
}
320315

321316
/**
@@ -2647,12 +2642,6 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
26472642
return result;
26482643
}
26492644

2650-
private void handleZoomEvent(Event event) {
2651-
int newNativeZoom = event.detail;
2652-
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(newNativeZoom) / getZoom();
2653-
DPIZoomChangeRegistry.applyChange(this, newNativeZoom, scalingFactor);
2654-
}
2655-
26562645
private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) {
26572646
if (!(widget instanceof Shell shell)) {
26582647
return;

0 commit comments

Comments
 (0)