29
29
30
30
namespace osu . Framework . Graphics . UserInterface
31
31
{
32
- public abstract partial class TextBox : TabbableContainer , IHasCurrentValue < string > , IKeyBindingHandler < PlatformAction >
32
+ public abstract partial class TextBox : TabbableContainer , IHasCurrentValue < string > , IKeyBindingHandler < PlatformAction > , ICanSuppressKeyEventLogging
33
33
{
34
34
protected FillFlowContainer TextFlow { get ; private set ; }
35
35
protected Container TextContainer { get ; private set ; }
36
36
37
37
public override bool HandleNonPositionalInput => HasFocus ;
38
38
39
+ /// <summary>
40
+ /// A character displayed whenever the type of text input set by <see cref="TextInputProperties.Type"/> is hidden.
41
+ /// </summary>
42
+ protected virtual char MaskCharacter => '*' ;
43
+
39
44
/// <summary>
40
45
/// Padding to be used within the TextContainer. Requires special handling due to the sideways scrolling of text content.
41
46
/// </summary>
@@ -50,12 +55,14 @@ public abstract partial class TextBox : TabbableContainer, IHasCurrentValue<stri
50
55
/// <summary>
51
56
/// Whether clipboard copying functionality is allowed.
52
57
/// </summary>
53
- protected virtual bool AllowClipboardExport => true ;
58
+ protected virtual bool AllowClipboardExport => ! InputProperties . Type . IsPassword ( ) ;
54
59
55
60
/// <summary>
56
61
/// Whether seeking to word boundaries is allowed.
57
62
/// </summary>
58
- protected virtual bool AllowWordNavigation => true ;
63
+ protected virtual bool AllowWordNavigation => ! InputProperties . Type . IsPassword ( ) ;
64
+
65
+ bool ICanSuppressKeyEventLogging . SuppressKeyEventLogging => InputProperties . Type . IsPassword ( ) ;
59
66
60
67
/// <summary>
61
68
/// Represents the left/right selection coordinates of the word double clicked on when dragging.
@@ -67,17 +74,13 @@ public abstract partial class TextBox : TabbableContainer, IHasCurrentValue<stri
67
74
/// </summary>
68
75
public virtual bool HandleLeftRightArrows => true ;
69
76
77
+ [ Obsolete ( $ "Use { nameof ( InputProperties ) } instead.") ] // can be removed 20250506
78
+ protected virtual bool AllowIme => true ;
79
+
70
80
/// <summary>
71
- /// Whether to allow IME input when this text box has input focus .
81
+ /// A set of properties to consider when interacting with this <see cref="TextBox"/> .
72
82
/// </summary>
73
- /// <remarks>
74
- /// This is just a hint to the native implementation, some might respect this,
75
- /// while others will ignore and always have the IME (dis)allowed.
76
- /// </remarks>
77
- /// <example>
78
- /// Useful for situations where IME input is not wanted, such as for passwords, numbers, or romanised text.
79
- /// </example>
80
- protected virtual bool AllowIme => true ;
83
+ public TextInputProperties InputProperties { get ; init ; }
81
84
82
85
/// <summary>
83
86
/// Check if a character can be added to this TextBox.
@@ -87,9 +90,14 @@ public abstract partial class TextBox : TabbableContainer, IHasCurrentValue<stri
87
90
protected virtual bool CanAddCharacter ( char character ) => true ;
88
91
89
92
/// <summary>
90
- /// Private helper for <see cref="CanAddCharacter"/>, additionally requiring that the character is not a control character.
93
+ /// Private helper for <see cref="CanAddCharacter"/>, additionally requiring that the character is not a control character and obeys <see cref="TextInputProperties.Type"/> .
91
94
/// </summary>
92
- private bool canAddCharacter ( char character ) => ! char . IsControl ( character ) && CanAddCharacter ( character ) ;
95
+ private bool canAddCharacter ( char character )
96
+ {
97
+ return ! char . IsControl ( character )
98
+ && ( ! InputProperties . Type . IsNumerical ( ) || char . IsAsciiDigit ( character ) )
99
+ && CanAddCharacter ( character ) ;
100
+ }
93
101
94
102
private bool readOnly ;
95
103
@@ -158,6 +166,10 @@ public bool ReadOnly
158
166
159
167
protected TextBox ( )
160
168
{
169
+ #pragma warning disable CS0618 // Type or member is obsolete
170
+ InputProperties = new TextInputProperties ( TextInputType . Text , AllowIme ) ;
171
+ #pragma warning restore CS0618 // Type or member is obsolete
172
+
161
173
Masking = true ;
162
174
163
175
Children = new Drawable [ ]
@@ -790,6 +802,9 @@ private string removeCharacters(int number = 1)
790
802
791
803
protected virtual Drawable AddCharacterToFlow ( char c )
792
804
{
805
+ if ( InputProperties . Type . IsPassword ( ) )
806
+ c = MaskCharacter ;
807
+
793
808
// Remove all characters to the right and store them in a local list,
794
809
// such that their depth can be updated.
795
810
List < Drawable > charsRight = new List < Drawable > ( ) ;
@@ -1340,7 +1355,7 @@ protected override void OnFocusLost(FocusLostEvent e)
1340
1355
protected override bool OnClick ( ClickEvent e )
1341
1356
{
1342
1357
if ( ! ReadOnly && textInputBound )
1343
- textInput . EnsureActivated ( AllowIme ) ;
1358
+ textInput . EnsureActivated ( InputProperties ) ;
1344
1359
1345
1360
return ! ReadOnly ;
1346
1361
}
@@ -1367,17 +1382,17 @@ private void bindInput([CanBeNull] TextBox previous)
1367
1382
1368
1383
if ( textInputBound )
1369
1384
{
1370
- textInput . EnsureActivated ( AllowIme ) ;
1385
+ textInput . EnsureActivated ( InputProperties ) ;
1371
1386
return ;
1372
1387
}
1373
1388
1374
1389
// TextBox has special handling of text input activation when focus is changed directly from one TextBox to another.
1375
1390
// We don't deactivate and activate, but instead keep text input active during the focus handoff, so that virtual keyboards on phones don't flicker.
1376
1391
1377
1392
if ( previous ? . textInput == textInput )
1378
- textInput . EnsureActivated ( AllowIme , ScreenSpaceDrawQuad . AABBFloat ) ;
1393
+ textInput . EnsureActivated ( InputProperties , ScreenSpaceDrawQuad . AABBFloat ) ;
1379
1394
else
1380
- textInput . Activate ( AllowIme , ScreenSpaceDrawQuad . AABBFloat ) ;
1395
+ textInput . Activate ( InputProperties , ScreenSpaceDrawQuad . AABBFloat ) ;
1381
1396
1382
1397
textInput . OnTextInput += handleTextInput ;
1383
1398
textInput . OnImeComposition += handleImeComposition ;
0 commit comments