SystemTray/src/dorkbox/systemTray/linux/GtkSystemTray.java

119 lines
3.9 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.
*/
package dorkbox.systemTray.linux;
2014-11-03 02:11:03 +01:00
import com.sun.jna.NativeLong;
2014-11-24 17:40:06 +01:00
import com.sun.jna.Pointer;
import dorkbox.systemTray.linux.jna.Gobject;
import dorkbox.systemTray.linux.jna.Gtk;
import dorkbox.systemTray.linux.jna.GtkSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
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 GtkTypeSystemTray {
private Pointer trayIcon;
2014-11-03 02:11:03 +01: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>();
// This is required if we have JavaFX or SWT shutdown hooks (to prevent us from shutting down twice...)
private AtomicBoolean shuttingDown = new AtomicBoolean();
private volatile boolean isActive = false;
public
GtkSystemTray() {
super();
GtkSupport.startGui();
2014-11-03 02:11:03 +01:00
dispatch(new Runnable() {
2014-11-03 02:11:03 +01:00
@Override
public
void run() {
final Pointer trayIcon_ = gtk.gtk_status_icon_new();
gtk.gtk_status_icon_set_title(trayIcon_, GnomeShellExtension.UID);
gtk.gtk_status_icon_set_name(trayIcon_, "SystemTray");
trayIcon = trayIcon_;
final Gobject.GEventCallback gtkCallback = new Gobject.GEventCallback() {
@Override
public
void callback(Pointer notUsed, final Gtk.GdkEventButton event) {
// BUTTON_PRESS only (any mouse click)
if (event.type == 4) {
gtk.gtk_menu_popup(getMenu(), null, null, Gtk.gtk_status_icon_position_menu, trayIcon, 0, event.time);
}
}
};
final NativeLong button_press_event = gobject.g_signal_connect_object(trayIcon, "button_press_event", gtkCallback, null, 0);
// have to do this to prevent GC on these objects
gtkCallbacks.add(gtkCallback);
gtkCallbacks.add(button_press_event);
2014-11-03 02:11:03 +01:00
}
});
2014-11-03 02:11:03 +01:00
}
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
2014-11-24 17:40:06 +01:00
@Override
public
void shutdown() {
if (!shuttingDown.getAndSet(true)) {
dispatch(new Runnable() {
@Override
public
void run() {
// this hides the indicator
gtk.gtk_status_icon_set_visible(trayIcon, false);
gobject.g_object_unref(trayIcon);
2014-11-03 02:11:03 +01:00
// mark for GC
trayIcon = null;
gtkCallbacks.clear();
}
});
super.shutdown();
}
2014-11-24 17:40:06 +01:00
}
2014-11-24 17:40:06 +01:00
@Override
protected
void setIcon_(final String iconPath) {
dispatch(new Runnable() {
@Override
public
void run() {
gtk.gtk_status_icon_set_from_file(trayIcon, iconPath);
if (!isActive) {
isActive = true;
gtk.gtk_status_icon_set_visible(trayIcon, true);
}
}
});
2014-11-03 02:11:03 +01:00
}
}