simplified some more bindings

This commit is contained in:
Sebastian Stenzel
2022-10-10 12:09:30 +02:00
parent 76e7a0a7b8
commit 8040b71a51
14 changed files with 63 additions and 76 deletions

View File

@@ -23,6 +23,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import javafx.beans.binding.Binding;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableValue;
import java.net.InetSocketAddress;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
@@ -130,16 +131,16 @@ public abstract class CommonsModule {
@Provides
@Singleton
static Binding<InetSocketAddress> provideServerSocketAddressBinding(Settings settings) {
return Bindings.createObjectBinding(() -> {
static ObservableValue<InetSocketAddress> provideServerSocketAddressBinding(Settings settings) {
return settings.port().map(port -> {
String host = SystemUtils.IS_OS_WINDOWS ? "127.0.0.1" : "localhost";
return InetSocketAddress.createUnresolved(host, settings.port().intValue());
}, settings.port());
});
}
@Provides
@Singleton
static WebDavServer provideWebDavServer(Binding<InetSocketAddress> serverSocketAddressBinding) {
static WebDavServer provideWebDavServer(ObservableValue<InetSocketAddress> serverSocketAddressBinding) {
WebDavServer server = WebDavServer.create();
// no need to unsubscribe eventually, because server is a singleton
EasyBind.subscribe(serverSocketAddressBinding, server::bind);

View File

@@ -10,6 +10,7 @@ import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import java.util.Optional;
@Singleton
@@ -18,7 +19,7 @@ public class LicenseHolder {
private final Settings settings;
private final LicenseChecker licenseChecker;
private final ObjectProperty<DecodedJWT> validJwtClaims;
private final StringBinding licenseSubject;
private final ObservableValue<String> licenseSubject;
private final BooleanBinding validLicenseProperty;
@Inject
@@ -26,7 +27,7 @@ public class LicenseHolder {
this.settings = settings;
this.licenseChecker = licenseChecker;
this.validJwtClaims = new SimpleObjectProperty<>();
this.licenseSubject = Bindings.createStringBinding(this::getLicenseSubject, validJwtClaims);
this.licenseSubject = validJwtClaims.map(DecodedJWT::getSubject);
this.validLicenseProperty = validJwtClaims.isNotNull();
Optional<DecodedJWT> claims = licenseChecker.check(settings.licenseKey().get());
@@ -55,17 +56,12 @@ public class LicenseHolder {
}
}
public StringBinding licenseSubjectProperty() {
public ObservableValue<String> licenseSubjectProperty() {
return licenseSubject;
}
public String getLicenseSubject() {
DecodedJWT claims = validJwtClaims.get();
if (claims != null) {
return claims.getSubject();
} else {
return null;
}
return licenseSubject.getValue();
}
public BooleanBinding validLicenseProperty() {

View File

@@ -12,6 +12,7 @@ import com.google.common.io.BaseEncoding;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.binding.StringExpression;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
@@ -20,6 +21,7 @@ import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
@@ -56,11 +58,11 @@ public class VaultSettings {
private final ObjectProperty<WhenUnlocked> actionAfterUnlock = new SimpleObjectProperty<>(DEFAULT_ACTION_AFTER_UNLOCK);
private final BooleanProperty autoLockWhenIdle = new SimpleBooleanProperty(DEFAULT_AUTOLOCK_WHEN_IDLE);
private final IntegerProperty autoLockIdleSeconds = new SimpleIntegerProperty(DEFAULT_AUTOLOCK_IDLE_SECONDS);
private final StringBinding mountName;
private final StringExpression mountName;
public VaultSettings(String id) {
this.id = Objects.requireNonNull(id);
this.mountName = Bindings.createStringBinding(this::normalizeDisplayName, displayName);
this.mountName = StringExpression.stringExpression(displayName.map(VaultSettings::normalizeDisplayName).orElse(""));
}
Observable[] observables() {
@@ -78,8 +80,7 @@ public class VaultSettings {
}
//visible for testing
String normalizeDisplayName() {
var original = displayName.getValueSafe();
static String normalizeDisplayName(String original) {
if (original.isBlank() || ".".equals(original) || "..".equals(original)) {
return "_";
}
@@ -105,7 +106,7 @@ public class VaultSettings {
return displayName;
}
public StringBinding mountName() {
public StringExpression mountName() {
return mountName;
}

View File

@@ -34,6 +34,7 @@ import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleBooleanProperty;
import java.io.IOException;
import java.nio.file.Path;
@@ -60,7 +61,6 @@ public class Vault {
private final ObjectProperty<Exception> lastKnownException;
private final VaultConfigCache configCache;
private final VaultStats stats;
private final StringBinding displayName;
private final StringBinding displayablePath;
private final BooleanBinding locked;
private final BooleanBinding processing;
@@ -84,7 +84,6 @@ public class Vault {
this.state = state;
this.lastKnownException = lastKnownException;
this.stats = stats;
this.displayName = Bindings.createStringBinding(this::getDisplayName, vaultSettings.displayName());
this.displayablePath = Bindings.createStringBinding(this::getDisplayablePath, vaultSettings.path());
this.locked = Bindings.createBooleanBinding(this::isLocked, state);
this.processing = Bindings.createBooleanBinding(this::isProcessing, state);
@@ -266,8 +265,8 @@ public class Vault {
return state.get() == VaultState.Value.ERROR;
}
public StringBinding displayNameProperty() {
return displayName;
public ReadOnlyStringProperty displayNameProperty() {
return vaultSettings.displayName();
}
public String getDisplayName() {

View File

@@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
import javax.inject.Named;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.binding.StringExpression;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
@@ -68,7 +69,7 @@ public class VaultModule {
@DefaultMountFlags
public StringBinding provideDefaultMountFlags(Settings settings, VaultSettings vaultSettings) {
ObjectProperty<VolumeImpl> preferredVolumeImpl = settings.preferredVolumeImpl();
StringBinding mountName = vaultSettings.mountName();
StringExpression mountName = vaultSettings.mountName();
BooleanProperty readOnly = vaultSettings.usesReadOnlyMode();
return Bindings.createStringBinding(() -> {
@@ -88,7 +89,7 @@ public class VaultModule {
}
// see: https://github.com/osxfuse/osxfuse/wiki/Mount-options
private String getMacFuseDefaultMountFlags(StringBinding mountName, ReadOnlyBooleanProperty readOnly) {
private String getMacFuseDefaultMountFlags(StringExpression mountName, ReadOnlyBooleanProperty readOnly) {
assert SystemUtils.IS_OS_MAC_OSX;
StringBuilder flags = new StringBuilder();
if (readOnly.get()) {
@@ -139,7 +140,7 @@ public class VaultModule {
// see https://github.com/billziss-gh/winfsp/blob/5d0b10d0b643652c00ebb4704dc2bb28e7244973/src/dll/fuse/fuse_main.c#L53-L62 for syntax guide
// see https://github.com/billziss-gh/winfsp/blob/5d0b10d0b643652c00ebb4704dc2bb28e7244973/src/dll/fuse/fuse.c#L295-L319 for options (-o <...>)
// see https://github.com/billziss-gh/winfsp/wiki/Frequently-Asked-Questions/5ba00e4be4f5e938eaae6ef1500b331de12dee77 (FUSE 4.) on why the given defaults were chosen
private String getWindowsFuseDefaultMountFlags(StringBinding mountName, ReadOnlyBooleanProperty readOnly) {
private String getWindowsFuseDefaultMountFlags(StringExpression mountName, ReadOnlyBooleanProperty readOnly) {
assert SystemUtils.IS_OS_WINDOWS;
StringBuilder flags = new StringBuilder();

View File

@@ -102,7 +102,7 @@ public class CreateNewVaultPasswordController implements FxController {
this.masterkeyFileAccess = masterkeyFileAccess;
this.processing = new SimpleBooleanProperty();
this.readyToCreateVault = new SimpleBooleanProperty();
this.createVaultButtonState = Bindings.createObjectBinding(this::getCreateVaultButtonState, processing);
this.createVaultButtonState = Bindings.when(processing).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY);
}
@FXML
@@ -231,6 +231,6 @@ public class CreateNewVaultPasswordController implements FxController {
}
public ContentDisplay getCreateVaultButtonState() {
return processing.get() ? ContentDisplay.LEFT : ContentDisplay.TEXT_ONLY;
return createVaultButtonState.get();
}
}

View File

@@ -46,7 +46,7 @@ public class NiceSecurePasswordField extends StackPane {
nonPrintableCharsIcon.managedProperty().bind(passwordField.containingNonPrintableCharsProperty());
revealPasswordIcon.setGlyph(FontAwesome5Icon.EYE);
revealPasswordIcon.glyphProperty().bind(Bindings.createObjectBinding(this::getRevealPasswordGlyph, revealPasswordButton.selectedProperty()));
revealPasswordIcon.glyphProperty().bind(Bindings.when(revealPasswordButton.selectedProperty()).then(FontAwesome5Icon.EYE_SLASH).otherwise(FontAwesome5Icon.EYE));
revealPasswordIcon.setGlyphSize(ICON_SIZE);
revealPasswordButton.setContentDisplay(ContentDisplay.LEFT);
@@ -61,10 +61,6 @@ public class NiceSecurePasswordField extends StackPane {
disabledProperty().addListener(this::disabledChanged);
}
private FontAwesome5Icon getRevealPasswordGlyph() {
return revealPasswordButton.isSelected() ? FontAwesome5Icon.EYE_SLASH : FontAwesome5Icon.EYE;
}
private void disabledChanged(@SuppressWarnings("unused") Observable observable) {
revealPasswordButton.setSelected(false);
}

View File

@@ -13,13 +13,11 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javafx.application.Platform;
import javafx.beans.binding.Binding;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableObjectValue;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Tooltip;
@@ -40,9 +38,9 @@ public class ResultListCellController implements FxController {
private final ObjectProperty<Result> result;
private final ObservableValue<DiagnosticResult.Severity> severity;
private final Binding<String> description;
private final ObservableValue<String> description;
private final ResultFixApplier fixApplier;
private final ObservableObjectValue<Result.FixState> fixState;
private final ObservableValue<Result.FixState> fixState;
private final ObjectBinding<FontAwesome5Icon> severityGlyph;
private final ObjectBinding<FontAwesome5Icon> fixGlyph;
private final BooleanBinding fixable;
@@ -64,9 +62,9 @@ public class ResultListCellController implements FxController {
public ResultListCellController(ResultFixApplier fixApplier, ResourceBundle resourceBundle) {
this.result = new SimpleObjectProperty<>(null);
this.severity = result.map(Result::diagnosis).map(DiagnosticResult::getSeverity);
this.description = EasyBind.wrapNullable(result).map(Result::getDescription).orElse("");
this.description = result.map(Result::getDescription).orElse("");
this.fixApplier = fixApplier;
this.fixState = EasyBind.wrapNullable(result).mapObservable(Result::fixState).asOrdinary();
this.fixState = result.flatMap(Result::fixState);
this.severityGlyph = Bindings.createObjectBinding(this::getSeverityGlyph, result);
this.fixGlyph = Bindings.createObjectBinding(this::getFixGlyph, fixState);
this.fixable = Bindings.createBooleanBinding(this::isFixable, fixState);
@@ -89,9 +87,10 @@ public class ResultListCellController implements FxController {
EasyBind.includeWhen(severityView.getStyleClass(), "glyph-icon-orange", severity.map(DiagnosticResult.Severity.WARN::equals).orElse(false)), //
EasyBind.includeWhen(severityView.getStyleClass(), "glyph-icon-red", severity.map(DiagnosticResult.Severity.CRITICAL::equals).orElse(false)) //
));
var animation = Animations.createDiscrete360Rotation(fixView);
this.fixRunningRotator = AutoAnimator.animate(animation) //
.onCondition(Bindings.equal(fixState, Result.FixState.FIXING)) //
.onCondition(fixing) //
.afterStop(() -> fixView.setRotate(0)) //
.build();
}
@@ -128,7 +127,7 @@ public class ResultListCellController implements FxController {
return result;
}
public Binding<String> descriptionProperty() {
public ObservableValue<String> descriptionProperty() {
return description;
}
@@ -174,7 +173,7 @@ public class ResultListCellController implements FxController {
}
public boolean isFixable() {
return Result.FixState.FIXABLE.equals(fixState.get());
return Result.FixState.FIXABLE.equals(fixState.getValue());
}
public BooleanBinding fixingProperty() {
@@ -182,7 +181,7 @@ public class ResultListCellController implements FxController {
}
public boolean isFixing() {
return Result.FixState.FIXING.equals(fixState.get());
return Result.FixState.FIXING.equals(fixState.getValue());
}
public BooleanBinding fixedProperty() {
@@ -190,7 +189,7 @@ public class ResultListCellController implements FxController {
}
public boolean isFixed() {
return Result.FixState.FIXED.equals(fixState.get());
return Result.FixState.FIXED.equals(fixState.getValue());
}
public BooleanBinding fixFailedProperty() {
@@ -198,7 +197,7 @@ public class ResultListCellController implements FxController {
}
public Boolean isFixFailed() {
return Result.FixState.FIX_FAILED.equals(fixState.get());
return Result.FixState.FIX_FAILED.equals(fixState.getValue());
}
public BooleanBinding fixRunningOrDoneProperty() {

View File

@@ -50,7 +50,7 @@ public class PassphraseEntryController implements FxController {
private final KeychainManager keychain;
private final StringBinding vaultName;
private final BooleanProperty unlockInProgress = new SimpleBooleanProperty();
private final ObjectBinding<ContentDisplay> unlockButtonContentDisplay = Bindings.createObjectBinding(this::getUnlockButtonContentDisplay, unlockInProgress);
private final ObjectBinding<ContentDisplay> unlockButtonContentDisplay = Bindings.when(unlockInProgress).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY);
private final BooleanProperty unlockButtonDisabled = new SimpleBooleanProperty();
/* FXML */
@@ -186,7 +186,7 @@ public class PassphraseEntryController implements FxController {
}
public ContentDisplay getUnlockButtonContentDisplay() {
return unlockInProgress.get() ? ContentDisplay.LEFT : ContentDisplay.TEXT_ONLY;
return unlockButtonContentDisplay.get();
}
public ReadOnlyBooleanProperty userInteractionDisabledProperty() {

View File

@@ -46,7 +46,7 @@ public class ResizeController implements FxController {
ResizeController(@MainWindow Stage window, Settings settings) {
this.window = window;
this.settings = settings;
this.showResizingArrows = Bindings.createBooleanBinding(this::isShowResizingArrows, window.fullScreenProperty());
this.showResizingArrows = window.fullScreenProperty().not();
}
@FXML
@@ -181,8 +181,7 @@ public class ResizeController implements FxController {
}
public boolean isShowResizingArrows() {
//If in fullscreen resizing is not be possible;
return !window.isFullScreen();
return showResizingArrows.get();
}
}

View File

@@ -8,10 +8,10 @@ import org.cryptomator.ui.vaultoptions.SelectedVaultOptionsTab;
import org.cryptomator.ui.vaultoptions.VaultOptionsComponent;
import javax.inject.Inject;
import javafx.beans.binding.BooleanExpression;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.stage.Stage;
@@ -23,7 +23,7 @@ public class VaultDetailLockedController implements FxController {
private final VaultOptionsComponent.Factory vaultOptionsWindow;
private final KeychainManager keychain;
private final Stage mainWindow;
private final BooleanExpression passwordSaved;
private final ObservableValue<Boolean> passwordSaved;
@Inject
VaultDetailLockedController(ObjectProperty<Vault> vault, FxApplicationWindows appWindows, VaultOptionsComponent.Factory vaultOptionsWindow, KeychainManager keychain, @MainWindow Stage mainWindow) {
@@ -33,8 +33,7 @@ public class VaultDetailLockedController implements FxController {
this.keychain = keychain;
this.mainWindow = mainWindow;
if (keychain.isSupported() && !keychain.isLocked()) {
var stored = vault.flatMap(v -> keychain.getPassphraseStoredProperty(v.getId())).orElse(false);
this.passwordSaved = BooleanExpression.booleanExpression(stored);
this.passwordSaved = vault.flatMap(v -> keychain.getPassphraseStoredProperty(v.getId())).orElse(false);
} else {
this.passwordSaved = new SimpleBooleanProperty(false);
}
@@ -65,13 +64,11 @@ public class VaultDetailLockedController implements FxController {
return vault.get();
}
public BooleanExpression passwordSavedProperty() {
public ObservableValue<Boolean> passwordSavedProperty() {
return passwordSaved;
}
public boolean isPasswordSaved() {
if (keychain.isSupported() && vault.get() != null) {
return keychain.getPassphraseStoredProperty(vault.get().getId()).get();
} else return false;
return passwordSaved.getValue();
}
}

View File

@@ -83,7 +83,7 @@ public class MigrationRunController implements FxController {
this.appWindows = appWindows;
this.startScene = startScene;
this.successScene = successScene;
this.migrateButtonContentDisplay = Bindings.createObjectBinding(this::getMigrateButtonContentDisplay, vault.stateProperty());
this.migrateButtonContentDisplay = Bindings.when(vault.processingProperty()).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY);
this.capabilityErrorScene = capabilityErrorScene;
this.migrationButtonDisabled = new SimpleBooleanProperty();
this.migrationProgress = new SimpleDoubleProperty(volatileMigrationProgress);
@@ -211,10 +211,7 @@ public class MigrationRunController implements FxController {
}
public ContentDisplay getMigrateButtonContentDisplay() {
return switch (vault.getState()) {
case PROCESSING -> ContentDisplay.LEFT;
default -> ContentDisplay.TEXT_ONLY;
};
return migrateButtonContentDisplay.get();
}
public ReadOnlyDoubleProperty migrationProgressProperty() {

View File

@@ -2,7 +2,6 @@ package org.cryptomator.ui.vaultoptions;
import org.cryptomator.common.keychain.KeychainManager;
import org.cryptomator.common.vaults.Vault;
import org.cryptomator.integrations.keychain.KeychainAccessException;
import org.cryptomator.ui.changepassword.ChangePasswordComponent;
import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.forgetPassword.ForgetPasswordComponent;
@@ -11,9 +10,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanExpression;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.stage.Stage;
@@ -28,7 +26,7 @@ public class MasterkeyOptionsController implements FxController {
private final RecoveryKeyComponent.Builder recoveryKeyWindow;
private final ForgetPasswordComponent.Builder forgetPasswordWindow;
private final KeychainManager keychain;
private final BooleanExpression passwordSaved;
private final ObservableValue<Boolean> passwordSaved;
@Inject
@@ -40,7 +38,7 @@ public class MasterkeyOptionsController implements FxController {
this.forgetPasswordWindow = forgetPasswordWindow;
this.keychain = keychain;
if (keychain.isSupported() && !keychain.isLocked()) {
this.passwordSaved = Bindings.createBooleanBinding(this::isPasswordSaved, keychain.getPassphraseStoredProperty(vault.getId()));
this.passwordSaved = keychain.getPassphraseStoredProperty(vault.getId()).orElse(false);
} else {
this.passwordSaved = new SimpleBooleanProperty(false);
}
@@ -67,13 +65,11 @@ public class MasterkeyOptionsController implements FxController {
forgetPasswordWindow.vault(vault).owner(window).build().showForgetPassword();
}
public BooleanExpression passwordSavedProperty() {
public ObservableValue<Boolean> passwordSavedProperty() {
return passwordSaved;
}
public boolean isPasswordSaved() {
if (keychain.isSupported() && !keychain.isLocked() && vault != null) {
return keychain.getPassphraseStoredProperty(vault.getId()).get();
} else return false;
return passwordSaved.getValue();
}
}

View File

@@ -15,12 +15,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class VaultSettingsTest {
@ParameterizedTest
@CsvSource({"a\u000Fa,a_a", ": \\,_ _", "汉语,汉语", "..,_", "a\ta,a\u0020a", "\t\n\r,_"})
@ParameterizedTest(name = "VaultSettings.normalizeDisplayName({0}) = {1}")
@CsvSource(value = {
"a\u000Fa,a_a",
": \\,_ _",
"汉语,汉语",
"..,_",
"a\ta,a\u0020a",
"'\t\n\r',_"
})
public void testNormalize(String test, String expected) {
VaultSettings settings = new VaultSettings("id");
settings.displayName().setValue(test);
assertEquals(expected, settings.normalizeDisplayName());
assertEquals(expected, VaultSettings.normalizeDisplayName(test));
}
}