From 14fdca3a283d6a17d05436535492c1edae847769 Mon Sep 17 00:00:00 2001 From: arunjose696 Date: Wed, 21 Jan 2026 12:06:59 +0100 Subject: [PATCH] Correct wrongly scaled images in colors/fonts preference page The colors/fonts preference page uses images for the items shown in a tree viewer that preview the selected color. The current implementation has two limitations: - It does not properly scale with the current monitor zoom - It uses a wrong image size based on the tree item height instead of the tree item image size, such that the rendered image is scaled and may loose information This change removes all custom extraction of the image size as the image simply needs to have the same size as all other images default to (16x16) to fit. It also wraps the implementation info an ImageGcDrawer to be monitor-scale-aware. --- .../themes/ColorsAndFontsPreferencePage.java | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java index ce48e8432231..dbc41e8bfd1a 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java @@ -63,6 +63,7 @@ import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; @@ -333,10 +334,6 @@ private class PresentationLabelProvider extends LabelProvider implements IFontPr private final HashMap images = new HashMap<>(); - private int imageSize = -1; - - private int usableImageSize = -1; - private static final int REFRESH_INTERVAL_IN_MS = 300; private final Runnable updateControlsAndRefreshTreeRunnable = () -> { @@ -440,25 +437,20 @@ public Image getImage(Object element) { c = display.getSystemColor(SWT.COLOR_WHITE); foregroundColor = display.getSystemColor(DEFINITION_NOT_AVAIL_COLOR); } + final Color gcBackgroundcolor = c; + final Color gcForegroundColor = foregroundColor; Image image = images.get(c); if (image == null) { - ensureImageSize(); - image = new Image(display, imageSize, imageSize); - - GC gc = new GC(image); - gc.setBackground(tree.getViewer().getControl().getBackground()); - gc.setForeground(tree.getViewer().getControl().getBackground()); - gc.drawRectangle(0, 0, imageSize - 1, imageSize - 1); - - gc.setForeground(foregroundColor); - gc.setBackground(c); - - int offset = (imageSize - usableImageSize) / 2; - gc.drawRectangle(offset, offset, usableImageSize - offset, usableImageSize - offset); - gc.fillRectangle(offset + 1, offset + 1, usableImageSize - offset - 1, - usableImageSize - offset - 1); - gc.dispose(); - + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground(tree.getViewer().getControl().getBackground()); + gc.drawRectangle(0, 0, imageWidth - 1, imageHeight - 1); + + gc.setForeground(gcForegroundColor); + gc.setBackground(gcBackgroundcolor); + gc.fillRectangle(2, 2, 11, 11); + gc.drawRectangle(2, 2, 11, 11); + }; + image = new Image(display, imageGcDrawer, 16, 16); images.put(c, image); } return image; @@ -470,13 +462,6 @@ public Image getImage(Object element) { } } - private void ensureImageSize() { - if (imageSize == -1) { - imageSize = tree.getViewer().getTree().getItemHeight(); - usableImageSize = Math.max(1, imageSize - 4); - } - } - @Override public String getText(Object element) { if (element instanceof IHierarchalThemeElementDefinition themeElement) {