From 164c0f4224e750046558340cbf31caa2d1d0bb8e Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Wed, 22 Jan 2025 15:02:07 +0100 Subject: [PATCH] Calculate chevron size in CTabFolderRenderer without Device#getDPI() The chevron font size is currently determined using Device#getDPI(). This value is static across the application lifecyle, always returns the DPI for the primary monitor at application startup. The font does thus not adapt properly to other zoom values. This change replaces the logic to calculate the chevron font size to use a scaled version of the font used for CTabItem. It also adds a listener for the zoom change event to trigger redraw. --- .../common/org/eclipse/swt/custom/CTabFolder.java | 9 ++++++++- .../org/eclipse/swt/custom/CTabFolderRenderer.java | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java index 7e340642da0..2843e253899 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java @@ -341,6 +341,7 @@ void init(int style) { case SWT.Selection: onSelection(event); break; case SWT.Activate: onActivate(event); break; case SWT.Deactivate: onDeactivate(event); break; + case SWT.ZoomChanged: onZoomChange(event); break; } }; @@ -362,7 +363,8 @@ void init(int style) { SWT.Resize, SWT.Traverse, SWT.Activate, - SWT.Deactivate + SWT.Deactivate, + SWT.ZoomChanged }; for (int folderEvent : folderEvents) { addListener(folderEvent, listener); @@ -370,6 +372,11 @@ void init(int style) { initAccessible(); } + +private void onZoomChange(Event event) { + update(); +} + void onDeactivate(Event event) { if (!highlightEnabled) { return; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java index 3e2c18a1343..9edf33fbf84 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java @@ -102,7 +102,8 @@ public class CTabFolderRenderer { static final int FLAGS = SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER; static final String ELLIPSIS = "..."; //$NON-NLS-1$ private static final String CHEVRON_ELLIPSIS = "99+"; //$NON-NLS-1$ - private static final int CHEVRON_FONT_HEIGHT = 10; + private static final float CHEVRON_FONT_SIZE_FACTOR = 0.7f; + private static final int CHEVRON_BOTTOM_INDENT = 4; //Part constants /** @@ -926,7 +927,7 @@ void drawChevron(GC gc, Rectangle chevronRect, int chevronImageState) { Display display = parent.getDisplay(); Font font = getChevronFont(display); int fontHeight = font.getFontData()[0].getHeight(); - int indent = Math.max(2, (chevronRect.height - fontHeight - 4) /2); + int indent = Math.max(2, (chevronRect.height - fontHeight - CHEVRON_BOTTOM_INDENT) /2); int x = chevronRect.x + 2; int y = chevronRect.y + indent; int count = parent.getChevronCount(); @@ -1696,9 +1697,8 @@ Color getFillColor() { private Font getChevronFont(Display display) { if (chevronFont == null) { - Point dpi = display.getDPI(); - int fontHeight = 72 * CHEVRON_FONT_HEIGHT / dpi.y; FontData fd = parent.getFont().getFontData()[0]; + int fontHeight = (int) (fd.height * CHEVRON_FONT_SIZE_FACTOR); fd.setHeight(fontHeight); chevronFont = new Font(display, fd); }