24 private Color _backgroundColor = Color.FromArgb(
30 private Color _borderColor = Color.FromArgb(
36 private Color _focusedBorderColor = Color.FromArgb(
42 private SKColor _textColor = SKColors.White;
43 private SKColor _placeholderColor =
new(170, 170, 180);
45 private string _text =
string.Empty;
46 private string _placeholder =
string.Empty;
47 private int _caretIndex;
48 private int? _maxLength;
49 private double _initialKeyRepeatDelaySeconds = 1.25;
50 private double _repeatedKeyIntervalSeconds = 0.05;
51 private int? _repeatingKey;
52 private long _keyPressedTick;
53 private long _lastRepeatTick;
54 private Func<WidgetKeyboardEventArgs, char?> _characterResolver = ResolveWindowsVirtualKeyCharacter;
80 string? placeholder =
null,
81 string? nickname =
null)
85 ValidateBounds(bounds).Location,
88 ArgumentNullException.ThrowIfNull(view);
90 _text = NormalizeText(text);
91 _placeholder = placeholder ??
string.Empty;
92 _caretIndex = _text.Length;
94 Background = CreateBackground(renderSurfaceHost, view, bounds);
95 TextBlock = CreateTextBlock(renderSurfaceHost, view, bounds);
100 CompleteInitialization();
117 string? placeholder =
null,
118 string? nickname =
null)
122 ValidateBounds(bounds).Location,
125 ArgumentNullException.ThrowIfNull(sceneLayer);
127 _text = NormalizeText(text);
128 _placeholder = placeholder ??
string.Empty;
129 _caretIndex = _text.Length;
131 Background = CreateBackground(renderSurfaceHost, sceneLayer, bounds);
132 TextBlock = CreateTextBlock(renderSurfaceHost, sceneLayer, bounds);
137 CompleteInitialization();
167 _placeholder = value ??
string.Empty;
168 RefreshDisplayedText();
180 _caretIndex = Math.Clamp(value, 0, _text.Length);
181 RefreshDisplayedText();
193 if (value.HasValue && value.Value < 0)
194 throw new ArgumentOutOfRangeException(nameof(value));
198 if (_maxLength.HasValue && _text.Length > _maxLength.Value)
199 SetText(_text[.._maxLength.Value]);
209 get => _initialKeyRepeatDelaySeconds;
212 if (!
double.IsFinite(value) || value < 0d)
213 throw new ArgumentOutOfRangeException(nameof(value));
215 _initialKeyRepeatDelaySeconds = value;
226 get => _repeatedKeyIntervalSeconds;
229 if (!
double.IsFinite(value) || value < 0d)
230 throw new ArgumentOutOfRangeException(nameof(value));
232 _repeatedKeyIntervalSeconds = value;
245 ? Background.ScreenBounds
288 get => _characterResolver;
289 set => _characterResolver = value ?? NoCharacterResolver;
299 string normalized = NormalizeText(text);
302 normalized = normalized[..
MaxLength.Value];
304 if (_text == normalized)
306 _caretIndex = Math.Min(_caretIndex, _text.Length);
307 RefreshDisplayedText();
312 _caretIndex = _text.Length;
313 RefreshDisplayedText();
328 string insertion = NormalizeText(text);
330 if (insertion.Length == 0)
335 int available = MaxLength.Value - _text.Length;
340 if (insertion.Length > available)
341 insertion = insertion[..available];
344 _text = _text.Insert(_caretIndex, insertion);
345 _caretIndex += insertion.Length;
346 RefreshDisplayedText();
357 if (
IsReadOnly || _caretIndex <= 0 || _text.Length == 0)
360 _text = _text.
Remove(_caretIndex - 1, 1);
362 RefreshDisplayedText();
373 if (
IsReadOnly || _caretIndex < 0 || _caretIndex >= _text.Length)
376 _text = _text.
Remove(_caretIndex, 1);
377 RefreshDisplayedText();
389 CaretIndex = Math.Clamp(_caretIndex + delta, 0, _text.Length);
405 _backgroundColor = background;
406 _borderColor = border;
407 _focusedBorderColor = focusedBorder;
410 .SetBorderColor(
IsFocused ? _focusedBorderColor : _borderColor);
425 _placeholderColor = placeholder;
426 RefreshDisplayedText();
437 Background.ZOrder = zOrder;
445 base.OnPointerClick(args);
450 _caretIndex = _text.Length;
451 RefreshDisplayedText();
458 base.OnFocusGained();
459 Background.SetBorderColor(_focusedBorderColor);
461 RefreshDisplayedText();
468 ResetKeyRepeatState();
471 RefreshDisplayedText();
477 base.OnKeyboardInput(args);
481 if (_repeatingKey == args.
Key)
482 ResetKeyRepeatState();
490 long currentTick = HighResTimer.GetCurrentTick();
494 _repeatingKey = args.
Key;
495 _keyPressedTick = currentTick;
496 _lastRepeatTick = currentTick;
500 if (_repeatingKey != args.
Key ||
501 _keyPressedTick == 0 ||
502 HighResTimer.GetDuration(_keyPressedTick, currentTick) < _initialKeyRepeatDelaySeconds ||
503 (_lastRepeatTick != 0 &&
504 HighResTimer.GetDuration(_lastRepeatTick, currentTick) < _repeatedKeyIntervalSeconds))
510 _lastRepeatTick = currentTick;
570 if (!character.HasValue)
577 private void ResetKeyRepeatState()
579 _repeatingKey =
null;
584 private void CompleteInitialization()
589 RefreshDisplayedText();
592 private void RefreshDisplayedText()
596 string withCaret = _text.Insert(_caretIndex,
"|");
599 .
SetColors(_textColor, SKColors.Transparent);
604 bool showPlaceholder = _text.Length == 0 && _placeholder.Length > 0;
607 .
SetColors(showPlaceholder ? _placeholderColor : _textColor, SKColors.Transparent);
610 private DirectRectangle CreateBackground(
611 RenderSurfaceHostBase host,
615 return ConfigureBackground(
623 private DirectRectangle CreateBackground(
624 RenderSurfaceHostBase host,
628 return ConfigureBackground(
636 private DirectRectangle ConfigureBackground(DirectRectangle background)
644 private static TextBlock CreateTextBlock(
645 RenderSurfaceHostBase host,
649 return ConfigureTextBlock(
new TextBlock(host, view, bounds));
652 private static TextBlock CreateTextBlock(
653 RenderSurfaceHostBase host,
657 return ConfigureTextBlock(
662 worldBounds: bounds));
667 return textBlock.
SetFont(SKTypeface.Default, 16f, minSize: 10f)
668 .
SetColors(SKColors.White, SKColors.Transparent)
674 private static string NormalizeText(
string? text)
676 return (text ??
string.Empty)
681 private static char? NoCharacterResolver(WidgetKeyboardEventArgs args)
686 private static char? ResolveWindowsVirtualKeyCharacter(WidgetKeyboardEventArgs args)
694 if (key is >= 65 and <= 90)
696 char letter = (char)key;
697 return shift ? letter :
char.ToLowerInvariant(letter);
700 if (key is >= 48 and <= 57)
705 const string shiftedDigits =
")!@#$%^&*(";
706 return shiftedDigits[key - 48];
709 if (key is >= 96 and <= 105)
710 return (
char)(
'0' + key - 96);
720 186 => shift ?
':' :
';',
721 187 => shift ?
'+' :
'=',
722 188 => shift ?
'<' :
',',
723 189 => shift ?
'_' :
'-',
724 190 => shift ?
'>' :
'.',
725 191 => shift ?
'?' :
'/',
726 192 => shift ?
'~' :
'`',
727 219 => shift ?
'{' :
'[',
728 220 => shift ?
'|' :
'\\',
729 221 => shift ?
'}' :
']',
730 222 => shift ?
'"' :
'\'',
735 private static void Refresh(DirectRectangle rectangle)
740 private static Rectangle ValidateBounds(Rectangle bounds)
742 if (bounds.Width < 40 || bounds.Height < 18)
743 throw new ArgumentOutOfRangeException(nameof(bounds), bounds,
"Text-box bounds are too small.");
virtual DirectComposite Remove(IDirectCompositeChild child)
Removes a child without disposing it.
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
int ZOrder
Gets or sets the Z-order (depth) used for sorting this direct drawing relative to other drawables.
Vector2 GetPosition()
Gets the current position of this direct drawing in pixel coordinates.
void SetPosition(Vector2 p)
Sets the position of this direct drawing to the specified pixel coordinates.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
DirectRectangle SetFilled(bool isFilled)
Sets whether the rectangle interior is filled with the base color (or pattern).
DirectRectangle SetBorderColor(Color color)
Sets a distinct border (stroke) color, independent of the fill color.
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
DirectRectangle SetCornerRadius(float radius)
Sets the radius for rounded corners in pixels.
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
TextBlock SetFont(SKTypeface typeface, float size, float? minSize=null)
Sets the font face and size (and optional minimum size for auto-shrink).
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
TextBlock SetAlignment(SKTextAlign h, VerticalAlign v)
Sets the horizontal and vertical alignment used when drawing the laid-out lines within the bounds.
TextBlock SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
Action< string >? TextChanged
Occurs when the text changes.
TextBoxWidget InsertText(string? text)
Inserts text at the current caret position.
double InitialKeyRepeatDelaySeconds
Gets or sets the delay, in seconds, before a held key begins repeating. Pressed events remain immedia...
override void OnFocusLost()
Called when the widget loses focus.
TextBoxWidget SetText(string? text)
Replaces the text and moves the caret to the end.
Func< WidgetKeyboardEventArgs, char?> CharacterResolver
Gets or sets the function used to convert routed key events into printable characters.
TextBoxWidget SetTextColors(SKColor text, SKColor placeholder)
Sets the text and placeholder colors.
TextBoxWidget MoveCaret(int delta)
Moves the caret by the specified character delta.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
TextBoxWidget Backspace()
Removes the character before the caret when possible.
string Placeholder
Gets or sets placeholder text shown while the control is empty and unfocused.
int RightKey
Gets or sets the key code used to move right. Defaults to 39.
string Text
Gets or sets the current text.
int LeftKey
Gets or sets the key code used to move left. Defaults to 37.
TextBoxWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string text="", string? placeholder=null, string? nickname=null)
Creates a view-level text box.
Action< string >? Submitted
Occurs when the configured submit key is pressed.
TextBoxWidget Delete()
Removes the character at the caret when possible.
bool IsReadOnly
Gets or sets whether user editing is disabled.
TextBoxWidget SetColors(Color background, Color border, Color focusedBorder)
Sets the background and border colors used for normal and focused states.
int EndKey
Gets or sets the key code used to move to the end. Defaults to 35.
DirectRectangle Background
Gets the text-box background and border drawing.
int BackspaceKey
Gets or sets the key code used for backspace. Defaults to 8.
TextBlock TextBlock
Gets the text drawing used by the text box.
override void OnFocusGained()
Called when the widget gains focus.
Rectangle Bounds
Gets the text-box bounds in its native coordinate space.
int? MaxLength
Gets or sets the maximum permitted text length. A null value means unlimited.
int DeleteKey
Gets or sets the key code used for Delete. Defaults to 46.
int SubmitKey
Gets or sets the key code used to submit the text. Defaults to Enter (13).
double RepeatedKeyIntervalSeconds
Gets or sets the minimum interval, in seconds, between repeated held-key actions after InitialKeyRepe...
TextBoxWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string text="", string? placeholder=null, string? nickname=null)
Creates a scene-layer text box.
int CaretIndex
Gets or sets the caret position in the range zero through Text length.
int HomeKey
Gets or sets the key code used to move to the beginning. Defaults to 36.
TextBoxWidget SetTextBoxZOrder(int zOrder)
Sets the base Z-order used by the text-box visuals.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.