Skip to content

Commit d858871

Browse files
committed
Added API to set Autoscale Disabled with better convenience
This commit adds an API to set Autoscaling Modes using the new enum AutoscalingMode with types Enabled, Disabled and Disabled_Inherited. Moreover, it also provides an API Shell#getNativeZoom to still make the consumer be able to obtain the actual zoom at a screen. These features are only enabled on win32 and will not perform any action on other platforms if called.
1 parent 383ed2a commit d858871

7 files changed

Lines changed: 161 additions & 10 deletions

File tree

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,25 @@ NSTouch findTouchWithId(NSArray touches, NSObject identity) {
35273527
return null;
35283528
}
35293529

3530+
/**
3531+
* Sets the autoscaling mode for this widget.
3532+
* The capability is not supported on every platform, such that calling this method may not have an effect on unsupported platforms. The return value indicates if the autoscale mode was set properly. With {@link #isAutoScalable()}, the autoscale enablement can also be evaluated at any later pointin time.
3533+
* <p>
3534+
* Currently, this is only supported on Windows.
3535+
* </p>
3536+
*
3537+
* @param autoscalingMode
3538+
* the autoscaling mode to request; this argument is accepted but
3539+
* ignored on this platform
3540+
*
3541+
* @return {@code false} if the operation was called on an unsupported platform
3542+
*
3543+
* @since 3.133
3544+
*/
3545+
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
3546+
return false;
3547+
}
3548+
35303549
void setBackground () {
35313550
if (!drawsBackground()) return;
35323551
Control control = findBackgroundControl ();

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Shell.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.swt.*;
1919
import org.eclipse.swt.events.*;
2020
import org.eclipse.swt.graphics.*;
21+
import org.eclipse.swt.internal.*;
2122
import org.eclipse.swt.internal.cocoa.*;
2223

2324
/**
@@ -1992,6 +1993,17 @@ public void setModified (boolean modified) {
19921993
window.setDocumentEdited (modified);
19931994
}
19941995

1996+
/**
1997+
* Returns the native zoom of the device which is used globally by SWT.
1998+
*
1999+
* @return the native zoom for the current device
2000+
*
2001+
* @since 3.133
2002+
*/
2003+
public int getNativeZoom() {
2004+
return DPIUtil.getNativeDeviceZoom();
2005+
}
2006+
19952007
/**
19962008
* Sets the shape of the shell to the region specified
19972009
* by the argument. When the argument is null, the
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.eclipse.swt.graphics;
2+
3+
4+
/**
5+
* Defines the autoscaling behavior used when rendering or computing layout
6+
* for widgets or graphics that support DPI‑aware scaling.
7+
* <p>
8+
* This mode determines whether autoscaling logic is applied, disabled, or
9+
* inherited from a parent component. It allows callers to explicitly control
10+
* how scaling should be handled in contexts where DPI, zoom factors, or
11+
* user accessibility settings may influence size computation.
12+
* </p>
13+
*
14+
* <ul>
15+
* <li><b>{@link #ENABLED}</b> – Autoscaling is explicitly enabled.
16+
* Values (such as coordinates, dimensions, or layout metrics) will be
17+
* adjusted according to the effective scaling factor.</li>
18+
*
19+
* <li><b>{@link #DISABLED}</b> – Autoscaling is explicitly disabled.
20+
* Values will be used as-is without applying any scaling
21+
* transformations.</li>
22+
*
23+
* <li><b>{@link #DISABLED_INHERITED}</b> – Autoscaling is disabled for this
24+
* component, but the decision originates from a parent or owner context.
25+
* This is typically used to indicate that the disabled state was not set
26+
* explicitly at this level but inherited from upstream configuration.</li>
27+
* </ul>
28+
*
29+
* @since 3.133
30+
*/
31+
32+
public enum AutoscalingMode {
33+
ENABLED,
34+
DISABLED,
35+
DISABLED_INHERITED
36+
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Control.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5143,6 +5143,25 @@ void gtk_label_set_align(long label, float xAlign, float yAlign) {
51435143
GTK.gtk_label_set_yalign(label, yAlign);
51445144
}
51455145

5146+
/**
5147+
* Sets the autoscaling mode for this widget.
5148+
* The capability is not supported on every platform, such that calling this method may not have an effect on unsupported platforms. The return value indicates if the autoscale mode was set properly. With {@link #isAutoScalable()}, the autoscale enablement can also be evaluated at any later pointin time.
5149+
* <p>
5150+
* Currently, this is only supported on Windows.
5151+
* </p>
5152+
*
5153+
* @param autoscalingMode
5154+
* the autoscaling mode to request; this argument is accepted but
5155+
* ignored on this platform
5156+
*
5157+
* @return {@code false} if the operation was called on an unsupported platform
5158+
*
5159+
* @since 3.133
5160+
*/
5161+
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
5162+
return false;
5163+
}
5164+
51465165
void setBackground () {
51475166
if ((state & BACKGROUND) == 0 && backgroundImage == null) {
51485167
if ((state & PARENT_BACKGROUND) != 0) {

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Shell.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,17 @@ public void setModified (boolean modified) {
27862786
this.modified = modified;
27872787
}
27882788

2789+
/**
2790+
* Returns the native zoom of the device which is used globally by SWT.
2791+
*
2792+
* @return the native zoom for the current device
2793+
*
2794+
* @since 3.133
2795+
*/
2796+
public int getNativeZoom() {
2797+
return DPIUtil.getNativeDeviceZoom();
2798+
}
2799+
27892800
/**
27902801
* Sets the shape of the shell to the region specified
27912802
* by the argument. When the argument is null, the

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

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public abstract class Control extends Widget implements Drawable {
7777
Region region;
7878
Font font;
7979
int drawCount, foreground, background, backgroundAlpha = 255;
80-
boolean autoScaleDisabled = false;
80+
AutoscalingMode autoscalingMode = AutoscalingMode.ENABLED;
8181

8282
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM";
8383

@@ -123,11 +123,11 @@ public abstract class Control extends Widget implements Drawable {
123123
public Control (Composite parent, int style) {
124124
super (parent, style);
125125
this.parent = parent;
126-
Boolean parentPropagateAutoscaleDisabled = (Boolean) parent.getData(PROPOGATE_AUTOSCALE_DISABLED);
127-
if (parentPropagateAutoscaleDisabled == null || parentPropagateAutoscaleDisabled) {
128-
this.autoScaleDisabled = parent.autoScaleDisabled;
126+
AutoscalingMode parentAutoscaleMode = parent.autoscalingMode;
127+
if (parentAutoscaleMode == AutoscalingMode.DISABLED_INHERITED) {
128+
this.autoscalingMode = parent.autoscalingMode;
129129
}
130-
if (!autoScaleDisabled) {
130+
if (isAutoScalable()) {
131131
this.nativeZoom = getShellZoom();
132132
}
133133
createWidget ();
@@ -1286,12 +1286,20 @@ public Object getData(String key) {
12861286
public void setData(String key, Object value) {
12871287
super.setData(key, value);
12881288
if (DATA_AUTOSCALE_DISABLED.equals(key)) {
1289-
autoScaleDisabled = Boolean.parseBoolean(value.toString());
1289+
boolean autoScaleDisabled = Boolean.parseBoolean(value.toString());
12901290
if (autoScaleDisabled) {
1291+
Object autoscaleDisablementValue = getData(PROPOGATE_AUTOSCALE_DISABLED);
1292+
boolean propagateAutoscaling = autoscaleDisablementValue != null && Boolean.parseBoolean(autoscaleDisablementValue.toString());
1293+
autoscalingMode = propagateAutoscaling ? AutoscalingMode.DISABLED_INHERITED : AutoscalingMode.DISABLED;
12911294
this.nativeZoom = 100;
12921295
} else {
1296+
autoscalingMode = AutoscalingMode.ENABLED;
12931297
this.nativeZoom = getShellZoom();
12941298
}
1299+
} else if (PROPOGATE_AUTOSCALE_DISABLED.equals(key)) {
1300+
boolean propagateAutoscaling = value != null && Boolean.parseBoolean(value.toString());
1301+
autoscalingMode = propagateAutoscaling ? AutoscalingMode.DISABLED_INHERITED : AutoscalingMode.DISABLED;
1302+
this.nativeZoom = 100;
12951303
}
12961304
}
12971305

@@ -1873,6 +1881,11 @@ boolean isActive () {
18731881
return shell.getEnabled ();
18741882
}
18751883

1884+
@Override
1885+
public boolean isAutoScalable() {
1886+
return autoscalingMode == AutoscalingMode.ENABLED;
1887+
}
1888+
18761889
/**
18771890
* Returns <code>true</code> if the receiver is enabled and all
18781891
* ancestors up to and including the receiver's nearest ancestor
@@ -3352,6 +3365,31 @@ private void fitInParentBounds(Rectangle boundsInPixels, int zoom) {
33523365
}
33533366
}
33543367

3368+
/**
3369+
* Sets the autoscaling mode for this widget.
3370+
* The capability is not supported on every platform, such that calling this method may not have an effect on unsupported platforms. The return value indicates if the autoscale mode was set properly. With {@link #isAutoScalable()}, the autoscale enablement can also be evaluated at any later pointin time.
3371+
* <p>
3372+
* Currently, this is only supported on Windows.
3373+
* </p>
3374+
*
3375+
* @param autoscalingMode
3376+
* the autoscaling mode to request; this argument is accepted but
3377+
* ignored on this platform
3378+
*
3379+
* @return {@code false} if the operation was called on an unsupported platform
3380+
*
3381+
* @since 3.133
3382+
*/
3383+
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
3384+
this.autoscalingMode = autoscalingMode;
3385+
if (!isAutoScalable()) {
3386+
this.nativeZoom = 100;
3387+
} else {
3388+
this.nativeZoom = getShellZoom();
3389+
}
3390+
return true;
3391+
}
3392+
33553393
void setBoundsInPixels (Rectangle rect) {
33563394
setBoundsInPixels (rect.x, rect.y, rect.width, rect.height);
33573395
}
@@ -4865,15 +4903,15 @@ public boolean setParent (Composite parent) {
48654903
@Override
48664904
GC createNewGC(long hDC, GCData data) {
48674905
data.nativeZoom = nativeZoom;
4868-
if (autoScaleDisabled && data.font != null) {
4906+
if (!isAutoScalable() && data.font != null) {
48694907
data.font = SWTFontProvider.getFont(display, data.font.getFontData()[0], 100);
48704908
}
48714909
return GC.win32_new(hDC, data);
48724910
}
48734911

48744912
@Override
48754913
int getZoom() {
4876-
if (autoScaleDisabled) {
4914+
if (!isAutoScalable()) {
48774915
return 100;
48784916
}
48794917
return super.getZoom();
@@ -4887,7 +4925,7 @@ int getShellZoom() {
48874925
}
48884926

48894927
int computeGetBoundsZoom() {
4890-
if (parent != null && !autoScaleDisabled) {
4928+
if (parent != null && isAutoScalable()) {
48914929
return parent.getZoom();
48924930
}
48934931
return getZoom();
@@ -6035,7 +6073,7 @@ void sendZoomChangedEvent(Event event, Shell shell) {
60356073
@Override
60366074
void handleDPIChange(Event event, float scalingFactor) {
60376075
super.handleDPIChange(event, scalingFactor);
6038-
if (this.autoScaleDisabled) {
6076+
if (!this.isAutoScalable()) {
60396077
this.nativeZoom = 100;
60406078
}
60416079
resizeFont(this, nativeZoom);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,22 @@ public ToolBar getToolBar() {
12331233
return null;
12341234
}
12351235

1236+
/**
1237+
* Returns the native zoom level of this shell.
1238+
* <p>
1239+
* The value is derived from the underlying operating system's DPI for the
1240+
* window and converted into a zoom percentage using
1241+
* {@link DPIUtil#mapDPIToZoom(int)}.
1242+
* </p>
1243+
*
1244+
* @return the native zoom level of the shell, expressed as a percentage
1245+
*
1246+
* @since 3.133
1247+
*/
1248+
public int getNativeZoom() {
1249+
return DPIUtil.mapDPIToZoom(OS.GetDpiForWindow(handle));
1250+
}
1251+
12361252
@Override
12371253
Composite findDeferredControl () {
12381254
return layoutCount > 0 ? this : null;

0 commit comments

Comments
 (0)