2015-11-09 16:13:57 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2014 dorkbox, llc
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2016-02-13 15:06:19 +01:00
|
|
|
package dorkbox.systemTray.swing;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-11-20 12:25:30 +01:00
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.MouseInfo;
|
|
|
|
import java.awt.Point;
|
2015-06-28 01:47:02 +02:00
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
|
2016-09-28 17:20:13 +02:00
|
|
|
import javax.swing.JPopupMenu;
|
2016-09-28 18:35:25 +02:00
|
|
|
import javax.swing.border.EmptyBorder;
|
2016-09-28 17:20:13 +02:00
|
|
|
|
|
|
|
import dorkbox.util.DelayTimer;
|
|
|
|
import dorkbox.util.Property;
|
|
|
|
import dorkbox.util.SwingUtil;
|
|
|
|
|
|
|
|
public
|
2016-02-13 15:06:19 +01:00
|
|
|
class SwingSystemTrayMenuPopup 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-11-20 12:25:30 +01:00
|
|
|
@Property
|
2016-02-21 00:56:14 +01:00
|
|
|
/** Customize the delay (for hiding the popup) when the cursor is "moused out" of the popup menu */
|
2015-11-20 12:25:30 +01:00
|
|
|
public static long POPUP_HIDE_DELAY = 1000L;
|
2015-01-23 02:52:09 +01:00
|
|
|
|
2016-02-21 00:56:14 +01:00
|
|
|
@Property
|
|
|
|
/** Customize the minimum amount of movement needed to cause the popup-delay to hide the popup */
|
|
|
|
public static int MOVEMENT_DELTA = 20;
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private DelayTimer timer;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-02-21 00:56:14 +01:00
|
|
|
protected volatile Point previousLocation = null;
|
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
// protected boolean mouseStillOnMenu;
|
2015-01-23 02:52:09 +01:00
|
|
|
// private JDialog hiddenDialog;
|
|
|
|
|
2016-09-28 17:20:13 +02:00
|
|
|
public
|
2016-02-13 15:06:19 +01:00
|
|
|
SwingSystemTrayMenuPopup() {
|
2014-11-24 17:40:06 +01:00
|
|
|
super();
|
2015-01-23 02:52:09 +01:00
|
|
|
setFocusable(true);
|
2016-09-28 18:35:25 +02:00
|
|
|
// setBorder(new BorderUIResource.EmptyBorderUIResource(0, 0, 0, 0)); // borderUI resource border type will get changed!
|
|
|
|
setBorder(new EmptyBorder(1, 1, 1, 1));
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-12-13 21:04:31 +01:00
|
|
|
this.timer = new DelayTimer("PopupMenuHider", true, new Runnable() {
|
2016-02-21 00:56:14 +01:00
|
|
|
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2015-12-13 21:04:31 +01:00
|
|
|
void run() {
|
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();
|
2016-02-21 00:56:14 +01:00
|
|
|
Point menuLocation = getLocationOnScreen();
|
2014-11-24 17:40:06 +01:00
|
|
|
Dimension size = getSize();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-02-21 00:56:14 +01:00
|
|
|
// is the mouse pointer still inside of the popup?
|
|
|
|
if (location.x >= menuLocation.x && location.x < menuLocation.x + size.width &&
|
|
|
|
location.y >= menuLocation.y && location.y < menuLocation.y + size.height) {
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-02-21 00:56:14 +01:00
|
|
|
// restart the timer
|
|
|
|
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY);
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
2016-02-21 00:56:14 +01:00
|
|
|
|
|
|
|
// has the mouse pointer moved > delta pixels from it's original location (when the tray icon was clicked)?
|
|
|
|
else if (previousLocation != null &&
|
|
|
|
location.x >= previousLocation.x - MOVEMENT_DELTA && location.x < previousLocation.x + MOVEMENT_DELTA &&
|
|
|
|
location.y >= previousLocation.y - MOVEMENT_DELTA && location.y < previousLocation.y + MOVEMENT_DELTA) {
|
|
|
|
|
|
|
|
// restart the timer
|
|
|
|
SwingSystemTrayMenuPopup.this.timer.delay(POPUP_HIDE_DELAY);
|
|
|
|
}
|
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
else {
|
2016-02-21 00:56:14 +01:00
|
|
|
// else, we hide it
|
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
|
2016-02-13 15:06:19 +01:00
|
|
|
SwingSystemTrayMenuPopup.this.timer.delay(SwingSystemTrayMenuPopup.this.timer.getDelay());
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-21 00:56:14 +01:00
|
|
|
|
2015-12-13 21:04:31 +01:00
|
|
|
// Does not work correctly on linux. a window in the taskbar still shows up
|
|
|
|
// Initialize the hidden dialog as a headless, titleless dialog window
|
2015-01-23 02:52:09 +01:00
|
|
|
// 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) {
|
2016-02-21 00:56:14 +01:00
|
|
|
previousLocation = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
// if the mouse isn't inside the popup in x seconds, close the popup
|
2015-11-20 12:25:30 +01:00
|
|
|
this.timer.delay(POPUP_HIDE_DELAY);
|
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
|
|
|
}
|