Browse Source

adjust to latest API

Sebastian Stenzel 3 years ago
parent
commit
d7da78fc07

+ 1 - 1
pom.xml

@@ -28,7 +28,7 @@
 
 		<!-- cryptomator dependencies -->
 		<cryptomator.cryptofs.version>2.4.0</cryptomator.cryptofs.version>
-		<cryptomator.integrations.version>1.1.0-beta2</cryptomator.integrations.version>
+		<cryptomator.integrations.version>1.1.0-beta3</cryptomator.integrations.version>
 		<cryptomator.integrations.win.version>1.1.0-beta1</cryptomator.integrations.win.version>
 		<cryptomator.integrations.mac.version>1.1.0-beta1</cryptomator.integrations.mac.version>
 		<cryptomator.integrations.linux.version>1.1.0-beta1</cryptomator.integrations.linux.version>

+ 1 - 0
src/main/java/module-info.java

@@ -29,6 +29,7 @@ module org.cryptomator.desktop {
 	requires logback.classic;
 	requires logback.core;
 
+	exports org.cryptomator.ui.traymenu to org.cryptomator.integrations.api;
 	provides TrayMenuController with AwtTrayMenuController;
 
 	opens org.cryptomator.common.settings to com.google.gson;

+ 1 - 1
src/main/java/org/cryptomator/ui/fxapp/FxApplication.java

@@ -44,7 +44,7 @@ public class FxApplication {
 
 		// init system tray
 		final boolean hasTrayIcon;
-		if (SystemTray.isSupported() && settings.showTrayIcon().get()) {
+		if (settings.showTrayIcon().get() && trayMenu.get().isSupported()) {
 			trayMenu.get().initializeTrayIcon();
 			Platform.setImplicitExit(false); // don't quit when closing all windows
 			hasTrayIcon = true;

+ 13 - 6
src/main/java/org/cryptomator/ui/traymenu/AwtTrayMenuController.java

@@ -1,11 +1,13 @@
 package org.cryptomator.ui.traymenu;
 
 import org.apache.commons.lang3.SystemUtils;
+import org.cryptomator.integrations.common.CheckAvailability;
 import org.cryptomator.integrations.common.Priority;
 import org.cryptomator.integrations.tray.ActionItem;
 import org.cryptomator.integrations.tray.SeparatorItem;
 import org.cryptomator.integrations.tray.SubMenuItem;
 import org.cryptomator.integrations.tray.TrayMenuController;
+import org.cryptomator.integrations.tray.TrayMenuException;
 import org.cryptomator.integrations.tray.TrayMenuItem;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -21,18 +23,23 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
+@CheckAvailability
 @Priority(Priority.FALLBACK)
 public class AwtTrayMenuController implements TrayMenuController {
 
 	private static final Logger LOG = LoggerFactory.getLogger(AwtTrayMenuController.class);
 
-	private TrayIcon trayIcon;
-	private PopupMenu menu = new PopupMenu();
+	private final PopupMenu menu = new PopupMenu();
+
+	@CheckAvailability
+	public static boolean isAvailable() {
+		return SystemTray.isSupported();
+	}
 
 	@Override
-	public void showTrayIcon(InputStream rawImageData, Runnable defaultAction, String tooltip) throws IOException {
-		var image = Toolkit.getDefaultToolkit().createImage(rawImageData.readAllBytes());
-		trayIcon = new TrayIcon(image, tooltip, menu);
+	public void showTrayIcon(byte[] rawImageData, Runnable defaultAction, String tooltip) {
+		var image = Toolkit.getDefaultToolkit().createImage(rawImageData);
+		var trayIcon = new TrayIcon(image, tooltip, menu);
 
 		trayIcon.setImageAutoSize(true);
 		if (SystemUtils.IS_OS_WINDOWS) {
@@ -59,7 +66,7 @@ public class AwtTrayMenuController implements TrayMenuController {
 			if (item instanceof ActionItem a) {
 				var menuItem = new MenuItem(a.title());
 				menuItem.addActionListener(evt -> a.action().run());
-				// TODO menuItem.setEnabled(a.enabled());
+				menuItem.setEnabled(a.enabled());
 				menu.add(menuItem);
 			} else if (item instanceof SeparatorItem) {
 				menu.addSeparator();

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

@@ -7,11 +7,14 @@ import org.cryptomator.integrations.tray.ActionItem;
 import org.cryptomator.integrations.tray.SeparatorItem;
 import org.cryptomator.integrations.tray.SubMenuItem;
 import org.cryptomator.integrations.tray.TrayMenuController;
+import org.cryptomator.integrations.tray.TrayMenuException;
 import org.cryptomator.integrations.tray.TrayMenuItem;
 import org.cryptomator.ui.common.VaultService;
 import org.cryptomator.ui.fxapp.FxApplicationTerminator;
 import org.cryptomator.ui.fxapp.FxApplicationWindows;
 import org.cryptomator.ui.preferences.SelectedPreferencesTab;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import javafx.application.Platform;
@@ -27,6 +30,7 @@ import java.util.ResourceBundle;
 @TrayMenuScoped
 public class TrayMenuBuilder {
 
+	private static final Logger LOG = LoggerFactory.getLogger(TrayMenuBuilder.class);
 	private static final String TRAY_ICON_MAC = "/img/tray_icon_mac.png";
 	private static final String TRAY_ICON = "/img/tray_icon.png";
 
@@ -56,15 +60,16 @@ public class TrayMenuBuilder {
 		vaults.forEach(v -> {
 			v.displayNameProperty().addListener(this::vaultListChanged);
 		});
-		rebuildMenu();
 
 		try (var image = getClass().getResourceAsStream(SystemUtils.IS_OS_MAC_OSX ? TRAY_ICON_MAC : TRAY_ICON)) {
-			trayMenu.showTrayIcon(image, this::showMainWindow, "Cryptomator");
+			trayMenu.showTrayIcon(image.readAllBytes(), this::showMainWindow, "Cryptomator");
+			rebuildMenu();
+			initialized = true;
 		} catch (IOException e) {
 			throw new UncheckedIOException("Failed to load embedded resource", e);
+		} catch (TrayMenuException e) {
+			LOG.error("Adding tray icon failed", e);
 		}
-
-		initialized = true;
 	}
 
 	public boolean isInitialized() {
@@ -88,11 +93,14 @@ public class TrayMenuBuilder {
 			menu.add(new SubMenuItem(label, submenu));
 		}
 		menu.add(new SeparatorItem());
-		menu.add(new ActionItem(resourceBundle.getString("traymenu.lockAllVaults"), this::lockAllVaults));
+		menu.add(new ActionItem(resourceBundle.getString("traymenu.lockAllVaults"), this::lockAllVaults, vaults.stream().anyMatch(Vault::isUnlocked)));
 		menu.add(new ActionItem(resourceBundle.getString("traymenu.quitApplication"), this::quitApplication));
-// 		lockAllItem.setEnabled(!vaults.filtered(Vault::isUnlocked).isEmpty());
 
-		trayMenu.updateTrayMenu(menu);
+		try {
+			trayMenu.updateTrayMenu(menu);
+		} catch (TrayMenuException e) {
+			LOG.error("Updating tray menu failed", e);
+		}
 	}
 
 	private List<TrayMenuItem> buildSubmenu(Vault vault) {

+ 5 - 4
src/main/java/org/cryptomator/ui/traymenu/TrayMenuComponent.java

@@ -24,8 +24,7 @@ public interface TrayMenuComponent {
 	 * @return <code>true</code> if a tray icon can be installed
 	 */
 	default boolean isSupported() {
-		// TODO add isSupported to API and move SystemTray.isSupported() to impl
-		return trayMenuController().isPresent() && SystemTray.isSupported();
+		return trayMenuController().isPresent();
 	}
 
 	/**
@@ -38,11 +37,13 @@ public interface TrayMenuComponent {
 	/**
 	 * Installs a tray icon to the system tray.
 	 *
-	 * @throws IllegalStateException If already added
+	 * @throws IllegalStateException If not {@link #isSupported() supported}
 	 */
 	default void initializeTrayIcon() throws IllegalStateException {
 		Preconditions.checkState(isSupported(), "system tray not supported");
-		trayMenuBuilder().initTrayMenu();
+		if (!trayMenuBuilder().isInitialized()) {
+			trayMenuBuilder().initTrayMenu();
+		}
 	}
 
 	@Subcomponent.Builder