From ad8479ce2f96237c4219bffb0c89143860bb2333 Mon Sep 17 00:00:00 2001 From: Federico Jeanne Date: Fri, 7 Nov 2025 11:36:38 +0100 Subject: [PATCH] Delay call to addListener in constructors of CCombo and StyledText #2733 The method is not final, which may cause issues if a subclass overrides it because it ends up being called upon instantiation, when the object is not fully initialized. Fixes https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733 --- .../common/org/eclipse/swt/custom/CCombo.java | 13 ++++++++++++- .../common/org/eclipse/swt/custom/StyledText.java | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java index 965bbfecc1..c700fd1632 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java @@ -173,7 +173,18 @@ public CCombo (Composite parent, int style) { } initAccessible(); - addListener(SWT.ZoomChanged, this::handleDPIChange); + + // Add listener asynchronously in order to delay execution. This works because + // other DPI changes are also executed asynchronously but if we choose to revert + // back to synchronous execution of the listeners then this async call may be + // executed too late and zoom change events may happen too early and not get + // propagated. + // See https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733 + getDisplay().asyncExec(() -> { + if (!isDisposed()) { + addListener(SWT.ZoomChanged, this::handleDPIChange); + } + }); } static int checkStyle (int style) { int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.LEAD | SWT.CENTER | SWT.TRAIL; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java index 993a27c529..b92d9fd4a6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java @@ -758,7 +758,18 @@ public StyledText(Composite parent, int style) { initializeAccessible(); setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this)); if (IS_MAC) setData(STYLEDTEXT_KEY); - addListener(SWT.ZoomChanged, this::handleDPIChange); + + // Add listener asynchronously in order to delay execution. This works because + // other DPI changes are also executed asynchronously but if we choose to revert + // back to synchronous execution of the listeners then this async call may be + // executed too late and zoom change events may happen too early and not get + // propagated. + // See https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733 + getDisplay().asyncExec(() -> { + if (!isDisposed()) { + addListener(SWT.ZoomChanged, this::handleDPIChange); + } + }); } /** * Adds an extended modify listener. An ExtendedModify event is sent by the