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-11-03 02:11:03 +01:00
|
|
|
import dorkbox.util.jna.linux.Gobject;
|
|
|
|
import dorkbox.util.jna.linux.Gtk;
|
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;
|
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
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
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
private final Map<String, MenuEntry> menuEntries = new HashMap<String, MenuEntry>(2);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
private volatile Pointer menu;
|
|
|
|
private volatile Pointer 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-08-04 21:10:01 +02:00
|
|
|
|
|
|
|
// have to make this a field, to prevent GC on this object
|
|
|
|
@SuppressWarnings("FieldCanBeLocal")
|
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) {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2015-01-23 02:52:09 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
this.menu = libgtk.gtk_menu_new();
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
final Pointer trayIcon = libgtk.gtk_status_icon_new();
|
|
|
|
libgtk.gtk_status_icon_set_from_file(trayIcon, iconPath(iconName));
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
this.gtkCallback = new Gobject.GEventCallback() {
|
2014-11-03 02:11:03 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2015-10-27 02:31:15 +01:00
|
|
|
void callback(Pointer system_tray, final Gtk.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) {
|
2015-10-27 02:31:15 +01:00
|
|
|
libgtk.gtk_menu_popup(menu, null, null, Gtk.gtk_status_icon_position_menu, system_tray, 0, event.time);
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
};
|
2015-10-27 02:31:15 +01:00
|
|
|
|
|
|
|
libgobject.g_signal_connect_data(trayIcon, "button_press_event", gtkCallback, menu, null, 0);
|
|
|
|
|
|
|
|
// This is unreliable to use in our gnome-shell notification hook, because of race conditions, it will only sometimes be correct
|
|
|
|
// libgtk.gtk_status_icon_set_title(trayIcon, "something");
|
|
|
|
|
|
|
|
libgtk.gtk_status_icon_set_tooltip(trayIcon, this.appName);
|
|
|
|
libgtk.gtk_status_icon_set_visible(trayIcon, true);
|
|
|
|
|
|
|
|
this.trayIcon = trayIcon;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
|
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
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
// unrefs the children too
|
|
|
|
libgobject.g_object_unref(this.menu);
|
|
|
|
this.menu = null;
|
|
|
|
|
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.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
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-24 17:40:06 +01:00
|
|
|
super.removeTray();
|
|
|
|
}
|
2014-11-16 15:27:54 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
@SuppressWarnings({"FieldRepeatedlyAccessedInMethod", "Duplicates"})
|
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
|
|
|
libgtk.gdk_threads_enter();
|
2015-10-27 02:31:15 +01:00
|
|
|
if (this.connectionStatusItem == null) {
|
|
|
|
this.connectionStatusItem = libgtk.gtk_menu_item_new_with_label(infoString);
|
|
|
|
this.widgets.add(this.connectionStatusItem);
|
|
|
|
libgtk.gtk_widget_set_sensitive(this.connectionStatusItem, Gtk.FALSE);
|
|
|
|
libgtk.gtk_menu_shell_append(this.menu, this.connectionStatusItem);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
libgtk.gtk_menu_item_set_label(this.connectionStatusItem, infoString);
|
|
|
|
}
|
|
|
|
|
|
|
|
libgtk.gtk_widget_show_all(this.connectionStatusItem);
|
|
|
|
|
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
|
|
|
|
*/
|
2015-10-27 02:31:15 +01:00
|
|
|
@SuppressWarnings("Duplicates")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
2015-10-27 02:31:15 +01:00
|
|
|
synchronized (this.menuEntries) {
|
|
|
|
MenuEntry menuEntry = this.menuEntries.get(menuText);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
if (menuEntry == null) {
|
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
Pointer dashboardItem = libgtk.gtk_menu_item_new_with_label(menuText);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-10-27 02:31:15 +01:00
|
|
|
// have to watch out! These can get garbage collected!
|
|
|
|
Gobject.GCallback gtkCallback = new Gobject.GCallback() {
|
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void callback(Pointer instance, Pointer data) {
|
|
|
|
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2015-10-27 02:31:15 +01:00
|
|
|
void run() {
|
|
|
|
callback.onClick(GtkSystemTray.this);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-10-27 02:31:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
libgobject.g_signal_connect_data(dashboardItem, "activate", gtkCallback, null, null, 0);
|
|
|
|
libgtk.gtk_menu_shell_append(this.menu, dashboardItem);
|
|
|
|
libgtk.gtk_widget_show_all(dashboardItem);
|
|
|
|
|
|
|
|
libgtk.gdk_threads_leave();
|
|
|
|
|
|
|
|
menuEntry = new MenuEntry();
|
|
|
|
menuEntry.dashboardItem = dashboardItem;
|
|
|
|
menuEntry.gtkCallback = gtkCallback;
|
|
|
|
|
|
|
|
this.menuEntries.put(menuText, menuEntry);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2015-10-27 02:31:15 +01:00
|
|
|
else {
|
|
|
|
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)
|
|
|
|
*/
|
2015-10-27 02:31:15 +01:00
|
|
|
@SuppressWarnings("Duplicates")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
|
2015-10-27 02:31:15 +01:00
|
|
|
synchronized (this.menuEntries) {
|
|
|
|
MenuEntry menuEntry = this.menuEntries.get(origMenuText);
|
|
|
|
|
|
|
|
if (menuEntry != null) {
|
|
|
|
libgtk.gdk_threads_enter();
|
|
|
|
libgtk.gtk_menu_item_set_label(menuEntry.dashboardItem, newMenuText);
|
|
|
|
|
|
|
|
// have to watch out! These can get garbage collected!
|
|
|
|
menuEntry.gtkCallback = new Gobject.GCallback() {
|
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void callback(Pointer instance, Pointer data) {
|
|
|
|
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2015-10-27 02:31:15 +01:00
|
|
|
void run() {
|
|
|
|
newCallback.onClick(GtkSystemTray.this);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-10-27 02:31:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
libgobject.g_signal_connect_data(menuEntry.dashboardItem, "activate", menuEntry.gtkCallback, null, null, 0);
|
|
|
|
|
|
|
|
libgtk.gtk_widget_show_all(menuEntry.dashboardItem);
|
|
|
|
libgtk.gdk_threads_leave();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addMenuEntry(origMenuText, newCallback);
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2015-10-27 02:31:15 +01:00
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|