Fixed issues with submenu's and our popup auto-hide tracker

This commit is contained in:
nathan 2016-09-29 13:07:58 +02:00
parent 05231587d2
commit 14ff3fae06
4 changed files with 71 additions and 21 deletions

View File

@ -22,6 +22,13 @@ import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
class AdjustedJMenu extends JMenu { class AdjustedJMenu extends JMenu {
private final SwingSystemTrayMenuPopup mainPopup;
public
AdjustedJMenu(final SwingSystemTrayMenuPopup mainPopup) {
this.mainPopup = mainPopup;
}
@Override @Override
public public
Insets getMargin() { Insets getMargin() {
@ -37,4 +44,20 @@ class AdjustedJMenu extends JMenu {
return margin; return margin;
} }
@Override
public
void setPopupMenuVisible(final boolean visible) {
mainPopup.track(getPopupMenu(), visible);
super.setPopupMenuVisible(visible);
}
@Override
public
void removeNotify() {
// have to make sure that when we are removed, we remove ourself from the tracker
mainPopup.track(getPopupMenu(), false);
super.removeNotify();
}
} }

View File

@ -56,7 +56,7 @@ class SwingMenu extends Menu implements MenuEntry {
public public
void run() { void run() {
if (parent != null) { if (parent != null) {
_native = new AdjustedJMenu(); _native = new AdjustedJMenu((SwingSystemTrayMenuPopup)((SwingMenu) systemTray.getMenu())._native);
((SwingMenu) parent)._native.add(_native); ((SwingMenu) parent)._native.add(_native);
} else { } else {
// when we are the system tray // when we are the system tray

View File

@ -54,9 +54,6 @@ class SwingSystemTray extends SwingMenu {
SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) { SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) {
super(systemTray, null); super(systemTray, null);
// have to reassign our type...
_native = new SwingSystemTrayMenuPopup();
ImageUtils.determineIconSize(dorkbox.systemTray.SystemTray.TYPE_SWING); ImageUtils.determineIconSize(dorkbox.systemTray.SystemTray.TYPE_SWING);
SwingUtil.invokeAndWait(new Runnable() { SwingUtil.invokeAndWait(new Runnable() {

View File

@ -20,6 +20,8 @@ import java.awt.MouseInfo;
import java.awt.Point; import java.awt.Point;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
@ -44,6 +46,8 @@ class SwingSystemTrayMenuPopup extends JPopupMenu {
protected volatile Point mouseClickLocation = null; protected volatile Point mouseClickLocation = null;
private final List<JPopupMenu> trackedMenus = new ArrayList<JPopupMenu>(4);
// protected boolean mouseStillOnMenu; // protected boolean mouseStillOnMenu;
// private JDialog hiddenDialog; // private JDialog hiddenDialog;
@ -53,10 +57,9 @@ class SwingSystemTrayMenuPopup extends JPopupMenu {
setFocusable(true); setFocusable(true);
// setBorder(new BorderUIResource.EmptyBorderUIResource(0, 0, 0, 0)); // borderUI resource border type will get changed! // setBorder(new BorderUIResource.EmptyBorderUIResource(0, 0, 0, 0)); // borderUI resource border type will get changed!
setBorder(new EmptyBorder(1, 1, 1, 1)); setBorder(new EmptyBorder(1, 1, 1, 1));
trackedMenus.add(this);
this.timer = new DelayTimer("PopupMenuHider", true, new Runnable() { this.timer = new DelayTimer("PopupMenuHider", true, new Runnable() {
@Override @Override
public public
void run() { void run() {
@ -64,31 +67,41 @@ class SwingSystemTrayMenuPopup extends JPopupMenu {
@Override @Override
public public
void run() { void run() {
Point location = MouseInfo.getPointerInfo().getLocation(); Point location = MouseInfo.getPointerInfo()
Point menuLocation = getLocationOnScreen(); .getLocation();
Dimension size = getSize();
// is the mouse pointer still inside of the popup? // are we inside one of our tracked menus (the root menu is included)
if (location.x >= menuLocation.x && location.x < menuLocation.x + size.width && synchronized (trackedMenus) {
location.y >= menuLocation.y && location.y < menuLocation.y + size.height) { for (JPopupMenu trackedMenu : trackedMenus) {
Point menuLocation = trackedMenu.getLocationOnScreen();
Dimension size = trackedMenu.getSize();
// restart the timer if (location.x >= menuLocation.x && location.x < menuLocation.x + size.width &&
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY); location.y >= menuLocation.y && location.y < menuLocation.y + size.height
) {
// restart the timer
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY);
return;
}
}
} }
// has the mouse pointer moved > delta pixels from it's original location (when the tray icon was clicked)? // has the mouse pointer moved > delta pixels from it's original location (when the tray icon was clicked)?
else if (mouseClickLocation != null && if (mouseClickLocation != null &&
location.x >= mouseClickLocation.x - MOVEMENT_DELTA && location.x < mouseClickLocation.x + MOVEMENT_DELTA && location.x >= mouseClickLocation.x - MOVEMENT_DELTA && location.x < mouseClickLocation.x + MOVEMENT_DELTA &&
location.y >= mouseClickLocation.y - MOVEMENT_DELTA && location.y < mouseClickLocation.y + MOVEMENT_DELTA) { location.y >= mouseClickLocation.y - MOVEMENT_DELTA && location.y < mouseClickLocation.y + MOVEMENT_DELTA
) {
// restart the timer // restart the timer
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY); SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY);
return;
} }
else {
// else, we hide it // else, we hide it
setVisible(false); setVisible(false);
}
} }
}); });
} }
@ -138,4 +151,21 @@ class SwingSystemTrayMenuPopup extends JPopupMenu {
// this.hiddenDialog.setVisible(makeVisible); // this.hiddenDialog.setVisible(makeVisible);
super.setVisible(makeVisible); super.setVisible(makeVisible);
} }
public
void track(final JPopupMenu menu, final boolean visible) {
if (visible) {
synchronized (trackedMenus) {
trackedMenus.add(menu);
}
} else {
synchronized (trackedMenus) {
trackedMenus.remove(menu);
}
}
// restart the timer
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY);
}
} }