Added SystemTray.setEnabled(). Updated test examples.

This commit is contained in:
nathan 2016-10-10 01:15:17 +02:00
parent 53a9110605
commit 919120701d
7 changed files with 101 additions and 13 deletions

View File

@ -59,10 +59,6 @@ class SystemTray implements Menu {
public static final int TYPE_APP_INDICATOR = 2; public static final int TYPE_APP_INDICATOR = 2;
public static final int TYPE_SWING = 3; public static final int TYPE_SWING = 3;
@Property
/** How long to wait when updating menu entries before the request times-out */
public static final int TIMEOUT = 2;
@Property @Property
/** Enables auto-detection for the system tray. This should be mostly successful. /** Enables auto-detection for the system tray. This should be mostly successful.
* <p> * <p>
@ -105,12 +101,14 @@ class SystemTray implements Menu {
* <p> * <p>
* This is an advanced feature, and it is recommended to leave at 0. * This is an advanced feature, and it is recommended to leave at 0.
*/ */
public static int FORCE_TRAY_TYPE = 2; public static int FORCE_TRAY_TYPE = 0;
@Property @Property
/** /**
* When in compatibility mode, and the JavaFX/SWT primary windows are closed, we want to make sure that the SystemTray is also closed. * When in compatibility mode, and the JavaFX/SWT primary windows are closed, we want to make sure that the SystemTray is also closed.
* This property is available to disable this functionality in situations where you don't want this to happen. * This property is available to disable this functionality in situations where you don't want this to happen.
* <p>
* This is an advanced feature, and it is recommended to leave as true.
*/ */
public static boolean ENABLE_SHUTDOWN_HOOK = true; public static boolean ENABLE_SHUTDOWN_HOOK = true;
@ -761,13 +759,13 @@ class SystemTray implements Menu {
systemTrayMenu.addSeparator(); systemTrayMenu.addSeparator();
} }
/** /**
* Does nothing. You cannot set the text for the system tray * Shows (if hidden), or hides (if showing) the system tray.
*/ */
@Override @Override
public public
void setEnabled(final boolean enabled) { void setEnabled(final boolean enabled) {
systemTrayMenu.setEnabled(enabled);
} }
/** /**

View File

@ -96,6 +96,8 @@ class _AppIndicatorTray extends _Tray {
// necessary to provide a menu (which we draw over) so we get the "on open" event when the menu is opened via clicking // necessary to provide a menu (which we draw over) so we get the "on open" event when the menu is opened via clicking
private Pointer dummyMenu; private Pointer dummyMenu;
// is the system tray visible or not.
private volatile boolean visible = true;
// appindicators DO NOT support anything other than PLAIN gtk-menus (which we hack to support swing menus) // appindicators DO NOT support anything other than PLAIN gtk-menus (which we hack to support swing menus)
// they ALSO do not support tooltips, so we cater to the lowest common denominator // they ALSO do not support tooltips, so we cater to the lowest common denominator
@ -198,17 +200,18 @@ class _AppIndicatorTray extends _Tray {
public public
void shutdown() { void shutdown() {
if (!shuttingDown.getAndSet(true)) { if (!shuttingDown.getAndSet(true)) {
// must happen asap, so our hook properly notices we are in shutdown mode
final AppIndicatorInstanceStruct savedAppI = appIndicator;
appIndicator = null;
Gtk.dispatch(new Runnable() { Gtk.dispatch(new Runnable() {
@Override @Override
public public
void run() { void run() {
// STATUS_PASSIVE hides the indicator // STATUS_PASSIVE hides the indicator
AppIndicator.app_indicator_set_status(appIndicator, AppIndicator.STATUS_PASSIVE); AppIndicator.app_indicator_set_status(savedAppI, AppIndicator.STATUS_PASSIVE);
Pointer p = appIndicator.getPointer(); Pointer p = savedAppI.getPointer();
Gobject.g_object_unref(p); Gobject.g_object_unref(p);
appIndicator = null;
} }
}); });
@ -251,4 +254,23 @@ class _AppIndicatorTray extends _Tray {
} }
}); });
} }
public
void setEnabled(final boolean setEnabled) {
visible = !setEnabled;
Gtk.dispatch(new Runnable() {
@Override
public
void run() {
if (visible && !setEnabled) {
// STATUS_PASSIVE hides the indicator
AppIndicator.app_indicator_set_status(appIndicator, AppIndicator.STATUS_PASSIVE);
}
else if (!visible && setEnabled) {
AppIndicator.app_indicator_set_status(appIndicator, AppIndicator.STATUS_ACTIVE);
}
}
});
}
} }

View File

@ -54,6 +54,9 @@ class _GtkStatusIconTray extends _Tray {
private volatile boolean isActive = false; private volatile boolean isActive = false;
// is the system tray visible or not.
private volatile boolean visible = true;
// called on the EDT // called on the EDT
public public
_GtkStatusIconTray(final SystemTray systemTray) { _GtkStatusIconTray(final SystemTray systemTray) {
@ -193,4 +196,21 @@ class _GtkStatusIconTray extends _Tray {
} }
}); });
} }
public
void setEnabled(final boolean setEnabled) {
visible = !setEnabled;
Gtk.dispatch(new Runnable() {
@Override
public
void run() {
if (visible && !setEnabled) {
Gtk.gtk_status_icon_set_visible(trayIcon, setEnabled);
} else if (!visible && setEnabled) {
Gtk.gtk_status_icon_set_visible(trayIcon, setEnabled);
}
}
});
}
} }

View File

@ -39,8 +39,11 @@ import dorkbox.systemTray.Entry;
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "WeakerAccess"}) @SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "WeakerAccess"})
public public
class _SwingTray extends _Tray { class _SwingTray extends _Tray {
volatile SystemTray tray; private volatile SystemTray tray;
volatile TrayIcon trayIcon; private volatile TrayIcon trayIcon;
// is the system tray visible or not.
private volatile boolean visible = true;
// Called in the EDT // Called in the EDT
public public
@ -114,4 +117,27 @@ class _SwingTray extends _Tray {
} }
}); });
} }
public
void setEnabled(final boolean setEnabled) {
visible = !setEnabled;
dispatch(new Runnable() {
@Override
public
void run() {
if (visible && !setEnabled) {
tray.remove(trayIcon);
}
else if (!visible && setEnabled) {
try {
tray.add(trayIcon);
} catch (AWTException e) {
dorkbox.systemTray.SystemTray.logger.error("Error adding the icon back to the tray");
}
}
}
});
}
} }

View File

@ -95,6 +95,14 @@ class TestTray {
submenu.setEnabled(false); submenu.setEnabled(false);
} }
}); });
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
@Override
public
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
systemTray.setEnabled(false);
}
});
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() { submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
@Override @Override
public public

View File

@ -129,6 +129,13 @@ class TestTrayJavaFX extends Application {
submenu.setEnabled(false); submenu.setEnabled(false);
} }
}); });
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
@Override
public
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
systemTray.setEnabled(false);
}
});
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() { submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
@Override @Override
public public

View File

@ -113,6 +113,13 @@ class TestTraySwt {
submenu.setEnabled(false); submenu.setEnabled(false);
} }
}); });
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
@Override
public
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
systemTray.setEnabled(false);
}
});
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() { submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
@Override @Override
public public