Skip to content

Commit

Permalink
[win32] Forward ZoomChanged to all child widgets
Browse files Browse the repository at this point in the history
This commit adds forwarding of a ZoomChanged events in the DPI change handler of Composite. With that all non top level widgets will notified about the ZoomChanged event in the win32 implementation so custom widgets can register listeners to react on that.

Contributes to #62 and #131
  • Loading branch information
akoch-yatta committed Jul 26, 2024
1 parent c3f7474 commit 14407e9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.*;
import java.util.Map.*;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class DPIZoomChangeRegistry {
Expand Down Expand Up @@ -54,6 +55,12 @@ public static void applyChange(Widget widget, int newZoom, float scalingFactor)
handler.handleDPIChange(widget, newZoom, scalingFactor);
}
}
Event event = new Event();
event.type = SWT.ZoomChanged;
event.widget = widget;
event.detail = newZoom;
event.doit = true;
widget.notifyListeners(SWT.ZoomChanged, event);
}

public static void registerHandler(DPIZoomChangeHandler zoomChangeVisitor, Class<? extends Widget> clazzToRegisterFor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4900,7 +4900,7 @@ LRESULT WM_DESTROY (long wParam, long lParam) {
LRESULT WM_DPICHANGED (long wParam, long lParam) {
// Map DPI to Zoom and compare
int newNativeZoom = DPIUtil.mapDPIToZoom (OS.HIWORD (wParam));
int oldNativeZoom = getShell().nativeZoom;
int oldNativeZoom = nativeZoom;

// Throw the DPI change event if zoom value changes
if (newNativeZoom != oldNativeZoom) {
Expand All @@ -4914,7 +4914,8 @@ LRESULT WM_DPICHANGED (long wParam, long lParam) {
DPIUtil.setDeviceZoom (newNativeZoom);
}

notifyListeners(SWT.ZoomChanged, event);
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(newNativeZoom) / DPIUtil.getZoomForAutoscaleProperty(oldNativeZoom);
DPIZoomChangeRegistry.applyChange(this, newNativeZoom, scalingFactor);

if (getDisplay().isRescalingAtRuntime()) {
RECT rect = new RECT ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,6 @@ public Shell (Display display, int style) {

reskinWidget();
createWidget ();


if (getDisplay().isRescalingAtRuntime()) {
addListener(SWT.ZoomChanged, this::handleZoomEvent);
}
}

/**
Expand Down Expand Up @@ -2647,12 +2642,6 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
return result;
}

private void handleZoomEvent(Event event) {
int newNativeZoom = event.detail;
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(newNativeZoom) / getZoom();
DPIZoomChangeRegistry.applyChange(this, newNativeZoom, scalingFactor);
}

private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) {
if (!(widget instanceof Shell shell)) {
return;
Expand Down

0 comments on commit 14407e9

Please sign in to comment.