@@ -85,6 +85,11 @@ public string Text
8585 /// <summary>The style applied during the last <see cref="Element"/> call.</summary>
8686 public TextInputStyle CurrentStyle { get ; private set ; }
8787
88+ /// <summary>Returns the text to display — masked if in password mode, otherwise the real text.</summary>
89+ public string DisplayText => CurrentStyle . Password
90+ ? new string ( CurrentStyle . PasswordChar == '\0 ' ? '*' : CurrentStyle . PasswordChar , _chars . Count )
91+ : Text ;
92+
8893 /// <summary>Current vertical scroll offset in pixels (for multiline inputs).</summary>
8994 public float ScrollY => _scrollY ;
9095
@@ -171,7 +176,15 @@ public float MeasureSubstring(int from, int to)
171176 {
172177 if ( from >= to || from >= _chars . Count ) return 0 ;
173178 to = Math . Min ( to , _chars . Count ) ;
174- var span = CollectionsMarshal . AsSpan ( _chars ) . Slice ( from , to - from ) ;
179+ int len = to - from ;
180+ if ( CurrentStyle . Password )
181+ {
182+ char mask = CurrentStyle . PasswordChar == '\0 ' ? '*' : CurrentStyle . PasswordChar ;
183+ Span < char > maskBuf = len <= 256 ? stackalloc char [ len ] : new char [ len ] ;
184+ maskBuf . Fill ( mask ) ;
185+ return _measurer . MeasureText ( maskBuf , _fontId , _fontSize , _letterSpacing ) . Width ;
186+ }
187+ var span = CollectionsMarshal . AsSpan ( _chars ) . Slice ( from , len ) ;
175188 return _measurer . MeasureText ( span , _fontId , _fontSize , _letterSpacing ) . Width ;
176189 }
177190
@@ -322,6 +335,12 @@ float ITextEditHandler.GetCharWidth(int lineStartIndex, int charIndex)
322335 if ( idx >= _chars . Count ) return 0 ;
323336 var span = CollectionsMarshal . AsSpan ( _chars ) ;
324337 if ( span [ idx ] == '\n ' ) return 0 ;
338+ if ( CurrentStyle . Password )
339+ {
340+ char mask = CurrentStyle . PasswordChar == '\0 ' ? '*' : CurrentStyle . PasswordChar ;
341+ ReadOnlySpan < char > maskSpan = stackalloc char [ ] { mask } ;
342+ return _measurer . MeasureText ( maskSpan , _fontId , _fontSize , _letterSpacing ) . Width ;
343+ }
325344 return _measurer . MeasureText ( span . Slice ( idx , 1 ) , _fontId , _fontSize , _letterSpacing ) . Width ;
326345 }
327346
@@ -339,7 +358,19 @@ void ITextEditHandler.LayoutRow(out TextEditRow row, int lineStartIndex)
339358
340359 float width = 0 ;
341360 if ( textChars > 0 )
342- width = _measurer . MeasureText ( span . Slice ( lineStartIndex , textChars ) , _fontId , _fontSize , _letterSpacing ) . Width ;
361+ {
362+ if ( CurrentStyle . Password )
363+ {
364+ char mask = CurrentStyle . PasswordChar == '\0 ' ? '*' : CurrentStyle . PasswordChar ;
365+ Span < char > maskBuf = textChars <= 256 ? stackalloc char [ textChars ] : new char [ textChars ] ;
366+ maskBuf . Fill ( mask ) ;
367+ width = _measurer . MeasureText ( maskBuf , _fontId , _fontSize , _letterSpacing ) . Width ;
368+ }
369+ else
370+ {
371+ width = _measurer . MeasureText ( span . Slice ( lineStartIndex , textChars ) , _fontId , _fontSize , _letterSpacing ) . Width ;
372+ }
373+ }
343374
344375 float lh = _cachedLineHeight ;
345376 row = new TextEditRow
0 commit comments