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
|
|
|
|
2016-09-26 02:06:27 +02:00
|
|
|
import java.io.File;
|
2016-09-21 22:13:13 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
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;
|
2016-09-21 22:13:13 +02:00
|
|
|
|
2016-09-22 22:36:04 +02:00
|
|
|
import dorkbox.systemTray.SystemTray;
|
2016-05-08 15:51:07 +02:00
|
|
|
import dorkbox.systemTray.linux.jna.GEventCallback;
|
|
|
|
import dorkbox.systemTray.linux.jna.GdkEventButton;
|
2016-02-14 20:22:55 +01:00
|
|
|
import dorkbox.systemTray.linux.jna.Gobject;
|
|
|
|
import dorkbox.systemTray.linux.jna.Gtk;
|
2016-09-26 02:06:27 +02:00
|
|
|
import dorkbox.systemTray.util.ImageUtils;
|
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 {
|
2016-09-22 22:36:04 +02:00
|
|
|
private volatile Pointer trayIcon;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-09-23 14:24:37 +02:00
|
|
|
// http://code.metager.de/source/xref/gnome/Platform/gtk%2B/gtk/deprecated/gtkstatusicon.c
|
|
|
|
// https://github.com/djdeath/glib/blob/master/gobject/gobject.c
|
|
|
|
|
2016-04-24 17:32:07 +02:00
|
|
|
// have to save these in a field to prevent GC on the objects (since they go out-of-scope from java)
|
|
|
|
private final List<Object> gtkCallbacks = new ArrayList<Object>();
|
2016-02-12 01:51:15 +01:00
|
|
|
|
2016-02-15 13:27:33 +01:00
|
|
|
// This is required if we have JavaFX or SWT shutdown hooks (to prevent us from shutting down twice...)
|
|
|
|
private AtomicBoolean shuttingDown = new AtomicBoolean();
|
|
|
|
|
2016-02-12 02:30:33 +01:00
|
|
|
private volatile boolean isActive = false;
|
2014-11-16 22:01:27 +01:00
|
|
|
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-02-12 02:30:33 +01:00
|
|
|
GtkSystemTray() {
|
2015-11-09 16:13:57 +01:00
|
|
|
super();
|
2016-05-08 15:51:07 +02:00
|
|
|
Gtk.startGui();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-04-05 14:07:41 +02:00
|
|
|
dispatch(new Runnable() {
|
2014-11-03 02:11:03 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-02-13 15:06:19 +01:00
|
|
|
void run() {
|
2016-05-08 15:51:07 +02:00
|
|
|
final Pointer trayIcon_ = Gtk.gtk_status_icon_new();
|
2016-09-22 22:36:04 +02:00
|
|
|
trayIcon = trayIcon_;
|
2016-02-13 15:06:19 +01:00
|
|
|
|
2016-09-23 14:24:37 +02:00
|
|
|
final GEventCallback gtkCallback = new GEventCallback() {
|
2016-02-13 15:06:19 +01:00
|
|
|
@Override
|
|
|
|
public
|
2016-05-08 15:51:07 +02:00
|
|
|
void callback(Pointer notUsed, final GdkEventButton event) {
|
2016-02-13 15:06:19 +01:00
|
|
|
// BUTTON_PRESS only (any mouse click)
|
|
|
|
if (event.type == 4) {
|
2016-05-08 15:51:07 +02:00
|
|
|
Gtk.gtk_menu_popup(getMenu(), null, null, Gtk.gtk_status_icon_position_menu, trayIcon, 0, event.time);
|
2016-02-13 15:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-09-23 14:24:37 +02:00
|
|
|
final NativeLong button_press_event = Gobject.g_signal_connect_object(trayIcon, "button_press_event", gtkCallback,
|
2016-09-22 22:36:04 +02:00
|
|
|
null, 0);
|
2016-04-24 17:32:07 +02:00
|
|
|
|
|
|
|
// have to do this to prevent GC on these objects
|
2016-09-23 14:24:37 +02:00
|
|
|
gtkCallbacks.add(gtkCallback);
|
2016-04-24 17:32:07 +02:00
|
|
|
gtkCallbacks.add(button_press_event);
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2016-02-13 15:06:19 +01:00
|
|
|
});
|
2016-09-22 22:36:04 +02:00
|
|
|
|
2016-09-26 02:06:27 +02:00
|
|
|
super.waitForStartup();
|
2016-09-23 14:24:37 +02:00
|
|
|
|
2016-09-26 02:44:36 +02:00
|
|
|
ImageUtils.determineIconSize(SystemTray.TYPE_GTK_STATUSICON);
|
2016-09-26 02:06:27 +02:00
|
|
|
|
|
|
|
// we have to be able to set our title, otherwise the gnome-shell extension WILL NOT work
|
|
|
|
dispatch(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void run() {
|
|
|
|
// by default, the title/name of the tray icon is "java". We are the only java-based tray icon, so we just use that.
|
|
|
|
// If you change "SystemTray" to something else, make sure to change it in extension.js as well
|
|
|
|
|
|
|
|
// necessary for gnome icon detection/placement because we move tray icons around by title. This is hardcoded
|
|
|
|
// in extension.js, so don't change it
|
|
|
|
Gtk.gtk_status_icon_set_title(trayIcon, "SystemTray");
|
|
|
|
|
|
|
|
// can cause
|
|
|
|
// Gdk-CRITICAL **: gdk_window_thaw_toplevel_updates: assertion 'window->update_and_descendants_freeze_count > 0' failed
|
|
|
|
// Gdk-CRITICAL **: IA__gdk_window_thaw_toplevel_updates_libgtk_only: assertion 'private->update_and_descendants_freeze_count > 0' failed
|
2016-09-26 02:43:10 +02:00
|
|
|
|
|
|
|
// ... so, bizzaro things going on here. These errors DO NOT happen if JavaFX is dispatching the events.
|
|
|
|
// BUT this is REQUIRED when running JavaFX. For unknown reasons, the title isn't pushed to GTK, so our
|
|
|
|
// gnome-shell extension cannot see our tray icon -- so naturally, it won't move it to the "top" area and
|
|
|
|
// we appear broken.
|
|
|
|
if (SystemTray.isJavaFxLoaded) {
|
|
|
|
Gtk.gtk_status_icon_set_name(trayIcon, "SystemTray");
|
|
|
|
}
|
2016-09-22 22:36:04 +02:00
|
|
|
}
|
2016-09-26 02:06:27 +02:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 14:24:37 +02:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2016-02-13 15:06:19 +01:00
|
|
|
public
|
2015-11-09 16:13:57 +01:00
|
|
|
void shutdown() {
|
2016-02-15 13:27:33 +01:00
|
|
|
if (!shuttingDown.getAndSet(true)) {
|
2016-04-05 14:07:41 +02:00
|
|
|
dispatch(new Runnable() {
|
2016-02-15 13:27:33 +01:00
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void run() {
|
|
|
|
// this hides the indicator
|
2016-05-08 15:51:07 +02:00
|
|
|
Gtk.gtk_status_icon_set_visible(trayIcon, false);
|
|
|
|
Gobject.g_object_unref(trayIcon);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-04-24 17:32:07 +02:00
|
|
|
// mark for GC
|
2016-02-15 13:27:33 +01:00
|
|
|
trayIcon = null;
|
2016-04-24 17:32:07 +02:00
|
|
|
gtkCallbacks.clear();
|
2016-02-15 13:27:33 +01:00
|
|
|
}
|
|
|
|
});
|
2014-12-12 02:25:50 +01:00
|
|
|
|
2016-02-15 13:27:33 +01:00
|
|
|
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-13 15:06:19 +01:00
|
|
|
protected
|
2016-09-26 02:06:27 +02:00
|
|
|
void setIcon_(final File iconFile) {
|
2016-04-05 14:07:41 +02:00
|
|
|
dispatch(new Runnable() {
|
2016-02-13 15:06:19 +01:00
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void run() {
|
2016-09-26 02:06:27 +02:00
|
|
|
Gtk.gtk_status_icon_set_from_file(trayIcon, iconFile.getAbsolutePath());
|
2016-02-12 02:30:33 +01:00
|
|
|
|
2016-02-13 15:06:19 +01:00
|
|
|
if (!isActive) {
|
|
|
|
isActive = true;
|
2016-05-08 15:51:07 +02:00
|
|
|
Gtk.gtk_status_icon_set_visible(trayIcon, true);
|
2016-02-13 15:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|