Correct (based on menu item size) transparent icon for spacing

This commit is contained in:
nathan 2017-07-01 16:16:15 +02:00
parent 4c92097ff6
commit f64feecaed

View File

@ -33,12 +33,12 @@ import dorkbox.util.SwingUtil;
class SwingMenuItem implements MenuItemPeer { class SwingMenuItem implements MenuItemPeer {
// necessary to have the correct scaling + padding for the menu entries. // necessary to have the correct scaling + padding for the menu entries.
private static ImageIcon transparentIcon; protected static ImageIcon transparentIcon;
private final SwingMenu parent; protected final SwingMenu parent;
private final JMenuItem _native = new JMenuItem(); protected final JMenuItem _native = new JMenuItem();
private volatile ActionListener swingCallback; protected volatile ActionListener callback;
// this is ALWAYS called on the EDT. // this is ALWAYS called on the EDT.
@ -53,8 +53,23 @@ class SwingMenuItem implements MenuItemPeer {
parent._native.add(_native); parent._native.add(_native);
if (transparentIcon == null) { if (transparentIcon == null) {
File uncheckedFile = ImageResizeUtil.getTransparentImage(); try {
transparentIcon = new ImageIcon(uncheckedFile.getAbsolutePath()); JMenuItem jMenuItem = new JMenuItem();
// do the same modifications that would also happen (if specified) for the actual displayed menu items
if (SystemTray.SWING_UI != null) {
jMenuItem.setUI(SystemTray.SWING_UI.getItemUI(jMenuItem, null));
}
// this is the largest size of an image used in a JMenuItem, before the size of the JMenuItem is forced to be larger
int menuImageSize = SystemTray.get()
.getMenuImageSize();
transparentIcon = new ImageIcon(ImageResizeUtil.getTransparentImage(menuImageSize)
.getAbsolutePath());
} catch (Exception e) {
SystemTray.logger.error("Error creating transparent image.", e);
}
} }
_native.setIcon(transparentIcon); _native.setIcon(transparentIcon);
@ -107,12 +122,12 @@ class SwingMenuItem implements MenuItemPeer {
@Override @Override
public public
void setCallback(final MenuItem menuItem) { void setCallback(final MenuItem menuItem) {
if (swingCallback != null) { if (callback != null) {
_native.removeActionListener(swingCallback); _native.removeActionListener(callback);
} }
if (menuItem.getCallback() != null) { if (menuItem.getCallback() != null) {
swingCallback = new ActionListener() { callback = new ActionListener() {
@Override @Override
public public
void actionPerformed(ActionEvent e) { void actionPerformed(ActionEvent e) {
@ -128,10 +143,10 @@ class SwingMenuItem implements MenuItemPeer {
} }
}; };
_native.addActionListener(swingCallback); _native.addActionListener(callback);
} }
else { else {
swingCallback = null; callback = null;
} }
} }
@ -159,9 +174,9 @@ class SwingMenuItem implements MenuItemPeer {
@Override @Override
public public
void run() { void run() {
if (swingCallback != null) { if (callback != null) {
_native.removeActionListener(swingCallback); _native.removeActionListener(callback);
swingCallback = null; callback = null;
} }
parent._native.remove(_native); parent._native.remove(_native);
_native.removeAll(); _native.removeAll();