From e16b888895cc64b6470ce34dfbeb1ae861d48c89 Mon Sep 17 00:00:00 2001 From: axpoems <145597137+axpoems@users.noreply.github.com> Date: Wed, 8 Jan 2025 09:41:08 +0100 Subject: [PATCH] Allow negative numbers for percentage price --- .../main/content/bisq_easy/BisqEasyViewUtils.java | 3 ++- .../main/content/bisq_easy/components/PriceInput.java | 4 +++- .../content/bisq_easy/components/PriceInputBox.java | 10 ++++------ .../amount_selection/QuoteAmountInputBox.java | 2 +- .../trade_wizard/price/TradeWizardPriceView.java | 4 +++- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/BisqEasyViewUtils.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/BisqEasyViewUtils.java index 4a66a90029..dc760e01c8 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/BisqEasyViewUtils.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/BisqEasyViewUtils.java @@ -38,7 +38,8 @@ import java.util.List; public class BisqEasyViewUtils { - public static final String NUMERIC_WITH_DECIMAL_REGEX = "\\d*([.,]\\d*)?"; + public static final String POSITIVE_NUMERIC_WITH_DECIMAL_REGEX = "\\d*([.,]\\d*)?"; + public static final String NUMERIC_WITH_DECIMAL_REGEX = "-?\\d*([.,]\\d*)?"; private static final String[] customPaymentIconIds = { "CUSTOM_PAYMENT_1", "CUSTOM_PAYMENT_2", "CUSTOM_PAYMENT_3", "CUSTOM_PAYMENT_4", "CUSTOM_PAYMENT_5", "CUSTOM_PAYMENT_6"}; diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/PriceInput.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/PriceInput.java index 26d15f3e0d..94c8fe6f24 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/PriceInput.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/PriceInput.java @@ -24,6 +24,7 @@ import bisq.common.util.MathUtils; import bisq.desktop.common.threading.UIThread; import bisq.desktop.components.controls.validator.NumberValidator; +import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils; import bisq.i18n.Res; import bisq.presentation.formatters.PriceFormatter; import bisq.presentation.parser.PriceParser; @@ -266,7 +267,8 @@ public static class View extends bisq.desktop.common.view.View textInputTextListener; - public PriceInputBox(String description, String prompt) { + public PriceInputBox(String description, String prompt, String numericRegex) { super(description, prompt); bg.getStyleClass().setAll("bisq-dual-amount-bg"); @@ -67,8 +66,7 @@ public PriceInputBox(String description, String prompt) { getStyleClass().add("price-input-box"); textInputTextListener = (observable, oldValue, newValue) -> { - if (newValue.length() > INPUT_TEXT_MAX_LENGTH - || !newValue.matches(BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX)) { + if (newValue.length() > INPUT_TEXT_MAX_LENGTH || !newValue.matches(numericRegex)) { textInputControl.setText(oldValue); } applyFontStyle(textInputControl.getLength()); @@ -76,8 +74,8 @@ public PriceInputBox(String description, String prompt) { initialize(); } - public PriceInputBox(String description) { - this(description, null); + public PriceInputBox(String description, String numericRegex) { + this(description, null, numericRegex); } public void initialize() { diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/amount_selection/QuoteAmountInputBox.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/amount_selection/QuoteAmountInputBox.java index 9b3ddb791e..dd70c7b6e8 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/amount_selection/QuoteAmountInputBox.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/components/amount_selection/QuoteAmountInputBox.java @@ -260,7 +260,7 @@ protected void onViewDetached() { private void onTextChanged(ObservableValue observable, String oldValue, String newValue) { if (model.textInputMaxCharCount.isPresent()) { if (newValue.length() > model.textInputMaxCharCount.get() - || !newValue.matches(BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX)) { + || !newValue.matches(BisqEasyViewUtils.POSITIVE_NUMERIC_WITH_DECIMAL_REGEX)) { textInput.setText(oldValue); } } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/trade_wizard/price/TradeWizardPriceView.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/trade_wizard/price/TradeWizardPriceView.java index 33821f2b8e..a857f33a9b 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/trade_wizard/price/TradeWizardPriceView.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/trade_wizard/price/TradeWizardPriceView.java @@ -23,6 +23,7 @@ import bisq.desktop.components.containers.Spacer; import bisq.desktop.components.controls.UnorderedList; import bisq.desktop.components.controls.validator.PercentageValidator; +import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils; import bisq.desktop.main.content.bisq_easy.components.PriceInput; import bisq.desktop.main.content.bisq_easy.components.PriceInputBox; import bisq.i18n.Res; @@ -80,7 +81,8 @@ public TradeWizardPriceView(TradeWizardPriceModel model, TradeWizardPriceControl pricingModels.getStyleClass().addAll("selection-models", "bisq-text-3"); // Input box - percentageInput = new PriceInputBox(Res.get("bisqEasy.price.percentage.inputBoxText")); + percentageInput = new PriceInputBox(Res.get("bisqEasy.price.percentage.inputBoxText"), + BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX); percentageInput.setValidator(new PercentageValidator()); percentageInput.textInputSymbolTextProperty().set("%"); VBox fieldsBox = new VBox(20, priceInput.getRoot(), percentageInput);