Skip to content

Commit bb94992

Browse files
committed
Scale cursor width by monitor specific zoom
This commit adapts the scaling of Cursor width with respect to the monitor zoom since the OS call OS.SystemParametersInfo returns the caret width for the primary zoom at startup.
1 parent fdbb769 commit bb94992

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private ImageData getImageDataUsingExtension(int zoom) {
417417
if (extension != null) {
418418
// OS.SHGetFileInfo is System DPI-aware, hence it retrieves the icon with zoom
419419
// of primary monitor at the application startup
420-
int initialNativeZoom = getPrimaryMonitorZoomAtStartup();
420+
int initialNativeZoom = Win32DPIUtils.getPrimaryMonitorZoomAtStartup();
421421
SHFILEINFO shfi = new SHFILEINFO ();
422422
int flags = OS.SHGFI_ICON | OS.SHGFI_USEFILEATTRIBUTES;
423423
boolean useLargeIcon = 100 * zoom / initialNativeZoom >= 200;
@@ -439,13 +439,6 @@ private ImageData getImageDataUsingExtension(int zoom) {
439439
return null;
440440
}
441441

442-
private int getPrimaryMonitorZoomAtStartup() {
443-
long hDC = OS.GetDC(0);
444-
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSX);
445-
OS.ReleaseDC(0, hDC);
446-
return DPIUtil.mapDPIToZoom(dpi);
447-
}
448-
449442
/**
450443
* Returns the receiver's name. This is as short and
451444
* descriptive a name as possible for the program. If

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ public static boolean isMonitorSpecificScalingActive() {
306306
return updateOnRuntimeValue;
307307
}
308308

309+
public static int getPrimaryMonitorZoomAtStartup() {
310+
long hDC = OS.GetDC(0);
311+
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSX);
312+
OS.ReleaseDC(0, hDC);
313+
return DPIUtil.mapDPIToZoom(dpi);
314+
}
315+
309316
/**
310317
* AutoScale ImageDataProvider.
311318
*/

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,24 @@ Rectangle getBoundsInPixels () {
130130
return new Rectangle (getXInPixels(), getYInPixels(), rect.width, rect.height);
131131
}
132132
if (width == 0) {
133-
int [] buffer = new int [1];
134-
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
135-
return new Rectangle (getXInPixels(), getYInPixels(), buffer [0], getHeightInPixels());
133+
int widthInPixels = getSystemCaretWidthInPixelsForCurrentMonitor();
134+
if (widthInPixels != 0) {
135+
return new Rectangle (getXInPixels(), getYInPixels(), widthInPixels, getHeightInPixels());
136136
}
137137
}
138138
return new Rectangle (getXInPixels(), getYInPixels(), getWidthInPixels(), getHeightInPixels());
139139
}
140140

141+
private int getSystemCaretWidthInPixelsForCurrentMonitor() {
142+
int [] buffer = new int [1];
143+
int widthInPixels = 0;
144+
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
145+
int width = DPIUtil.pixelToPoint(buffer [0], Win32DPIUtils.getPrimaryMonitorZoomAtStartup());
146+
widthInPixels = Win32DPIUtils.pointToPixel(width, getNativeZoom());
147+
}
148+
return widthInPixels;
149+
}
150+
141151
/**
142152
* Returns the font that the receiver will use to paint textual information.
143153
*
@@ -224,9 +234,9 @@ Point getSizeInPixels () {
224234
return new Point (rect.width, rect.height);
225235
}
226236
if (width == 0) {
227-
int [] buffer = new int [1];
228-
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
229-
return new Point (buffer [0], getHeightInPixels());
237+
int widthInPixels = getSystemCaretWidthInPixelsForCurrentMonitor();
238+
if (widthInPixels != 0) {
239+
return new Point (widthInPixels, getHeightInPixels());
230240
}
231241
}
232242
return new Point (getWidthInPixels(), getHeightInPixels());
@@ -368,9 +378,9 @@ void resize () {
368378
long hBitmap = image != null ? Image.win32_getHandle(image, getZoom()) : 0;
369379
int widthInPixels = this.getWidthInPixels();
370380
if (image == null && widthInPixels == 0) {
371-
int [] buffer = new int [1];
372-
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
373-
widthInPixels = buffer [0];
381+
int systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
382+
if (systemCaretWidthInPixelsForCurrentMonitor != 0) {
383+
widthInPixels = systemCaretWidthInPixelsForCurrentMonitor;
374384
}
375385
}
376386
OS.CreateCaret (hwnd, hBitmap, widthInPixels, getHeightInPixels());
@@ -447,9 +457,9 @@ void setFocus () {
447457
if (image != null) hBitmap = Image.win32_getHandle(image, getZoom());
448458
int widthInPixels = this.getWidthInPixels();
449459
if (image == null && widthInPixels == 0) {
450-
int [] buffer = new int [1];
451-
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
452-
widthInPixels = buffer [0];
460+
int systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
461+
if (systemCaretWidthInPixelsForCurrentMonitor != 0) {
462+
widthInPixels = systemCaretWidthInPixelsForCurrentMonitor;
453463
}
454464
}
455465
OS.CreateCaret (hwnd, hBitmap, widthInPixels, getHeightInPixels());

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ private long getMenuItemIconSelectedBitmapHandle() {
911911
}
912912

913913
private int adaptZoomForMenuItem(int currentZoom, Image image) {
914-
int primaryMonitorZoomAtAppStartUp = getPrimaryMonitorZoomAtStartup();
914+
int primaryMonitorZoomAtAppStartUp = Win32DPIUtils.getPrimaryMonitorZoomAtStartup();
915915
/*
916916
* Windows has inconsistent behavior when setting the size of MenuItem image and
917917
* hence we need to adjust the size of the images as per different kind of zoom
@@ -940,13 +940,6 @@ private static boolean isQuarterZoom(int zoom) {
940940
return zoom % 10 != 0 && zoom % 25 == 0;
941941
}
942942

943-
private static int getPrimaryMonitorZoomAtStartup() {
944-
long hDC = OS.GetDC(0);
945-
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSX);
946-
OS.ReleaseDC(0, hDC);
947-
return DPIUtil.mapDPIToZoom(dpi);
948-
}
949-
950943
/**
951944
* Sets the receiver's pull down menu to the argument.
952945
* Only <code>CASCADE</code> menu items can have a

0 commit comments

Comments
 (0)