Skip to content

Commit 34bb179

Browse files
arunjose696akoch-yatta
authored andcommitted
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.
1 parent 0714402 commit 34bb179

1 file changed

Lines changed: 13 additions & 28 deletions

File tree

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.eclipse.swt.graphics.FontMetrics;
6464
import org.eclipse.swt.graphics.GC;
6565
import org.eclipse.swt.graphics.Image;
66+
import org.eclipse.swt.graphics.ImageGcDrawer;
6667
import org.eclipse.swt.graphics.RGB;
6768
import org.eclipse.swt.graphics.Rectangle;
6869
import org.eclipse.swt.layout.FillLayout;
@@ -333,10 +334,6 @@ private class PresentationLabelProvider extends LabelProvider implements IFontPr
333334

334335
private final HashMap<Color, Image> images = new HashMap<>();
335336

336-
private int imageSize = -1;
337-
338-
private int usableImageSize = -1;
339-
340337
private static final int REFRESH_INTERVAL_IN_MS = 300;
341338

342339
private final Runnable updateControlsAndRefreshTreeRunnable = () -> {
@@ -440,25 +437,20 @@ public Image getImage(Object element) {
440437
c = display.getSystemColor(SWT.COLOR_WHITE);
441438
foregroundColor = display.getSystemColor(DEFINITION_NOT_AVAIL_COLOR);
442439
}
440+
final Color gcBackgroundcolor = c;
441+
final Color gcForegroundColor = foregroundColor;
443442
Image image = images.get(c);
444443
if (image == null) {
445-
ensureImageSize();
446-
image = new Image(display, imageSize, imageSize);
447-
448-
GC gc = new GC(image);
449-
gc.setBackground(tree.getViewer().getControl().getBackground());
450-
gc.setForeground(tree.getViewer().getControl().getBackground());
451-
gc.drawRectangle(0, 0, imageSize - 1, imageSize - 1);
452-
453-
gc.setForeground(foregroundColor);
454-
gc.setBackground(c);
455-
456-
int offset = (imageSize - usableImageSize) / 2;
457-
gc.drawRectangle(offset, offset, usableImageSize - offset, usableImageSize - offset);
458-
gc.fillRectangle(offset + 1, offset + 1, usableImageSize - offset - 1,
459-
usableImageSize - offset - 1);
460-
gc.dispose();
461-
444+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
445+
gc.setForeground(tree.getViewer().getControl().getBackground());
446+
gc.drawRectangle(0, 0, imageWidth - 1, imageHeight - 1);
447+
448+
gc.setForeground(gcForegroundColor);
449+
gc.setBackground(gcBackgroundcolor);
450+
gc.fillRectangle(2, 2, 11, 11);
451+
gc.drawRectangle(2, 2, 11, 11);
452+
};
453+
image = new Image(display, imageGcDrawer, 16, 16);
462454
images.put(c, image);
463455
}
464456
return image;
@@ -470,13 +462,6 @@ public Image getImage(Object element) {
470462
}
471463
}
472464

473-
private void ensureImageSize() {
474-
if (imageSize == -1) {
475-
imageSize = tree.getViewer().getTree().getItemHeight();
476-
usableImageSize = Math.max(1, imageSize - 4);
477-
}
478-
}
479-
480465
@Override
481466
public String getText(Object element) {
482467
if (element instanceof IHierarchalThemeElementDefinition themeElement) {

0 commit comments

Comments
 (0)