Skip to content

Commit c4655bd

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 4b7c450 commit c4655bd

4 files changed

Lines changed: 114 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 ();
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 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/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ 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

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

@@ -124,11 +124,11 @@ public abstract class Control extends Widget implements Drawable {
124124
public Control (Composite parent, int style) {
125125
super (parent, style);
126126
this.parent = parent;
127-
Boolean parentPropagateAutoscaleDisabled = (Boolean) parent.getData(PROPOGATE_AUTOSCALE_DISABLED);
128-
if (parentPropagateAutoscaleDisabled == null || parentPropagateAutoscaleDisabled) {
129-
this.autoScaleDisabled = parent.autoScaleDisabled;
127+
AutoscalingMode parentAutoscaleMode = parent.autoscalingMode;
128+
if (parentAutoscaleMode == AutoscalingMode.DISABLED_INHERITED) {
129+
this.autoscalingMode = parent.autoscalingMode;
130130
}
131-
if (!autoScaleDisabled) {
131+
if (isAutoScalable()) {
132132
this.nativeZoom = getShellZoom();
133133
}
134134
createWidget ();
@@ -1287,12 +1287,20 @@ public Object getData(String key) {
12871287
public void setData(String key, Object value) {
12881288
super.setData(key, value);
12891289
if (DATA_AUTOSCALE_DISABLED.equals(key)) {
1290-
autoScaleDisabled = Boolean.parseBoolean(value.toString());
1290+
boolean autoScaleDisabled = Boolean.parseBoolean(value.toString());
12911291
if (autoScaleDisabled) {
1292+
Object autoscaleDisablementValue = getData(PROPOGATE_AUTOSCALE_DISABLED);
1293+
boolean propagateAutoscaling = autoscaleDisablementValue != null && Boolean.parseBoolean(autoscaleDisablementValue.toString());
1294+
autoscalingMode = propagateAutoscaling ? AutoscalingMode.DISABLED_INHERITED : AutoscalingMode.DISABLED;
12921295
this.nativeZoom = 100;
12931296
} else {
1297+
autoscalingMode = AutoscalingMode.ENABLED;
12941298
this.nativeZoom = getShellZoom();
12951299
}
1300+
} else if (PROPOGATE_AUTOSCALE_DISABLED.equals(key)) {
1301+
boolean propagateAutoscaling = value != null && Boolean.parseBoolean(value.toString());
1302+
autoscalingMode = propagateAutoscaling ? AutoscalingMode.DISABLED_INHERITED : AutoscalingMode.DISABLED;
1303+
this.nativeZoom = 100;
12961304
}
12971305
}
12981306

@@ -1874,6 +1882,11 @@ boolean isActive () {
18741882
return shell.getEnabled ();
18751883
}
18761884

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

3369+
/**
3370+
* Sets the autoscaling mode for this widget.
3371+
* 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.
3372+
* <p>
3373+
* Currently, this is only supported on Windows.
3374+
* </p>
3375+
*
3376+
* @param autoscalingMode
3377+
* the autoscaling mode to request; this argument is accepted but
3378+
* ignored on this platform
3379+
*
3380+
* @return {@code false} if the operation was called on an unsupported platform
3381+
*
3382+
* @since 3.133
3383+
*/
3384+
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
3385+
this.autoscalingMode = autoscalingMode;
3386+
if (!isAutoScalable()) {
3387+
this.nativeZoom = 100;
3388+
} else {
3389+
this.nativeZoom = getShellZoom();
3390+
}
3391+
return true;
3392+
}
3393+
33563394
void setBoundsInPixels (Rectangle rect) {
33573395
setBoundsInPixels (rect.x, rect.y, rect.width, rect.height);
33583396
}
@@ -4866,15 +4904,15 @@ public boolean setParent (Composite parent) {
48664904
@Override
48674905
GC createNewGC(long hDC, GCData data) {
48684906
data.nativeZoom = nativeZoom;
4869-
if (autoScaleDisabled && data.font != null) {
4907+
if (!isAutoScalable() && data.font != null) {
48704908
data.font = SWTFontProvider.getFont(display, data.font.getFontData()[0], 100);
48714909
}
48724910
return GC.win32_new(hDC, data);
48734911
}
48744912

48754913
@Override
48764914
int getAutoscalingZoom() {
4877-
if (autoScaleDisabled) {
4915+
if (!isAutoScalable()) {
48784916
return 100;
48794917
}
48804918
return super.getAutoscalingZoom();
@@ -4888,7 +4926,7 @@ int getShellZoom() {
48884926
}
48894927

48904928
int computeGetBoundsZoom() {
4891-
if (parent != null && !autoScaleDisabled) {
4929+
if (parent != null && isAutoScalable()) {
48924930
return parent.getAutoscalingZoom();
48934931
}
48944932
return getAutoscalingZoom();
@@ -6079,7 +6117,7 @@ void sendZoomChangedEvent(Event event, Shell shell) {
60796117
@Override
60806118
void handleDPIChange(Event event, float scalingFactor) {
60816119
super.handleDPIChange(event, scalingFactor);
6082-
if (this.autoScaleDisabled) {
6120+
if (!this.isAutoScalable()) {
60836121
this.nativeZoom = 100;
60846122
}
60856123
resizeFont(this, nativeZoom);

0 commit comments

Comments
 (0)