refactor: replace Key constants with KeyCode for improved clarity and performance

This commit is contained in:
HueByte
2026-07-16 19:40:18 +02:00
parent 4f2cffa372
commit 2157884e61
+62 -57
View File
@@ -11,6 +11,7 @@ using Serilog;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;
using Terminal.Gui.Drawing;
using Terminal.Gui.Drivers;
using Terminal.Gui.Input;
using Terminal.Gui.Text;
using Terminal.Gui.ViewBase;
@@ -41,23 +42,24 @@ public sealed partial class MainWindow : Runnable
private bool _usersPanelVisible = true;
private const int UsersPanelWidth = 22;
private const string DefaultInputTitle = "Message │ Enter=send │ Tab=complete │ Ctrl+K=search │ F6=pick message";
private static readonly Key F2Key = Key.F2;
private const KeyCode F2Key = KeyCode.F2;
private bool _hasStagedAttachments;
internal static readonly string AppVersion =
typeof(MainWindow).Assembly.GetName().Version?.ToString(3) ?? "?";
// Cached Key constants compare via .KeyCode to avoid Key.Equals (which also checks Handled)
private static readonly Key EnterKey = Key.Enter;
private static readonly Key NewlineKey = Key.N.WithCtrl;
private static readonly Key AltQKey = Key.Q.WithAlt;
private static readonly Key TabKey = Key.Tab;
private static readonly Key CtrlKKey = Key.K.WithCtrl;
private static readonly Key CtrlVKey = Key.V.WithCtrl;
private static readonly Key CtrlXKey = Key.X.WithCtrl;
private static readonly Key CtrlCKey = Key.C.WithCtrl;
private static readonly Key CtrlYKey = Key.Y.WithCtrl;
private static readonly Key F6Key = Key.F6;
// Key bindings as KeyCode constants: comparing raw KeyCodes avoids Key.Equals (which also
// checks Handled), and constants make them usable as switch case labels.
private const KeyCode EnterKey = KeyCode.Enter;
private const KeyCode NewlineKey = KeyCode.N | KeyCode.CtrlMask;
private const KeyCode AltQKey = KeyCode.Q | KeyCode.AltMask;
private const KeyCode TabKey = KeyCode.Tab;
private const KeyCode CtrlKKey = KeyCode.K | KeyCode.CtrlMask;
private const KeyCode CtrlVKey = KeyCode.V | KeyCode.CtrlMask;
private const KeyCode CtrlXKey = KeyCode.X | KeyCode.CtrlMask;
private const KeyCode CtrlCKey = KeyCode.C | KeyCode.CtrlMask;
private const KeyCode CtrlYKey = KeyCode.Y | KeyCode.CtrlMask;
private const KeyCode F6Key = KeyCode.F6;
// Available slash commands for Tab autocomplete
private static readonly string[] SlashCommands =
@@ -569,7 +571,7 @@ public sealed partial class MainWindow : Runnable
private void OnMessageListKeyDown(object? sender, Key e)
{
// F6 returns focus to the input box.
if (e.KeyCode == F6Key.KeyCode)
if (e.KeyCode == F6Key)
{
_inputField.SetFocus();
e.Handled = true;
@@ -715,18 +717,17 @@ public sealed partial class MainWindow : Runnable
private void OnInputKeyDown(object? sender, Key e)
{
if (e.KeyCode == TabKey.KeyCode)
switch (e.KeyCode)
{
case TabKey:
TryAutocompleteCommand();
e.Handled = true;
}
else if (e.KeyCode == NewlineKey.KeyCode)
{
break;
case NewlineKey:
_inputField.InsertText("\n");
e.Handled = true;
}
else if (e.KeyCode == EnterKey.KeyCode)
{
break;
case EnterKey:
var text = _inputField.Text?.Trim() ?? string.Empty;
// Send when there's text, or when only attachments are staged (empty caption).
if ((!string.IsNullOrEmpty(text) || _hasStagedAttachments)
@@ -735,27 +736,24 @@ public sealed partial class MainWindow : Runnable
OnMessageSubmitted?.Invoke(_messageManager.CurrentChannel, text);
_inputField.Text = string.Empty;
}
e.Handled = true;
}
else if (e.KeyCode == AltQKey.KeyCode)
{
break;
case AltQKey:
_app.RequestStop();
e.Handled = true;
}
else if (e.KeyCode == CtrlKKey.KeyCode)
{
break;
case CtrlKKey:
ShowSearchDialog();
e.Handled = true;
}
else if (e.KeyCode == F6Key.KeyCode)
{
break;
case F6Key:
// Move focus into the message list so you can select a message (arrows) and
// delete it (Delete). F6 again returns focus here. (Esc is the app quit key.)
FocusMessageList();
e.Handled = true;
}
else if (e.KeyCode == CtrlVKey.KeyCode || e.KeyCode == CtrlYKey.KeyCode)
{
break;
case CtrlVKey:
case CtrlYKey:
// Discord-style paste priority. Copied files in the OS file manager put a file
// list (not text) on the clipboard — attach them all. Copied image data (browser
// right-click copy, screenshot tools) is attached as a PNG. Otherwise paste text.
@@ -767,18 +765,21 @@ public sealed partial class MainWindow : Runnable
OnImagePasted?.Invoke(_messageManager.CurrentChannel, pastedPng);
else
GuardedClipboardAction(() => _inputField.Paste(), "paste");
e.Handled = true;
}
else if (e.KeyCode == CtrlXKey.KeyCode)
{
break;
case CtrlXKey:
GuardedClipboardAction(() => _inputField.Cut(), "cut");
e.Handled = true;
}
else if (e.KeyCode == CtrlCKey.KeyCode)
{
break;
case CtrlCKey:
GuardedClipboardAction(() => _inputField.Copy(), "copy");
e.Handled = true;
break;
default:
return; // not one of ours — leave e.Handled false so the key types normally
}
e.Handled = true;
}
/// <summary>
@@ -913,21 +914,25 @@ public sealed partial class MainWindow : Runnable
private void OnWindowKeyDown(object? sender, Key e)
{
if (e.KeyCode == AltQKey.KeyCode)
switch (e.KeyCode)
{
case AltQKey:
_app.RequestStop();
e.Handled = true;
}
else if (e.KeyCode == F2Key.KeyCode)
{
break;
case F2Key:
ToggleUsersPanel();
e.Handled = true;
}
else if (e.KeyCode == CtrlKKey.KeyCode)
{
break;
case CtrlKKey:
ShowSearchDialog();
e.Handled = true;
break;
default:
return;
}
e.Handled = true;
}
private void ShowSearchDialog()