From 919120701da84d0084e39da67c5eb7f681dbae62 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 10 Oct 2016 01:15:17 +0200 Subject: [PATCH] Added SystemTray.setEnabled(). Updated test examples. --- src/dorkbox/systemTray/SystemTray.java | 12 ++++---- .../systemTray/swing/_AppIndicatorTray.java | 30 ++++++++++++++++--- .../systemTray/swing/_GtkStatusIconTray.java | 20 +++++++++++++ src/dorkbox/systemTray/swing/_SwingTray.java | 30 +++++++++++++++++-- test/dorkbox/TestTray.java | 8 +++++ test/dorkbox/TestTrayJavaFX.java | 7 +++++ test/dorkbox/TestTraySwt.java | 7 +++++ 7 files changed, 101 insertions(+), 13 deletions(-) diff --git a/src/dorkbox/systemTray/SystemTray.java b/src/dorkbox/systemTray/SystemTray.java index 320a4902..be2b84d8 100644 --- a/src/dorkbox/systemTray/SystemTray.java +++ b/src/dorkbox/systemTray/SystemTray.java @@ -59,10 +59,6 @@ class SystemTray implements Menu { public static final int TYPE_APP_INDICATOR = 2; 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 /** Enables auto-detection for the system tray. This should be mostly successful. *

@@ -105,12 +101,14 @@ class SystemTray implements Menu { *

* 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 /** * 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 is an advanced feature, and it is recommended to leave as true. */ public static boolean ENABLE_SHUTDOWN_HOOK = true; @@ -761,13 +759,13 @@ class SystemTray implements Menu { systemTrayMenu.addSeparator(); } - /** - * Does nothing. You cannot set the text for the system tray + * Shows (if hidden), or hides (if showing) the system tray. */ @Override public void setEnabled(final boolean enabled) { + systemTrayMenu.setEnabled(enabled); } /** diff --git a/src/dorkbox/systemTray/swing/_AppIndicatorTray.java b/src/dorkbox/systemTray/swing/_AppIndicatorTray.java index 28734741..dd64b7cb 100644 --- a/src/dorkbox/systemTray/swing/_AppIndicatorTray.java +++ b/src/dorkbox/systemTray/swing/_AppIndicatorTray.java @@ -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 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) // they ALSO do not support tooltips, so we cater to the lowest common denominator @@ -198,17 +200,18 @@ class _AppIndicatorTray extends _Tray { public void shutdown() { 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() { @Override public void run() { // STATUS_PASSIVE hides the indicator - AppIndicator.app_indicator_set_status(appIndicator, AppIndicator.STATUS_PASSIVE); - Pointer p = appIndicator.getPointer(); + AppIndicator.app_indicator_set_status(savedAppI, AppIndicator.STATUS_PASSIVE); + Pointer p = savedAppI.getPointer(); 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); + } + } + }); + } } diff --git a/src/dorkbox/systemTray/swing/_GtkStatusIconTray.java b/src/dorkbox/systemTray/swing/_GtkStatusIconTray.java index 2382eedd..f04de23d 100644 --- a/src/dorkbox/systemTray/swing/_GtkStatusIconTray.java +++ b/src/dorkbox/systemTray/swing/_GtkStatusIconTray.java @@ -54,6 +54,9 @@ class _GtkStatusIconTray extends _Tray { private volatile boolean isActive = false; + // is the system tray visible or not. + private volatile boolean visible = true; + // called on the EDT public _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); + } + } + }); + } } diff --git a/src/dorkbox/systemTray/swing/_SwingTray.java b/src/dorkbox/systemTray/swing/_SwingTray.java index bae2bb55..483db219 100644 --- a/src/dorkbox/systemTray/swing/_SwingTray.java +++ b/src/dorkbox/systemTray/swing/_SwingTray.java @@ -39,8 +39,11 @@ import dorkbox.systemTray.Entry; @SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "WeakerAccess"}) public class _SwingTray extends _Tray { - volatile SystemTray tray; - volatile TrayIcon trayIcon; + private volatile SystemTray tray; + private volatile TrayIcon trayIcon; + + // is the system tray visible or not. + private volatile boolean visible = true; // Called in the EDT 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"); + } + } + } + }); + } } diff --git a/test/dorkbox/TestTray.java b/test/dorkbox/TestTray.java index 8095db21..4d936471 100644 --- a/test/dorkbox/TestTray.java +++ b/test/dorkbox/TestTray.java @@ -95,6 +95,14 @@ class TestTray { 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() { @Override public diff --git a/test/dorkbox/TestTrayJavaFX.java b/test/dorkbox/TestTrayJavaFX.java index f7d6a0d6..6d38253f 100644 --- a/test/dorkbox/TestTrayJavaFX.java +++ b/test/dorkbox/TestTrayJavaFX.java @@ -129,6 +129,13 @@ class TestTrayJavaFX extends Application { 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() { @Override public diff --git a/test/dorkbox/TestTraySwt.java b/test/dorkbox/TestTraySwt.java index de3fb6dc..327810fd 100644 --- a/test/dorkbox/TestTraySwt.java +++ b/test/dorkbox/TestTraySwt.java @@ -113,6 +113,13 @@ class TestTraySwt { 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() { @Override public