From 16740266240403ddb93066d005f81839045378ed Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Mon, 18 Aug 2025 15:45:23 +0200 Subject: [PATCH] Scale Tree.INSET by zoom level instead of using fixed pixels The Tree.INSET constant defines the spacing between an item's image and text. Previously, this was a fixed value of 3px, which looks correct at 100% zoom but causes the image and text to appear cramped on higher-DPI displays. This change defines INSET in points and converts it to pixels based on the current zoom level (e.g. 6px at 200%), ensuring consistent spacing across different monitor scales. --- .../win32/org/eclipse/swt/widgets/Tree.java | 20 +++++++++---------- .../org/eclipse/swt/widgets/TreeItem.java | 13 ++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java index 73f45dffa13..90bdbadc39c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java @@ -750,7 +750,7 @@ LRESULT CDDS_ITEMPOSTPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) { } } } - rect.left += INSET - 1; + rect.left += Win32DPIUtils.pointToPixel(INSET - 1, zoom) ; if (drawImage) { Image image = null; if (index == 0) { @@ -759,8 +759,8 @@ LRESULT CDDS_ITEMPOSTPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) { Image [] images = item.images; if (images != null) image = images [index]; } - int inset = i != 0 ? INSET : 0; - int offset = i != 0 ? INSET : INSET + 2; + int inset = i != 0 ? Win32DPIUtils.pointToPixel(INSET, zoom) : 0; + int offset = i != 0 ? Win32DPIUtils.pointToPixel(INSET, zoom) : Win32DPIUtils.pointToPixel(INSET + 2, zoom); if (image != null) { Rectangle bounds = image.getBounds (); // Points if (size == null) size = Win32DPIUtils.pixelToPoint (getImageSize (), zoom); // To Points @@ -5488,7 +5488,7 @@ public void showColumn (TreeColumn column) { SCROLLINFO info = new SCROLLINFO(); info.cbSize = SCROLLINFO.sizeof; info.fMask = OS.SIF_POS; - info.nPos = Math.max(0, headerRect.left - Tree.INSET / 2); + info.nPos = Math.max(0, headerRect.left - Win32DPIUtils.pointToPixel(INSET / 2, getZoom())); OS.SetScrollInfo(hwndParent, OS.SB_HORZ, info, true); setScrollWidth(); } else if (scrollBecauseRight) { @@ -5502,8 +5502,8 @@ public void showColumn (TreeColumn column) { // info.nPos + wideRect = headerRect.left + wideHeader // info.nPos = headerRect.left + wideHeader - wideRect info.nPos = Math.max(0, wideHeader + headerRect.left - wideRect - - Tree.INSET / 2); - info.nPos = Math.min(rect.right - Tree.INSET / 2, info.nPos); + - Win32DPIUtils.pointToPixel(INSET / 2, getZoom()) ); + info.nPos = Math.min(rect.right - Win32DPIUtils.pointToPixel(INSET / 2, getZoom()), info.nPos); OS.SetScrollInfo(hwndParent, OS.SB_HORZ, info, true); setScrollWidth(); @@ -7930,7 +7930,7 @@ LRESULT wmNotifyHeader (NMHDR hdr, long wParam, long lParam) { } } - int x = rects[i].left + INSET + 2; + int x = rects[i].left + Win32DPIUtils.pointToPixel(INSET + 2, getZoom()); if (columns[i].image != null) { GCData data = new GCData(); data.device = display; @@ -8248,7 +8248,7 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) { data.background = OS.GetBkColor (nmcd.hdc); data.font = Font.win32_new (display, hFont); GC gc = createNewGC(nmcd.hdc, data); - int x = cellRect [0].left + INSET; + int x = cellRect [0].left + Win32DPIUtils.pointToPixel(INSET, getZoom()); if (index [0] != 0) x -= gridWidth; Image image = item [0].getImage (index [0]); if (image != null || index [0] == 0) { @@ -8259,11 +8259,11 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) { Rectangle rect = image.getBounds (); // Points int zoom = getZoom(); gc.drawImage (image, rect.x, rect.y, rect.width, rect.height, DPIUtil.pixelToPoint(x, zoom), DPIUtil.pixelToPoint(imageRect.top, zoom), DPIUtil.pixelToPoint(size.x, zoom), DPIUtil.pixelToPoint(size.y, zoom)); - x += INSET + (index [0] == 0 ? 1 : 0); + x += Win32DPIUtils.pointToPixel(INSET, getZoom()) + (index [0] == 0 ? 1 : 0); } x += size.x; } else { - x += INSET; + x += Win32DPIUtils.pointToPixel(INSET, getZoom()); } String string = item [0].getText (index [0]); if (string != null) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java index 426c9ee35ac..0d120169752 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java @@ -444,7 +444,7 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText, if (getImage && !fullImage) { if (OS.SendMessage (hwnd, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0) != 0) { Point size = parent.getImageSize (); - rect.left -= size.x + Tree.INSET; + rect.left -= size.x + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom()); if (!getText) rect.right = rect.left + size.x; } else { if (!getText) rect.right = rect.left; @@ -496,7 +496,7 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText, } if (getText) { if (fullText && clip) { - rect.left = rect.right + Tree.INSET; + rect.left = rect.right + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom()); rect.right = headerRect.right; } else { String string = index == 0 ? text : strings != null ? strings [index] : null; @@ -517,10 +517,10 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText, OS.ReleaseDC (hwnd, hNewDC); } if (getImage) { - rect.right += textRect.right - textRect.left + Tree.INSET * 3; + rect.right += textRect.right - textRect.left + Win32DPIUtils.pointToPixel(Tree.INSET * 3, getZoom()); } else { - rect.left = rect.right + Tree.INSET; - rect.right = rect.left + (textRect.right - textRect.left) + Tree.INSET; + rect.left = rect.right + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom()); + rect.right = rect.left + (textRect.right - textRect.left) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom()); } } } @@ -918,9 +918,8 @@ public Rectangle getTextBounds (int index) { Rectangle getTextBoundsInPixels (int index) { if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED); RECT rect = getBounds (index, true, false, true); - if (index == 0) rect.left += Tree.INSET - 1; rect.left = Math.min (rect.left, rect.right); - rect.right = rect.right - Tree.INSET + 1; // Add 1 px margin to avoid truncation of text seen with "Segoe UI" font + rect.right = rect.right + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom()); // Add INSET margin to avoid truncation of text seen with "Segoe UI" font int width = Math.max (0, rect.right - rect.left); int height = Math.max (0, rect.bottom - rect.top); return new Rectangle (rect.left, rect.top, width, height);