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;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
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.AppIndicator;
|
|
|
|
import dorkbox.util.jna.linux.Gobject;
|
|
|
|
import dorkbox.util.jna.linux.Gtk;
|
|
|
|
import dorkbox.util.tray.SystemTray;
|
|
|
|
import dorkbox.util.tray.SystemTrayMenuAction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for handling all system tray interactions.
|
|
|
|
*
|
|
|
|
* specialization for using app indicators in ubuntu unity
|
|
|
|
*
|
|
|
|
* Heavily modified from
|
|
|
|
*
|
2014-11-16 22:01:27 +01:00
|
|
|
* Lantern: https://github.com/getlantern/lantern/ Apache 2.0 License Copyright 2010 Brave New Software Project, Inc.
|
2014-11-03 02:11:03 +01:00
|
|
|
*/
|
|
|
|
public class AppIndicatorTray extends SystemTray {
|
2014-11-24 17:40:06 +01:00
|
|
|
private static final AppIndicator libappindicator = AppIndicator.INSTANCE;
|
|
|
|
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, MenuEntry> menuEntries = new HashMap<String, MenuEntry>(2);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private volatile AppIndicator.AppIndicatorInstanceStruct appIndicator;
|
|
|
|
private volatile Pointer menu;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private volatile Pointer connectionStatusItem;
|
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);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
public AppIndicatorTray() {
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
@Override
|
2014-11-24 17:40:06 +01:00
|
|
|
public void createTray(String iconName) {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +01:00
|
|
|
this.appIndicator =
|
|
|
|
libappindicator.app_indicator_new(this.appName, "indicator-messages-new", AppIndicator.CATEGORY_APPLICATION_STATUS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* basically a hack -- we should subclass the AppIndicator type and override the fallback entry in the 'vtable', instead we just
|
|
|
|
* hack the app indicator class itself. Not an issue unless we need other appindicators.
|
|
|
|
*/
|
|
|
|
AppIndicator.AppIndicatorClassStruct aiclass =
|
|
|
|
new AppIndicator.AppIndicatorClassStruct(this.appIndicator.parent.g_type_instance.g_class);
|
|
|
|
|
|
|
|
|
|
|
|
aiclass.fallback = new AppIndicator.Fallback() {
|
|
|
|
@Override
|
|
|
|
public Pointer callback(final AppIndicator.AppIndicatorInstanceStruct self) {
|
|
|
|
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
logger.warn("Failed to create appindicator system tray.");
|
|
|
|
|
|
|
|
if (AppIndicatorTray.this.failureCallback != null) {
|
|
|
|
AppIndicatorTray.this.failureCallback.createTrayFailed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return null;
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
};
|
|
|
|
aiclass.write();
|
|
|
|
|
|
|
|
this.menu = libgtk.gtk_menu_new();
|
|
|
|
libappindicator.app_indicator_set_menu(this.appIndicator, this.menu);
|
|
|
|
|
|
|
|
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
|
|
|
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_ACTIVE);
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
|
|
|
|
2015-01-22 03:24:18 +01:00
|
|
|
this.active = true;
|
2014-11-16 22:01:27 +01:00
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
|
|
|
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
|
|
|
|
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_PASSIVE);
|
|
|
|
this.appIndicator.write();
|
|
|
|
Pointer p = this.appIndicator.getPointer();
|
|
|
|
libgobject.g_object_unref(p);
|
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.appIndicator = null;
|
|
|
|
this.widgets.clear();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// unrefs the children too
|
|
|
|
libgobject.g_object_unref(this.menu);
|
|
|
|
this.menu = null;
|
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.connectionStatusItem = null;
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gtk_main_quit();
|
2014-11-16 15:27:54 +01:00
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-24 17:40:06 +01:00
|
|
|
super.removeTray();
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
|
|
|
public void setStatus(String infoString, String iconName) {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +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);
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
libgtk.gtk_widget_show_all(this.connectionStatusItem);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-11-16 22:01:27 +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(String menuText, final SystemTrayMenuAction callback) {
|
|
|
|
synchronized (this.menuEntries) {
|
|
|
|
MenuEntry menuEntry = this.menuEntries.get(menuText);
|
|
|
|
|
|
|
|
if (menuEntry == null) {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
Pointer dashboardItem = libgtk.gtk_menu_item_new_with_label(menuText);
|
2014-12-12 02:25:50 +01:00
|
|
|
|
|
|
|
// have to watch out! These can get garbage collected!
|
2014-11-24 17:40:06 +01:00
|
|
|
Gobject.GCallback gtkCallback = new Gobject.GCallback() {
|
|
|
|
@Override
|
|
|
|
public void callback(Pointer instance, Pointer data) {
|
|
|
|
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
callback.onClick(AppIndicatorTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
menuEntry = new MenuEntry();
|
|
|
|
menuEntry.dashboardItem = dashboardItem;
|
2014-12-12 02:25:50 +01:00
|
|
|
menuEntry.gtkCallback = gtkCallback;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
this.menuEntries.put(menuText, menuEntry);
|
|
|
|
} else {
|
|
|
|
updateMenuEntry(menuText, menuText, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 22:01:27 +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(String origMenuText, String newMenuText, final SystemTrayMenuAction newCallback) {
|
|
|
|
synchronized (this.menuEntries) {
|
|
|
|
MenuEntry menuEntry = this.menuEntries.get(origMenuText);
|
|
|
|
|
|
|
|
if (menuEntry != null) {
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_enter();
|
2014-11-24 17:40:06 +01:00
|
|
|
libgtk.gtk_menu_item_set_label(menuEntry.dashboardItem, newMenuText);
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
// have to watch out! These can get garbage collected!
|
|
|
|
menuEntry.gtkCallback = new Gobject.GCallback() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
|
|
|
public void callback(Pointer instance, Pointer data) {
|
|
|
|
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
newCallback.onClick(AppIndicatorTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-12 02:25:50 +01:00
|
|
|
libgobject.g_signal_connect_data(menuEntry.dashboardItem, "activate", menuEntry.gtkCallback, null, null, 0);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
libgtk.gtk_widget_show_all(menuEntry.dashboardItem);
|
2014-12-12 02:25:50 +01:00
|
|
|
libgtk.gdk_threads_leave();
|
2014-11-24 17:40:06 +01:00
|
|
|
} else {
|
|
|
|
addMenuEntry(origMenuText, newCallback);
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|