DTG: Support notification disable/enable in non-router context

by moving all the code from InternalTrayManager to TrayManager
and adding to the ExternalTrayManager menu
As requested by R4SAS
This commit is contained in:
zzz
2023-02-09 14:09:10 -05:00
parent d3e5879068
commit 4881ac32b6
3 changed files with 113 additions and 68 deletions

View File

@ -51,6 +51,9 @@ class ExternalTrayManager extends TrayManager {
} }
}); });
popup.add(startItem); popup.add(startItem);
initializeNotificationItems();
popup.add(_notificationItem2);
popup.add(_notificationItem1);
return popup; return popup;
} }
@ -79,6 +82,9 @@ class ExternalTrayManager extends TrayManager {
} }
}); });
popup.add(startItem); popup.add(startItem);
initializeJNotificationItems();
popup.add(_jnotificationItem2);
popup.add(_jnotificationItem1);
return popup; return popup;
} }
@ -86,5 +92,14 @@ class ExternalTrayManager extends TrayManager {
* Update the menu * Update the menu
* @since 0.9.26 * @since 0.9.26
*/ */
protected void updateMenu() {} protected void updateMenu() {
if (_notificationItem1 != null)
_notificationItem1.setEnabled(_showNotifications);
if (_notificationItem2 != null)
_notificationItem2.setEnabled(!_showNotifications);
if (_jnotificationItem1 != null)
_jnotificationItem1.setVisible(_showNotifications);
if (_jnotificationItem2 != null)
_jnotificationItem2.setVisible(!_showNotifications);
}
} }

View File

@ -32,11 +32,9 @@ class InternalTrayManager extends TrayManager {
private final Log log; private final Log log;
private final Main _main; private final Main _main;
private MenuItem _statusItem, _browserItem, _configItem, _restartItem, _stopItem, private MenuItem _statusItem, _browserItem, _configItem, _restartItem, _stopItem,
_restartHardItem, _stopHardItem, _cancelItem, _restartHardItem, _stopHardItem, _cancelItem;
_notificationItem1, _notificationItem2;
private JMenuItem _jstatusItem, _jbrowserItem, _jconfigItem, _jrestartItem, _jstopItem, private JMenuItem _jstatusItem, _jbrowserItem, _jconfigItem, _jrestartItem, _jstopItem,
_jrestartHardItem, _jstopHardItem, _jcancelItem, _jrestartHardItem, _jstopHardItem, _jcancelItem;
_jnotificationItem1, _jnotificationItem2;
private static final boolean CONSOLE_ENABLED = Desktop.isDesktopSupported() && private static final boolean CONSOLE_ENABLED = Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Action.BROWSE); Desktop.getDesktop().isSupported(Action.BROWSE);
@ -86,33 +84,6 @@ class InternalTrayManager extends TrayManager {
} }
PopupMenu desktopguiConfigurationLauncher = new PopupMenu(_t("Configure I2P System Tray")); PopupMenu desktopguiConfigurationLauncher = new PopupMenu(_t("Configure I2P System Tray"));
final MenuItem notificationItem2 = new MenuItem(_t("Enable notifications"));
notificationItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(true);
return null;
}
}.execute();
}
});
final MenuItem notificationItem1 = new MenuItem(_t("Disable notifications"));
notificationItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(false);
return null;
}
}.execute();
}
});
MenuItem configSubmenu = new MenuItem(_t("Disable system tray")); MenuItem configSubmenu = new MenuItem(_t("Disable system tray"));
configSubmenu.addActionListener(new ActionListener() { configSubmenu.addActionListener(new ActionListener() {
@ -214,8 +185,9 @@ class InternalTrayManager extends TrayManager {
popup.add(browserLauncher); popup.add(browserLauncher);
popup.addSeparator(); popup.addSeparator();
} }
desktopguiConfigurationLauncher.add(notificationItem2); initializeNotificationItems();
desktopguiConfigurationLauncher.add(notificationItem1); desktopguiConfigurationLauncher.add(_notificationItem2);
desktopguiConfigurationLauncher.add(_notificationItem1);
desktopguiConfigurationLauncher.add(configSubmenu); desktopguiConfigurationLauncher.add(configSubmenu);
popup.add(desktopguiConfigurationLauncher); popup.add(desktopguiConfigurationLauncher);
popup.addSeparator(); popup.addSeparator();
@ -230,8 +202,6 @@ class InternalTrayManager extends TrayManager {
_statusItem = statusItem; _statusItem = statusItem;
_browserItem = browserLauncher; _browserItem = browserLauncher;
_configItem = desktopguiConfigurationLauncher; _configItem = desktopguiConfigurationLauncher;
_notificationItem1 = notificationItem1;
_notificationItem2 = notificationItem2;
_restartItem = restartItem; _restartItem = restartItem;
_stopItem = stopItem; _stopItem = stopItem;
_restartHardItem = restartItem2; _restartHardItem = restartItem2;
@ -270,33 +240,6 @@ class InternalTrayManager extends TrayManager {
} }
JMenu desktopguiConfigurationLauncher = new JMenu(_t("Configure I2P System Tray")); JMenu desktopguiConfigurationLauncher = new JMenu(_t("Configure I2P System Tray"));
final JMenuItem notificationItem2 = new JMenuItem(_t("Enable notifications"));
notificationItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(true);
return null;
}
}.execute();
}
});
final JMenuItem notificationItem1 = new JMenuItem(_t("Disable notifications"));
notificationItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(false);
return null;
}
}.execute();
}
});
JMenuItem configSubmenu = new JMenuItem(_t("Disable system tray")); JMenuItem configSubmenu = new JMenuItem(_t("Disable system tray"));
configSubmenu.addActionListener(new ActionListener() { configSubmenu.addActionListener(new ActionListener() {
@ -398,8 +341,9 @@ class InternalTrayManager extends TrayManager {
popup.add(browserLauncher); popup.add(browserLauncher);
popup.addSeparator(); popup.addSeparator();
} }
desktopguiConfigurationLauncher.add(notificationItem2); initializeJNotificationItems();
desktopguiConfigurationLauncher.add(notificationItem1); desktopguiConfigurationLauncher.add(_jnotificationItem2);
desktopguiConfigurationLauncher.add(_jnotificationItem1);
desktopguiConfigurationLauncher.add(configSubmenu); desktopguiConfigurationLauncher.add(configSubmenu);
popup.add(desktopguiConfigurationLauncher); popup.add(desktopguiConfigurationLauncher);
popup.addSeparator(); popup.addSeparator();
@ -414,8 +358,6 @@ class InternalTrayManager extends TrayManager {
_jstatusItem = statusItem; _jstatusItem = statusItem;
_jbrowserItem = browserLauncher; _jbrowserItem = browserLauncher;
_jconfigItem = desktopguiConfigurationLauncher; _jconfigItem = desktopguiConfigurationLauncher;
_jnotificationItem1 = notificationItem1;
_jnotificationItem2 = notificationItem2;
_jrestartItem = restartItem; _jrestartItem = restartItem;
_jstopItem = stopItem; _jstopItem = stopItem;
_jrestartHardItem = restartItem2; _jrestartHardItem = restartItem2;
@ -511,7 +453,8 @@ class InternalTrayManager extends TrayManager {
/** /**
* @since 0.9.53 * @since 0.9.53
*/ */
private void configureNotifications(boolean enable) { @Override
protected void configureNotifications(boolean enable) {
_showNotifications = enable; _showNotifications = enable;
String value = Boolean.toString(enable); String value = Boolean.toString(enable);
if (!_context.router().saveConfig(PROP_NOTIFICATIONS, value)) if (!_context.router().saveConfig(PROP_NOTIFICATIONS, value))

View File

@ -17,7 +17,9 @@ import java.awt.event.MouseListener;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.awt.MenuItem;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.event.MenuKeyEvent; import javax.swing.event.MenuKeyEvent;
@ -43,6 +45,8 @@ abstract class TrayManager {
///Our tray icon, or null if unsupported ///Our tray icon, or null if unsupported
protected TrayIcon trayIcon; protected TrayIcon trayIcon;
protected volatile boolean _showNotifications; protected volatile boolean _showNotifications;
protected MenuItem _notificationItem1, _notificationItem2;
protected JMenuItem _jnotificationItem1, _jnotificationItem2;
private static final String PNG_DIR = "/desktopgui/resources/images/"; private static final String PNG_DIR = "/desktopgui/resources/images/";
private static final String MAC_ICON = "itoopie_black_24.png"; private static final String MAC_ICON = "itoopie_black_24.png";
@ -288,6 +292,89 @@ abstract class TrayManager {
return 0; return 0;
} }
/**
* Does not save. See InternalTrayManager.
*
* @since 0.9.58 moved up from InternalTrayManager
*/
protected void configureNotifications(boolean enable) {
_showNotifications = enable;
}
/**
* Initializes _notificationItem 1 and 2
*
* @since 0.9.58 pulled out of InternalTrayManager
*/
protected void initializeNotificationItems() {
final MenuItem notificationItem2 = new MenuItem(_t("Enable notifications"));
notificationItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(true);
return null;
}
}.execute();
}
});
_notificationItem2 = notificationItem2;
final MenuItem notificationItem1 = new MenuItem(_t("Disable notifications"));
notificationItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(false);
return null;
}
}.execute();
}
});
_notificationItem1 = notificationItem1;
}
/**
* Initializes _jnotificationItem 1 and 2
*
* @since 0.9.58 pulled out of InternalTrayManager
*/
protected void initializeJNotificationItems() {
final JMenuItem notificationItem2 = new JMenuItem(_t("Enable notifications"));
notificationItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(true);
return null;
}
}.execute();
}
});
_jnotificationItem2 = notificationItem2;
final JMenuItem notificationItem1 = new JMenuItem(_t("Disable notifications"));
notificationItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
configureNotifications(false);
return null;
}
}.execute();
}
});
_jnotificationItem1 = notificationItem1;
}
protected String _t(String s) { protected String _t(String s) {
return DesktopguiTranslator._t(_appContext, s); return DesktopguiTranslator._t(_appContext, s);
} }