Skip to content

Commit

Permalink
Merge pull request #3088 from axpoems/fix-cropping
Browse files Browse the repository at this point in the history
Fix cropping when using integer numbers
  • Loading branch information
HenrikJannsen authored Jan 8, 2025
2 parents 746acac + 428d14c commit 808020f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.List;

public class BisqEasyViewUtils {
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"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -266,7 +267,8 @@ public static class View extends bisq.desktop.common.view.View<Pane, Model, Cont
private View(Model model, Controller controller, NumberValidator validator) {
super(new VBox(), model, controller);

textInput = new PriceInputBox(model.description.get(), Res.get("component.priceInput.prompt"));
textInput = new PriceInputBox(model.description.get(), Res.get("component.priceInput.prompt"),
BisqEasyViewUtils.POSITIVE_NUMERIC_WITH_DECIMAL_REGEX);
textInput.setPrefWidth(WIDTH);
textInput.setValidator(validator);
textInput.conversionPriceSymbolTextProperty().set("%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Slf4j
public class PriceInputBox extends MaterialTextField {
public static final int AMOUNT_BOX_HEIGHT = 127;
private static final int INPUT_TEXT_MAX_LENGTH = 15;
private static final int INPUT_TEXT_MAX_LENGTH = 14;
private static final String INPUT_TEXT_9_STYLE_CLASS = "input-text-9";
private static final String INPUT_TEXT_10_STYLE_CLASS = "input-text-10";
private static final String INPUT_TEXT_11_STYLE_CLASS = "input-text-11";
Expand All @@ -42,7 +42,7 @@ public class PriceInputBox extends MaterialTextField {
private final HBox textInputAndSymbolHBox;
private final ChangeListener<String> 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");
Expand All @@ -66,16 +66,20 @@ public PriceInputBox(String description, String prompt) {
getStyleClass().add("price-input-box");

textInputTextListener = (observable, oldValue, newValue) -> {
if (newValue.length() > INPUT_TEXT_MAX_LENGTH) {
if (newValue.length() > INPUT_TEXT_MAX_LENGTH || !newValue.matches(numericRegex)) {
textInputControl.setText(oldValue);
}
applyFontStyle(textInputControl.getLength());
// If using an integer we need to count one more char since a dot occupies much less space.
int calculatedLength = !textInputControl.getText().contains(".")
? textInputControl.getLength() + 1
: textInputControl.getLength();
applyFontStyle(calculatedLength);
};
initialize();
}

public PriceInputBox(String description) {
this(description, null);
public PriceInputBox(String description, String numericRegex) {
this(description, null, numericRegex);
}

public void initialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AmountSelectionView extends View<VBox, AmountSelectionModel, Amount
public static final int AMOUNT_BOX_WIDTH = 300;
public static final int AMOUNT_BOX_HEIGHT = 120;
private static final int RANGE_INPUT_TEXT_MAX_LENGTH = 9;
private static final int FIXED_INPUT_TEXT_MAX_LENGTH = 19;
private static final int FIXED_INPUT_TEXT_MAX_LENGTH = 18;
private static final Insets SLIDER_INDICATORS_RANGE_MARGIN = new Insets(-15, 0, 0, 0);
private static final Insets SLIDER_INDICATORS_FIXED_MARGIN = new Insets(2.5, 0, 0, 0);
private static final String INPUT_TEXT_9_STYLE_CLASS = "input-text-9";
Expand Down Expand Up @@ -296,27 +296,42 @@ private void onInputTextFieldFocus(boolean isOtherFocused, boolean focus) {
}

private void applyTextInputPrefWidth() {
int charCount = model.getIsRangeAmountEnabled().get()
? minQuoteAmount.getTextInputLength() + maxOrFixedQuoteAmount.getTextInputLength() + 1 // for the dash
: maxOrFixedQuoteAmount.getTextInputLength();
int charCount = getCalculatedTotalCharCount();

int length = minQuoteAmount.getTextInputLength();
int length = getCalculatedTextInputLength(minQuoteAmount);
minQuoteAmount.setTextInputPrefWidth(length == 0 ? 1 : length * getFontCharWidth(charCount));

length = maxOrFixedQuoteAmount.getTextInputLength();
length = getCalculatedTextInputLength(maxOrFixedQuoteAmount);
maxOrFixedQuoteAmount.setTextInputPrefWidth(length == 0 ? 1 : length * getFontCharWidth(charCount));
}

private void applyTextInputFontStyle() {
quoteAmountSelectionHBox.getStyleClass().clear();
quoteAmountSelectionHBox.getStyleClass().add("quote-amount");

int charCount = model.getIsRangeAmountEnabled().get()
? minQuoteAmount.getTextInputLength() + maxOrFixedQuoteAmount.getTextInputLength() + 1 // for the dash
: maxOrFixedQuoteAmount.getTextInputLength();
int charCount = getCalculatedTotalCharCount();
quoteAmountSelectionHBox.getStyleClass().add(getFontStyleBasedOnTextLength(charCount));
}

private int getCalculatedTotalCharCount() {
int count = model.getIsRangeAmountEnabled().get()
? minQuoteAmount.getTextInputLength() + maxOrFixedQuoteAmount.getTextInputLength() + 1 // 1 for the dash
: maxOrFixedQuoteAmount.getTextInputLength();

if (!minQuoteAmount.getTextInput().contains(".") || !maxOrFixedQuoteAmount.getTextInput().contains(".")) {
// If using an integer we need to count one more char since a dot occupies much less space.
++count;
}
return count;
}

private int getCalculatedTextInputLength(QuoteAmountInputBox quoteAmountInputBox) {
// If using an integer we need to count one more char since a dot occupies much less space.
return !quoteAmountInputBox.getTextInput().contains(".")
? quoteAmountInputBox.getTextInputLength() + 1
: quoteAmountInputBox.getTextInputLength();
}

private void deselectAll() {
minQuoteAmount.deselect();
maxOrFixedQuoteAmount.deselect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.common.monetary.Monetary;
import bisq.common.util.MathUtils;
import bisq.common.util.StringUtils;
import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils;
import bisq.presentation.formatters.AmountFormatter;
import bisq.presentation.parser.AmountParser;
import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -84,6 +85,10 @@ public int getTextInputLength() {
return controller.view.textInput.getLength();
}

public String getTextInput() {
return controller.view.textInput.getText();
}

public void setTextInputPrefWidth(int prefWidth) {
controller.view.textInput.setPrefWidth(prefWidth);
}
Expand Down Expand Up @@ -258,7 +263,8 @@ protected void onViewDetached() {

private void onTextChanged(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (model.textInputMaxCharCount.isPresent()) {
if (newValue.length() > model.textInputMaxCharCount.get()) {
if (newValue.length() > model.textInputMaxCharCount.get()
|| !newValue.matches(BisqEasyViewUtils.POSITIVE_NUMERIC_WITH_DECIMAL_REGEX)) {
textInput.setText(oldValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/desktop/src/main/resources/css/bisq_easy.css
Original file line number Diff line number Diff line change
Expand Up @@ -829,12 +829,12 @@
/******** ALL SIZES ********/
.input-text-9 .text-input,
.input-text-9 .quote-separator {
-fx-font-size: 4em !important;
-fx-font-size: 3.99em !important;
}

.input-text-10 .text-input,
.input-text-10 .quote-separator {
-fx-font-size: 3.58em !important;
-fx-font-size: 3.55em !important;
}

.input-text-11 .text-input,
Expand Down

0 comments on commit 808020f

Please sign in to comment.