Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void testWidgetZoomShouldChangeOnZoomLevelChange() {
button.setText("Widget Test");
button.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
shell.open();
assertEquals("The initial zoom is wrong", zoom, button.getZoom()); // pre-condition
assertEquals("The initial zoom is wrong", zoom, button.getAutoscalingZoom()); // pre-condition
DPITestUtil.changeDPIZoom(shell, scaledZoom);
assertEquals("The Zoom Level should be updated for button on zoom change event on its shell", scaledZoom,
button.getZoom());
button.getAutoscalingZoom());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,25 @@ NSTouch findTouchWithId(NSArray touches, NSObject identity) {
return null;
}

/**
* Sets the autoscaling mode for this widget.
* 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.
* <p>
* Currently, this is only supported on Windows.
* </p>
*
* @param autoscalingMode
* the autoscaling mode to request; this argument is accepted but
* ignored on this platform
*
* @return {@code false} if the operation was called on an unsupported platform
*
* @since 3.133
*/
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
return false;
}

void setBackground () {
if (!drawsBackground()) return;
Control control = findBackgroundControl ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.cocoa.*;

/**
Expand Down Expand Up @@ -1992,6 +1993,17 @@ public void setModified (boolean modified) {
window.setDocumentEdited (modified);
}

/**
* Returns the native zoom of the device which is used globally by SWT.
*
* @return the native zoom for the current device
*
* @since 3.133
*/
public int getNativeZoom() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, we should call this getZoom(), as consumers should not bother with native/non-native zoms. And the effective zoom of a shell to deal with is always the native zoom, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not always true. When autoscaling is disabled getZoom returns autoscaled zoom depending on the scaling mode i.e. quarter, half, etc. Hence to do so, we need to rename the internal method getZoom to getAutoscaledZoom and then call this API Shell#getZoom. I'll create a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return DPIUtil.getNativeDeviceZoom();
}

/**
* Sets the shape of the shell to the region specified
* by the argument. When the argument is null, the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.eclipse.swt.graphics;


/**
* Defines the autoscaling behavior used when rendering or computing layout for
* controls that support DPI‑aware scaling.
* <p>
* This mode determines whether SWT takes care of the scaling of widget with
* respect to the zoom of the monitor or not.
* </p>
*
* <ul>
* <li><b>{@link #ENABLED}</b> – Autoscaling is explicitly enabled. Values (such
* as coordinates, dimensions, or layout metrics) will be adjusted according to
* the effective scaling factor.</li>
*
* <li><b>{@link #DISABLED}</b> – Autoscaling is explicitly disabled. Values
* will be used as-is without applying any scaling transformations.</li>
*
* <li><b>{@link #DISABLED_INHERITED}</b> – Autoscaling is disabled for this
* component, but the decision originates from a parent or owner context. This
* is typically used to indicate that the disabled state was not set explicitly
* at this level but inherited from upstream configuration.</li>
* </ul>
*
* @since 3.133
*/
public enum AutoscalingMode {
ENABLED,
DISABLED,
DISABLED_INHERITED
}
Original file line number Diff line number Diff line change
Expand Up @@ -5143,6 +5143,25 @@ void gtk_label_set_align(long label, float xAlign, float yAlign) {
GTK.gtk_label_set_yalign(label, yAlign);
}

/**
* Sets the autoscaling mode for this widget.
* 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.
* <p>
* Currently, this is only supported on Windows.
* </p>
*
* @param autoscalingMode
* the autoscaling mode to request; this argument is accepted but
* ignored on this platform
*
* @return {@code false} if the operation was called on an unsupported platform
*
* @since 3.133
*/
public boolean setAutoscalingMode(AutoscalingMode autoscalingMode) {
return false;
}

void setBackground () {
if ((state & BACKGROUND) == 0 && backgroundImage == null) {
if ((state & PARENT_BACKGROUND) != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2786,6 +2786,17 @@ public void setModified (boolean modified) {
this.modified = modified;
}

/**
* Returns the native zoom of the device which is used globally by SWT.
*
* @return the native zoom for the current device
*
* @since 3.133
*/
public int getNativeZoom() {
return DPIUtil.getNativeDeviceZoom();
}

/**
* Sets the shape of the shell to the region specified
* by the argument. When the argument is null, the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void _setImage (Image image) {
if (imageList != null) imageList.dispose ();
imageList = null;
if (image != null) {
imageList = new ImageList (style & SWT.RIGHT_TO_LEFT, getZoom());
imageList = new ImageList (style & SWT.RIGHT_TO_LEFT, getAutoscalingZoom());
if (OS.IsWindowEnabled (handle)) {
imageList.add (image);
} else {
Expand All @@ -147,7 +147,7 @@ void _setImage (Image image) {
imageList.add (disabledImage);
}
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
buttonImageList.himl = imageList.getHandle(getZoom());
buttonImageList.himl = imageList.getHandle(getAutoscalingZoom());
int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
newBits &= ~(OS.BS_LEFT | OS.BS_CENTER | OS.BS_RIGHT);
if ((style & SWT.LEFT) != 0) newBits |= OS.BS_LEFT;
Expand Down Expand Up @@ -188,7 +188,7 @@ void _setText (String text) {
if ((style & SWT.RIGHT) != 0) newBits |= OS.BS_RIGHT;
if (imageList != null) {
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
buttonImageList.himl = imageList.getHandle(getZoom());
buttonImageList.himl = imageList.getHandle(getAutoscalingZoom());
if (text.length () == 0) {
if ((style & SWT.LEFT) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
if ((style & SWT.CENTER) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
Expand Down Expand Up @@ -295,7 +295,7 @@ int computeLeftMargin () {
if ((style & (SWT.PUSH | SWT.TOGGLE)) == 0) return MARGIN;
int margin = 0;
if (image != null && text.length () != 0) {
Rectangle bounds = Win32DPIUtils.scaleBounds(image.getBounds(), this.getZoom(), 100);
Rectangle bounds = Win32DPIUtils.scaleBounds(image.getBounds(), this.getAutoscalingZoom(), 100);
margin += bounds.width + MARGIN * 2;
long oldFont = 0;
long hDC = OS.GetDC (handle);
Expand Down Expand Up @@ -359,13 +359,13 @@ Point computeSizeInPixels (Point hintInPoints, int zoom, boolean changed) {
boolean hasImage = image != null, hasText = true;
if (hasImage) {
if (image != null) {
Rectangle rect = Win32DPIUtils.scaleBounds(image.getBounds(), this.getZoom(), 100);
Rectangle rect = Win32DPIUtils.scaleBounds(image.getBounds(), this.getAutoscalingZoom(), 100);
width = rect.width;
if (hasText && text.length () != 0) {
width += DPIUtil.pointToPixel(MARGIN * 2, getZoom());;
width += DPIUtil.pointToPixel(MARGIN * 2, getAutoscalingZoom());;
}
height = rect.height;
extra = DPIUtil.pointToPixel(MARGIN * 2, getZoom());;
extra = DPIUtil.pointToPixel(MARGIN * 2, getAutoscalingZoom());;
}
}
if (hasText) {
Expand All @@ -379,7 +379,7 @@ Point computeSizeInPixels (Point hintInPoints, int zoom, boolean changed) {
if (length == 0) {
height = Math.max (height, lptm.tmHeight);
} else {
extra = Math.max (DPIUtil.pointToPixel(MARGIN * 2, getZoom()), lptm.tmAveCharWidth);
extra = Math.max (DPIUtil.pointToPixel(MARGIN * 2, getAutoscalingZoom()), lptm.tmAveCharWidth);
char [] buffer = text.toCharArray ();
RECT rect = new RECT ();
int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
Expand Down Expand Up @@ -780,7 +780,7 @@ public void setAlignment (int alignment) {
if ((style & SWT.RIGHT) != 0) newBits |= OS.BS_RIGHT;
if (imageList != null) {
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
buttonImageList.himl = imageList.getHandle(getZoom());
buttonImageList.himl = imageList.getHandle(getAutoscalingZoom());
if (text.length () == 0) {
if ((style & SWT.LEFT) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
if ((style & SWT.CENTER) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
Expand Down Expand Up @@ -1035,15 +1035,15 @@ void updateImageList () {
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
OS.SendMessage (handle, OS.BCM_GETIMAGELIST, 0, buttonImageList);
if (imageList != null) imageList.dispose ();
imageList = new ImageList (style & SWT.RIGHT_TO_LEFT, getZoom());
imageList = new ImageList (style & SWT.RIGHT_TO_LEFT, getAutoscalingZoom());
if (OS.IsWindowEnabled (handle)) {
imageList.add (image);
} else {
if (disabledImage != null) disabledImage.dispose ();
disabledImage = new Image (display, image, SWT.IMAGE_DISABLE);
imageList.add (disabledImage);
}
buttonImageList.himl = imageList.getHandle(getZoom());
buttonImageList.himl = imageList.getHandle(getAutoscalingZoom());
OS.SendMessage (handle, OS.BCM_SETIMAGELIST, 0, buttonImageList);
/*
* Bug in Windows. Under certain cirumstances yet to be
Expand Down Expand Up @@ -1392,13 +1392,13 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) {
GC gc = createNewGC(nmcd.hdc, data);

int margin = computeLeftMargin();
Rectangle imageBounds = Win32DPIUtils.scaleBounds(image.getBounds(), this.getZoom(), 100);
Rectangle imageBounds = Win32DPIUtils.scaleBounds(image.getBounds(), this.getAutoscalingZoom(), 100);
int imageWidth = imageBounds.width;
left += (imageWidth + (isRadioOrCheck() ? 2 * MARGIN : MARGIN)); // for SWT.RIGHT_TO_LEFT right and left are inverted

int x = margin + (isRadioOrCheck() ? radioOrCheckTextPadding : 3);
int y = Math.max (0, (nmcd.bottom - imageBounds.height) / 2);
int zoom = getZoom();
int zoom = getAutoscalingZoom();
gc.drawImage (image, DPIUtil.pixelToPoint(x, zoom), DPIUtil.pixelToPoint(y, zoom));
gc.dispose ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Canvas (Composite parent, int style) {
* @since 3.2
*/
public void drawBackground (GC gc, int x, int y, int width, int height) {
int zoom = getZoom();
int zoom = getAutoscalingZoom();
Rectangle rectangle = Win32DPIUtils.pointToPixel(new Rectangle(x, y, width, height), zoom);
drawBackgroundInPixels(gc, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0, 0);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ void reskinChildren (int flags) {
*/
public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
checkWidget ();
int zoom = getZoom();
int zoom = getAutoscalingZoom();
destX = DPIUtil.pointToPixel(destX, zoom);
destY = DPIUtil.pointToPixel(destY, zoom);
Rectangle rectangle = Win32DPIUtils.pointToPixel(new Rectangle(x, y, width, height), zoom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ long defaultFont () {
*/
public Rectangle getBounds () {
checkWidget();
return Win32DPIUtils.pixelToPoint(getBoundsInPixels(), getZoom());
return Win32DPIUtils.pixelToPoint(getBoundsInPixels(), getAutoscalingZoom());
}

Rectangle getBoundsInPixels () {
if (image != null) {
Rectangle rect = Win32DPIUtils.pointToPixel(image.getBounds(), getZoom());
Rectangle rect = Win32DPIUtils.pointToPixel(image.getBounds(), getAutoscalingZoom());
return new Rectangle (getXInPixels(), getYInPixels(), rect.width, rect.height);
}
if (width == 0) {
Expand Down Expand Up @@ -223,12 +223,12 @@ public Canvas getParent () {
*/
public Point getSize () {
checkWidget();
return Win32DPIUtils.pixelToPointAsSize(getSizeInPixels(), getZoom());
return Win32DPIUtils.pixelToPointAsSize(getSizeInPixels(), getAutoscalingZoom());
}

Point getSizeInPixels () {
if (image != null) {
Rectangle rect = Win32DPIUtils.pointToPixel(image.getBounds(), getZoom());
Rectangle rect = Win32DPIUtils.pointToPixel(image.getBounds(), getAutoscalingZoom());
return new Point (rect.width, rect.height);
}
if (width == 0) {
Expand All @@ -241,19 +241,19 @@ Point getSizeInPixels () {
}

private int getWidthInPixels() {
return DPIUtil.pointToPixel(width, getZoom());
return DPIUtil.pointToPixel(width, getAutoscalingZoom());
}

private int getHeightInPixels() {
return DPIUtil.pointToPixel(height, getZoom());
return DPIUtil.pointToPixel(height, getAutoscalingZoom());
}

private int getXInPixels() {
return DPIUtil.pointToPixel(x, getZoom());
return DPIUtil.pointToPixel(x, getAutoscalingZoom());
}

private int getYInPixels() {
return DPIUtil.pointToPixel(y, getZoom());
return DPIUtil.pointToPixel(y, getAutoscalingZoom());
}

/**
Expand Down Expand Up @@ -373,7 +373,7 @@ void resize () {
resized = false;
long hwnd = parent.handle;
OS.DestroyCaret ();
long hBitmap = image != null ? Image.win32_getHandle(image, getZoom()) : 0;
long hBitmap = image != null ? Image.win32_getHandle(image, getAutoscalingZoom()) : 0;
int widthInPixels = this.getWidthInPixels();
if (image == null && widthInPixels == 0) {
OptionalInt systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
Expand Down Expand Up @@ -452,7 +452,7 @@ public void setBounds (Rectangle rect) {
void setFocus () {
long hwnd = parent.handle;
long hBitmap = 0;
if (image != null) hBitmap = Image.win32_getHandle(image, getZoom());
if (image != null) hBitmap = Image.win32_getHandle(image, getAutoscalingZoom());
int widthInPixels = this.getWidthInPixels();
if (image == null && widthInPixels == 0) {
OptionalInt systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ boolean dragDetect (long hwnd, int x, int y, boolean filter, boolean [] detect,
*/
public Point getCaretLocation () {
checkWidget ();
return Win32DPIUtils.pixelToPointAsLocation(getCaretLocationInPixels(), getZoom());
return Win32DPIUtils.pixelToPointAsLocation(getCaretLocationInPixels(), getAutoscalingZoom());
}

Point getCaretLocationInPixels () {
Expand Down Expand Up @@ -1079,7 +1079,7 @@ public int getItemCount () {
*/
public int getItemHeight () {
checkWidget ();
return DPIUtil.pixelToPoint(getItemHeightInPixels(), getZoom());
return DPIUtil.pixelToPoint(getItemHeightInPixels(), getAutoscalingZoom());
}

int getItemHeightInPixels () {
Expand Down Expand Up @@ -1345,7 +1345,7 @@ public String getText () {
*/
public int getTextHeight () {
checkWidget ();
return DPIUtil.pixelToPoint(getTextHeightInPixels(), getZoom());
return DPIUtil.pixelToPoint(getTextHeightInPixels(), getAutoscalingZoom());
}

int getTextHeightInPixels () {
Expand Down
Loading
Loading