Skip to content

Commit 70e8d3f

Browse files
committed
Design improvement for scaling win32 widgets #62
This commit improves thes design implementation for widgets in win32 by moving away from DPIZoomChangeRegistry to an event-driven design using ZoomChanged event and inheritance. contributes to #62 and #128
1 parent ec3d589 commit 70e8d3f

File tree

32 files changed

+273
-491
lines changed

32 files changed

+273
-491
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public CCombo (Composite parent, int style) {
173173
}
174174

175175
initAccessible();
176+
addListener(SWT.ZoomChanged, this::handleDPIChange);
176177
}
177178
static int checkStyle (int style) {
178179
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 +2027,24 @@ public boolean traverse(int event){
20262027
return super.traverse(event);
20272028
}
20282029

2030+
private void handleDPIChange(Event event) {
2031+
updateAndRefreshChildren(childWidget -> {
2032+
childWidget.notifyListeners(SWT.ZoomChanged, event);
2033+
});
2034+
}
2035+
20292036
/**
20302037
* The method accepts a combo and a callback which takes
20312038
* all the child of the CCombo as the argument and executes it.
20322039
* All children are refreshed after the execution of the callback.
20332040
*
2034-
* @noreference This method is not intended to be referenced by clients.
20352041
* @param combo the Combo to get the children widget from
20362042
* @param childUpdater the callback which works with the child widgets
20372043
*/
2038-
public static void updateAndRefreshChildren(CCombo combo, Consumer<Widget> childUpdater) {
2039-
childUpdater.accept(combo.text);
2040-
childUpdater.accept(combo.list);
2041-
childUpdater.accept(combo.arrow);
2042-
childUpdater.accept(combo.popup);
2044+
private void updateAndRefreshChildren(Consumer<Widget> childUpdater) {
2045+
childUpdater.accept(text);
2046+
childUpdater.accept(list);
2047+
childUpdater.accept(arrow);
2048+
childUpdater.accept(popup);
20432049
}
20442050
}

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020

2121
import java.util.*;
22-
import java.util.function.*;
2322
import java.util.stream.*;
2423

2524
import org.eclipse.swt.*;
@@ -759,6 +758,7 @@ public StyledText(Composite parent, int style) {
759758
initializeAccessible();
760759
setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this));
761760
if (IS_MAC) setData(STYLEDTEXT_KEY);
761+
addListener(SWT.ZoomChanged, this::handleDPIChange);
762762
}
763763
/**
764764
* Adds an extended modify listener. An ExtendedModify event is sent by the
@@ -10901,29 +10901,20 @@ void updateSelection(int startOffset, int replacedLength, int newLength) {
1090110901
setCaretLocations();
1090210902
}
1090310903

10904-
/**
10905-
* The method accepts a StyledText and a callback which takes
10906-
* all the carets of the StyledText as the argument and executes it.
10907-
* The caret is refreshed after the execution of the callback.
10908-
*
10909-
* @param styledText the StyledText to get the carets from
10910-
* @param caretUpdater the callback which works with the carets
10911-
*
10912-
* @noreference This method is not intended to be referenced by clients.
10913-
*/
10914-
public static void updateAndRefreshCarets(StyledText styledText, Consumer<Caret> caretUpdater) {
10915-
styledText.updateCaretVisibility();
10916-
styledText.setCaretLocations();
10904+
private void handleDPIChange(Event event) {
10905+
updateCaretVisibility();
10906+
setCaretLocations();
1091710907
Set<Caret> caretSet = new LinkedHashSet<>();
10918-
caretSet.add(styledText.defaultCaret);
10919-
caretSet.add(styledText.getCaret());
10920-
if (styledText.carets != null) {
10921-
for (Caret caret : styledText.carets) {
10908+
caretSet.add(defaultCaret);
10909+
caretSet.add(getCaret());
10910+
if (carets != null) {
10911+
for (Caret caret : carets) {
1092210912
caretSet.add(caret);
1092310913
}
1092410914
}
10925-
caretSet.stream().filter(Objects::nonNull).forEach(caretUpdater);
10926-
10915+
caretSet.stream().filter(Objects::nonNull).forEach(caretToRefresh -> {
10916+
caretToRefresh.notifyListeners(SWT.ZoomChanged, event);
10917+
});
1092710918
}
1092810919

1092910920
}

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

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

16+
import org.eclipse.swt.*;
1617
import org.eclipse.swt.widgets.*;
1718

1819
public final class DPITestUtil {
@@ -22,8 +23,12 @@ private DPITestUtil() {
2223

2324
public static void changeDPIZoom (Shell shell, int nativeZoom) {
2425
DPIUtil.setDeviceZoom(nativeZoom);
25-
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom);
26-
DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor);
26+
Event event = new Event();
27+
event.type = SWT.ZoomChanged;
28+
event.widget = shell;
29+
event.detail = nativeZoom;
30+
event.doit = true;
31+
shell.notifyListeners(SWT.ZoomChanged, event);
2732
}
2833

2934
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/Item.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public abstract class Item extends Widget {
7575
public Item (Widget parent, int style) {
7676
super (parent, style);
7777
text = "";
78+
this.addListener(SWT.ZoomChanged, this::handleDPIChange);
7879
}
7980

8081
/**
@@ -224,4 +225,13 @@ boolean updateTextDirection(int textDirection) {
224225
return textDirection == AUTO_TEXT_DIRECTION;
225226
}
226227

228+
229+
private void handleDPIChange(Event event) {
230+
// Refresh the image
231+
Image image = getImage();
232+
if (image != null) {
233+
setImage(image);
234+
}
235+
}
236+
227237
}

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 0 additions & 74 deletions
This file was deleted.

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public class Button extends Control {
6565
WNDCLASS lpWndClass = new WNDCLASS ();
6666
OS.GetClassInfo (0, ButtonClass, lpWndClass);
6767
ButtonProc = lpWndClass.lpfnWndProc;
68-
69-
DPIZoomChangeRegistry.registerHandler(Button::handleDPIChange, Button.class);
7068
}
7169

7270
/**
@@ -1561,16 +1559,15 @@ LRESULT wmDrawChild (long wParam, long lParam) {
15611559
return null;
15621560
}
15631561

1564-
private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) {
1565-
if (!(widget instanceof Button button)) {
1566-
return;
1567-
}
1562+
@Override
1563+
void handleDPIChange(Event event, float scalingFactor) {
1564+
super.handleDPIChange(event, scalingFactor);
15681565
//Refresh the CheckSize
1569-
button.refreshCheckSize(newZoom);
1566+
refreshCheckSize(event.detail);
15701567
// Refresh the image
1571-
if (button.image != null) {
1572-
button._setImage(button.image);
1573-
button.updateImageList();
1568+
if (image != null) {
1569+
_setImage(image);
1570+
updateImageList();
15741571
}
15751572
}
15761573
}

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public class Caret extends Widget {
5151
Font font;
5252
LOGFONT oldFont;
5353

54-
static {
55-
DPIZoomChangeRegistry.registerHandler(Caret::handleDPIChange, Caret.class);
56-
}
57-
5854
/**
5955
* Constructs a new instance of this class given its parent
6056
* and a style value describing its behavior and appearance.
@@ -658,20 +654,18 @@ public void setVisible (boolean visible) {
658654
}
659655
}
660656

661-
private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) {
662-
if (!(widget instanceof Caret caret)) {
663-
return;
664-
}
665-
666-
Image image = caret.getImage();
657+
@Override
658+
void handleDPIChange(Event event, float scalingFactor) {
659+
super.handleDPIChange(event, scalingFactor);
660+
Image image = getImage();
667661
if (image != null) {
668-
caret.setImage(image);
662+
setImage(image);
669663
}
670664

671-
if (caret.font != null) {
672-
caret.setFont(caret.font);
665+
if (font != null) {
666+
setFont(font);
673667
}
674-
if (caret.isVisible && caret.hasFocus ()) caret.resize();
668+
if (isVisible && hasFocus ()) resize();
675669
}
676670
}
677671

0 commit comments

Comments
 (0)