From 9feefe820325314eedb0e121c287066bc38f8ee7 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 27 Apr 2021 12:52:42 +0200 Subject: [PATCH] show file open/save dialogs on the same screen as the currently focussed window --- .../sparrowwallet/sparrow/AppController.java | 4 +++ .../sparrowwallet/sparrow/AppServices.java | 25 +++++++++++++++---- .../sparrow/control/FileImportPane.java | 2 ++ .../sparrow/control/FileWalletExportPane.java | 1 + .../ServerPreferencesController.java | 1 + .../transaction/HeadersController.java | 2 ++ .../sparrow/wallet/AddressesController.java | 1 + .../wallet/TransactionsController.java | 1 + 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index a740dbf4..ab5da962 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -384,6 +384,7 @@ public class AppController implements Initializable { new FileChooser.ExtensionFilter("TXN", "*.txn") ); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showOpenDialog(window); if (file != null) { openTransactionFile(file); @@ -523,6 +524,7 @@ public class AppController implements Initializable { fileChooser.setInitialFileName(fileName); } + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { try(PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8)) { @@ -574,6 +576,7 @@ public class AppController implements Initializable { fileChooser.setInitialFileName(fileName); } + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { if(!asText && !file.getName().toLowerCase().endsWith(".psbt")) { @@ -715,6 +718,7 @@ public class AppController implements Initializable { fileChooser.setTitle("Open Wallet"); fileChooser.setInitialDirectory(Storage.getWalletsDir()); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showOpenDialog(window); if(file != null) { openWalletFile(file, forceSameWindow); diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index 48d0902c..f0e1051f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -524,13 +524,24 @@ public class AppServices { } } + public static Window getActiveWindow() { + return Stage.getWindows().stream().filter(Window::isFocused).findFirst().orElse(get().walletWindows.keySet().iterator().hasNext() ? get().walletWindows.keySet().iterator().next() : null); + } + public static void moveToActiveWindowScreen(Dialog dialog) { - Window activeWindow = Stage.getWindows().stream().filter(Window::isFocused).findFirst().orElse(get().walletWindows.keySet().iterator().hasNext() ? get().walletWindows.keySet().iterator().next() : null); + Window activeWindow = getActiveWindow(); if(activeWindow != null) { moveToWindowScreen(activeWindow, dialog); } } + public static void moveToActiveWindowScreen(Window newWindow, double newWindowWidth, double newWindowHeight) { + Window activeWindow = getActiveWindow(); + if(activeWindow != null) { + moveToWindowScreen(activeWindow, newWindow, newWindowWidth, newWindowHeight); + } + } + public void moveToWalletWindowScreen(Storage storage, Dialog dialog) { moveToWindowScreen(getWindowForWallet(storage), dialog); } @@ -538,12 +549,16 @@ public class AppServices { public static void moveToWindowScreen(Window currentWindow, Dialog dialog) { Window newWindow = dialog.getDialogPane().getScene().getWindow(); DialogPane dialogPane = dialog.getDialogPane(); + double dialogWidth = dialogPane.getPrefWidth() > 0.0 ? dialogPane.getPrefWidth() : (dialogPane.getWidth() > 0.0 ? dialogPane.getWidth() : 360); + double dialogHeight = dialogPane.getPrefHeight() > 0.0 ? dialogPane.getPrefHeight() : (dialogPane.getHeight() > 0.0 ? dialogPane.getHeight() : 200); + moveToWindowScreen(currentWindow, newWindow, dialogWidth, dialogHeight); + } + + public static void moveToWindowScreen(Window currentWindow, Window newWindow, double newWindowWidth, double newWindowHeight) { Screen currentScreen = Screen.getScreens().stream().filter(screen -> screen.getVisualBounds().contains(currentWindow.getX(), currentWindow.getY())).findFirst().orElse(null); if(currentScreen != null && !Screen.getPrimary().getVisualBounds().contains(currentWindow.getX(), currentWindow.getY()) && !currentScreen.getVisualBounds().contains(newWindow.getX(), newWindow.getY())) { - double dialogWidth = dialogPane.getPrefWidth() > 0.0 ? dialogPane.getPrefWidth() : (dialogPane.getWidth() > 0.0 ? dialogPane.getWidth() : 360); - double dialogHeight = dialogPane.getPrefHeight() > 0.0 ? dialogPane.getPrefHeight() : (dialogPane.getHeight() > 0.0 ? dialogPane.getHeight() : 200); - double x = currentWindow.getX() + (currentWindow.getWidth() / 2) - (dialogWidth / 2); - double y = currentWindow.getY() + (currentWindow.getHeight() / 2.2) - (dialogHeight / 2); + double x = currentWindow.getX() + (currentWindow.getWidth() / 2) - (newWindowWidth / 2); + double y = currentWindow.getY() + (currentWindow.getHeight() / 2.2) - (newWindowHeight / 2); newWindow.setX(x); newWindow.setY(y); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java index 128ba7a7..6208988f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java @@ -5,6 +5,7 @@ import com.sparrowwallet.drongo.crypto.InvalidPasswordException; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.Wallet; +import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.io.FileImport; import com.sparrowwallet.sparrow.io.ImportException; @@ -95,6 +96,7 @@ public abstract class FileImportPane extends TitledDescriptionPane { new FileChooser.ExtensionFilter("TXT", "*.txt") ); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showOpenDialog(window); if(file != null) { importFile(file, null); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java index d47c21fe..1700d68b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java @@ -87,6 +87,7 @@ public class FileWalletExportPane extends TitledDescriptionPane { exporter.getWalletModel().toDisplayString().toLowerCase().replace(" ", "") + (extension == null || extension.isEmpty() ? "" : "." + extension)); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { exportWallet(file); diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java index 967d34c0..83e09b0f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java @@ -277,6 +277,7 @@ public class ServerPreferencesController extends PreferencesDetailController { new FileChooser.ExtensionFilter("CRT", "*.crt") ); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showOpenDialog(window); if(file != null) { electrumCertificate.setText(file.getAbsolutePath()); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index a4166fdd..ec273d12 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -670,6 +670,7 @@ public class HeadersController extends TransactionFormController implements Init fileChooser.setInitialFileName(headersForm.getName() + ".psbt"); } + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { if(!file.getName().toLowerCase().endsWith(".psbt")) { @@ -880,6 +881,7 @@ public class HeadersController extends TransactionFormController implements Init fileChooser.setInitialFileName(headersForm.getName().replace(".psbt", "") + ".txn"); } + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { try { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java index d3e5a99e..9c5edbf1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java @@ -117,6 +117,7 @@ public class AddressesController extends WalletFormController implements Initial WalletNode purposeNode = copy.getNode(keyPurpose); purposeNode.fillToIndex(Math.max(purposeNode.getChildren().size(), DEFAULT_EXPORT_ADDRESSES_LENGTH)); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { try(FileOutputStream outputStream = new FileOutputStream(file)) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java index c402e4d4..35234d68 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java @@ -108,6 +108,7 @@ public class TransactionsController extends WalletFormController implements Init fileChooser.setTitle("Export Transactions as CSV"); fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + ".csv"); + AppServices.moveToActiveWindowScreen(window, 800, 450); File file = fileChooser.showSaveDialog(window); if(file != null) { try(FileOutputStream outputStream = new FileOutputStream(file)) {