SystemTray/src/dorkbox/util/tray/linux/GtkSystemTray.java

311 lines
12 KiB
Java
Raw Normal View History

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;
import dorkbox.util.ScreenUtil;
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;
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;
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.
* <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.
*/
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
final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
2014-11-03 02:11:03 +01:00
volatile SystemTrayMenuPopup jmenu;
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);
// have to make this a field, to prevent GC on this object
@SuppressWarnings("FieldCanBeLocal")
private Gobject.GEventCallback gtkCallback;
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
public
void createTray(String iconName) {
SwingUtil.invokeAndWait(new Runnable() {
@Override
public
void run() {
GtkSystemTray.this.jmenu = new SystemTrayMenuPopup();
}
});
libgtk.gdk_threads_enter();
final Pointer trayIcon = libgtk.gtk_status_icon_new();
this.trayIcon = trayIcon;
libgtk.gtk_status_icon_set_from_file(trayIcon, iconPath(iconName));
libgtk.gtk_status_icon_set_tooltip(trayIcon, this.appName);
libgtk.gtk_status_icon_set_visible(trayIcon, true);
2014-11-03 02:11:03 +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
public
void callback(Pointer system_tray, final GdkEventButton event) {
// BUTTON_PRESS only (any mouse click)
2014-11-24 17:40:06 +01:00
if (event.type == 4) {
SwingUtil.invokeLater(new Runnable() {
2014-11-24 17:40:06 +01:00
@Override
public
void run() {
// test this using cinnamon (which still uses status icon)
final SystemTrayMenuPopup jmenu = GtkSystemTray.this.jmenu;
if (jmenu.isVisible()) {
jmenu.setVisible(false);
}
else {
Dimension size = jmenu.getPreferredSize();
2014-11-24 17:40:06 +01:00
int x = (int) event.x_root;
int y = (int) event.y_root;
2014-11-24 17:40:06 +01:00
Point point = new Point(x, y);
Rectangle bounds = ScreenUtil.getScreenBoundsAt(point);
2014-11-24 17:40:06 +01:00
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
}
// 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;
}
else {
y -= distanceToEdgeOfTray + 4;
2014-11-24 17:40:06 +01:00
}
jmenu.setInvoker(jmenu);
jmenu.setLocation(x, y);
jmenu.setVisible(true);
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
libgobject.g_signal_connect_data(trayIcon, "button_press_event", this.gtkCallback, null, null, 0);
libgtk.gdk_threads_leave();
2014-11-24 17:40:06 +01:00
this.active = true;
2014-11-03 02:11:03 +01:00
}
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
2014-11-24 17:40:06 +01:00
@Override
public
void removeTray() {
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
GtkSupport.shutdownGTK();
libgtk.gdk_threads_leave();
2014-11-24 17:40:06 +01:00
super.removeTray();
}
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
2014-11-24 17:40:06 +01:00
@Override
public
void setStatus(final String infoString, String iconName) {
SwingUtil.invokeAndWait(new Runnable() {
2014-11-24 17:40:06 +01:00
@Override
public
void run() {
2014-11-24 17:40:06 +01:00
if (GtkSystemTray.this.connectionStatusItem == null) {
final JMenuItem connectionStatusItem = new JMenuItem(infoString);
GtkSystemTray.this.connectionStatusItem = connectionStatusItem;
connectionStatusItem.setEnabled(false);
GtkSystemTray.this.jmenu.add(connectionStatusItem);
}
else {
2014-11-24 17:40:06 +01:00
GtkSystemTray.this.connectionStatusItem.setText(infoString);
}
}
});
2014-11-24 17:40:06 +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));
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
public
void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
SwingUtil.invokeAndWait(new Runnable() {
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
2014-11-24 17:40:06 +01:00
@Override
public
void run() {
final Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
2014-11-24 17:40:06 +01:00
synchronized (menuEntries2) {
JMenuItem menuEntry = menuEntries2.get(menuText);
if (menuEntry == null) {
SystemTrayMenuPopup menu = GtkSystemTray.this.jmenu;
menuEntry = new JMenuItem(menuText);
menuEntry.addActionListener(new ActionListener() {
@Override
public
void actionPerformed(ActionEvent e) {
// SystemTrayMenuPopup source = (SystemTrayMenuPopup) ((JMenuItem)e.getSource()).getParent();
2014-11-24 17:40:06 +01:00
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
@Override
public
void run() {
2014-11-24 17:40:06 +01:00
callback.onClick(GtkSystemTray.this);
}
});
}
});
menu.add(menuEntry);
menuEntries2.put(menuText, menuEntry);
}
else {
2014-11-24 17:40:06 +01:00
updateMenuEntry(menuText, menuText, callback);
}
}
}
});
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
public
void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
SwingUtil.invokeAndWait(new Runnable() {
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
2014-11-24 17:40:06 +01:00
@Override
public
void run() {
final Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
2014-11-24 17:40:06 +01:00
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
public
void actionPerformed(ActionEvent e) {
2014-11-24 17:40:06 +01:00
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
@Override
public
void run() {
2014-11-24 17:40:06 +01:00
newCallback.onClick(GtkSystemTray.this);
}
});
}
});
menuEntry.setText(newMenuText);
menuEntry.revalidate();
}
else {
2014-11-24 17:40:06 +01:00
addMenuEntry(origMenuText, newCallback);
}
}
2014-11-03 02:11:03 +01:00
}
});
2014-11-03 02:11:03 +01:00
}
}