request focus on password field for encrypted locked wallets when wallet window becomes active

This commit is contained in:
Craig Raw 2022-02-02 08:58:39 +02:00
parent 542cc7de6f
commit 9bf53ab0cd

View file

@ -12,6 +12,9 @@ import com.sparrowwallet.sparrow.io.Storage;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.WeakChangeListener;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -48,8 +51,19 @@ public class WalletController extends WalletFormController implements Initializa
private BorderPane lockPane; private BorderPane lockPane;
private CustomPasswordField passwordField;
private final BooleanProperty walletEncryptedProperty = new SimpleBooleanProperty(false); private final BooleanProperty walletEncryptedProperty = new SimpleBooleanProperty(false);
private final ChangeListener<Boolean> lockFocusListener = new ChangeListener<>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(newValue && getWalletForm().isLocked() && passwordField != null && passwordField.isVisible()) {
passwordField.requestFocus();
}
}
};
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
EventManager.get().register(this); EventManager.get().register(this);
@ -148,7 +162,7 @@ public class WalletController extends WalletFormController implements Initializa
Label label = new Label("Enter password to unlock:"); Label label = new Label("Enter password to unlock:");
label.managedProperty().bind(label.visibleProperty()); label.managedProperty().bind(label.visibleProperty());
label.visibleProperty().bind(walletEncryptedProperty); label.visibleProperty().bind(walletEncryptedProperty);
CustomPasswordField passwordField = new ViewPasswordField(); passwordField = new ViewPasswordField();
passwordField.setMaxWidth(300); passwordField.setMaxWidth(300);
passwordField.managedProperty().bind(passwordField.visibleProperty()); passwordField.managedProperty().bind(passwordField.visibleProperty());
passwordField.visibleProperty().bind(walletEncryptedProperty); passwordField.visibleProperty().bind(walletEncryptedProperty);
@ -165,6 +179,8 @@ public class WalletController extends WalletFormController implements Initializa
stackPane.getChildren().add(vBox); stackPane.getChildren().add(vBox);
lockPane.setCenter(stackPane); lockPane.setCenter(stackPane);
walletPane.getChildren().add(lockPane); walletPane.getChildren().add(lockPane);
walletPane.getScene().getWindow().focusedProperty().addListener(new WeakChangeListener<>(lockFocusListener));
} }
private void unlockWallet(CustomPasswordField passwordField) { private void unlockWallet(CustomPasswordField passwordField) {