2014-11-24 17:40:06 +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-12 01:51:15 +01:00
|
|
|
package dorkbox.systemTray.swing;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-11-09 16:13:57 +01:00
|
|
|
import java.awt.AWTException;
|
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.Point;
|
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.SystemTray;
|
|
|
|
import java.awt.TrayIcon;
|
2014-11-03 02:11:03 +01:00
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
2016-09-26 02:06:27 +02:00
|
|
|
import java.io.File;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-09-22 15:21:22 +02:00
|
|
|
import javax.swing.ImageIcon;
|
2016-10-03 20:13:00 +02:00
|
|
|
import javax.swing.JPopupMenu;
|
2016-09-22 15:21:22 +02:00
|
|
|
|
|
|
|
import dorkbox.systemTray.MenuEntry;
|
2016-09-26 02:06:27 +02:00
|
|
|
import dorkbox.systemTray.util.ImageUtils;
|
2016-09-22 15:21:22 +02:00
|
|
|
import dorkbox.util.ScreenUtil;
|
|
|
|
|
2014-11-03 02:11:03 +01:00
|
|
|
/**
|
2016-09-22 15:21:22 +02:00
|
|
|
* Class for handling all system tray interaction, via SWING.
|
|
|
|
*
|
|
|
|
* It doesn't work well on linux. See bugs:
|
|
|
|
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6267936
|
|
|
|
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6453521
|
|
|
|
* https://stackoverflow.com/questions/331407/java-trayicon-using-image-with-transparent-background/3882028#3882028
|
2014-11-03 02:11:03 +01:00
|
|
|
*/
|
2016-09-26 02:06:27 +02:00
|
|
|
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "WeakerAccess"})
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-09-29 01:44:11 +02:00
|
|
|
class SwingSystemTray extends SwingMenu {
|
2015-08-04 21:10:01 +02:00
|
|
|
volatile SystemTray tray;
|
|
|
|
volatile TrayIcon trayIcon;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new system tray handler class.
|
|
|
|
*/
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-09-29 01:44:11 +02:00
|
|
|
SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) {
|
|
|
|
super(systemTray, null);
|
2016-09-26 02:06:27 +02:00
|
|
|
|
2016-10-04 16:59:22 +02:00
|
|
|
ImageUtils.determineIconSize();
|
2016-09-26 02:06:27 +02:00
|
|
|
|
2016-10-04 00:21:40 +02:00
|
|
|
dispatchAndWait(new Runnable() {
|
2014-11-03 02:11:03 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.tray = SystemTray.getSystemTray();
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2015-11-09 16:13:57 +01:00
|
|
|
public
|
|
|
|
void shutdown() {
|
2016-10-04 00:21:40 +02:00
|
|
|
dispatchAndWait(new Runnable() {
|
2015-11-09 16:13:57 +01:00
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void run() {
|
2016-09-28 17:20:13 +02:00
|
|
|
tray.remove(trayIcon);
|
2015-11-09 16:13:57 +01:00
|
|
|
|
2016-09-28 17:20:13 +02:00
|
|
|
synchronized (menuEntries) {
|
|
|
|
for (MenuEntry menuEntry : menuEntries) {
|
2015-11-15 14:51:47 +01:00
|
|
|
menuEntry.remove();
|
|
|
|
}
|
2016-09-28 17:20:13 +02:00
|
|
|
menuEntries.clear();
|
2015-11-15 14:51:47 +01:00
|
|
|
}
|
2016-10-03 23:12:00 +02:00
|
|
|
|
|
|
|
remove();
|
2015-11-09 16:13:57 +01:00
|
|
|
}
|
|
|
|
});
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-02-15 13:27:33 +01:00
|
|
|
String getStatus() {
|
2016-09-28 02:51:06 +02:00
|
|
|
synchronized (menuEntries) {
|
|
|
|
MenuEntry menuEntry = menuEntries.get(0);
|
2016-10-03 20:13:00 +02:00
|
|
|
if (menuEntry instanceof SwingEntryStatus) {
|
2016-09-28 02:51:06 +02:00
|
|
|
return menuEntry.getText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-02-15 13:27:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public
|
|
|
|
void setStatus(final String statusText) {
|
2016-04-05 14:07:41 +02:00
|
|
|
dispatch(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2016-09-28 17:20:13 +02:00
|
|
|
synchronized (menuEntries) {
|
|
|
|
// status is ALWAYS at 0 index...
|
2016-10-03 20:13:00 +02:00
|
|
|
SwingEntry menuEntry = null;
|
2016-09-28 17:20:13 +02:00
|
|
|
if (!menuEntries.isEmpty()) {
|
2016-10-03 20:13:00 +02:00
|
|
|
menuEntry = (SwingEntry) menuEntries.get(0);
|
2016-09-28 17:20:13 +02:00
|
|
|
}
|
2015-11-15 14:51:47 +01:00
|
|
|
|
2016-10-03 20:13:00 +02:00
|
|
|
if (menuEntry instanceof SwingEntryStatus) {
|
2016-09-28 17:20:13 +02:00
|
|
|
// set the text or delete...
|
2015-11-15 14:51:47 +01:00
|
|
|
|
2016-09-28 17:20:13 +02:00
|
|
|
if (statusText == null) {
|
|
|
|
// delete
|
2016-10-03 20:13:00 +02:00
|
|
|
remove(menuEntry);
|
2016-09-28 17:20:13 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// set text
|
|
|
|
menuEntry.setText(statusText);
|
2016-09-28 02:51:06 +02:00
|
|
|
}
|
2016-09-28 17:20:13 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// create a new one
|
2016-10-03 20:13:00 +02:00
|
|
|
menuEntry = new SwingEntryStatus(SwingSystemTray.this, statusText);
|
2016-09-28 17:20:13 +02:00
|
|
|
// status is ALWAYS at 0 index...
|
|
|
|
menuEntries.add(0, menuEntry);
|
2015-11-15 14:51:47 +01:00
|
|
|
}
|
2016-09-28 02:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public
|
2016-09-26 02:06:27 +02:00
|
|
|
void setIcon_(final File iconFile) {
|
2016-04-05 14:07:41 +02:00
|
|
|
dispatch(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2016-09-26 02:06:27 +02:00
|
|
|
// stupid java won't scale it right away, so we have to do this twice to get the correct size
|
|
|
|
final Image trayImage = new ImageIcon(iconFile.getAbsolutePath()).getImage();
|
|
|
|
trayImage.flush();
|
|
|
|
|
2016-09-29 03:00:29 +02:00
|
|
|
if (trayIcon == null) {
|
2016-09-28 17:20:13 +02:00
|
|
|
// here we init. everything
|
|
|
|
trayIcon = new TrayIcon(trayImage);
|
|
|
|
|
2016-10-08 17:41:21 +02:00
|
|
|
JPopupMenu popupMenu = (JPopupMenu) _native;
|
|
|
|
popupMenu.pack();
|
|
|
|
popupMenu.setFocusable(true);
|
|
|
|
|
2016-09-28 17:20:13 +02:00
|
|
|
// appindicators DO NOT support anything other than PLAIN gtk-menus
|
|
|
|
// they ALSO do not support tooltips, so we cater to the lowest common denominator
|
|
|
|
// trayIcon.setToolTip(SwingSystemTray.this.appName);
|
|
|
|
|
|
|
|
trayIcon.addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void mousePressed(MouseEvent e) {
|
2016-09-29 01:44:11 +02:00
|
|
|
Dimension size = _native.getPreferredSize();
|
2016-09-28 17:20:13 +02:00
|
|
|
|
|
|
|
Point point = e.getPoint();
|
|
|
|
Rectangle bounds = ScreenUtil.getScreenBoundsAt(point);
|
|
|
|
|
|
|
|
int x = point.x;
|
|
|
|
int y = point.y;
|
|
|
|
|
|
|
|
if (y < bounds.y) {
|
|
|
|
y = bounds.y;
|
|
|
|
}
|
|
|
|
else if (y + size.height > bounds.y + bounds.height) {
|
|
|
|
// our menu cannot have the top-edge snap to the mouse
|
|
|
|
// so we make the bottom-edge snap to the mouse
|
|
|
|
y -= size.height; // snap to edge of mouse
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < bounds.x) {
|
|
|
|
x = bounds.x;
|
|
|
|
}
|
|
|
|
else if (x + size.width > bounds.x + bounds.width) {
|
|
|
|
// our menu cannot have the left-edge snap to the mouse
|
|
|
|
// so we make the right-edge snap to the mouse
|
|
|
|
x -= size.width; // snap to edge of mouse
|
2016-02-12 02:30:33 +01:00
|
|
|
}
|
|
|
|
|
2016-10-08 17:41:21 +02:00
|
|
|
SwingSystemTrayMenuWindowsPopup popupMenu = (SwingSystemTrayMenuWindowsPopup) _native;
|
|
|
|
popupMenu.doShow(x, y);
|
2016-02-12 02:30:33 +01:00
|
|
|
}
|
2016-09-28 17:20:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2016-09-29 03:00:29 +02:00
|
|
|
tray.add(trayIcon);
|
2016-10-08 17:41:21 +02:00
|
|
|
((SwingSystemTrayMenuWindowsPopup) _native).setIcon(iconFile);
|
2016-09-28 17:20:13 +02:00
|
|
|
} catch (AWTException e) {
|
2016-09-29 01:44:11 +02:00
|
|
|
dorkbox.systemTray.SystemTray.logger.error("TrayIcon could not be added.", e);
|
2016-02-12 02:30:33 +01:00
|
|
|
}
|
2016-09-28 17:20:13 +02:00
|
|
|
} else {
|
2016-10-08 17:41:21 +02:00
|
|
|
((SwingSystemTrayMenuWindowsPopup) _native).setIcon(iconFile);
|
2016-09-28 17:20:13 +02:00
|
|
|
trayIcon.setImage(trayImage);
|
2015-11-15 14:51:47 +01:00
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|