From 2c4de99fad1891c04ab57f38adc74ea22af821bd Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 13 Mar 2025 13:52:01 +0200 Subject: [PATCH] improve capture display efficiency, fix resizing bug and refactor --- .../sparrow/control/QRScanDialog.java | 26 ++++++++++++------- .../sparrow/control/WebcamView.java | 23 ++++++++-------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java index a5b96f6d..f9b8fa03 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java @@ -98,21 +98,26 @@ public class QRScanDialog extends Dialog { this.webcamService = new WebcamService(webcamResolutionProperty.get(), null); webcamService.setPeriod(Duration.millis(SCAN_PERIOD_MILLIS)); webcamService.setRestartOnFailure(false); - WebcamView webcamView = new WebcamView(webcamService, Config.get().isMirrorCapture()); final DialogPane dialogPane = new QRScanDialogPane(); setDialogPane(dialogPane); AppServices.setStageIcon(dialogPane.getScene().getWindow()); - StackPane stackPane = new StackPane(); - stackPane.getChildren().add(webcamView.getView()); - Node wrappedView = Borders.wrap(stackPane).lineBorder().buildAll(); + WebcamView webcamView = new WebcamView(webcamService, Config.get().isMirrorCapture()); ProgressBar progressBar = new ProgressBar(); progressBar.setMinHeight(20); progressBar.setPadding(new Insets(0, 10, 0, 10)); progressBar.setPrefWidth(Integer.MAX_VALUE); progressBar.progressProperty().bind(percentComplete); + + VBox vBox = new VBox(20); + StackPane stackPane = new StackPane(); + stackPane.getChildren().add(webcamView.getView()); + Node wrappedView = Borders.wrap(stackPane).lineBorder().buildAll(); + vBox.getChildren().addAll(wrappedView, progressBar); + dialogPane.setContent(vBox); + webcamService.openingProperty().addListener((_, _, opening) -> { if(percentComplete.get() <= 0.0) { Platform.runLater(() -> percentComplete.set(opening ? 0.0 : -1.0)); @@ -155,24 +160,25 @@ public class QRScanDialog extends Dialog { }); } }); - - VBox vBox = new VBox(20); - vBox.getChildren().addAll(wrappedView, progressBar); - - dialogPane.setContent(vBox); - webcamService.resultProperty().addListener(new QRResultListener()); webcamService.setOnFailed(failedEvent -> { Throwable exception = Throwables.getRootCause(failedEvent.getSource().getException()); Platform.runLater(() -> setResult(new Result(exception))); }); webcamService.start(); + webcamResolutionProperty.addListener((_, oldResolution, newResolution) -> { if(newResolution != null) { if(newResolution.isStandardAspect() && oldResolution.isWidescreenAspect()) { setHeight(getHeight() + 100); + dialogPane.setMaxHeight(dialogPane.getPrefHeight() + 100); + dialogPane.setPrefHeight(dialogPane.getMaxHeight()); + dialogPane.setMinHeight(dialogPane.getMaxHeight()); } else if(newResolution.isWidescreenAspect() && oldResolution.isStandardAspect()) { setHeight(getHeight() - 100); + dialogPane.setMaxHeight(dialogPane.getPrefHeight() - 100); + dialogPane.setPrefHeight(dialogPane.getMaxHeight()); + dialogPane.setMinHeight(dialogPane.getMaxHeight()); } EventManager.get().post(new WebcamResolutionChangedEvent(newResolution)); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WebcamView.java b/src/main/java/com/sparrowwallet/sparrow/control/WebcamView.java index acf9626e..41c2b448 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WebcamView.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WebcamView.java @@ -57,27 +57,29 @@ public class WebcamView { this.view = new Region() { { service.stateProperty().addListener((obs, oldState, newState) -> { - switch (newState) { + switch(newState) { case READY: if(imageProperty.get() == null) { statusPlaceholder.setText("Initializing"); getChildren().setAll(statusPlaceholder); } - break ; + break; case SCHEDULED: if(imageProperty.get() == null) { statusPlaceholder.setText("Waiting"); getChildren().setAll(statusPlaceholder); } - break ; + break; case RUNNING: - imageView.imageProperty().unbind(); - imageView.imageProperty().bind(imageProperty); - getChildren().setAll(imageView); - break ; + if(imageProperty.get() == null) { + imageView.imageProperty().unbind(); + imageView.imageProperty().bind(imageProperty); + getChildren().setAll(imageView); + } + break; case CANCELLED: + imageProperty.set(null); imageView.imageProperty().unbind(); - imageView.setImage(null); statusPlaceholder.setText("Stopped"); getChildren().setAll(statusPlaceholder); break; @@ -93,7 +95,6 @@ public class WebcamView { statusPlaceholder.setText(""); getChildren().clear(); } - requestLayout(); }); } @@ -102,14 +103,14 @@ public class WebcamView { super.layoutChildren(); double w = getWidth(); double h = getHeight(); - if (service.isRunning()) { + if(service.isRunning()) { imageView.setFitWidth(w); imageView.setFitHeight(h); imageView.resizeRelocate(0, 0, w, h); } else { double labelHeight = statusPlaceholder.prefHeight(w); double labelWidth = statusPlaceholder.prefWidth(labelHeight); - statusPlaceholder.resizeRelocate((w - labelWidth)/2, (h-labelHeight)/2, labelWidth, labelHeight); + statusPlaceholder.resizeRelocate((w - labelWidth) / 2, (h - labelHeight) / 2, labelWidth, labelHeight); } }