diff --git a/src/dorkbox/systemTray/Menu.java b/src/dorkbox/systemTray/Menu.java index f244964..3c5bd42 100644 --- a/src/dorkbox/systemTray/Menu.java +++ b/src/dorkbox/systemTray/Menu.java @@ -29,7 +29,7 @@ import dorkbox.systemTray.util.ImageUtils; */ @SuppressWarnings({"WeakerAccess", "unused"}) public abstract -class Menu { +class Menu implements MenuEntry { public static final AtomicInteger MENU_ID_COUNTER = new AtomicInteger(); private final int id = Menu.MENU_ID_COUNTER.getAndIncrement(); diff --git a/src/dorkbox/systemTray/SystemTray.java b/src/dorkbox/systemTray/SystemTray.java index 2251ad5..eacab9e 100644 --- a/src/dorkbox/systemTray/SystemTray.java +++ b/src/dorkbox/systemTray/SystemTray.java @@ -746,72 +746,19 @@ class SystemTray extends Menu { } protected - void setIcon_(File iconPath) { + void setImage_(File iconPath) { final Menu menu = systemTrayMenu; if (menu instanceof AppIndicatorTray) { - ((AppIndicatorTray) menu).setIcon_(iconPath); + ((AppIndicatorTray) menu).setImage_(iconPath); } else if (menu instanceof GtkSystemTray) { - ((GtkSystemTray) menu).setIcon_(iconPath); + ((GtkSystemTray) menu).setImage_(iconPath); } else { - // swing - ((SwingSystemTray) menu).setIcon_(iconPath); + // swing (windows/mac) + ((SwingSystemTray) menu).setImage_(iconPath); } } - /** - * Changes the tray icon used. - * - * Because the cross-platform, underlying system uses a file path to load icons for the system tray, - * this will directly use the contents of the specified file. - * - * @param imagePath the path of the icon to use - */ - public - void setIcon(String imagePath) { - setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imagePath)); - } - - /** - * Changes the tray icon used. - * - * Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of - * the URL to a temporary location on disk, based on the path specified by the URL. - * - * @param imageUrl the URL of the icon to use - */ - public - void setIcon(URL imageUrl) { - setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageUrl)); - } - - /** - * Changes the tray icon used. - * - * Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of - * the imageStream to a temporary location on disk, based on the `cacheName` specified. - * - * @param cacheName the name to use for lookup in the cache for the iconStream - * @param imageStream the InputStream of the icon to use - */ - public - void setIcon(String cacheName, InputStream imageStream) { - setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, cacheName, imageStream)); - } - - /** - * Changes the tray icon used. - * - * Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of - * the imageStream to a temporary location on disk. - * - * @param imageStream the InputStream of the icon to use - */ - public - void setIcon(InputStream imageStream) { - setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageStream)); - } - /** * @return the parent menu (of this menu) or null if we are the root menu */ @@ -866,6 +813,110 @@ class SystemTray extends Menu { void setEnabled(final boolean enabled) { } + @Override + public + String getText() { + return ""; + } + + // NO OP. + @Override + public + void setText(final String newText) { + } + + /** + * Changes the tray image used. + * + * Because the cross-platform, underlying system uses a file path to load images for the system tray, + * this will directly use the contents of the specified file. + * + * @param imageFile the path of the image to use + */ + @Override + public + void setImage(final File imageFile) { + setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageFile)); + } + + /** + * Changes the tray image used. + * + * Because the cross-platform, underlying system uses a file path to load images for the system tray, + * this will directly use the contents of the specified file. + * + * @param imagePath the path of the image to use + */ + @Override + public + void setImage(final String imagePath) { + setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imagePath)); + } + + /** + * Changes the tray image used. + * + * Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of + * the URL to a temporary location on disk, based on the path specified by the URL. + * + * @param imageUrl the URL of the image to use + */ + @Override + public + void setImage(URL imageUrl) { + setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageUrl)); + } + + /** + * Changes the tray image used. + * + * Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of + * the imageStream to a temporary location on disk, based on the `cacheName` specified. + * + * @param cacheName the name to use for lookup in the cache for the imageStream + * @param imageStream the InputStream of the image to use + */ + @Override + public + void setImage(String cacheName, InputStream imageStream) { + setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, cacheName, imageStream)); + } + + /** + * Changes the tray image used. + * + * Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of + * the imageStream to a temporary location on disk. + * + * @param imageStream the InputStream of the image to use + */ + @Override + public + void setImage(final InputStream imageStream) { + setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageStream)); + } + + /** + * By default, we always have an image for the system tray + */ + @Override + public + boolean hasImage() { + return true; + } + + // NO OP. + @Override + public + void setCallback(final SystemTrayMenuAction callback) { + } + + // NO OP. + @Override + public + void setShortcut(final char key) { + } + // NO OP. @Override protected diff --git a/src/dorkbox/systemTray/linux/AppIndicatorTray.java b/src/dorkbox/systemTray/linux/AppIndicatorTray.java index 323ee29..11194e8 100644 --- a/src/dorkbox/systemTray/linux/AppIndicatorTray.java +++ b/src/dorkbox/systemTray/linux/AppIndicatorTray.java @@ -105,7 +105,7 @@ class AppIndicatorTray extends GtkTypeSystemTray { } public - void setIcon_(final File iconFile) { + void setImage_(final File iconFile) { dispatch(new Runnable() { @Override public diff --git a/src/dorkbox/systemTray/linux/GtkMenu.java b/src/dorkbox/systemTray/linux/GtkMenu.java index 10b3542..ffba45c 100644 --- a/src/dorkbox/systemTray/linux/GtkMenu.java +++ b/src/dorkbox/systemTray/linux/GtkMenu.java @@ -34,7 +34,8 @@ import dorkbox.systemTray.SystemTrayMenuAction; import dorkbox.systemTray.linux.jna.Gobject; import dorkbox.systemTray.linux.jna.Gtk; -class GtkMenu extends Menu implements MenuEntry { +@SuppressWarnings("ForLoopReplaceableByForEach") +class GtkMenu extends Menu { // menu entry that this menu is attached to. Will be NULL when it's the system tray private final GtkEntryItem menuEntry; @@ -233,9 +234,8 @@ class GtkMenu extends Menu implements MenuEntry { if (_native != null) { // have to remove all other menu entries synchronized (menuEntries) { - for (int i = 0; i < menuEntries.size(); i++) { - MenuEntry menuEntry__ = menuEntries.get(i); - + for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) { + final MenuEntry menuEntry__ = menuEntries.get(i); if (menuEntry__ instanceof GtkEntry) { GtkEntry entry = (GtkEntry) menuEntry__; @@ -282,11 +282,13 @@ class GtkMenu extends Menu implements MenuEntry { // now add back other menu entries synchronized (menuEntries) { - for (MenuEntry menuEntry__ : menuEntries) { + for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) { + final MenuEntry menuEntry__ = menuEntries.get(i); hasImages |= menuEntry__.hasImage(); } - for (MenuEntry menuEntry__ : menuEntries) { + for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) { + final MenuEntry menuEntry__ = menuEntries.get(i); // the menu entry looks FUNKY when there are a mis-match of entries WITH and WITHOUT images if (menuEntry__ instanceof GtkEntry) { GtkEntry entry = (GtkEntry) menuEntry__; @@ -327,8 +329,8 @@ class GtkMenu extends Menu implements MenuEntry { // have to remove all other menu entries synchronized (menuEntries) { - for (int i = 0; i < menuEntries.size(); i++) { - MenuEntry menuEntry__ = menuEntries.get(i); + for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) { + final MenuEntry menuEntry__ = menuEntries.get(i); menuEntry__.remove(); } menuEntries.clear(); diff --git a/src/dorkbox/systemTray/linux/GtkSystemTray.java b/src/dorkbox/systemTray/linux/GtkSystemTray.java index 152ac81..764b0a4 100644 --- a/src/dorkbox/systemTray/linux/GtkSystemTray.java +++ b/src/dorkbox/systemTray/linux/GtkSystemTray.java @@ -141,7 +141,7 @@ class GtkSystemTray extends GtkTypeSystemTray { } public - void setIcon_(final File iconFile) { + void setImage_(final File iconFile) { dispatch(new Runnable() { @Override public diff --git a/src/dorkbox/systemTray/linux/GtkTypeSystemTray.java b/src/dorkbox/systemTray/linux/GtkTypeSystemTray.java index 6a9cfae..1a45d98 100644 --- a/src/dorkbox/systemTray/linux/GtkTypeSystemTray.java +++ b/src/dorkbox/systemTray/linux/GtkTypeSystemTray.java @@ -70,7 +70,8 @@ class GtkTypeSystemTray extends GtkMenu { menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText); // status is ALWAYS at 0 index... menuEntries.add(0, menuEntry); - } else if (menuEntry instanceof GtkEntryStatus) { + } + else if (menuEntry instanceof GtkEntryStatus) { // change the text? if (statusText != null) { menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText); diff --git a/src/dorkbox/systemTray/swing/SwingMenu.java b/src/dorkbox/systemTray/swing/SwingMenu.java index f68570a..45c6b23 100644 --- a/src/dorkbox/systemTray/swing/SwingMenu.java +++ b/src/dorkbox/systemTray/swing/SwingMenu.java @@ -35,7 +35,7 @@ import dorkbox.systemTray.util.ImageUtils; import dorkbox.util.SwingUtil; // this is a weird composite class, because it must be a Menu, but ALSO a MenuEntry -- so it has both -class SwingMenu extends Menu implements MenuEntry { +class SwingMenu extends Menu { volatile JComponent _native; diff --git a/src/dorkbox/systemTray/swing/SwingSystemTray.java b/src/dorkbox/systemTray/swing/SwingSystemTray.java index a18e6ae..146ef7e 100644 --- a/src/dorkbox/systemTray/swing/SwingSystemTray.java +++ b/src/dorkbox/systemTray/swing/SwingSystemTray.java @@ -134,7 +134,7 @@ class SwingSystemTray extends SwingMenu { } public - void setIcon_(final File iconFile) { + void setImage_(final File iconFile) { dispatch(new Runnable() { @Override public diff --git a/test/dorkbox/TestTray.java b/test/dorkbox/TestTray.java index c103e11..679f98f 100644 --- a/test/dorkbox/TestTray.java +++ b/test/dorkbox/TestTray.java @@ -50,7 +50,7 @@ class TestTray { throw new RuntimeException("Unable to load SystemTray!"); } - systemTray.setIcon(LT_GRAY_MAIL); + systemTray.setImage(LT_GRAY_MAIL); systemTray.setStatus("No Mail"); callbackGreen = new SystemTrayMenuAction() { @@ -58,7 +58,7 @@ class TestTray { public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { systemTray.setStatus("Some Mail!"); - systemTray.setIcon(GREEN_MAIL); + systemTray.setImage(GREEN_MAIL); menuEntry.setCallback(callbackGray); menuEntry.setImage(BLACK_MAIL); @@ -72,7 +72,7 @@ class TestTray { public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { systemTray.setStatus(null); - systemTray.setIcon(BLACK_MAIL); + systemTray.setImage(BLACK_MAIL); menuEntry.setCallback(null); // systemTray.setStatus("Mail Empty"); @@ -81,7 +81,10 @@ class TestTray { } }; - this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + // case does not matter + menuEntry.setShortcut('G'); + this.systemTray.addSeparator(); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); @@ -108,6 +111,6 @@ class TestTray { systemTray.shutdown(); //System.exit(0); not necessary if all non-daemon threads have stopped. } - }); + }).setShortcut('q'); // case does not matter } } diff --git a/test/dorkbox/TestTrayJavaFX.java b/test/dorkbox/TestTrayJavaFX.java index 64c5b37..ba40341 100644 --- a/test/dorkbox/TestTrayJavaFX.java +++ b/test/dorkbox/TestTrayJavaFX.java @@ -83,7 +83,7 @@ class TestTrayJavaFX extends Application { throw new RuntimeException("Unable to load SystemTray!"); } - this.systemTray.setIcon(LT_GRAY_MAIL); + this.systemTray.setImage(LT_GRAY_MAIL); systemTray.setStatus("No Mail"); @@ -91,7 +91,7 @@ class TestTrayJavaFX extends Application { @Override public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { - systemTray.setIcon(GREEN_MAIL); + systemTray.setImage(GREEN_MAIL); systemTray.setStatus("Some Mail!"); menuEntry.setCallback(callbackGray); @@ -106,7 +106,7 @@ class TestTrayJavaFX extends Application { public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { systemTray.setStatus(null); - systemTray.setIcon(BLACK_MAIL); + systemTray.setImage(BLACK_MAIL); menuEntry.setCallback(null); // systemTray.setStatus("Mail Empty"); @@ -115,7 +115,10 @@ class TestTrayJavaFX extends Application { } }; - this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + // case does not matter + menuEntry.setShortcut('G'); + this.systemTray.addSeparator(); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); @@ -142,6 +145,6 @@ class TestTrayJavaFX extends Application { Platform.exit(); // necessary to close javaFx //System.exit(0); not necessary if all non-daemon threads have stopped. } - }); + }).setShortcut('q'); // case does not matter } } diff --git a/test/dorkbox/TestTraySwt.java b/test/dorkbox/TestTraySwt.java index b9e35ba..13296f4 100644 --- a/test/dorkbox/TestTraySwt.java +++ b/test/dorkbox/TestTraySwt.java @@ -67,7 +67,7 @@ class TestTraySwt { throw new RuntimeException("Unable to load SystemTray!"); } - this.systemTray.setIcon(LT_GRAY_MAIL); + this.systemTray.setImage(LT_GRAY_MAIL); systemTray.setStatus("No Mail"); @@ -76,7 +76,7 @@ class TestTraySwt { public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { systemTray.setStatus("Some Mail!"); - systemTray.setIcon(GREEN_MAIL); + systemTray.setImage(GREEN_MAIL); menuEntry.setCallback(callbackGray); menuEntry.setImage(BLACK_MAIL); @@ -90,7 +90,7 @@ class TestTraySwt { public void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { systemTray.setStatus(null); - systemTray.setIcon(BLACK_MAIL); + systemTray.setImage(BLACK_MAIL); menuEntry.setCallback(null); // systemTray.setStatus("Mail Empty"); @@ -99,7 +99,10 @@ class TestTraySwt { } }; - this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); + // case does not matter + menuEntry.setShortcut('G'); + this.systemTray.addSeparator(); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); @@ -132,7 +135,7 @@ class TestTraySwt { //System.exit(0); not necessary if all non-daemon threads have stopped. } - }); + }).setShortcut('q'); // case does not matter