Skip to content

Commit c6a29a9

Browse files
amartya4256HeikoKlare
authored andcommitted
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 f858239 commit c6a29a9

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-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: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package org.eclipse.swt.widgets;
1515

1616

17+
import java.util.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
1921
import org.eclipse.swt.internal.*;
@@ -130,14 +132,24 @@ Rectangle getBoundsInPixels () {
130132
return new Rectangle (getXInPixels(), getYInPixels(), rect.width, rect.height);
131133
}
132134
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());
135+
OptionalInt widthInPixels = getSystemCaretWidthInPixelsForCurrentMonitor();
136+
if (widthInPixels.isPresent()) {
137+
return new Rectangle (getXInPixels(), getYInPixels(), widthInPixels.getAsInt(), getHeightInPixels());
136138
}
137139
}
138140
return new Rectangle (getXInPixels(), getYInPixels(), getWidthInPixels(), getHeightInPixels());
139141
}
140142

143+
private OptionalInt getSystemCaretWidthInPixelsForCurrentMonitor() {
144+
int [] buffer = new int [1];
145+
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
146+
int width = DPIUtil.pixelToPoint(buffer [0], Win32DPIUtils.getPrimaryMonitorZoomAtStartup());
147+
int widthInPixels = Win32DPIUtils.pointToPixel(width, getNativeZoom());
148+
return OptionalInt.of(widthInPixels);
149+
}
150+
return OptionalInt.empty();
151+
}
152+
141153
/**
142154
* Returns the font that the receiver will use to paint textual information.
143155
*
@@ -224,9 +236,9 @@ Point getSizeInPixels () {
224236
return new Point (rect.width, rect.height);
225237
}
226238
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());
239+
OptionalInt widthInPixels = getSystemCaretWidthInPixelsForCurrentMonitor();
240+
if (widthInPixels.isPresent()) {
241+
return new Point (widthInPixels.getAsInt(), getHeightInPixels());
230242
}
231243
}
232244
return new Point (getWidthInPixels(), getHeightInPixels());
@@ -368,9 +380,9 @@ void resize () {
368380
long hBitmap = image != null ? Image.win32_getHandle(image, getZoom()) : 0;
369381
int widthInPixels = this.getWidthInPixels();
370382
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];
383+
OptionalInt systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
384+
if (systemCaretWidthInPixelsForCurrentMonitor.isPresent()) {
385+
widthInPixels = systemCaretWidthInPixelsForCurrentMonitor.getAsInt();
374386
}
375387
}
376388
OS.CreateCaret (hwnd, hBitmap, widthInPixels, getHeightInPixels());
@@ -447,9 +459,9 @@ void setFocus () {
447459
if (image != null) hBitmap = Image.win32_getHandle(image, getZoom());
448460
int widthInPixels = this.getWidthInPixels();
449461
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];
462+
OptionalInt systemCaretWidthInPixelsForCurrentMonitor = getSystemCaretWidthInPixelsForCurrentMonitor();
463+
if (systemCaretWidthInPixelsForCurrentMonitor.isPresent()) {
464+
widthInPixels = systemCaretWidthInPixelsForCurrentMonitor.getAsInt();
453465
}
454466
}
455467
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)