Skip to content

Commit 458b970

Browse files
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.
1 parent 93a484e commit 458b970

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ LRESULT CDDS_ITEMPOSTPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
749749
}
750750
}
751751
}
752-
rect.left += INSET - 1;
752+
rect.left += Win32DPIUtils.pointToPixel(INSET - 1, zoom) ;
753753
if (drawImage) {
754754
Image image = null;
755755
if (index == 0) {
@@ -758,8 +758,8 @@ LRESULT CDDS_ITEMPOSTPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
758758
Image [] images = item.images;
759759
if (images != null) image = images [index];
760760
}
761-
int inset = i != 0 ? INSET : 0;
762-
int offset = i != 0 ? INSET : INSET + 2;
761+
int inset = i != 0 ? Win32DPIUtils.pointToPixel(INSET, zoom) : 0;
762+
int offset = i != 0 ? Win32DPIUtils.pointToPixel(INSET, zoom) : Win32DPIUtils.pointToPixel(INSET + 2, zoom);
763763
if (image != null) {
764764
Rectangle bounds = image.getBounds (); // Points
765765
if (size == null) size = Win32DPIUtils.pixelToPoint (getImageSize (), zoom); // To Points
@@ -1114,7 +1114,7 @@ LRESULT CDDS_ITEMPREPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
11141114
} else {
11151115
RECT textRect = item.getBounds (index, true, false, false, false, true, hDC);
11161116
if (measureEvent != null) {
1117-
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width);
1117+
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
11181118
}
11191119
fillBackground (hDC, clrTextBk, textRect);
11201120
}
@@ -1133,7 +1133,7 @@ LRESULT CDDS_ITEMPREPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
11331133
RECT pRect = item.getBounds (index, true, true, false, false, false, hDC);
11341134
RECT pClipRect = item.getBounds (index, true, true, true, false, true, hDC);
11351135
if (measureEvent != null) {
1136-
pRect.right = Math.min (pClipRect.right, boundsInPixels.x + boundsInPixels.width);
1136+
pRect.right = Math.min (pClipRect.right, boundsInPixels.x + boundsInPixels.width) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
11371137
} else {
11381138
pRect.right += EXPLORER_EXTRA;
11391139
pClipRect.right += EXPLORER_EXTRA;
@@ -1163,7 +1163,7 @@ LRESULT CDDS_ITEMPREPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
11631163
} else {
11641164
RECT textRect = item.getBounds (index, true, false, false, false, true, hDC);
11651165
if (measureEvent != null) {
1166-
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width);
1166+
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
11671167
}
11681168
fillBackground (hDC, OS.GetBkColor (hDC), textRect);
11691169
}
@@ -1194,7 +1194,7 @@ LRESULT CDDS_ITEMPREPAINT (NMTVCUSTOMDRAW nmcd, long wParam, long lParam) {
11941194
if (focused && !ignoreDrawFocus && (style & SWT.FULL_SELECTION) == 0) {
11951195
RECT textRect = item.getBounds (index, true, explorerTheme, false, false, true, hDC);
11961196
if (measureEvent != null) {
1197-
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width);
1197+
textRect.right = Math.min (cellRect.right, boundsInPixels.x + boundsInPixels.width) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
11981198
}
11991199
nmcd.uItemState &= ~OS.CDIS_FOCUS;
12001200
OS.MoveMemory (lParam, nmcd, NMTVCUSTOMDRAW.sizeof);
@@ -5484,7 +5484,7 @@ public void showColumn (TreeColumn column) {
54845484
SCROLLINFO info = new SCROLLINFO();
54855485
info.cbSize = SCROLLINFO.sizeof;
54865486
info.fMask = OS.SIF_POS;
5487-
info.nPos = Math.max(0, headerRect.left - Tree.INSET / 2);
5487+
info.nPos = Math.max(0, headerRect.left - Win32DPIUtils.pointToPixel(INSET / 2, getZoom()));
54885488
OS.SetScrollInfo(hwndParent, OS.SB_HORZ, info, true);
54895489
setScrollWidth();
54905490
} else if (scrollBecauseRight) {
@@ -5498,8 +5498,8 @@ public void showColumn (TreeColumn column) {
54985498
// info.nPos + wideRect = headerRect.left + wideHeader
54995499
// info.nPos = headerRect.left + wideHeader - wideRect
55005500
info.nPos = Math.max(0, wideHeader + headerRect.left - wideRect
5501-
- Tree.INSET / 2);
5502-
info.nPos = Math.min(rect.right - Tree.INSET / 2, info.nPos);
5501+
- Win32DPIUtils.pointToPixel(INSET / 2, getZoom()) );
5502+
info.nPos = Math.min(rect.right - Win32DPIUtils.pointToPixel(INSET / 2, getZoom()), info.nPos);
55035503

55045504
OS.SetScrollInfo(hwndParent, OS.SB_HORZ, info, true);
55055505
setScrollWidth();
@@ -7925,7 +7925,7 @@ LRESULT wmNotifyHeader (NMHDR hdr, long wParam, long lParam) {
79257925
}
79267926
}
79277927

7928-
int x = rects[i].left + INSET + 2;
7928+
int x = rects[i].left + Win32DPIUtils.pointToPixel(INSET + 2, getZoom());
79297929
if (columns[i].image != null) {
79307930
GCData data = new GCData();
79317931
data.device = display;
@@ -8243,7 +8243,7 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) {
82438243
data.background = OS.GetBkColor (nmcd.hdc);
82448244
data.font = Font.win32_new (display, hFont);
82458245
GC gc = createNewGC(nmcd.hdc, data);
8246-
int x = cellRect [0].left + INSET;
8246+
int x = cellRect [0].left + Win32DPIUtils.pointToPixel(INSET, getZoom());
82478247
if (index [0] != 0) x -= gridWidth;
82488248
Image image = item [0].getImage (index [0]);
82498249
if (image != null || index [0] == 0) {
@@ -8254,11 +8254,11 @@ LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) {
82548254
Rectangle rect = image.getBounds (); // Points
82558255
int zoom = getZoom();
82568256
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));
8257-
x += INSET + (index [0] == 0 ? 1 : 0);
8257+
x += Win32DPIUtils.pointToPixel(INSET, getZoom()) + (index [0] == 0 ? 1 : 0);
82588258
}
82598259
x += size.x;
82608260
} else {
8261-
x += INSET;
8261+
x += Win32DPIUtils.pointToPixel(INSET, getZoom());
82628262
}
82638263
String string = item [0].getText (index [0]);
82648264
if (string != null) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText,
444444
if (getImage && !fullImage) {
445445
if (OS.SendMessage (hwnd, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0) != 0) {
446446
Point size = parent.getImageSize ();
447-
rect.left -= size.x + Tree.INSET;
447+
rect.left -= size.x + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
448448
if (!getText) rect.right = rect.left + size.x;
449449
} else {
450450
if (!getText) rect.right = rect.left;
@@ -496,7 +496,7 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText,
496496
}
497497
if (getText) {
498498
if (fullText && clip) {
499-
rect.left = rect.right + Tree.INSET;
499+
rect.left = rect.right + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
500500
rect.right = headerRect.right;
501501
} else {
502502
String string = index == 0 ? text : strings != null ? strings [index] : null;
@@ -517,10 +517,10 @@ RECT getBounds (int index, boolean getText, boolean getImage, boolean fullText,
517517
OS.ReleaseDC (hwnd, hNewDC);
518518
}
519519
if (getImage) {
520-
rect.right += textRect.right - textRect.left + Tree.INSET * 3;
520+
rect.right += textRect.right - textRect.left + Win32DPIUtils.pointToPixel(Tree.INSET * 3, getZoom());
521521
} else {
522-
rect.left = rect.right + Tree.INSET;
523-
rect.right = rect.left + (textRect.right - textRect.left) + Tree.INSET;
522+
rect.left = rect.right + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
523+
rect.right = rect.left + (textRect.right - textRect.left) + Win32DPIUtils.pointToPixel(Tree.INSET, getZoom());
524524
}
525525
}
526526
}
@@ -918,9 +918,9 @@ public Rectangle getTextBounds (int index) {
918918
Rectangle getTextBoundsInPixels (int index) {
919919
if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
920920
RECT rect = getBounds (index, true, false, true);
921-
if (index == 0) rect.left += Tree.INSET - 1;
921+
if (index == 0) rect.left += Win32DPIUtils.pointToPixel(Tree.INSET - 1, getZoom());
922922
rect.left = Math.min (rect.left, rect.right);
923-
rect.right = rect.right - Tree.INSET + 1; // Add 1 px margin to avoid truncation of text seen with "Segoe UI" font
923+
rect.right = rect.right - Win32DPIUtils.pointToPixel(Tree.INSET + 1, getZoom()); // Add 1 px margin to avoid truncation of text seen with "Segoe UI" font
924924
int width = Math.max (0, rect.right - rect.left);
925925
int height = Math.max (0, rect.bottom - rect.top);
926926
return new Rectangle (rect.left, rect.top, width, height);

0 commit comments

Comments
 (0)