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.
|
|
|
|
*/
|
2014-11-03 02:11:03 +01:00
|
|
|
package dorkbox.util.tray.linux;
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
import com.sun.jna.Pointer;
|
2014-12-12 02:25:50 +01:00
|
|
|
import dorkbox.util.SwingUtil;
|
2014-11-03 02:11:03 +01:00
|
|
|
import dorkbox.util.jna.linux.Gobject;
|
|
|
|
import dorkbox.util.jna.linux.Gtk;
|
|
|
|
import dorkbox.util.jna.linux.Gtk.GdkEventButton;
|
2015-01-23 02:52:09 +01:00
|
|
|
import dorkbox.util.jna.linux.GtkSupport;
|
2014-11-03 02:11:03 +01:00
|
|
|
import dorkbox.util.tray.SystemTray;
|
|
|
|
import dorkbox.util.tray.SystemTrayMenuAction;
|
|
|
|
import dorkbox.util.tray.SystemTrayMenuPopup;
|
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2014-11-03 02:11:03 +01:00
|
|
|
/**
|
|
|
|
* Class for handling all system tray interactions via GTK.
|
2015-06-28 01:47:02 +02:00
|
|
|
* <p/>
|
2014-11-03 02:11:03 +01:00
|
|
|
* This is the "old" way to do it, and does not work with some desktop environments.
|
|
|
|
*/
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
class GtkSystemTray extends SystemTray {
|
2014-11-24 17:40:06 +01:00
|
|
|
private static final Gobject libgobject = Gobject.INSTANCE;
|
|
|
|
private static final Gtk libgtk = Gtk.INSTANCE;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private volatile SystemTrayMenuPopup jmenu;
|
|
|
|
private volatile JMenuItem connectionStatusItem;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private volatile Pointer trayIcon;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// need to hang on to these to prevent gc
|
|
|
|
private final List<Pointer> widgets = new ArrayList<Pointer>(4);
|
2015-01-23 02:52:09 +01:00
|
|
|
private Gobject.GEventCallback gtkCallback;
|
2014-11-16 22:01:27 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
GtkSystemTray() {
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void createTray(String iconName) {
|
2015-01-23 02:52:09 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2015-01-23 02:52:09 +01:00
|
|
|
GtkSystemTray.this.jmenu = new SystemTrayMenuPopup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2015-01-23 02:52:09 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.trayIcon = libgtk.gtk_status_icon_new();
|
|
|
|
libgtk.gtk_status_icon_set_from_file(this.trayIcon, iconPath(iconName));
|
|
|
|
libgtk.gtk_status_icon_set_tooltip(this.trayIcon, this.appName);
|
|
|
|
libgtk.gtk_status_icon_set_visible(this.trayIcon, true);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
// have to make this a field, to prevent GC on this object
|
|
|
|
this.gtkCallback = new Gobject.GEventCallback() {
|
2014-11-03 02:11:03 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void callback(Pointer system_tray, final GdkEventButton event) {
|
2015-01-23 02:52:09 +01:00
|
|
|
// BUTTON_PRESS only (any mouse click)
|
2014-11-24 17:40:06 +01:00
|
|
|
if (event.type == 4) {
|
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() {
|
2015-01-23 02:52:09 +01:00
|
|
|
// test this using cinnamon (which still uses status icon)
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
if (GtkSystemTray.this.jmenu.isVisible()) {
|
|
|
|
GtkSystemTray.this.jmenu.setVisible(false);
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
Dimension size = GtkSystemTray.this.jmenu.getPreferredSize();
|
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
int x = (int) event.x_root;
|
2015-06-28 01:47:02 +02:00
|
|
|
int y = (int) event.y_root;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
Point point = new Point(x, y);
|
|
|
|
Rectangle bounds = SwingUtil.getScreenBoundsAt(point);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
if (y < bounds.y) {
|
|
|
|
y = bounds.y;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else if (y + size.height > bounds.y + bounds.height) {
|
2015-01-23 02:52:09 +01:00
|
|
|
// 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;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else if (x + size.width > bounds.x + bounds.width) {
|
2015-01-23 02:52:09 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMALL problem, is that on linux, the popup is BEHIND the tray bar!
|
|
|
|
// to solve the problem, we anchor the popup above (or below) the tray bar
|
|
|
|
int distanceToEdgeOfTray = (int) event.y;
|
|
|
|
// System.err.println(" distance: " + distanceToEdgeOfTray);
|
|
|
|
// we are at the top of the screen
|
|
|
|
if (y < 100) {
|
|
|
|
y += distanceToEdgeOfTray + 4;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-01-23 02:52:09 +01:00
|
|
|
y -= distanceToEdgeOfTray + 4;
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkSystemTray.this.jmenu.setInvoker(GtkSystemTray.this.jmenu);
|
2015-01-23 02:52:09 +01:00
|
|
|
GtkSystemTray.this.jmenu.setLocation(x, y);
|
2014-11-24 17:40:06 +01:00
|
|
|
GtkSystemTray.this.jmenu.setVisible(true);
|
2015-01-23 02:52:09 +01:00
|
|
|
GtkSystemTray.this.jmenu.requestFocus();
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
};
|
|
|
|
// all the clicks. This is because native menu popups are a pain to figure out, so we cheat and use some java bits to do the popup
|
2015-01-23 02:52:09 +01:00
|
|
|
libgobject.g_signal_connect_data(this.trayIcon, "button_press_event", this.gtkCallback, null, null, 0);
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-01-22 03:24:18 +01:00
|
|
|
this.active = true;
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void removeTray() {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +01:00
|
|
|
for (Pointer widget : this.widgets) {
|
|
|
|
libgtk.gtk_widget_destroy(widget);
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// this hides the indicator
|
|
|
|
libgtk.gtk_status_icon_set_visible(this.trayIcon, false);
|
|
|
|
libgobject.g_object_unref(this.trayIcon);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.active = false;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// GC it
|
|
|
|
this.trayIcon = null;
|
|
|
|
this.widgets.clear();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
synchronized (this.menuEntries) {
|
|
|
|
this.menuEntries.clear();
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.jmenu.setVisible(false);
|
|
|
|
this.jmenu.setEnabled(false);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.jmenu = null;
|
|
|
|
this.connectionStatusItem = null;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
GtkSupport.shutdownGTK();
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
super.removeTray();
|
|
|
|
}
|
2014-11-16 15:27:54 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void setStatus(final String infoString, String iconName) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(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
|
|
|
if (GtkSystemTray.this.connectionStatusItem == null) {
|
|
|
|
GtkSystemTray.this.connectionStatusItem = new JMenuItem(infoString);
|
|
|
|
GtkSystemTray.this.connectionStatusItem.setEnabled(false);
|
|
|
|
GtkSystemTray.this.jmenu.add(GtkSystemTray.this.connectionStatusItem);
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
GtkSystemTray.this.connectionStatusItem.setText(infoString);
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +01:00
|
|
|
libgtk.gtk_status_icon_set_from_file(GtkSystemTray.this.trayIcon, iconPath(iconName));
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
/**
|
|
|
|
* Will add a new menu entry, or update one if it already exists
|
|
|
|
*/
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(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
|
|
|
Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
|
|
|
|
|
|
|
|
synchronized (menuEntries2) {
|
|
|
|
JMenuItem menuEntry = menuEntries2.get(menuText);
|
|
|
|
|
|
|
|
if (menuEntry == null) {
|
|
|
|
SystemTrayMenuPopup menu = GtkSystemTray.this.jmenu;
|
|
|
|
|
|
|
|
menuEntry = new JMenuItem(menuText);
|
|
|
|
menuEntry.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void actionPerformed(ActionEvent e) {
|
2015-01-23 02:52:09 +01:00
|
|
|
// SystemTrayMenuPopup source = (SystemTrayMenuPopup) ((JMenuItem)e.getSource()).getParent();
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
callback.onClick(GtkSystemTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menu.add(menuEntry);
|
|
|
|
|
|
|
|
menuEntries2.put(menuText, menuEntry);
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
updateMenuEntry(menuText, menuText, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will update an already existing menu entry (or add a new one, if it doesn't exist)
|
|
|
|
*/
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(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
|
|
|
Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
|
|
|
|
|
|
|
|
synchronized (menuEntries2) {
|
|
|
|
JMenuItem menuEntry = menuEntries2.get(origMenuText);
|
|
|
|
|
|
|
|
if (menuEntry != null) {
|
|
|
|
ActionListener[] actionListeners = menuEntry.getActionListeners();
|
|
|
|
for (ActionListener l : actionListeners) {
|
|
|
|
menuEntry.removeActionListener(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
menuEntry.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void actionPerformed(ActionEvent e) {
|
2014-11-24 17:40:06 +01:00
|
|
|
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
newCallback.onClick(GtkSystemTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menuEntry.setText(newMenuText);
|
|
|
|
menuEntry.revalidate();
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
addMenuEntry(origMenuText, newCallback);
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|