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.linux;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-11-15 14:51:47 +01:00
|
|
|
import com.sun.jna.NativeLong;
|
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;
|
2015-06-28 01:47:02 +02:00
|
|
|
|
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
|
2015-11-09 16:13:57 +01:00
|
|
|
class GtkSystemTray extends GtkTypeSystemTray {
|
2015-11-15 14:51:47 +01:00
|
|
|
private Pointer trayIcon;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
// have to make this a field, to prevent GC on this object
|
|
|
|
@SuppressWarnings("FieldCanBeLocal")
|
2015-11-15 14:51:47 +01:00
|
|
|
private final Gobject.GEventCallback gtkCallback;
|
2016-02-12 01:51:15 +01:00
|
|
|
@SuppressWarnings({"FieldCanBeLocal", "unused"})
|
2015-11-15 14:51:47 +01:00
|
|
|
private NativeLong button_press_event;
|
2016-02-12 01:51:15 +01:00
|
|
|
|
|
|
|
|
2015-11-15 14:51:47 +01:00
|
|
|
private volatile Pointer menu;
|
2014-11-16 22:01:27 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-02-12 01:51:15 +01:00
|
|
|
GtkSystemTray(String iconPath) {
|
2015-11-09 16:13:57 +01:00
|
|
|
super();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gdk_threads_enter();
|
2015-01-23 02:52:09 +01:00
|
|
|
|
2015-11-15 17:43:55 +01:00
|
|
|
final Pointer trayIcon = gtk.gtk_status_icon_new();
|
|
|
|
gtk.gtk_status_icon_set_title(trayIcon, "SystemTray@Dorkbox");
|
|
|
|
gtk.gtk_status_icon_set_name(trayIcon, "SystemTray");
|
2015-11-09 16:13:57 +01:00
|
|
|
|
2015-10-29 02:17:25 +01:00
|
|
|
this.trayIcon = trayIcon;
|
|
|
|
|
2016-02-12 01:51:15 +01:00
|
|
|
gtk.gtk_status_icon_set_from_file(trayIcon, iconPath);
|
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-11-15 14:51:47 +01:00
|
|
|
void callback(Pointer notUsed, 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-11-15 17:43:55 +01:00
|
|
|
gtk.gtk_menu_popup(menu, null, null, Gtk.gtk_status_icon_position_menu, trayIcon, 0, event.time);
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
};
|
2015-11-15 17:43:55 +01:00
|
|
|
button_press_event = gobject.g_signal_connect_data(trayIcon, "button_press_event", gtkCallback, null, null, 0);
|
2015-11-15 14:51:47 +01:00
|
|
|
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gtk_status_icon_set_visible(trayIcon, true);
|
2015-10-27 02:31:15 +01:00
|
|
|
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gdk_threads_leave();
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-10-29 02:17:25 +01:00
|
|
|
GtkSupport.startGui();
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2015-11-15 14:51:47 +01:00
|
|
|
/**
|
|
|
|
* Called inside the gdk_threads block
|
|
|
|
*/
|
|
|
|
protected
|
|
|
|
void onMenuAdded(final Pointer menu) {
|
|
|
|
this.menu = menu;
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-11-15 14:51:47 +01:00
|
|
|
public synchronized
|
2015-11-09 16:13:57 +01:00
|
|
|
void shutdown() {
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gdk_threads_enter();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// this hides the indicator
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gtk_status_icon_set_visible(this.trayIcon, false);
|
|
|
|
gobject.g_object_unref(this.trayIcon);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
// GC it
|
|
|
|
this.trayIcon = null;
|
2014-12-12 02:25:50 +01:00
|
|
|
|
2015-11-09 16:13:57 +01:00
|
|
|
// libgtk.gdk_threads_leave(); called by parent class
|
|
|
|
super.shutdown();
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-11-16 15:27:54 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2016-02-12 01:51:15 +01:00
|
|
|
protected synchronized
|
|
|
|
void setIcon_(final String iconPath) {
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gdk_threads_enter();
|
2016-02-12 01:51:15 +01:00
|
|
|
gtk.gtk_status_icon_set_from_file(trayIcon, iconPath);
|
2015-11-15 17:43:55 +01:00
|
|
|
gtk.gdk_threads_leave();
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|