Skip to content

Commit 43f54cc

Browse files
amartya4256HeikoKlare
authored andcommitted
Design improvement for scaling win32 widgets
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. All the classes extending widget must override handleDPIChange method. The Widget class registers a listener for ZoomChanged event and Shell (Control) on getting a DPI_CHANGED event from the OS notifies its ZoomChanged listener as the root of the event which executes all the handleDPIChange methods for all the parent classes in hierarchial order and sending the event down in the widget tree vy notifying ZoomChanged listeners of other associated or children widgets.
1 parent 8444ec7 commit 43f54cc

File tree

33 files changed

+276
-499
lines changed

33 files changed

+276
-499
lines changed

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

Lines changed: 14 additions & 14 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,18 @@ public boolean traverse(int event){
20262027
return super.traverse(event);
20272028
}
20282029

2029-
/**
2030-
* The method accepts a combo and a callback which takes
2031-
* all the child of the CCombo as the argument and executes it.
2032-
* All children are refreshed after the execution of the callback.
2033-
*
2034-
* @noreference This method is not intended to be referenced by clients.
2035-
* @param combo the Combo to get the children widget from
2036-
* @param childUpdater the callback which works with the child widgets
2037-
*/
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);
2030+
private void handleDPIChange(Event event) {
2031+
if (text != null) {
2032+
text.notifyListeners(SWT.ZoomChanged, event);
2033+
}
2034+
if (list != null) {
2035+
list.notifyListeners(SWT.ZoomChanged, event);
2036+
}
2037+
if (arrow != null) {
2038+
arrow.notifyListeners(SWT.ZoomChanged, event);
2039+
}
2040+
if (popup != null) {
2041+
popup.notifyListeners(SWT.ZoomChanged, event);
2042+
}
20432043
}
20442044
}

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
@@ -10910,29 +10910,20 @@ void updateSelection(int startOffset, int replacedLength, int newLength) {
1091010910
setCaretLocations();
1091110911
}
1091210912

10913-
/**
10914-
* The method accepts a StyledText and a callback which takes
10915-
* all the carets of the StyledText as the argument and executes it.
10916-
* The caret is refreshed after the execution of the callback.
10917-
*
10918-
* @param styledText the StyledText to get the carets from
10919-
* @param caretUpdater the callback which works with the carets
10920-
*
10921-
* @noreference This method is not intended to be referenced by clients.
10922-
*/
10923-
public static void updateAndRefreshCarets(StyledText styledText, Consumer<Caret> caretUpdater) {
10924-
styledText.updateCaretVisibility();
10925-
styledText.setCaretLocations();
10913+
private void handleDPIChange(Event event) {
10914+
updateCaretVisibility();
10915+
setCaretLocations();
1092610916
Set<Caret> caretSet = new LinkedHashSet<>();
10927-
caretSet.add(styledText.defaultCaret);
10928-
caretSet.add(styledText.getCaret());
10929-
if (styledText.carets != null) {
10930-
for (Caret caret : styledText.carets) {
10917+
caretSet.add(defaultCaret);
10918+
caretSet.add(getCaret());
10919+
if (carets != null) {
10920+
for (Caret caret : carets) {
1093110921
caretSet.add(caret);
1093210922
}
1093310923
}
10934-
caretSet.stream().filter(Objects::nonNull).forEach(caretUpdater);
10935-
10924+
caretSet.stream().filter(Objects::nonNull).forEach(caretToRefresh -> {
10925+
caretToRefresh.notifyListeners(SWT.ZoomChanged, event);
10926+
});
1093610927
}
1093710928

1093810929
@Override

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/SWT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,10 @@ public class SWT {
10151015
* support dynamic DPI changes. This event is currently sent on Windows 10 and GTK
10161016
* only.
10171017
* </p>
1018+
* <p>
1019+
* Note: Event.detail: The new zoom value, e.g. 100, 125, 250, 175, etc. The
1020+
* Event.detail must not be modified by consumers.
1021+
* </p>
10181022
*
10191023
* @see org.eclipse.swt.widgets.Widget#addListener
10201024
* @see org.eclipse.swt.widgets.Display#addFilter

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: 8 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,16 @@ 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+
int newZoom = event.detail;
1567+
refreshCheckSize(newZoom);
15701568
// Refresh the image
1571-
if (button.image != null) {
1572-
button._setImage(button.image);
1573-
button.updateImageList();
1569+
if (image != null) {
1570+
_setImage(image);
1571+
updateImageList();
15741572
}
15751573
}
15761574
}

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)