feat: Enhance command input handling by adding support for multi-line text input and refining text change logic

This commit is contained in:
BruceChen 2026-04-05 17:41:11 +08:00
parent a6c832b744
commit 871bc7651f

View file

@ -118,6 +118,7 @@ namespace MinecraftClient.Tui
};
_commandInput.AddHandler(KeyDownEvent, OnCommandKeyDown, Avalonia.Interactivity.RoutingStrategies.Tunnel);
_commandInput.AddHandler(TextInputEvent, OnCommandTextInput, Avalonia.Interactivity.RoutingStrategies.Tunnel);
_commandInput.TextChanged += OnCommandTextChanged;
var promptLabel = new TextBlock
@ -487,18 +488,41 @@ namespace MinecraftClient.Tui
_commandInput.CaretIndex = pos;
}
private void OnCommandTextInput(object? sender, TextInputEventArgs e)
{
string? incoming = e.Text;
if (string.IsNullOrEmpty(incoming) || (!incoming.Contains('\n') && !incoming.Contains('\r')))
return;
e.Handled = true;
string[] lines = incoming.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
string prefix = _commandInput.Text ?? "";
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
bool isLast = i == lines.Length - 1;
if (line.Length > 0 || prefix.Length > 0)
{
_acceptingSuggestion = true;
try { _commandInput.Text = prefix + line; }
finally { _acceptingSuggestion = false; }
}
if (!isLast)
{
SubmitCommand();
prefix = "";
}
}
}
private void OnCommandTextChanged(object? sender, TextChangedEventArgs e)
{
string text = _commandInput.Text ?? string.Empty;
if (text.Contains('\n') || text.Contains('\r'))
{
string cleaned = text.Replace("\r\n", " ").Replace('\r', ' ').Replace('\n', ' ');
_commandInput.Text = cleaned;
_commandInput.CaretIndex = cleaned.Length;
return;
}
if (_acceptingSuggestion || _tabCycling)
return;
@ -1172,6 +1196,8 @@ namespace MinecraftClient.Tui
public bool HasOverlay => _overlayContent != null;
public Control? OverlayContent => _overlayContent;
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Escape && _overlayContent != null)