Added checkbox for auto start on macOS (#418)

This commit is contained in:
Sebastian Stenzel
2020-01-06 12:37:36 +01:00
parent 4f3ff55ae7
commit cc1517f1b8
8 changed files with 106 additions and 3 deletions

View File

@@ -25,7 +25,7 @@
<!-- cryptomator dependencies -->
<cryptomator.cryptofs.version>1.9.0-rc2</cryptomator.cryptofs.version>
<cryptomator.jni.version>2.2.1</cryptomator.jni.version>
<cryptomator.jni.version>2.2.2</cryptomator.jni.version>
<cryptomator.fuse.version>1.2.1</cryptomator.fuse.version>
<cryptomator.dokany.version>1.1.11</cryptomator.dokany.version>
<cryptomator.webdav.version>1.0.10</cryptomator.webdav.version>

View File

@@ -0,0 +1,39 @@
package org.cryptomator.ui.preferences;
import org.cryptomator.jni.MacFunctions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class AutoStartMacStrategy implements AutoStartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(AutoStartMacStrategy.class);
private final MacFunctions macFunctions;
public AutoStartMacStrategy(MacFunctions macFunctions) {
this.macFunctions = macFunctions;
}
@Override
public boolean isAutoStartEnabled() {
return macFunctions.launchServices().isLoginItemEnabled();
}
@Override
public void enableAutoStart() {
if (macFunctions.launchServices().enableLoginItem()) {
LOG.debug("Added login item.");
} else {
LOG.warn("Adding login item failed.");
}
}
@Override
public void disableAutoStart() {
if (macFunctions.launchServices().disableLoginItem()) {
LOG.debug("Removed login item.");
} else {
LOG.warn("Removing login item failed.");
}
}
}

View File

@@ -0,0 +1,26 @@
package org.cryptomator.ui.preferences;
import dagger.Module;
import dagger.Provides;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.jni.MacFunctions;
import java.util.Optional;
@Module
abstract class AutoStartModule {
@Provides
@PreferencesScoped
public static Optional<AutoStartStrategy> provideAutoStartStrategy(Optional<MacFunctions> macFunctions) {
if (SystemUtils.IS_OS_MAC_OSX && macFunctions.isPresent()) {
return Optional.of(new AutoStartMacStrategy(macFunctions.get()));
} else if (SystemUtils.IS_OS_WINDOWS) {
// TODO: add windows support
return Optional.empty();
} else {
return Optional.empty();
}
}
}

View File

@@ -0,0 +1,10 @@
package org.cryptomator.ui.preferences;
public interface AutoStartStrategy {
boolean isAutoStartEnabled();
void enableAutoStart();
void disableAutoStart();
}

View File

@@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Optional;
@PreferencesScoped
public class GeneralPreferencesController implements FxController {
@@ -25,17 +26,20 @@ public class GeneralPreferencesController implements FxController {
private final Settings settings;
private final boolean trayMenuSupported;
private final Optional<AutoStartStrategy> autoStartStrategy;
public ChoiceBox<UiTheme> themeChoiceBox;
public CheckBox startHiddenCheckbox;
public CheckBox debugModeCheckbox;
public CheckBox autoStartCheckbox;
public ToggleGroup nodeOrientation;
public RadioButton nodeOrientationLtr;
public RadioButton nodeOrientationRtl;
@Inject
GeneralPreferencesController(Settings settings, @Named("trayMenuSupported") boolean trayMenuSupported) {
GeneralPreferencesController(Settings settings, @Named("trayMenuSupported") boolean trayMenuSupported, Optional<AutoStartStrategy> autoStartStrategy) {
this.settings = settings;
this.trayMenuSupported = trayMenuSupported;
this.autoStartStrategy = autoStartStrategy;
}
@FXML
@@ -48,6 +52,9 @@ public class GeneralPreferencesController implements FxController {
debugModeCheckbox.selectedProperty().bindBidirectional(settings.debugMode());
autoStartCheckbox.setSelected(this.isAutoStartEnabled());
autoStartCheckbox.selectedProperty().addListener(this::toggleAutoStart);
nodeOrientationLtr.setSelected(settings.userInterfaceOrientation().get() == NodeOrientation.LEFT_TO_RIGHT);
nodeOrientationRtl.setSelected(settings.userInterfaceOrientation().get() == NodeOrientation.RIGHT_TO_LEFT);
nodeOrientation.selectedToggleProperty().addListener(this::toggleNodeOrientation);
@@ -57,6 +64,24 @@ public class GeneralPreferencesController implements FxController {
return this.trayMenuSupported;
}
public boolean isAutoStartSupported() {
return autoStartStrategy.isPresent();
}
private boolean isAutoStartEnabled() {
return autoStartStrategy.map(AutoStartStrategy::isAutoStartEnabled).orElse(false);
}
private void toggleAutoStart(@SuppressWarnings("unused") ObservableValue<? extends Boolean> observable, @SuppressWarnings("unused") boolean oldValue, boolean newValue) {
autoStartStrategy.ifPresent(autoStart -> {
if (newValue) {
autoStart.enableAutoStart();
} else {
autoStart.disableAutoStart();
}
});
}
private void toggleNodeOrientation(@SuppressWarnings("unused") ObservableValue<? extends Toggle> observable, @SuppressWarnings("unused") Toggle oldValue, Toggle newValue) {
if (nodeOrientationLtr.equals(newValue)) {
settings.userInterfaceOrientation().set(NodeOrientation.LEFT_TO_RIGHT);

View File

@@ -22,7 +22,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
@Module
@Module(includes = {AutoStartModule.class})
abstract class PreferencesModule {
@Provides

View File

@@ -33,5 +33,7 @@
<CheckBox fx:id="startHiddenCheckbox" text="%preferences.general.startHidden" visible="${controller.trayMenuSupported}" managed="${controller.trayMenuSupported}"/>
<CheckBox fx:id="debugModeCheckbox" text="%preferences.general.debugLogging"/>
<CheckBox fx:id="autoStartCheckbox" text="%preferences.general.autoStart" visible="${controller.autoStartSupported}" managed="${controller.autoStartSupported}"/>
</children>
</VBox>

View File

@@ -203,3 +203,4 @@ passwordStrength.messageLabel.4=Very strong
# Quit
quit.prompt=Quit application? There are unlocked vaults.
quit.lockAndQuit=Lock and Quit
preferences.general.autoStart=Launch Cryptomator on system start