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 8dbc16ff941..b6e892d72fd 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,6 +173,9 @@ public CCombo (Composite parent, int style) { } initAccessible(); + addListener(SWT.ZoomChanged, event -> { + handleCComboDPIChange(event); + }); } 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; @@ -2026,19 +2029,24 @@ public boolean traverse(int event){ return super.traverse(event); } +private void handleCComboDPIChange(Event event) { + updateAndRefreshChildren(childWidget -> { + childWidget.notifyListeners(SWT.ZoomChanged, event); + }); +} + /** * The method accepts a combo and a callback which takes * all the child of the CCombo as the argument and executes it. * All children are refreshed after the execution of the callback. * - * @noreference This method is not intended to be referenced by clients. * @param combo the Combo to get the children widget from * @param childUpdater the callback which works with the child widgets */ -public static void updateAndRefreshChildren(CCombo combo, Consumer childUpdater) { - childUpdater.accept(combo.text); - childUpdater.accept(combo.list); - childUpdater.accept(combo.arrow); - childUpdater.accept(combo.popup); +private void updateAndRefreshChildren(Consumer childUpdater) { + childUpdater.accept(text); + childUpdater.accept(list); + childUpdater.accept(arrow); + childUpdater.accept(popup); } } 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 0a052957681..a548c80de21 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 @@ -19,7 +19,6 @@ import java.util.*; -import java.util.function.*; import java.util.stream.*; import org.eclipse.swt.*; @@ -759,6 +758,9 @@ public StyledText(Composite parent, int style) { initializeAccessible(); setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this)); if (IS_MAC) setData(STYLEDTEXT_KEY); + addListener(SWT.ZoomChanged, event -> { + handleStyledTextDPIChange(event); + }); } /** * Adds an extended modify listener. An ExtendedModify event is sent by the @@ -10901,30 +10903,20 @@ void updateSelection(int startOffset, int replacedLength, int newLength) { setCaretLocations(); } -/** - * The method accepts a StyledText and a callback which takes - * all the carets of the StyledText as the argument and executes it. - * The caret is refreshed after the execution of the callback. - * - * @param styledText the StyledText to get the carets from - * @param caretUpdater the callback which works with the carets - * - * @noreference This method is not intended to be referenced by clients. - */ -public static void updateAndRefreshCarets(StyledText styledText, Consumer caretUpdater) { +private void handleStyledTextDPIChange(Event event) { + updateCaretVisibility(); + setCaretLocations(); Set caretSet = new HashSet<>(); - caretSet.add(styledText.getCaret()); - caretSet.add(styledText.defaultCaret); - if (styledText.carets != null) { - for (Caret caret : styledText.carets) { + caretSet.add(getCaret()); + caretSet.add(defaultCaret); + if (carets != null) { + for (Caret caret : carets) { caretSet.add(caret); } } - caretSet.stream().filter(Objects::nonNull).forEach(caretUpdater); - - styledText.updateCaretVisibility(); - styledText.setCaretLocations(); - + caretSet.stream().filter(Objects::nonNull).forEach(caretToRefresh -> { + ((Caret) caretToRefresh).notifyListeners(SWT.ZoomChanged, event); + }); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/DPITestUtil.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/DPITestUtil.java index c278fc9bef7..ca2c1652969 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/DPITestUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/DPITestUtil.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.internal; +import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public final class DPITestUtil { @@ -22,8 +23,12 @@ private DPITestUtil() { public static void changeDPIZoom (Shell shell, int nativeZoom) { DPIUtil.setDeviceZoom(nativeZoom); - float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom); - DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor); + Event event = new Event(); + event.type = SWT.ZoomChanged; + event.widget = shell; + event.detail = nativeZoom; + event.doit = true; + shell.notifyListeners(SWT.ZoomChanged, event); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/Item.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/Item.java index 386a51a44fb..cbf9c0d815f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/Item.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/Item.java @@ -75,6 +75,9 @@ public abstract class Item extends Widget { public Item (Widget parent, int style) { super (parent, style); text = ""; + this.addListener(SWT.ZoomChanged, event -> { + handleItemDPIChange(event); + }); } /** @@ -224,4 +227,13 @@ boolean updateTextDirection(int textDirection) { return textDirection == AUTO_TEXT_DIRECTION; } + +private void handleItemDPIChange(Event event) { + // Refresh the image + Image image = getImage(); + if (image != null) { + setImage(image); + } +} + } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/CommonWidgetsDPIChangeHandlers.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/CommonWidgetsDPIChangeHandlers.java deleted file mode 100644 index 538def9e7cd..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/CommonWidgetsDPIChangeHandlers.java +++ /dev/null @@ -1,67 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 Yatta Solutions and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Yatta Solutions - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.internal; - -import org.eclipse.swt.custom.*; -import org.eclipse.swt.graphics.*; -import org.eclipse.swt.widgets.*; - -/** - * This class is used in the win32 implementation only to support - * adjusting widgets in the common package to DPI changes - *

- * IMPORTANT: This class is not part of the public - * API for SWT. It is marked public only so that it can be shared - * within the packages provided by SWT. It is not available on all - * platforms, and should never be called from application code. - *

- * @noreference This class is not intended to be referenced by clients - */ -public class CommonWidgetsDPIChangeHandlers { - - public static void registerCommonHandlers() { - DPIZoomChangeRegistry.registerHandler(CommonWidgetsDPIChangeHandlers::handleItemDPIChange, Item.class); - DPIZoomChangeRegistry.registerHandler(CommonWidgetsDPIChangeHandlers::handleStyledTextDPIChange, StyledText.class); - DPIZoomChangeRegistry.registerHandler(CommonWidgetsDPIChangeHandlers::handleCComboDPIChange, CCombo.class); - } - - private static void handleItemDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Item item)) { - return; - } - // Refresh the image - Image image = item.getImage(); - if (image != null) { - item.setImage(image); - } - } - - private static void handleStyledTextDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof StyledText styledText)) { - return; - } - - - StyledText.updateAndRefreshCarets(styledText, caretToRefresh -> { - DPIZoomChangeRegistry.applyChange(caretToRefresh, newZoom, scalingFactor); - Caret.win32_setHeight(caretToRefresh, styledText.getLineHeight()); - }); - } - private static void handleCComboDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof CCombo combo)) { - return; - } - CCombo.updateAndRefreshChildren(combo, childWidget -> DPIZoomChangeRegistry.applyChange(childWidget, newZoom, scalingFactor)); - } -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeHandler.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeHandler.java deleted file mode 100644 index ff406d0e040..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeHandler.java +++ /dev/null @@ -1,21 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 Yatta Solutions and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Yatta Solutions - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.internal; - -import org.eclipse.swt.widgets.*; - -@FunctionalInterface -public interface DPIZoomChangeHandler { - public void handleDPIChange(Widget widget, int newZoom, float scalingFactor); -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeRegistry.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeRegistry.java deleted file mode 100644 index 2547860fe91..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DPIZoomChangeRegistry.java +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 Yatta Solutions and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Yatta Solutions - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.internal; - -import java.util.*; -import java.util.Map.*; -import java.util.concurrent.*; - -import org.eclipse.swt.*; -import org.eclipse.swt.widgets.*; - -public class DPIZoomChangeRegistry { - - private static Map, DPIZoomChangeHandler> dpiZoomChangeHandlers = new ConcurrentSkipListMap<>( - (o1, o2) -> { - if(o1.isAssignableFrom(o2)) { - return -1; - } - if(o2.isAssignableFrom(o1)) { - return 1; - } - return o1.getName().compareTo(o2.getName()); - }); - - /** - * Calling this method will propagate the zoom change to all registered handlers that are responsible for the - * class of the provided widget or one of its super classes or interfaces. Usually there will be multiple handlers - * called per widget. To have a reliable and consistent execution order, the handler responsible for the most - * general class in the class hierarchy is called first, e.g. if a {@code Composite} is updated, the handlers are - * executed like ({@code Widget} -> {@code Control} -> {@code Scrollable} -> {@code Composite}). Each handler - * should only take care to update the attributes the class, it is registered for, adds to the hierarchy. - * - * @param widget widget the zoom change shall be applied to - * @param newZoom zoom in % of the standard resolution to be applied to the widget - * @param scalingFactor factor as division between new zoom and old zoom, e.g. 1.5 for a scaling from 100% to 150% - */ - public static void applyChange(Widget widget, int newZoom, float scalingFactor) { - if (widget == null) { - return; - } - for (Entry, DPIZoomChangeHandler> entry : dpiZoomChangeHandlers.entrySet()) { - Class clazz = entry.getKey(); - DPIZoomChangeHandler handler = entry.getValue(); - if (clazz.isInstance(widget)) { - try { - handler.handleDPIChange(widget, newZoom, scalingFactor); - } catch (RuntimeException ex) { - widget.getDisplay().getRuntimeExceptionHandler().accept(ex); - } - } - } - 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 clazzToRegisterFor) { - dpiZoomChangeHandlers.put(clazzToRegisterFor, zoomChangeVisitor); - } -} \ No newline at end of file diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java index f0c0180359b..e29a082cdbd 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java @@ -65,8 +65,6 @@ public class Button extends Control { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ButtonClass, lpWndClass); ButtonProc = lpWndClass.lpfnWndProc; - - DPIZoomChangeRegistry.registerHandler(Button::handleDPIChange, Button.class); } /** @@ -1561,16 +1559,15 @@ LRESULT wmDrawChild (long wParam, long lParam) { return null; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Button button)) { - return; - } +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); //Refresh the CheckSize - button.refreshCheckSize(newZoom); + refreshCheckSize(event.detail); // Refresh the image - if (button.image != null) { - button._setImage(button.image); - button.updateImageList(); + if (image != null) { + _setImage(image); + updateImageList(); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java index 1a5746e7478..29d365c782e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java @@ -49,10 +49,6 @@ public class Caret extends Widget { Font font; LOGFONT oldFont; -static { - DPIZoomChangeRegistry.registerHandler(Caret::handleDPIChange, Caret.class); -} - /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -669,19 +665,18 @@ public static void win32_setHeight(Caret caret, int height) { if(caret.isVisible && caret.hasFocus()) caret.resize(); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Caret caret)) { - return; - } - - Image image = caret.getImage(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Image image = getImage(); if (image != null) { - caret.setImage(image); + setImage(image); } - if (caret.font != null) { - caret.setFont(caret.font); + if (font != null) { + setFont(font); } + resize(); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java index 1c9a0657202..f6aba97c1f4 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java @@ -104,7 +104,6 @@ public class Combo extends Composite { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ComboClass, lpWndClass); ComboProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Combo::handleDPIChange, Combo.class); } /* Undocumented values. Remained the same at least between Win7 and Win10 */ @@ -3359,13 +3358,12 @@ LRESULT wmSysKeyDown (long hwnd, long wParam, long lParam) { return result; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Combo combo)) { - return; - } - if ((combo.style & SWT.H_SCROLL) != 0) { - combo.scrollWidth = 0; - combo.setScrollWidth(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + if ((style & SWT.H_SCROLL) != 0) { + scrollWidth = 0; + setScrollWidth(); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java index 32c3bfc0f26..795420ceebb 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java @@ -57,10 +57,6 @@ public class Composite extends Scrollable { static final int TOOLTIP_LIMIT = 4096; - static { - DPIZoomChangeRegistry.registerHandler(Composite::handleDPIChange, Composite.class); - } - /** * Prevents uninitialized instances from being created outside the package. */ @@ -1977,13 +1973,12 @@ public String toString() { return super.toString() + " [layout=" + layout + "]"; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Composite composite)) { - return; - } - for (Control child : composite.getChildren()) { - DPIZoomChangeRegistry.applyChange(child, newZoom, scalingFactor); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + for (Control child : getChildren()) { + child.notifyListeners(SWT.ZoomChanged, event); } - composite.redrawInPixels (null, true); +// redrawInPixels (null, true); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java index be5a77eb33e..94bbffe95ba 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java @@ -16,6 +16,7 @@ import java.util.*; +import java.util.concurrent.atomic.*; import java.util.stream.*; import org.eclipse.swt.*; @@ -54,10 +55,6 @@ */ public abstract class Control extends Widget implements Drawable { - static { - DPIZoomChangeRegistry.registerHandler(Control::handleDPIChange, Control.class); - } - /** * the handle to the OS resource * (Warning: This field is platform dependent) @@ -4760,10 +4757,9 @@ public boolean setParent (Composite parent) { this.parent = parent; // If parent changed, zoom level might need to be adjusted if (parent.nativeZoom != nativeZoom) { - int oldZoom = nativeZoom; int newZoom = parent.nativeZoom; - float scalingFactor = 1f * newZoom / oldZoom; - DPIZoomChangeRegistry.applyChange(this, newZoom, scalingFactor); + Event zoomChangedEvent = createZoomChangedEvent(newZoom); + notifyListeners(SWT.ZoomChanged, zoomChangedEvent); } int flags = OS.SWP_NOSIZE | OS.SWP_NOMOVE | OS.SWP_NOACTIVATE; OS.SetWindowPos (topHandle, OS.HWND_BOTTOM, 0, 0, 0, 0, flags); @@ -4957,12 +4953,22 @@ LRESULT WM_DESTROY (long wParam, long lParam) { } void handleMonitorSpecificDpiChange(int newNativeZoom, Rectangle newBoundsInPixels) { - float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(newNativeZoom) / DPIUtil.getZoomForAutoscaleProperty(nativeZoom); DPIUtil.setDeviceZoom (newNativeZoom); - DPIZoomChangeRegistry.applyChange(this, newNativeZoom, scalingFactor); + Event zoomChangedEvent = createZoomChangedEvent(newNativeZoom); + notifyListeners(SWT.ZoomChanged, zoomChangedEvent); this.setBoundsInPixels(newBoundsInPixels.x, newBoundsInPixels.y, newBoundsInPixels.width, newBoundsInPixels.height); } +private Event createZoomChangedEvent(int zoom) { + Event event = new Event(); + event.type = SWT.ZoomChanged; + event.widget = this; + event.detail = DPIUtil.getZoomForAutoscaleProperty(zoom); + event.doit = true; + event.data = new AtomicInteger(0); + return event; +} + LRESULT WM_DPICHANGED (long wParam, long lParam) { // Map DPI to Zoom and compare int newNativeZoom = DPIUtil.mapDPIToZoom (OS.HIWORD (wParam)); @@ -5874,22 +5880,21 @@ LRESULT wmScrollChild (long wParam, long lParam) { } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Control control)) { - return; - } - resizeFont(control, control.getNativeZoom()); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + resizeFont(this, getNativeZoom()); - Image image = control.backgroundImage; + Image image = backgroundImage; if (image != null) { if (image.isDisposed()) { - control.setBackgroundImage(null); + setBackgroundImage(null); } else { - control.setBackgroundImage(image); + setBackgroundImage(image); } } - if (control.getRegion() != null) { - control.setRegion(control.getRegion()); + if (getRegion() != null) { + setRegion(getRegion()); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java index 3c5e4154162..eae3402bec3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java @@ -58,7 +58,6 @@ public class CoolBar extends Composite { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ReBarClass, lpWndClass); ReBarProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(CoolBar::handleDPIChange, CoolBar.class); } static final int SEPARATOR_WIDTH = 2; static final int MAX_WIDTH = 0x7FFF; @@ -1201,18 +1200,17 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { return super.wmNotifyChild (hdr, wParam, lParam); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof CoolBar coolBar)) { - return; - } - Point[] sizes = coolBar.getItemSizesInPixels(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event,scalingFactor); + Point[] sizes = getItemSizesInPixels(); Point[] scaledSizes = new Point[sizes.length]; Point[] prefSizes = new Point[sizes.length]; Point[] minSizes = new Point[sizes.length]; - int[] indices = coolBar.getWrapIndices(); - int[] itemOrder = coolBar.getItemOrder(); + int[] indices = getWrapIndices(); + int[] itemOrder = getItemOrder(); - CoolItem[] items = coolBar.getItems(); + CoolItem[] items = getItems(); for (int index = 0; index < sizes.length; index++) { minSizes[index] = items[index].getMinimumSizeInPixels(); prefSizes[index] = items[index].getPreferredSizeInPixels(); @@ -1223,24 +1221,24 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac Control control = item.control; if (control != null) { - DPIZoomChangeRegistry.applyChange(control, newZoom, scalingFactor); + control.notifyListeners(SWT.ZoomChanged, event); item.setControl(control); } Point preferredControlSize = item.getControl().computeSizeInPixels(SWT.DEFAULT, SWT.DEFAULT, true); int controlWidth = preferredControlSize.x; int controlHeight = preferredControlSize.y; - if (((coolBar.style & SWT.VERTICAL) != 0)) { - scaledSizes[index] = new Point(Math.round((sizes[index].x)*scalingFactor), Math.max(Math.round((sizes[index].y)*scalingFactor),0)); - item.setMinimumSizeInPixels(Math.round(minSizes[index].x*scalingFactor), Math.max(Math.round((minSizes[index].y)*scalingFactor),controlWidth)); - item.setPreferredSizeInPixels(Math.round(prefSizes[index].x*scalingFactor), Math.max(Math.round((prefSizes[index].y)*scalingFactor),controlWidth)); + if (((style & SWT.VERTICAL) != 0)) { + scaledSizes[index] = new Point(Math.round((sizes[index].x) * scalingFactor), Math.max(Math.round((sizes[index].y) * scalingFactor),0)); + item.setMinimumSizeInPixels(Math.round(minSizes[index].x * scalingFactor), Math.max(Math.round((minSizes[index].y) * scalingFactor),controlWidth)); + item.setPreferredSizeInPixels(Math.round(prefSizes[index].x * scalingFactor), Math.max(Math.round((prefSizes[index].y) * scalingFactor),controlWidth)); } else { - scaledSizes[index] = new Point(Math.round((sizes[index].x)*scalingFactor),Math.max(Math.round((sizes[index].y)*scalingFactor),0)); - item.setMinimumSizeInPixels(Math.round(minSizes[index].x*scalingFactor), controlHeight); - item.setPreferredSizeInPixels(Math.round(prefSizes[index].x*scalingFactor), controlHeight); + scaledSizes[index] = new Point(Math.round((sizes[index].x) * scalingFactor),Math.max(Math.round((sizes[index].y) * scalingFactor),0)); + item.setMinimumSizeInPixels(Math.round(minSizes[index].x * scalingFactor), controlHeight); + item.setPreferredSizeInPixels(Math.round(prefSizes[index].x * scalingFactor), controlHeight); } } - coolBar.setItemLayoutInPixels(itemOrder, indices, scaledSizes); - coolBar.updateLayout(true); + setItemLayoutInPixels(itemOrder, indices, scaledSizes); + updateLayout(true); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java index a97697a7a02..14017dd82d6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java @@ -16,7 +16,6 @@ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; -import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.win32.*; /** @@ -112,10 +111,6 @@ public class Decorations extends Canvas { int oldWidth = OS.CW_USEDEFAULT, oldHeight = OS.CW_USEDEFAULT; RECT maxRect = new RECT(); - static { - DPIZoomChangeRegistry.registerHandler(Decorations::handleDPIChange, Decorations.class); - } - /** * Prevents uninitialized instances from being created outside the package. */ @@ -1700,26 +1695,29 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) { return result; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Decorations decorations)) { - return; - } - - Image image = decorations.getImage(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Image image = getImage(); if (image != null) { - decorations.setImage(image); + setImage(image); } - Image[] images = decorations.getImages(); + Image[] images = getImages(); if (images != null && images.length > 0) { - decorations.setImages(images); + setImages(images); } - DPIZoomChangeRegistry.applyChange(decorations.getMenuBar(), newZoom, scalingFactor); + Menu menuBar = getMenuBar(); + if(menuBar != null) { + menuBar.notifyListeners(SWT.ZoomChanged, event); + } - if (decorations.menus != null) { - for (Menu menu : decorations.menus) { - DPIZoomChangeRegistry.applyChange(menu, newZoom, scalingFactor); + if (menus != null) { + for (Menu menu : menus) { + if(menu != null) { + menu.notifyListeners(SWT.ZoomChanged, event); + } } } } 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 9d316fe277a..af89032cf53 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 @@ -559,11 +559,6 @@ private static int retrieveDefaultIconSize() { }; } - static { - CommonWidgetsDPIChangeHandlers.registerCommonHandlers(); - } - - /* * TEMPORARY CODE. */ diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java index d7511ea77ce..06a5b5d9881 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java @@ -55,10 +55,6 @@ public class ExpandBar extends Composite { int yCurrentScroll; long hFont; - static { - DPIZoomChangeRegistry.registerHandler(ExpandBar::handleDPIChange, ExpandBar.class); - } - /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -871,14 +867,13 @@ LRESULT wmScroll (ScrollBar bar, boolean update, long hwnd, int msg, long wParam return result; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof ExpandBar expandBar)) { - return; - } - for (ExpandItem item : expandBar.getItems()) { - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + for (ExpandItem item : getItems()) { + item.notifyListeners(SWT.ZoomChanged, event); } - expandBar.layoutItems(0, true); - expandBar.redraw(); + layoutItems(0, true); + redraw(); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandItem.java index 869b43cbf0e..3bd4cd2f94b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandItem.java @@ -48,10 +48,6 @@ public class ExpandItem extends Item { static final int BORDER = 1; static final int CHEVRON_SIZE = 24; - static { - DPIZoomChangeRegistry.registerHandler(ExpandItem::handleDPIChange, ExpandItem.class); - } - /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -532,14 +528,13 @@ public void setText (String string) { redraw (true); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof ExpandItem item)) { - return; - } - if (item.height != 0 || item.width != 0) { - int newWidth = Math.round(item.width * scalingFactor); - int newHeight = Math.round(item.height * scalingFactor); - item.setBoundsInPixels(item.x, item.y, newWidth, newHeight, true, true); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + if (height != 0 || width != 0) { + int newWidth = Math.round(width * scalingFactor); + int newHeight = Math.round(height * scalingFactor); + setBoundsInPixels(x, y, newWidth, newHeight, true, true); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java index f23863896fb..bcad38c8a9b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java @@ -63,7 +63,6 @@ public class Label extends Control { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, LabelClass, lpWndClass); LabelProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Label::handleDPIChange, Label.class); } /** @@ -621,13 +620,12 @@ else if (isImageMode) return null; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Label label)) { - return; - } - Image image = label.getImage(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Image image = getImage(); if (image != null) { - label.setImage(image); + setImage(image); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/List.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/List.java index 0297d320214..a4655377978 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/List.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/List.java @@ -50,7 +50,6 @@ public class List extends Scrollable { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ListClass, lpWndClass); ListProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(List::handleDPIChange, List.class); } /** @@ -1852,13 +1851,12 @@ LRESULT wmCommandChild (long wParam, long lParam) { return super.wmCommandChild (wParam, lParam); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof List list)) { - return; - } - if((list.style & SWT.H_SCROLL) != 0) { +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + if((style & SWT.H_SCROLL) != 0) { // Recalculate the Scroll width, as length of items has changed - list.setScrollWidth(); + setScrollWidth(); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java index 3a6428e4a58..b1c73d9330b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java @@ -17,7 +17,6 @@ import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; -import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.win32.*; /** @@ -69,10 +68,6 @@ public class Menu extends Widget { /* Timer ID for MenuItem ToolTip */ static final int ID_TOOLTIP_TIMER = 110; - static { - DPIZoomChangeRegistry.registerHandler(Menu::handleDPIChange, Menu.class); - } - /** * Constructs a new instance of this class given its parent, * and sets the style for the instance so that the instance @@ -1367,12 +1362,11 @@ LRESULT wmTimer (long wParam, long lParam) { return null; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Menu menu)) { - return; - } - for (MenuItem item : menu.getItems()) { - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + for (MenuItem item : getItems()) { + item.notifyListeners(SWT.ZoomChanged, event); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java index 50c6e835561..7c5e3488d9f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java @@ -61,10 +61,6 @@ public class MenuItem extends Item { private final static int CUSTOM_SELECTION_IMAGE = (OsVersion.IS_WIN11_21H2) ? Integer.getInteger("org.eclipse.swt.internal.win32.menu.customSelectionImage", 2) : 0; - static { - DPIZoomChangeRegistry.registerHandler(MenuItem::handleDPIChange, MenuItem.class); - } - /** * Constructs a new instance of this class given its parent * (which must be a Menu) and a style value @@ -1464,18 +1460,17 @@ long hwndToolTip() { } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof MenuItem menuItem)) { - return; - } +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); // Refresh the image(s) - if (menuItem.getImage() != null) { - ((MenuItem)menuItem).updateImage(); + if (getImage() != null) { + updateImage(); } // Refresh the sub menu - Menu subMenu = menuItem.getMenu(); + Menu subMenu = getMenu(); if (subMenu != null) { - DPIZoomChangeRegistry.applyChange(subMenu, newZoom, scalingFactor); + subMenu.notifyListeners(SWT.ZoomChanged, event); } } } 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 da1e195fa23..8a32b4592da 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 @@ -148,7 +148,6 @@ public class Shell extends Decorations { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, DialogClass, lpWndClass); DialogProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Shell::handleDPIChange, Shell.class); } /** @@ -303,6 +302,7 @@ public Shell (Display display, int style) { reskinWidget(); createWidget (); this.nativeZoom = DPIUtil.mapDPIToZoom(OS.GetDpiForWindow(this.handle)); + registerDPIChangeListener(); } /** @@ -2719,10 +2719,9 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) { return result; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Shell shell)) { - return; - } - shell.layout (null, SWT.DEFER | SWT.ALL | SWT.CHANGED); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + layout (null, SWT.DEFER | SWT.ALL | SWT.CHANGED); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java index 6c9dc3e4339..40811f9bcab 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java @@ -90,7 +90,6 @@ public class TabFolder extends Composite { lpWndClass.hInstance = OS.GetModuleHandle (null); lpWndClass.style &= ~(OS.CS_HREDRAW | OS.CS_VREDRAW | OS.CS_GLOBALCLASS); OS.RegisterClass (TabFolderClass, lpWndClass); - DPIZoomChangeRegistry.registerHandler(TabFolder::handleDPIChange, TabFolder.class); } /** @@ -1128,18 +1127,17 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { return super.wmNotifyChild (hdr, wParam, lParam); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof TabFolder tabFolder)) { - return; - } - Display display = tabFolder.getDisplay(); - if (tabFolder.imageList != null) { - display.releaseImageList (tabFolder.imageList); - tabFolder.imageList = null; +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Display display = getDisplay(); + if (imageList != null) { + display.releaseImageList (imageList); + imageList = null; } - for (int i = 0; i < tabFolder.getItemCount(); i++) { - DPIZoomChangeRegistry.applyChange(tabFolder.items[i], newZoom, scalingFactor); + for (int i = 0; i < getItemCount(); i++) { + items[i].notifyListeners(SWT.ZoomChanged, event); } - tabFolder.layout(true, true); + layout(true, true); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java index f3cf5c5fc0e..13718c3f7c7 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java @@ -113,7 +113,6 @@ public class Table extends Composite { TableProc = lpWndClass.lpfnWndProc; OS.GetClassInfo (0, HeaderClass, lpWndClass); HeaderProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Table::handleDPIChange, Table.class); } /** @@ -7334,49 +7333,48 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) { return null; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Table table)) { - return; - } - table.settingItemHeight = true; +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + settingItemHeight = true; var scrollWidth = 0; // Request ScrollWidth - if (table.getColumns().length == 0) { - scrollWidth = Math.round(OS.SendMessage (table.handle, OS.LVM_GETCOLUMNWIDTH, 0, 0)*scalingFactor); + if (getColumns().length == 0) { + scrollWidth = Math.round(OS.SendMessage (handle, OS.LVM_GETCOLUMNWIDTH, 0, 0) * scalingFactor); } - Display display = table.getDisplay(); - ImageList headerImageList = table.headerImageList; + Display display = getDisplay(); + ImageList headerImageList = this.headerImageList; // Reset ImageList if (headerImageList != null) { display.releaseImageList(headerImageList); - table.headerImageList = null; + headerImageList = null; } - ImageList imageList = table.imageList; + ImageList imageList = this.imageList; if (imageList != null) { display.releaseImageList(imageList); - table.imageList = null; + imageList = null; } // if the item height was set at least once programmatically with CDDS_SUBITEMPREPAINT, // the item height of the table is not managed by the OS anymore e.g. when the zoom // on the monitor is changed, the height of the item will stay at the fixed size. // Resetting it will re-enable the default behavior again - table.setItemHeight(-1); + setItemHeight(-1); - for (TableItem item : table.getItems()) { - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); + for (TableItem item : getItems()) { + item.notifyListeners(SWT.ZoomChanged, event); } - for (TableColumn tableColumn : table.getColumns()) { - DPIZoomChangeRegistry.applyChange(tableColumn, newZoom, scalingFactor); + for (TableColumn tableColumn : getColumns()) { + tableColumn.notifyListeners(SWT.ZoomChanged, event); } - if (table.getColumns().length == 0 && scrollWidth != 0) { + if (getColumns().length == 0 && scrollWidth != 0) { // Update scrollbar width if no columns are available - table.setScrollWidth(scrollWidth); + setScrollWidth(scrollWidth); } - table.fixCheckboxImageListColor (true); - table.settingItemHeight = false; + fixCheckboxImageListColor (true); + settingItemHeight = false; } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableColumn.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableColumn.java index b7fae937436..f24d6cb6014 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableColumn.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableColumn.java @@ -44,10 +44,6 @@ public class TableColumn extends Item { String toolTipText; int id; - static { - DPIZoomChangeRegistry.registerHandler(TableColumn::handleDPIChange, TableColumn.class); - } - /** * Constructs a new instance of this class given its parent * (which must be a Table) and a style value @@ -887,20 +883,19 @@ void updateToolTip (int index) { } } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof TableColumn tableColumn)) { - return; - } - Table table = tableColumn.getParent(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Table table = getParent(); boolean ignoreColumnResize = table.ignoreColumnResize; table.ignoreColumnResize = true; - final int newColumnWidth = Math.round(tableColumn.getWidthInPixels() * scalingFactor); - tableColumn.setWidthInPixels(newColumnWidth); + final int newColumnWidth = Math.round(getWidthInPixels() * scalingFactor); + setWidthInPixels(newColumnWidth); table.ignoreColumnResize = ignoreColumnResize; - Image image = tableColumn.getImage(); + Image image = getImage(); if (image != null) { - tableColumn.setImage(image); + setImage(image); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java index 2d2f01686d1..b738005e674 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java @@ -46,10 +46,6 @@ public class TableItem extends Item { int imageIndent, background = -1, foreground = -1; int [] cellBackground, cellForeground; - static { - DPIZoomChangeRegistry.registerHandler(TableItem::handleDPIChange, TableItem.class); - } - /** * Constructs a new instance of this class given its parent * (which must be a Table) and a style value @@ -1271,19 +1267,17 @@ public void setText (String string) { setText (0, string); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof TableItem tableItem)) { - return; - } - Font font = tableItem.font; +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); if (font != null) { - tableItem.setFont(tableItem.font); + setFont(font); } - Font[] cellFonts = tableItem.cellFont; + Font[] cellFonts = cellFont; if (cellFonts != null) { for (int index = 0; index < cellFonts.length; index++) { Font cellFont = cellFonts[index]; - cellFonts[index] = cellFont == null ? null : Font.win32_new(cellFont, tableItem.getNativeZoom()); + cellFonts[index] = cellFont == null ? null : Font.win32_new(cellFont, getNativeZoom()); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java index 537ca150583..d63743d0ea8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java @@ -112,7 +112,6 @@ public class Text extends Scrollable { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, EditClass, lpWndClass); EditProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Text::handleDPIChange, Text.class); } /** @@ -3149,10 +3148,9 @@ LRESULT wmKeyDown (long hwnd, long wParam, long lParam) { return result; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Text text)) { - return; - } - text.setMargins(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + setMargins(); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolBar.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolBar.java index 5d57e7b5463..12f140f94c5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolBar.java @@ -62,7 +62,6 @@ public class ToolBar extends Composite { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ToolBarClass, lpWndClass); ToolBarProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(ToolBar::handleDPIChange, ToolBar.class); } /* @@ -1746,11 +1745,10 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { return super.wmNotifyChild (hdr, wParam, lParam); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof ToolBar toolBar)) { - return; - } - ToolItem[] toolItems = toolBar._getItems(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + ToolItem[] toolItems = _getItems(); var seperatorWidth = new int[toolItems.length]; int itemCount = toolItems.length; @@ -1765,21 +1763,21 @@ record ToolItemData(ToolItem toolItem, TBBUTTON button) { Stack buttondata = new Stack<>(); for (int i = itemCount - 1; i >= 0; i--) { TBBUTTON lpButton = new TBBUTTON (); - OS.SendMessage (toolBar.handle, OS.TB_GETBUTTON, i, lpButton); + OS.SendMessage (handle, OS.TB_GETBUTTON, i, lpButton); ToolItem item = toolItems[i]; if ((item.style & SWT.SEPARATOR) != 0 && item.getControl() != null) { // Take note of widths of separators with control, so they can be resized // at the end seperatorWidth[i] = item.getWidth(); } - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); + item.notifyListeners(SWT.ZoomChanged, event); buttondata.push(new ToolItemData(item, lpButton)); - OS.SendMessage(toolBar.handle, OS.TB_DELETEBUTTON, i, 0); + OS.SendMessage(handle, OS.TB_DELETEBUTTON, i, 0); } - OS.SendMessage(toolBar.handle, OS.TB_BUTTONSTRUCTSIZE, TBBUTTON.sizeof, 0); + OS.SendMessage(handle, OS.TB_BUTTONSTRUCTSIZE, TBBUTTON.sizeof, 0); while (!buttondata.isEmpty()) { ToolItemData itemData = buttondata.pop(); - OS.SendMessage(toolBar.handle, OS.TB_ADDBUTTONS, 1, itemData.button); + OS.SendMessage(handle, OS.TB_ADDBUTTONS, 1, itemData.button); ToolItem item = itemData.toolItem; if (item != null) { // The text is not retained correctly, so we need to reset it @@ -1800,10 +1798,10 @@ record ToolItemData(ToolItem toolItem, TBBUTTON button) { } // Refresh the image lists so the image list for the correct zoom is used - toolBar.setImageList(toolBar.getImageList()); - toolBar.setDisabledImageList(toolBar.getDisabledImageList()); - toolBar.setHotImageList(toolBar.getHotImageList()); - OS.SendMessage(toolBar.handle, OS.TB_AUTOSIZE, 0, 0); - toolBar.layout(true); + setImageList(getImageList()); + setDisabledImageList(getDisabledImageList()); + setHotImageList(getHotImageList()); + OS.SendMessage(handle, OS.TB_AUTOSIZE, 0, 0); + layout(true); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java index 14cc9c49f7c..263f36f0e8a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java @@ -132,7 +132,6 @@ public class Tree extends Composite { TreeProc = lpWndClass.lpfnWndProc; OS.GetClassInfo (0, HeaderClass, lpWndClass); HeaderProc = lpWndClass.lpfnWndProc; - DPIZoomChangeRegistry.registerHandler(Tree::handleDPIChange, Tree.class); } /** @@ -8292,40 +8291,39 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) { return null; } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof Tree tree)) { - return; - } - Display display = tree.getDisplay(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Display display = getDisplay(); // Reset ImageList - if (tree.headerImageList != null) { - display.releaseImageList(tree.headerImageList); - tree.headerImageList = null; + if (headerImageList != null) { + display.releaseImageList(headerImageList); + headerImageList = null; } - if (tree.imageList != null) { - display.releaseImageList(tree.imageList); + if (imageList != null) { + display.releaseImageList(imageList); // Reset the Imagelist of the OS as well; Will be recalculated when updating items - OS.SendMessage (tree.handle, OS.TVM_SETIMAGELIST, 0, 0); - tree.imageList = null; + OS.SendMessage (handle, OS.TVM_SETIMAGELIST, 0, 0); + imageList = null; } // if the item height was set at least once programmatically with TVM_SETITEMHEIGHT, // the item height of the tree is not managed by the OS anymore e.g. when the zoom // on the monitor is changed, the height of the item will stay at the fixed size. // Resetting it will re-enable the default behavior again - tree.setItemHeight(-1); + setItemHeight(-1); - for (TreeColumn treeColumn : tree.getColumns()) { - DPIZoomChangeRegistry.applyChange(treeColumn, newZoom, scalingFactor); + for (TreeColumn treeColumn : getColumns()) { + treeColumn.notifyListeners(SWT.ZoomChanged, event); } - for (TreeItem item : tree.getItems()) { - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); + for (TreeItem item : getItems()) { + item.notifyListeners(SWT.ZoomChanged, event); } - tree.calculateAndApplyIndentSize(); - tree.updateOrientation(); - tree.setScrollWidth(); + calculateAndApplyIndentSize(); + updateOrientation(); + setScrollWidth(); // Reset of CheckBox Size required (if SWT.Check is not set, this is a no-op) - tree.setCheckboxImageList(); + setCheckboxImageList(); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeColumn.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeColumn.java index 0e66a543765..6d588e386f8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeColumn.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeColumn.java @@ -46,10 +46,6 @@ public class TreeColumn extends Item { String toolTipText; int id; - static { - DPIZoomChangeRegistry.registerHandler(TreeColumn::handleDPIChange, TreeColumn.class); - } - /** * Constructs a new instance of this class given its parent * (which must be a Tree) and a style value @@ -760,20 +756,18 @@ void updateToolTip (int index) { } } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof TreeColumn treeColumn)) { - return; - } - Tree tree = treeColumn.getParent(); +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); + Tree tree = getParent(); boolean ignoreColumnResize = tree.ignoreColumnResize; tree.ignoreColumnResize = true; - final int newColumnWidth = Math.round(treeColumn.getWidthInPixels() * scalingFactor); - treeColumn.setWidthInPixels(newColumnWidth); + final int newColumnWidth = Math.round(getWidthInPixels() * scalingFactor); + setWidthInPixels(newColumnWidth); tree.ignoreColumnResize = ignoreColumnResize; - Image image = treeColumn.image; if (image != null) { - treeColumn.setImage(image); + setImage(image); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java index 36158c18041..b08efee21bd 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java @@ -60,10 +60,6 @@ public class TreeItem extends Item { int background = -1, foreground = -1; int [] cellBackground, cellForeground; - static { - DPIZoomChangeRegistry.registerHandler(TreeItem::handleDPIChange, TreeItem.class); - } - /** * Constructs TreeItem and inserts it into Tree. * Item is inserted as last direct child of the tree. @@ -1816,23 +1812,21 @@ String getNameText () { return super.getNameText (); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - if (!(widget instanceof TreeItem treeItem)) { - return; - } - Font font = treeItem.font; +@Override +void handleDPIChange(Event event, float scalingFactor) { + super.handleDPIChange(event, scalingFactor); if (font != null) { - treeItem.setFont(font); + setFont(font); } - Font[] cellFonts = treeItem.cellFont; + Font[] cellFonts = cellFont; if (cellFonts != null) { for (int index = 0; index < cellFonts.length; index++) { Font cellFont = cellFonts[index]; - cellFonts[index] = cellFont == null ? null : Font.win32_new(cellFont, treeItem.getNativeZoom()); + cellFonts[index] = cellFont == null ? null : Font.win32_new(cellFont, getNativeZoom()); } } - for (TreeItem item : treeItem.getItems()) { - DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor); + for (TreeItem item : getItems()) { + item.notifyListeners(SWT.ZoomChanged, event); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java index 6297100ff16..6ea335cc947 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java @@ -142,7 +142,6 @@ public abstract class Widget { icce.dwSize = INITCOMMONCONTROLSEX.sizeof; icce.dwICC = 0xffff; OS.InitCommonControlsEx (icce); - DPIZoomChangeRegistry.registerHandler(Widget::handleDPIChange, Widget.class); } /** @@ -191,6 +190,14 @@ public Widget (Widget parent, int style) { reskinWidget (); notifyCreationTracker(); this.setData(DATA_NATIVE_ZOOM, this.nativeZoom); + registerDPIChangeListener(); +} + +void registerDPIChangeListener() { + this.addListener(SWT.ZoomChanged, event -> { + float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(event.detail) / DPIUtil.getZoomForAutoscaleProperty(nativeZoom); + handleDPIChange(event, scalingFactor); + }); } void _addListener (int eventType, Listener listener) { @@ -2717,9 +2724,10 @@ int getZoom() { return DPIUtil.getZoomForAutoscaleProperty(nativeZoom); } -private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) { - widget.nativeZoom = newZoom; - widget.setData(DATA_NATIVE_ZOOM, newZoom); +void handleDPIChange(Event event, float scalingFactor) { + int newZoom = event.detail; + this.nativeZoom = newZoom; + this.setData(DATA_NATIVE_ZOOM, newZoom); } int getSystemMetrics(int nIndex) {