Skip to content

Commit 1075b9d

Browse files
authored
Merge pull request #6672 from bdach/negative-numbers-exist
Fix `TextBox` not allowing minus sign to be input in numerical type textboxes
2 parents 1aceac5 + ed80b5d commit 1075b9d

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

osu.Framework/Graphics/UserInterface/TextBox.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,22 @@ private bool canAddCharacter(char character)
103103
// discard control/special characters.
104104
return false;
105105

106-
var currentNumberFormat = CultureInfo.CurrentCulture.NumberFormat;
107-
108-
switch (InputProperties.Type)
106+
if (InputProperties.Type.IsNumerical())
109107
{
110-
case TextInputType.Decimal:
111-
return char.IsAsciiDigit(character) || currentNumberFormat.NumberDecimalSeparator.Contains(character);
108+
bool validNumericalCharacter = false;
112109

113-
case TextInputType.Number:
114-
case TextInputType.NumericalPassword:
115-
return char.IsAsciiDigit(character);
110+
var currentNumberFormat = CultureInfo.CurrentCulture.NumberFormat;
116111

117-
default:
118-
return true;
112+
validNumericalCharacter |= char.IsAsciiDigit(character);
113+
validNumericalCharacter |= selectionLeft == 0 && currentNumberFormat.NegativeSign.Contains(character);
114+
115+
if (InputProperties.Type == TextInputType.Decimal)
116+
validNumericalCharacter |= currentNumberFormat.NumberDecimalSeparator.Contains(character);
117+
118+
return validNumericalCharacter;
119119
}
120+
121+
return true;
120122
}
121123

122124
private bool readOnly;

osu.Framework/Input/TextInputType.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ public static bool IsPassword(this TextInputType type)
6666
}
6767
}
6868

69+
public static bool IsNumerical(this TextInputType type)
70+
{
71+
switch (type)
72+
{
73+
case TextInputType.Number:
74+
case TextInputType.NumericalPassword:
75+
case TextInputType.Decimal:
76+
return true;
77+
78+
default:
79+
return false;
80+
}
81+
}
82+
6983
public static bool SupportsIme(this TextInputType type) => type == TextInputType.Name || type == TextInputType.Text;
7084
}
7185
}

0 commit comments

Comments
 (0)