Skip to content

Commit 2b5c0a7

Browse files
amartya4256akoch-yatta
authored andcommitted
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 8afb3c5 commit 2b5c0a7

4 files changed

Lines changed: 139 additions & 16 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 point in time.
3533+
* <p>
3534+
* Currently, this is only supported on Windows.
3535+
* </p>
3536+
*
3537+
* @param autoscalingMode
3538+
* the autoscaling mode to set; 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 ();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.eclipse.swt.graphics;
2+
3+
/**
4+
* Defines the autoscaling behavior used when rendering or computing layout for
5+
* controls that support DPI‑aware scaling.
6+
* <p>
7+
* This mode determines whether SWT takes care of the scaling of widgets with
8+
* respect to the zoom level of the monitor.
9+
* </p>
10+
*
11+
* <ul>
12+
* <li><b>{@link #ENABLED}</b> – Autoscaling is explicitly enabled. Values (such
13+
* as coordinates, dimensions, or layout metrics) will be adjusted according to
14+
* the effective scaling factor.</li>
15+
*
16+
* <li><b>{@link #DISABLED}</b> – Autoscaling is explicitly disabled. Values
17+
* will be used as‑is without applying any scaling transformations.</li>
18+
*
19+
* <li><b>{@link #DISABLED_INHERITED}</b> – Autoscaling is disabled for this
20+
* control and this is inherited by its children. Child controls will also be
21+
* initialized with DISABLED_INHERITED AutoscalingMode.</li>
22+
* </ul>
23+
*
24+
* @since 3.133
25+
*/
26+
public enum AutoscalingMode {
27+
ENABLED, DISABLED, DISABLED_INHERITED
28+
}

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 point in time.
5149+
* <p>
5150+
* Currently, this is only supported on Windows.
5151+
* </p>
5152+
*
5153+
* @param autoscalingMode
5154+
* the autoscaling mode to set; 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/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,24 @@ public abstract class Control extends Widget implements Drawable {
7878
Region region;
7979
Font font;
8080
int drawCount, foreground, background, backgroundAlpha = 255;
81-
boolean autoScaleDisabled = false;
81+
AutoscalingMode autoscalingMode = AutoscalingMode.ENABLED;
8282

83+
/**
84+
* @deprecated use {@link Shell#getZoom(AutoscalingMode)} instead to obtain the zoom of the shell.
85+
*/
86+
@Deprecated(since = "2026-03", forRemoval = true)
8387
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM";
8488

89+
/**
90+
* @deprecated use {@link Control#setAutoscalingMode(AutoscalingMode)} instead to set the autoscaling mode directly.
91+
*/
92+
@Deprecated(since = "2026-03", forRemoval = true)
8593
private static final String DATA_AUTOSCALE_DISABLED = "AUTOSCALE_DISABLED";
8694

95+
/**
96+
* @deprecated use {@link Control#setAutoscalingMode(AutoscalingMode)} instead to set the autoscaling mode directly.
97+
*/
98+
@Deprecated(since = "2026-03", forRemoval = true)
8799
private static final String PROPOGATE_AUTOSCALE_DISABLED = "PROPOGATE_AUTOSCALE_DISABLED";
88100
/**
89101
* Prevents uninitialized instances from being created outside the package.
@@ -124,11 +136,11 @@ public abstract class Control extends Widget implements Drawable {
124136
public Control (Composite parent, int style) {
125137
super (parent, style);
126138
this.parent = parent;
127-
Boolean parentPropagateAutoscaleDisabled = (Boolean) parent.getData(PROPOGATE_AUTOSCALE_DISABLED);
128-
if (parentPropagateAutoscaleDisabled == null || parentPropagateAutoscaleDisabled) {
129-
this.autoScaleDisabled = parent.autoScaleDisabled;
139+
AutoscalingMode parentAutoscaleMode = parent.autoscalingMode;
140+
if (parentAutoscaleMode == AutoscalingMode.DISABLED_INHERITED) {
141+
this.autoscalingMode = parent.autoscalingMode;
130142
}
131-
if (!autoScaleDisabled) {
143+
if (isAutoScalable()) {
132144
this.nativeZoom = getShellZoom();
133145
}
134146
createWidget ();
@@ -1286,13 +1298,28 @@ public Object getData(String key) {
12861298
@Override
12871299
public void setData(String key, Object value) {
12881300
super.setData(key, value);
1289-
if (DATA_AUTOSCALE_DISABLED.equals(key)) {
1290-
autoScaleDisabled = Boolean.parseBoolean(value.toString());
1291-
if (autoScaleDisabled) {
1292-
this.nativeZoom = 100;
1293-
} else {
1294-
this.nativeZoom = getShellZoom();
1295-
}
1301+
if (DATA_AUTOSCALE_DISABLED.equals(key) || PROPOGATE_AUTOSCALE_DISABLED.equals(key)) {
1302+
updateAutoScalingModeFromData();
1303+
}
1304+
}
1305+
1306+
/**
1307+
* @deprecated use {@link Control#setAutoscalingMode(AutoscalingMode)} instead.
1308+
* These hidden data objects will be removed in a future release and one has to
1309+
* migrate to the new API for that purpose.
1310+
*/
1311+
@Deprecated(since = "2026-03", forRemoval = true)
1312+
private void updateAutoScalingModeFromData() {
1313+
Object propagateAutoscaleDisabled = getData(PROPOGATE_AUTOSCALE_DISABLED);
1314+
boolean propagateAutoscaling = propagateAutoscaleDisabled != null && Boolean.parseBoolean(propagateAutoscaleDisabled.toString());
1315+
Object autoscalingDisabledData = getData(DATA_AUTOSCALE_DISABLED);
1316+
boolean isAutoscaleDisabled = autoscalingDisabledData != null && Boolean.parseBoolean(autoscalingDisabledData.toString());
1317+
if (isAutoscaleDisabled) {
1318+
autoscalingMode = propagateAutoscaling ? AutoscalingMode.DISABLED_INHERITED : AutoscalingMode.DISABLED;
1319+
this.nativeZoom = 100;
1320+
} else {
1321+
autoscalingMode = AutoscalingMode.ENABLED;
1322+
this.nativeZoom = getShellZoom();
12961323
}
12971324
}
12981325

@@ -1874,6 +1901,11 @@ boolean isActive () {
18741901
return shell.getEnabled ();
18751902
}
18761903

1904+
@Override
1905+
public boolean isAutoScalable() {
1906+
return autoscalingMode == AutoscalingMode.ENABLED;
1907+
}
1908+
18771909
/**
18781910
* Returns <code>true</code> if the receiver is enabled and all
18791911
* ancestors up to and including the receiver's nearest ancestor
@@ -3353,6 +3385,31 @@ private void fitInParentBounds(Rectangle boundsInPixels, int zoom) {
33533385
}
33543386
}
33553387

3388+
/**
3389+
* Sets the autoscaling mode for this widget.
3390+
* 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 point in time.
3391+
* <p>
3392+
* Currently, this is only supported on Windows.
3393+
* </p>
3394+
*
3395+
* @param autoscalingMode
3396+
* the autoscaling mode to set; this argument is accepted but
3397+
* ignored on this platform
3398+
*
3399+
* @return {@code false} if the operation was called on an unsupported platform
3400+
*
3401+
* @since 3.133
3402+
*/
3403+
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
3404+
this.autoscalingMode = autoscalingMode;
3405+
if (!isAutoScalable()) {
3406+
this.nativeZoom = 100;
3407+
} else {
3408+
this.nativeZoom = getShellZoom();
3409+
}
3410+
return true;
3411+
}
3412+
33563413
void setBoundsInPixels (Rectangle rect) {
33573414
setBoundsInPixels (rect.x, rect.y, rect.width, rect.height);
33583415
}
@@ -4866,15 +4923,15 @@ public boolean setParent (Composite parent) {
48664923
@Override
48674924
GC createNewGC(long hDC, GCData data) {
48684925
data.nativeZoom = nativeZoom;
4869-
if (autoScaleDisabled && data.font != null) {
4926+
if (!isAutoScalable() && data.font != null) {
48704927
data.font = SWTFontProvider.getFont(display, data.font.getFontData()[0], 100);
48714928
}
48724929
return GC.win32_new(hDC, data);
48734930
}
48744931

48754932
@Override
48764933
int getAutoscalingZoom() {
4877-
if (autoScaleDisabled) {
4934+
if (!isAutoScalable()) {
48784935
return 100;
48794936
}
48804937
return super.getAutoscalingZoom();
@@ -4888,7 +4945,7 @@ int getShellZoom() {
48884945
}
48894946

48904947
int computeGetBoundsZoom() {
4891-
if (parent != null && !autoScaleDisabled) {
4948+
if (parent != null && isAutoScalable()) {
48924949
return parent.getAutoscalingZoom();
48934950
}
48944951
return getAutoscalingZoom();
@@ -6079,7 +6136,7 @@ void sendZoomChangedEvent(Event event, Shell shell) {
60796136
@Override
60806137
void handleDPIChange(Event event, float scalingFactor) {
60816138
super.handleDPIChange(event, scalingFactor);
6082-
if (this.autoScaleDisabled) {
6139+
if (!this.isAutoScalable()) {
60836140
this.nativeZoom = 100;
60846141
}
60856142
resizeFont(this, nativeZoom);

0 commit comments

Comments
 (0)