Skip to content

Commit 65ce2e0

Browse files
Refactor: Move Cocoa-specific DPI methods to CocoaDPIUtil
The `autoScale` methods no longer have relevance in the Win32 implementation, so this commit extracts the macOS-specific DPI scaling logic into a dedicated `CocoaDPIUtil` class. This improves platform separation and maintains cleaner, OS-specific code boundaries.
1 parent e2cbda9 commit 65ce2e0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,11 +1822,11 @@ public String toString () {
18221822
* @noreference This method is not intended to be referenced by clients.
18231823
*/
18241824
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
1825-
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
1825+
gc.drawImage (original, 0, 0, CocoaDPIUtil.autoScaleDown (width), CocoaDPIUtil.autoScaleDown (height),
18261826
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
18271827
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
18281828
*/
1829-
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
1829+
0, 0, Math.round (CocoaDPIUtil.autoScaleDown (width * scaleFactor)), Math.round (CocoaDPIUtil.autoScaleDown (height * scaleFactor)));
18301830
}
18311831

18321832
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
* Daniel Kruegler - #420 - [High DPI] "swt.autoScale" should add new "half" option
14+
* Yatta Solutions - #131 - Additional methods to specify target zoom directly
15+
*******************************************************************************/
16+
package org.eclipse.swt.internal;
17+
18+
/**
19+
* This class hold common constants and utility functions w.r.t. to SWT high DPI
20+
* functionality.
21+
* <p>
22+
* The {@code autoScaleUp(..)} methods convert from API coordinates (in SWT
23+
* points) to internal high DPI coordinates (in pixels) that interface with
24+
* native widgets.
25+
* </p>
26+
* <p>
27+
* The {@code autoScaleDown(..)} convert from high DPI pixels to API coordinates
28+
* (in SWT points).
29+
* </p>
30+
*
31+
* @since 3.105
32+
*/
33+
public class CocoaDPIUtil {
34+
35+
/**
36+
* Auto-scale down int dimensions.
37+
*/
38+
public static int autoScaleDown(int size) {
39+
return DPIUtil.scaleDown(size, DPIUtil.getDeviceZoom());
40+
}
41+
42+
/**
43+
* Auto-scale down float dimensions.
44+
*/
45+
public static float autoScaleDown(float size) {
46+
return DPIUtil.scaleDown(size, DPIUtil.getDeviceZoom());
47+
}
48+
}

0 commit comments

Comments
 (0)