Skip to content

[Win] Images in MenuItems are scaled incorrectly when autoscaling is disabled #2537

@tmssngr

Description

@tmssngr

Describe the bug
With the release of SWT R4_37 the Windows zoom level causes (bitmap) images to be upscaled though swt.autoScale had been set to false.

To Reproduce

  • run this snippet on a Windows with a monitor scaling set to >100% (modify the path to a small image):
import java.io.*;
import java.nio.file.*;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class MenuItemImageTest {

	public static void main(String[] args) throws IOException {
		System.setProperty("swt.autoScale", "false");

		final Display display = new Display();
		try {
			final ImageData imageData;
			try (InputStream inputStream = Files.newInputStream(Paths.get("src/main/resources/images/smartgit/open-submodule-2.png"))) {
				imageData = new ImageData(inputStream);
			}
			final Image image = new Image(display, imageData);

			final Shell shell = new Shell(display);

			final Menu menu = new Menu(shell, SWT.POP_UP);
			shell.setMenu(menu);
			createMenuItem(SWT.PUSH, "Push", image, menu);
			createMenuItem(SWT.CHECK, "Unselected Check", image, menu);
			createMenuItem(SWT.CHECK, "Slected Check", image, menu).setSelection(true);

			shell.addListener(SWT.Paint, event -> event.gc.drawImage(image, 0, 0));
			shell.setSize(500, 400);
			shell.open();

			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		}
		finally {
			display.dispose();
		}
	}

	private static MenuItem createMenuItem(int style, String text, Image image, Menu menu) {
		final MenuItem menuItem = new MenuItem(menu, style);
		menuItem.setText(text);
		menuItem.setImage(image);
		return menuItem;
	}
}
  • right-click to open a context menu

=> Results:

  1. the painted image is not scaled (correct)
  2. the image of the SWT.PUSH MenuItem is scaled incorrectly
  3. the image of the unchecked SWT.CHECK MenuItem is scaled incorrectly
  4. the image of the selected SWT.CHECK MenuItem is not scaled
  5. a checkmark is drawn onto the image of the SWT.CHECK MenuItem (incorrect)

2)-5) are regressions

Expected behavior

  • if swt.autoScale is set to true, scaling must not happen at all
  • no checkmark must be drawn onto an image (we solve it in our applications by using different images)

Screenshots
Windows 11 with 150% zoom.
Image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)

  2. JRE/JDK version

Version since
R4_37, still reproducible with the latest master build.

Workaround (or) Additional context
Workaround: Use SWT R4_36.

Metadata

Metadata

Assignees

No one assigned

    Labels

    WindowsHappens on Windows OSregressionSomething that used to work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions