Custom text editors
Start by extending the existing editor when you need one extra action. A new search or command field rarely needs its own caret, selection, Unicode boundaries, and native input bridge.
This field adds a hardware Return action while preserving ordinary editing. It ignores repeats and does not commit while the shared editor reports marked text.
using System;
using Xui.Core.Abstract.Events;
using Xui.Core.UI;
public sealed class CommitField : TextBox
{
public Action<string>? Committed { get; set; }
public override void OnKeyDown(ref KeyEventRef e)
{
if (e.Key == VirtualKey.Return && !e.IsRepeat && Editor.MarkedRange is null)
{
Committed?.Invoke(Text);
e.Handled = true;
return;
}
base.OnKeyDown(ref e);
}
}
Assign Committed when building the page and handle the accepted string there. Other keys call the base handler, preserving navigation and deletion. This example handles the hardware key path only: a native software-keyboard action can call the input-action contract directly. If both paths must invoke the same command, implement that behavior explicitly instead of assuming they are the same event.
Build from scratch only when the model requires it
A custom Canvas editor implements ITextEditingClient in addition to focus and input behavior. Its responsibilities include:
| Member | Required behavior |
|---|---|
Editor |
Return the same editing-state instance used to draw and process input. |
InputBounds |
Report the editor's bounds in logical window coordinates. |
GetCaretRect |
Map a UTF-16 offset to a caret rectangle in window coordinates. |
GetTextPosition |
Hit-test a window point and return a grapheme boundary. |
NativeSelection |
Respect when the native adapter owns caret/selection presentation. |
InputOptions and input actions |
Supply hints and implement Next, Previous, Done, or your explicit action policy. |
ITextInputClient alone is the smaller keyboard-request contract. It does not imply composition, range editing, or a native selection bridge.
Keep one editing truth
Draw the text and selection from Editor; do not maintain a second independent buffer for the platform. Subscribe to its change notifications and invalidate measurement when text affects size, then redraw. Respect the marked range during composition. Use the same font and measured geometry for painting, caret placement, and pointer hit testing.
Expose keyboard focus, show focus feedback, and blur on deactivation. Handle pointer capture cancellation and clean up subscriptions. A native adapter may query geometry after selection changes, so stale bounds produce misplaced handles even when the text itself looks correct.
The built-in model is single-line. A multiline, rich-text, or code editor needs additional document layout, scrolling, selection, and undo design; it is not a supported behavior obtained by overriding one drawing method. Verify native IME and clipboard behavior separately on every target, as described in IME and soft keyboards.