SWT TrayItem.setImage在Mac状态栏中无法正确缩放

bli*_*mer 10 java macos swt statusbar

在我的跨平台SWT Java应用程序中,我使用TrayItem的setImages()函数来设置停靠栏和状态栏图标.该图标是128x128透明PNG.状态和托盘图标在Windows和Linux发行版上都有适当的剪辑,但在Mac上我遇到的问题是状态栏图标出现两边都有奇怪的填充,如下所示:

我很奇怪,除了Mac之外,它还适用于所有其他平台.例如,这是我的Linux机箱上没有问题的相同状态栏图标:

有没有人知道如何在Mac上防止这种额外的填充?

Sor*_*ror 5

我在SWT Cocoa来源中发现了这个问题.

public void setImage (Image image) {
    checkWidget ();
    if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
    super.setImage (image);
    double /*float*/ width = 0;
    if (image == null) {
        view.setImage (null);
    } else {
        /*
         * Feature in Cocoa.  If the NSImage object being set into the view is
         * the same NSImage object that is already there then the new image is
         * not taken.  This results in the view's image not changing even if the
         * NSImage object's content has changed since it was last set into the
         * view.  The workaround is to temporarily set the view's image to null
         * so that the new image will then be taken.
         */
        NSImage current = view.image ();
        if (current != null && current.id == image.handle.id) {
            view.setImage (null);
        }
        view.setImage (image.handle);
        if (visible) {
            width = image.handle.size ().width + BORDER;
        }
    }
    item.setLength (width);
}
Run Code Online (Sandbox Code Playgroud)

问题出在线上width = image.handle.size ().width + BORDER;,只需要纯粹的图像尺寸(在你的情况下它是128像素).我没有找到任何合适的解决方法(我看到你发布关于SWT bugzilla的bug报告).

因此,只有这样才能避免这个错误(目前)是让您的托盘图像更小.

  • 只是一个后续行动 - 如果您遇到同样的问题,他们已经在[错误报告](https://bugs.eclipse.org/bugs/show_bug.cgi?id=350751)上提交了补丁. (2认同)