show file open/save dialogs on the same screen as the currently focussed window

This commit is contained in:
Craig Raw 2021-04-27 12:52:42 +02:00
parent 93f9539f7b
commit 9feefe8203
8 changed files with 32 additions and 5 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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);

View file

@ -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);

View file

@ -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());

View file

@ -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 {

View file

@ -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)) {

View file

@ -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)) {