2014-11-03 02:11:03 +01:00
|
|
|
package dorkbox.util.tray;
|
|
|
|
|
|
|
|
import dorkbox.util.DelayTimer;
|
2014-12-12 02:25:50 +01:00
|
|
|
import dorkbox.util.SwingUtil;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
|
|
|
|
public
|
|
|
|
class SystemTrayMenuPopup extends JPopupMenu {
|
2014-11-24 17:40:06 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
/**
|
|
|
|
* Allows you to customize the delay (for hiding the popup) when the cursor is "moused out" of the popup menu
|
|
|
|
*/
|
2015-01-23 02:52:09 +01:00
|
|
|
public static long hidePopupDelay = 1000L;
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private DelayTimer timer;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
// protected boolean mouseStillOnMenu;
|
2015-01-23 02:52:09 +01:00
|
|
|
// private JDialog hiddenDialog;
|
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
SystemTrayMenuPopup() {
|
2014-11-24 17:40:06 +01:00
|
|
|
super();
|
2015-01-23 02:52:09 +01:00
|
|
|
setFocusable(true);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.timer = new DelayTimer("PopupMenuHider", true, new DelayTimer.Callback() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void execute() {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeLater(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
Point location = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
Point locationOnScreen = getLocationOnScreen();
|
|
|
|
Dimension size = getSize();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
if (location.x >= locationOnScreen.x && location.x < locationOnScreen.x + size.width &&
|
|
|
|
location.y >= locationOnScreen.y && location.y < locationOnScreen.y + size.height) {
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void mouseExited(MouseEvent event) {
|
2014-11-24 17:40:06 +01:00
|
|
|
// wait before checking if mouse is still on the menu
|
|
|
|
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
// Does not work correctly on linux. a window in the taskbar shows up.
|
|
|
|
/* Initialize the hidden dialog as a headless, titleless dialog window */
|
|
|
|
// this.hiddenDialog = new JDialog((Frame)null);
|
|
|
|
// this.hiddenDialog.setEnabled(false);
|
|
|
|
// this.hiddenDialog.setUndecorated(true);
|
|
|
|
//
|
|
|
|
// this.hiddenDialog.setSize(5, 5);
|
|
|
|
// /* Add the window focus listener to the hidden dialog */
|
|
|
|
// this.hiddenDialog.addWindowFocusListener(new WindowFocusListener () {
|
|
|
|
// @Override
|
|
|
|
// public void windowLostFocus (WindowEvent we ) {
|
|
|
|
// SystemTrayMenuPopup.this.setVisible(false);
|
|
|
|
// }
|
|
|
|
// @Override
|
|
|
|
// public void windowGainedFocus (WindowEvent we) {}
|
|
|
|
// });
|
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void setVisible(boolean makeVisible) {
|
2014-11-24 17:40:06 +01:00
|
|
|
this.timer.cancel();
|
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
if (makeVisible) {
|
|
|
|
// if the mouse isn't inside the popup in x seconds, close the popup
|
|
|
|
this.timer.delay(hidePopupDelay);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
// this.hiddenDialog.setVisible(makeVisible);
|
|
|
|
super.setVisible(makeVisible);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-11-16 22:01:27 +01:00
|
|
|
}
|