Преглед на файлове

rename to onBeforeOpenMenu and init local reference in showTrayIcon()

Edward Chow преди 2 години
родител
ревизия
dc310c009f
променени са 3 файла, в които са добавени 16 реда и са изтрити 12 реда
  1. 1 1
      pom.xml
  2. 14 10
      src/main/java/org/cryptomator/ui/traymenu/AwtTrayMenuController.java
  3. 1 1
      src/main/java/org/cryptomator/ui/traymenu/TrayMenuBuilder.java

+ 1 - 1
pom.xml

@@ -29,7 +29,7 @@
 		<!-- cryptomator dependencies -->
 		<cryptomator.cryptolib.version>2.1.0-rc1</cryptomator.cryptolib.version>
 		<cryptomator.cryptofs.version>2.4.4</cryptomator.cryptofs.version>
-		<cryptomator.integrations.version>1.1.1</cryptomator.integrations.version>
+		<cryptomator.integrations.version>1.2.0</cryptomator.integrations.version>
 		<cryptomator.integrations.win.version>1.1.2</cryptomator.integrations.win.version>
 		<cryptomator.integrations.mac.version>1.1.2</cryptomator.integrations.mac.version>
 		<cryptomator.integrations.linux.version>1.1.0</cryptomator.integrations.linux.version>

+ 14 - 10
src/main/java/org/cryptomator/ui/traymenu/AwtTrayMenuController.java

@@ -30,6 +30,7 @@ public class AwtTrayMenuController implements TrayMenuController {
 	private static final Logger LOG = LoggerFactory.getLogger(AwtTrayMenuController.class);
 
 	private final PopupMenu menu = new PopupMenu();
+	private TrayIcon trayIcon;
 
 	@CheckAvailability
 	public static boolean isAvailable() {
@@ -39,7 +40,7 @@ public class AwtTrayMenuController implements TrayMenuController {
 	@Override
 	public void showTrayIcon(byte[] rawImageData, Runnable defaultAction, String tooltip) throws TrayMenuException {
 		var image = Toolkit.getDefaultToolkit().createImage(rawImageData);
-		var trayIcon = new TrayIcon(image, tooltip, menu);
+		trayIcon = new TrayIcon(image, tooltip, menu);
 
 		trayIcon.setImageAutoSize(true);
 		if (SystemUtils.IS_OS_WINDOWS) {
@@ -62,15 +63,18 @@ public class AwtTrayMenuController implements TrayMenuController {
 
 
 	@Override
-	public void onBeforeShow(Runnable listener) {
-		SystemTray systemTray = SystemTray.getSystemTray();
-		TrayIcon trayIcon = systemTray.getTrayIcons()[0];
-		trayIcon.addMouseListener(new MouseAdapter() {
-			@Override
-			public void mouseClicked(MouseEvent e) {
-				listener.run();
-			}
-		});
+	public void onBeforeOpenMenu(Runnable listener) {
+		try {
+			this.trayIcon.addMouseListener(new MouseAdapter() {
+				@Override
+				public void mouseClicked(MouseEvent e) {
+					listener.run();
+				}
+			});
+		} catch (Exception e) {
+			throw new IllegalStateException("Tray icon not found.", e);
+		}
+
 	}
 
 	private void addChildren(Menu menu, List<TrayMenuItem> items) {

+ 1 - 1
src/main/java/org/cryptomator/ui/traymenu/TrayMenuBuilder.java

@@ -68,7 +68,7 @@ public class TrayMenuBuilder {
 
 		try (var image = getClass().getResourceAsStream(SystemUtils.IS_OS_MAC_OSX ? TRAY_ICON_MAC : TRAY_ICON)) {
 			trayMenu.showTrayIcon(image.readAllBytes(), this::showMainWindow, "Cryptomator");
-			trayMenu.onBeforeShow(() -> {
+			trayMenu.onBeforeOpenMenu(() -> {
 				for (Vault vault : vaults) {
 					VaultListManager.redetermineVaultState(vault);
 				}