Skip to content

Commit 1143bb0

Browse files
Refactor: Move Win32-specific DPI logic to Win32DPIUtils
This commit introduces a dedicated Win32DPIUtils class to isolate Win32-specific DPI logic, improving platform separation and maintainability. Refactored moving with eclipse refactoring tool.
1 parent 115bd83 commit 1143bb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+838
-798
lines changed

bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public static Frame new_Frame (final Composite parent) {
250250

251251
parent.getDisplay().asyncExec(() -> {
252252
if (parent.isDisposed()) return;
253-
final Rectangle clientArea = DPIUtil.scaleUp(parent.getClientArea(), DPIUtil.getZoomForAutoscaleProperty(parent.nativeZoom)); // To Pixels
253+
final Rectangle clientArea = Win32DPIUtils.scaleUp(parent.getClientArea(), DPIUtil.getZoomForAutoscaleProperty(parent.nativeZoom)); // To Pixels
254254
EventQueue.invokeLater(() -> {
255255
frame.setSize (clientArea.width, clientArea.height);
256256
frame.validate ();
@@ -286,15 +286,14 @@ public static Shell new_Shell (final Display display, final Canvas parent) {
286286
SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e);
287287
}
288288
if (handle == 0) SWT.error (SWT.ERROR_INVALID_ARGUMENT, null, " [peer not created]");
289-
290289
final Shell shell = Shell.win32_new (display, handle);
291290
final ComponentListener listener = new ComponentAdapter () {
292291
@Override
293292
public void componentResized (ComponentEvent e) {
294293
display.syncExec (() -> {
295294
if (shell.isDisposed()) return;
296295
Dimension dim = parent.getSize ();
297-
shell.setSize(DPIUtil.scaleDown(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
296+
shell.setSize(Win32DPIUtils.scaleDown(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
298297
});
299298
}
300299
};

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/Edge.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,8 @@ int handleContextMenuRequested(long pView, long pArgs) {
12541254
// to PIXEL coordinates with the real native zoom value
12551255
// independent from the swt.autoScale property:
12561256
Point pt = new Point( //
1257-
DPIUtil.scaleUp(win32Point.x, DPIUtil.getNativeDeviceZoom()), //
1258-
DPIUtil.scaleUp(win32Point.y, DPIUtil.getNativeDeviceZoom()));
1257+
Win32DPIUtils.scaleUp(win32Point.x, DPIUtil.getNativeDeviceZoom()), //
1258+
Win32DPIUtils.scaleUp(win32Point.y, DPIUtil.getNativeDeviceZoom()));
12591259
// - then, scale back down from PIXEL to DISPLAY coordinates, taking
12601260
// swt.autoScale property into account
12611261
// which is also later considered in Menu#setLocation()

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ void handleDOMEvent (OleEvent e) {
18801880
int screenY = pVarResult.getInt();
18811881
pVarResult.dispose();
18821882

1883-
Point position = DPIUtil.scaleDown(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
1883+
Point position = Win32DPIUtils.scaleDown(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
18841884
position = browser.getDisplay().map(null, browser, position);
18851885
newEvent.x = position.x; newEvent.y = position.y;
18861886

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/DragSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ private void drag(Event dragEvent) {
510510
int offsetX = event.offsetX;
511511
hwndDrag = topControl.handle;
512512
if ((topControl.getStyle() & SWT.RIGHT_TO_LEFT) != 0) {
513-
offsetX = DPIUtil.scaleUp(image.getBounds(), zoom).width - offsetX;
513+
offsetX = Win32DPIUtils.scaleUp(image.getBounds(), zoom).width - offsetX;
514514
RECT rect = new RECT ();
515515
OS.GetClientRect (topControl.handle, rect);
516516
hwndDrag = OS.CreateWindowEx (
@@ -538,8 +538,8 @@ private void drag(Event dragEvent) {
538538
int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
539539
OS.RedrawWindow (topControl.handle, null, 0, flags);
540540
POINT pt = new POINT ();
541-
pt.x = DPIUtil.scaleUp(dragEvent.x, zoom);// To Pixels
542-
pt.y = DPIUtil.scaleUp(dragEvent.y, zoom);// To Pixels
541+
pt.x = Win32DPIUtils.scaleUp(dragEvent.x, zoom);// To Pixels
542+
pt.y = Win32DPIUtils.scaleUp(dragEvent.y, zoom);// To Pixels
543543
OS.MapWindowPoints (control.handle, 0, pt, 1);
544544
RECT rect = new RECT ();
545545
OS.GetWindowRect (hwndDrag, rect);

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/DropTarget.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
410410
if (this.control == null) {
411411
// If there is no control for context, the behavior remains as before
412412
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.nativeZoom);
413-
return DPIUtil.scaleDown(new Point(xInPixels, yInPixels), zoom);
413+
return Win32DPIUtils.scaleDown(new Point(xInPixels, yInPixels), zoom);
414414
}
415415
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.control.nativeZoom);
416416
// There is no API to convert absolute values in pixels to display relative
@@ -419,7 +419,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
419419
POINT pt = new POINT ();
420420
pt.x = xInPixels; pt.y = yInPixels;
421421
OS.ScreenToClient (this.control.handle, pt);
422-
Point p = DPIUtil.scaleDown(new Point (pt.x, pt.y), zoom);
422+
Point p = Win32DPIUtils.scaleDown(new Point (pt.x, pt.y), zoom);
423423
return this.control.toDisplay(p);
424424
}
425425

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TableDragSourceEffect.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.eclipse.swt.*;
1717
import org.eclipse.swt.graphics.*;
1818
import org.eclipse.swt.internal.*;
19-
import org.eclipse.swt.internal.DPIUtil.*;
2019
import org.eclipse.swt.internal.win32.*;
2120
import org.eclipse.swt.widgets.*;
2221

@@ -147,7 +146,7 @@ Image getDragSourceImage(DragSourceEvent event) {
147146
data.transparentPixel = shdi.crColorKey << 8;
148147
}
149148
Display display = control.getDisplay();
150-
dragSourceImage = new Image(display, new AutoScaleImageDataProvider(display, data, DPIUtil.getZoomForAutoscaleProperty(control.nativeZoom)));
149+
dragSourceImage = new Image(display, new Win32DPIUtils.AutoScaleImageDataProvider(display, data, DPIUtil.getZoomForAutoscaleProperty(control.nativeZoom)));
151150
OS.SelectObject (memHdc, oldMemBitmap);
152151
OS.DeleteDC (memHdc);
153152
OS.DeleteObject (memDib);

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TableDropTargetEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void dragOver(DropTargetEvent event) {
151151
int effect = checkEffect(event.feedback);
152152
long handle = table.handle;
153153
Point coordinates = new Point(event.x, event.y);
154-
coordinates = DPIUtil.scaleUp(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
154+
coordinates = Win32DPIUtils.scaleUp(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
155155
LVHITTESTINFO pinfo = new LVHITTESTINFO();
156156
pinfo.x = coordinates.x;
157157
pinfo.y = coordinates.y;

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TreeDragSourceEffect.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.eclipse.swt.*;
1717
import org.eclipse.swt.graphics.*;
1818
import org.eclipse.swt.internal.*;
19-
import org.eclipse.swt.internal.DPIUtil.*;
2019
import org.eclipse.swt.internal.win32.*;
2120
import org.eclipse.swt.widgets.*;
2221

@@ -146,7 +145,7 @@ Image getDragSourceImage(DragSourceEvent event) {
146145
data.transparentPixel = shdi.crColorKey << 8;
147146
}
148147
Display display = control.getDisplay ();
149-
dragSourceImage = new Image (display, new AutoScaleImageDataProvider(display, data, DPIUtil.getZoomForAutoscaleProperty(control.nativeZoom)));
148+
dragSourceImage = new Image (display, new Win32DPIUtils.AutoScaleImageDataProvider(display, data, DPIUtil.getZoomForAutoscaleProperty(control.nativeZoom)));
150149
OS.SelectObject (memHdc, oldMemBitmap);
151150
OS.DeleteDC (memHdc);
152151
OS.DeleteObject (memDib);

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TreeDropTargetEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void dragOver(DropTargetEvent event) {
165165
int effect = checkEffect(event.feedback);
166166
long handle = tree.handle;
167167
Point coordinates = new Point(event.x, event.y);
168-
coordinates = DPIUtil.scaleUp(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
168+
coordinates = Win32DPIUtils.scaleUp(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
169169
TVHITTESTINFO lpht = new TVHITTESTINFO ();
170170
lpht.x = coordinates.x;
171171
lpht.y = coordinates.y;

bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ protected int GetWindow(long phwnd) {
810810
return COM.S_OK;
811811
}
812812
RECT getRect() {
813-
Rectangle area = DPIUtil.scaleUp(getClientArea(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
813+
Rectangle area = Win32DPIUtils.scaleUp(getClientArea(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
814814
RECT rect = new RECT();
815815
rect.left = area.x;
816816
rect.top = area.y;
@@ -987,14 +987,14 @@ private int OnInPlaceDeactivate() {
987987
return COM.S_OK;
988988
}
989989
private int OnPosRectChange(long lprcPosRect) {
990-
Point size = DPIUtil.scaleUp(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
990+
Point size = Win32DPIUtils.scaleUp(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
991991
setExtent(size.x, size.y);
992992
return COM.S_OK;
993993
}
994994
private void onPaint(Event e) {
995995
if (state == STATE_RUNNING || state == STATE_INPLACEACTIVE) {
996996
SIZE size = getExtent();
997-
Rectangle area = DPIUtil.scaleUp(getClientArea(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
997+
Rectangle area = Win32DPIUtils.scaleUp(getClientArea(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
998998
RECT rect = new RECT();
999999
if (getProgramID().startsWith("Excel.Sheet")) { //$NON-NLS-1$
10001000
rect.left = area.x; rect.right = area.x + (area.height * size.cx / size.cy);
@@ -1370,7 +1370,7 @@ void setBorderSpace(RECT newBorderwidth) {
13701370
}
13711371
void setBounds() {
13721372
int zoom = DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
1373-
Rectangle area = DPIUtil.scaleUp(frame.getClientArea(), zoom); // To Pixels
1373+
Rectangle area = Win32DPIUtils.scaleUp(frame.getClientArea(), zoom); // To Pixels
13741374
setBounds(DPIUtil.scaleDown(borderWidths.left, zoom),
13751375
DPIUtil.scaleDown(borderWidths.top, zoom),
13761376
DPIUtil.scaleDown(area.width - borderWidths.left - borderWidths.right, zoom),

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 3 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static Optional<AutoScaleMethod> forString(String s) {
5959
private static AutoScaleMethod autoScaleMethod;
6060

6161
private static String autoScaleValue;
62-
private static final boolean USE_CAIRO_AUTOSCALE = SWT.getPlatform().equals("gtk");
6362

6463
/**
6564
* System property that controls the autoScale functionality.
@@ -116,65 +115,20 @@ public static Optional<AutoScaleMethod> forString(String s) {
116115
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
117116
}
118117

119-
public static float[] scaleDown(float size[], int zoom) {
120-
if (zoom == 100 || size == null) return size;
121-
float scaleFactor = getScalingFactor (zoom);
122-
float scaledSize[] = new float[size.length];
123-
for (int i = 0; i < scaledSize.length; i++) {
124-
scaledSize[i] = size[i] / scaleFactor;
125-
}
126-
return scaledSize;
127-
}
128-
129-
public static float[] scaleDown(Drawable drawable, float size[], int zoom) {
130-
if (drawable != null && !drawable.isAutoScalable()) return size;
131-
return scaleDown(size, zoom);
132-
}
133118

134119
public static int scaleDown(int size, int zoom) {
135120
if (zoom == 100 || size == SWT.DEFAULT) return size;
136121
float scaleFactor = getScalingFactor (zoom);
137122
return Math.round (size / scaleFactor);
138123
}
139124

140-
public static int scaleDown(Drawable drawable, int size, int zoom) {
141-
if (drawable != null && !drawable.isAutoScalable()) return size;
142-
return scaleDown (size, zoom);
143-
}
144125

145126
public static float scaleDown(float size, int zoom) {
146127
if (zoom == 100 || size == SWT.DEFAULT) return size;
147128
float scaleFactor = getScalingFactor (zoom);
148129
return (size / scaleFactor);
149130
}
150131

151-
public static float scaleDown(Drawable drawable, float size, int zoom) {
152-
if (drawable != null && !drawable.isAutoScalable()) return size;
153-
return scaleDown (size, zoom);
154-
}
155-
156-
public static Point scaleDown(Point point, int zoom) {
157-
if (zoom == 100 || point == null) return point;
158-
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point);
159-
float scaleFactor = getScalingFactor(zoom);
160-
float scaledX = fPoint.getX() / scaleFactor;
161-
float scaledY = fPoint.getY() / scaleFactor;
162-
return new Point.OfFloat(scaledX, scaledY);
163-
}
164-
165-
public static Point scaleDown(Drawable drawable, Point point, int zoom) {
166-
if (drawable != null && !drawable.isAutoScalable()) return point;
167-
return scaleDown (point, zoom);
168-
}
169-
170-
public static Rectangle scaleDown(Rectangle rect, int zoom) {
171-
return scaleBounds(rect, 100, zoom);
172-
}
173-
174-
public static Rectangle scaleDown(Drawable drawable, Rectangle rect, int zoom) {
175-
if (drawable != null && !drawable.isAutoScalable()) return rect;
176-
return scaleDown (rect, zoom);
177-
}
178132

179133
/**
180134
* Auto-scale image with ImageData
@@ -226,20 +180,7 @@ public static boolean isSmoothScalingEnabled() {
226180
}
227181

228182
/**
229-
* Returns a new rectangle as per the scaleFactor.
230-
*/
231-
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
232-
if (rect == null || targetZoom == currentZoom) return rect;
233-
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect);
234-
float scaleFactor = getScalingFactor(targetZoom, currentZoom);
235-
float scaledX = fRect.getX() * scaleFactor;
236-
float scaledY = fRect.getY() * scaleFactor;
237-
float scaledWidth = fRect.getWidth() * scaleFactor;
238-
float scaledHeight = fRect.getHeight() * scaleFactor;
239-
return new Rectangle.OfFloat(scaledX, scaledY, scaledWidth, scaledHeight);
240-
}
241183
242-
/**
243184
* Auto-scale ImageData to device zoom that are at given zoom factor.
244185
*/
245186
public static ImageData autoScaleImageData (Device device, final ImageData imageData, int imageDataZoomFactor) {
@@ -248,81 +189,17 @@ public static ImageData autoScaleImageData (Device device, final ImageData image
248189
return autoScaleImageData(device, imageData, scaleFactor);
249190
}
250191

251-
public static int[] scaleUp(int[] pointArray, int zoom) {
252-
if (zoom == 100 || pointArray == null) return pointArray;
253-
float scaleFactor = getScalingFactor(zoom);
254-
int[] returnArray = new int[pointArray.length];
255-
for (int i = 0; i < pointArray.length; i++) {
256-
returnArray [i] = Math.round (pointArray [i] * scaleFactor);
257-
}
258-
return returnArray;
259-
}
260-
261-
public static int[] scaleUp(Drawable drawable, int[] pointArray, int zoom) {
262-
if (drawable != null && !drawable.isAutoScalable()) return pointArray;
263-
return scaleUp (pointArray, zoom);
264-
}
265-
266-
/**
267-
* Auto-scale up int dimensions to match the given zoom level
268-
*/
269-
public static int scaleUp(int size, int zoom) {
270-
if (zoom == 100 || size == SWT.DEFAULT) return size;
271-
float scaleFactor = getScalingFactor(zoom);
272-
return Math.round (size * scaleFactor);
273-
}
274-
275-
public static int scaleUp(Drawable drawable, int size, int zoom) {
276-
if (drawable != null && !drawable.isAutoScalable()) return size;
277-
return scaleUp (size, zoom);
278-
}
279-
280-
public static float scaleUp(float size, int zoom) {
281-
if (zoom == 100 || size == SWT.DEFAULT) return size;
282-
float scaleFactor = getScalingFactor(zoom);
283-
return (size * scaleFactor);
284-
}
285-
286-
public static float scaleUp(Drawable drawable, float size, int zoom) {
287-
if (drawable != null && !drawable.isAutoScalable()) return size;
288-
return scaleUp (size, zoom);
289-
}
290-
291-
public static Point scaleUp(Point point, int zoom) {
292-
if (zoom == 100 || point == null) return point;
293-
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point);
294-
float scaleFactor = getScalingFactor(zoom);
295-
float scaledX = fPoint.getX() * scaleFactor;
296-
float scaledY = fPoint.getY() * scaleFactor;
297-
return new Point.OfFloat(scaledX, scaledY);
298-
}
299-
300-
public static Point scaleUp(Drawable drawable, Point point, int zoom) {
301-
if (drawable != null && !drawable.isAutoScalable()) return point;
302-
return scaleUp (point, zoom);
303-
}
304-
305-
public static Rectangle scaleUp(Rectangle rect, int zoom) {
306-
return scaleBounds(rect, zoom, 100);
307-
}
308-
309-
public static Rectangle scaleUp(Drawable drawable, Rectangle rect, int zoom) {
310-
if (drawable != null && !drawable.isAutoScalable()) return rect;
311-
return scaleUp (rect, zoom);
312-
}
313192

314193
/**
315194
* Returns scaling factor from the given device zoom
316195
* @return float scaling factor
317196
*/
318-
private static float getScalingFactor(int zoom) {
197+
public static float getScalingFactor(int zoom) {
319198
return getScalingFactor(zoom, 100);
320199
}
321200

322-
private static float getScalingFactor(int targetZoom, int currentZoom) {
323-
if (USE_CAIRO_AUTOSCALE) {
324-
return 1;
325-
}
201+
202+
public static float getScalingFactor(int targetZoom, int currentZoom) {
326203
if (targetZoom <= 0) {
327204
targetZoom = deviceZoom;
328205
}
@@ -562,37 +439,4 @@ private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
562439
return false;
563440
}
564441

565-
/**
566-
* AutoScale ImageDataProvider.
567-
*/
568-
public static final class AutoScaleImageDataProvider implements ImageDataProvider {
569-
Device device;
570-
ImageData imageData;
571-
int currentZoom;
572-
public AutoScaleImageDataProvider(Device device, ImageData data, int zoom){
573-
this.device = device;
574-
this.imageData = data;
575-
this.currentZoom = zoom;
576-
}
577-
@Override
578-
public ImageData getImageData(int zoom) {
579-
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
580-
}
581-
}
582-
583-
private class FloatAwareGeometryFactory {
584-
static Rectangle.OfFloat createFrom(Rectangle rectangle) {
585-
if (rectangle instanceof Rectangle.OfFloat) {
586-
return (Rectangle.OfFloat) rectangle;
587-
}
588-
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
589-
}
590-
591-
static Point.OfFloat createFrom(Point point) {
592-
if (point instanceof Point.OfFloat) {
593-
return (Point.OfFloat) point;
594-
}
595-
return new Point.OfFloat(point.x, point.y);
596-
}
597-
}
598442
}

0 commit comments

Comments
 (0)