SystemTray/src/dorkbox/systemTray/swing/SwingSystemTrayMenuPopup.java

72 lines
2.5 KiB
Java
Raw Normal View History

/*
* 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.
*/
package dorkbox.systemTray.swing;
2014-11-03 02:11:03 +01:00
2016-10-03 20:13:00 +02:00
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
2016-10-03 20:13:00 +02:00
import javax.swing.JDialog;
2016-09-28 17:20:13 +02:00
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
2016-09-28 17:20:13 +02:00
2016-10-03 20:13:00 +02:00
/**
* This custom popup is required, because we cannot close this popup by clicking OUTSIDE the popup. For whatever reason, that does not
* work, so we must implement an "auto-hide" feature that checks if our mouse is still inside a menu every POPUP_HIDE_DELAY seconds
*/
2016-09-28 17:20:13 +02:00
public
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
2016-10-03 20:13:00 +02:00
// NOTE: we can use the "hidden dialog" focus window trick... only on windows and mac
private JDialog hiddenDialog;
2016-02-21 00:56:14 +01:00
SwingSystemTrayMenuPopup() {
2014-11-24 17:40:06 +01:00
super();
setFocusable(true);
// 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
2016-10-03 20:13:00 +02: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
this.hiddenDialog = new JDialog((Frame)null);
this.hiddenDialog.setEnabled(false);
this.hiddenDialog.setUndecorated(true);
2016-02-21 00:56:14 +01:00
2016-10-03 20:13:00 +02:00
this.hiddenDialog.setSize(1, 1);
2016-10-03 20:13:00 +02:00
// Add the window focus listener to the hidden dialog
this.hiddenDialog.addWindowFocusListener(new WindowFocusListener() {
2014-11-24 17:40:06 +01:00
@Override
2016-10-03 20:13:00 +02:00
public void windowLostFocus (WindowEvent we ) {
SwingSystemTrayMenuPopup.this.setVisible(false);
2014-11-03 02:11:03 +01:00
}
2016-10-03 20:13:00 +02:00
@Override
public void windowGainedFocus (WindowEvent we) {}
2014-11-03 02:11:03 +01:00
});
}
2014-11-24 17:40:06 +01:00
@Override
public
void setVisible(boolean makeVisible) {
2016-10-03 20:13:00 +02:00
this.hiddenDialog.setVisible(makeVisible);
this.hiddenDialog.setEnabled(false);
2016-02-21 00:56:14 +01:00
super.setVisible(makeVisible);
2014-11-24 17:40:06 +01:00
}
}