From c7f7ea97e3a090e549ad0316f779f18670d763ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 Oct 2026 01:57:09 +0300 Subject: [PATCH 1/5] Port RichText key handling and context menu copy --- Source.GUI.Controls/RichText.cs | 92 +++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 3 deletions(-) diff --git a/Source.GUI.Controls/RichText.cs b/Source.GUI.Controls/RichText.cs index 8049e2f9..f3bbce3e 100644 --- a/Source.GUI.Controls/RichText.cs +++ b/Source.GUI.Controls/RichText.cs @@ -713,9 +713,94 @@ public int PixelToCursorSpace(int cx, int cy) { return i; } - //public override void OnKeyCodeTyped(ButtonCode code) // todo - //{ - //} + public override void OnKeyCodeTyped(ButtonCode code) { + bool shift = Input.IsKeyDown(ButtonCode.KeyLShift) || Input.IsKeyDown(ButtonCode.KeyRShift); + bool ctrl = Input.IsKeyDown(ButtonCode.KeyLControl) || Input.IsKeyDown(ButtonCode.KeyRControl); + bool alt = Input.IsKeyDown(ButtonCode.KeyLAlt) || Input.IsKeyDown(ButtonCode.KeyRAlt); + bool winkey = Input.IsKeyDown(ButtonCode.KeyLWin) || Input.IsKeyDown(ButtonCode.KeyRWin); + bool fallThrough = false; + + if (ctrl || (winkey && IsOSX)) { + switch (code) { + case ButtonCode.KeyA: + CursorPos = 0; + Select[0] = 0; + Select[1] = TextStream.Count; + return; + + case ButtonCode.KeyInsert: + case ButtonCode.KeyC: + case ButtonCode.KeyX: + CopySelected(); + return; + + case ButtonCode.KeyPageUp: + case ButtonCode.KeyHome: + GotoTextStart(); + break; + + case ButtonCode.KeyPageDown: + case ButtonCode.KeyEnd: + GotoTextEnd(); + break; + + default: + fallThrough = true; + break; + } + } + else if (alt) + fallThrough = true; + else { + switch (code) { + case ButtonCode.KeyTab: + case ButtonCode.KeyLShift: + case ButtonCode.KeyRShift: + case ButtonCode.KeyEscape: + case ButtonCode.KeyEnter: + fallThrough = true; + break; + + case ButtonCode.KeyDelete: + if (shift) + CopySelected(); + break; + + case ButtonCode.KeyHome: + GotoTextStart(); + break; + + case ButtonCode.KeyEnd: + GotoTextEnd(); + break; + + case ButtonCode.KeyPageUp: + if (VertScrollBar.IsVisible()) { + int window = VertScrollBar.GetRangeWindow(); + int newval = VertScrollBar.GetValue(); + VertScrollBar.SetValue(newval - window - 1); + } + break; + + case ButtonCode.KeyPageDown: + if (VertScrollBar.IsVisible()) { + int window = VertScrollBar.GetRangeWindow(); + int newval = VertScrollBar.GetValue(); + VertScrollBar.SetValue(newval + window + 1); + } + break; + + default: + return; + } + } + + if (code != ButtonCode.KeyLControl && code != ButtonCode.KeyRControl) + Select[1] = CursorPos; + + if (fallThrough) + base.OnKeyCodeTyped(code); + } public bool GetSelectedRange(out int start, out int end) { if (Select[0] == -1) { @@ -966,6 +1051,7 @@ public virtual void CutSelected() { RequestFocus(); } + [UiMessage("DoCopySelected")] public virtual void CopySelected() { if (GetSelectedRange(out int x0, out int x1)) { List buf = []; From 99ebed25d96b3ad21ab601bb74e297a1fad772e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:15:12 +0300 Subject: [PATCH 2/5] Fix menus not closing when focus moves to their parent --- Source.GUI.Controls/Menu.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source.GUI.Controls/Menu.cs b/Source.GUI.Controls/Menu.cs index d1afb5ba..7bd41b55 100644 --- a/Source.GUI.Controls/Menu.cs +++ b/Source.GUI.Controls/Menu.cs @@ -853,7 +853,7 @@ public override void OnMouseWheeled(int delta) { } public override void OnKillFocus(Panel? newPanel) { - if (newPanel == null || !HasParent(newPanel)) { + if (newPanel == null || !newPanel.HasParent(this)) { if (!IsKeyboardInputEnabled() && newPanel == null) return; From 06d6b006a814cb248819608ed24264c2dfe0edc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:34:47 +0300 Subject: [PATCH 3/5] Fix stay-at-back popups being raised above other popups --- Game.Client/GarrysMod/LuaPanel.cs | 4 +--- Source.Common/GUI/IPanel.cs | 2 ++ Source.GUI.Controls/Panel.cs | 3 +++ Source.MaterialSystem/Surface/MatSystemSurface.cs | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Game.Client/GarrysMod/LuaPanel.cs b/Game.Client/GarrysMod/LuaPanel.cs index 66e74a69..b575272e 100644 --- a/Game.Client/GarrysMod/LuaPanel.cs +++ b/Game.Client/GarrysMod/LuaPanel.cs @@ -1674,9 +1674,7 @@ static int Panel__SetTextHidden(ILuaInterface lua) { [LuaMethod] static int Panel__SetPopupStayAtBack(ILuaInterface lua) { - Panel? panel = GetPanelOrError(lua); - if (panel != null && lua.GetBool(2)) - Surface.MovePopupToBack(panel); + GetPanelOrError(lua)?.SetPopupStayAtBack(lua.GetBool(2)); return 0; } diff --git a/Source.Common/GUI/IPanel.cs b/Source.Common/GUI/IPanel.cs index a1384073..ffb463a2 100644 --- a/Source.Common/GUI/IPanel.cs +++ b/Source.Common/GUI/IPanel.cs @@ -127,6 +127,8 @@ public interface IPanel : IExtDisposable // Used by the drag/drop manager to always draw on top bool IsTopmostPopup(); void SetTopmostPopup(bool state); + bool IsPopupStayAtBack(); + void SetPopupStayAtBack(bool state); void InvalidateLayout(bool layoutNow = false, bool reloadScheme = false); int GetAlpha(); diff --git a/Source.GUI.Controls/Panel.cs b/Source.GUI.Controls/Panel.cs index ad7ce231..6620db98 100644 --- a/Source.GUI.Controls/Panel.cs +++ b/Source.GUI.Controls/Panel.cs @@ -310,6 +310,7 @@ public void SetScheme(IScheme scheme) { bool MouseInput; bool KbInput; bool TopmostPopup; + bool PopupStayAtBack; [PanelAnimationVar("alpha", "255")] float Alpha; IBorder? Border; IScheme? Scheme; @@ -1228,6 +1229,8 @@ public bool IsProportional() { } public bool IsTopmostPopup() => TopmostPopup; + public bool IsPopupStayAtBack() => PopupStayAtBack; + public void SetPopupStayAtBack(bool state) => PopupStayAtBack = state; public bool IsVisible() { return Visible; diff --git a/Source.MaterialSystem/Surface/MatSystemSurface.cs b/Source.MaterialSystem/Surface/MatSystemSurface.cs index 286e65c2..3150167f 100644 --- a/Source.MaterialSystem/Surface/MatSystemSurface.cs +++ b/Source.MaterialSystem/Surface/MatSystemSurface.cs @@ -1071,6 +1071,9 @@ public void MovePopupToBack(IPanel panel) { } public void MovePopupToFront(IPanel panel) { + if (panel.IsPopupStayAtBack()) + return; + int index = PopupList.IndexOf(panel); if (index == -1) return; From 9799dfb80e2cb78f3b573f26f7250f847546f012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:41:17 +0300 Subject: [PATCH 4/5] Add Ctrl+Backspace word deletion to TextEntry --- Source.GUI.Controls/TextEntry.cs | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Source.GUI.Controls/TextEntry.cs b/Source.GUI.Controls/TextEntry.cs index b806a15b..a0aa311c 100644 --- a/Source.GUI.Controls/TextEntry.cs +++ b/Source.GUI.Controls/TextEntry.cs @@ -772,6 +772,13 @@ public override void OnKeyCodeTyped(ButtonCode code) { GotoWordLeft(); break; + case ButtonCode.KeyBackspace: + if (GetSelectedRange(out _, out _)) + DeleteSelected(); + else + DeleteWordLeft(); + break; + case ButtonCode.KeyEnter: if (Multiline) { DeleteSelected(); @@ -987,6 +994,39 @@ private void GotoWordLeft() { Repaint(); } + enum CharClass { Whitespace, Word, Punctuation } + + static CharClass GetCharClass(char ch) { + if (char.IsWhiteSpace(ch)) + return CharClass.Whitespace; + if (char.IsLetterOrDigit(ch) || ch == '_') + return CharClass.Word; + return CharClass.Punctuation; + } + + private int FindWordLeft(int pos) { + while (pos > 0 && GetCharClass(TextStream[pos - 1]) == CharClass.Whitespace) + pos--; + + if (pos > 0) { + CharClass charClass = GetCharClass(TextStream[pos - 1]); + while (pos > 0 && GetCharClass(TextStream[pos - 1]) == charClass) + pos--; + } + + return pos; + } + + private void DeleteWordLeft() { + int start = FindWordLeft(CursorPos); + if (start == CursorPos) + return; + + Select[0] = start; + Select[1] = CursorPos; + DeleteSelected(); + } + private void ScrollLeft() { if (Multiline) return; From 7a2edb8a1f36c3a509cb99c9646221167cd5a1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:48:02 +0300 Subject: [PATCH 5/5] Move TextEntry word navigation by character class --- Source.GUI.Controls/TextEntry.cs | 39 +++++++++++++++----------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/Source.GUI.Controls/TextEntry.cs b/Source.GUI.Controls/TextEntry.cs index a0aa311c..a466f35a 100644 --- a/Source.GUI.Controls/TextEntry.cs +++ b/Source.GUI.Controls/TextEntry.cs @@ -976,16 +976,7 @@ private void GotoWordLeft() { if (CursorPos < 1) return; - while (--CursorPos >= 0) - if (!char.IsWhiteSpace(TextStream[CursorPos])) - break; - - - while (--CursorPos >= 0) - if (char.IsWhiteSpace(TextStream[CursorPos])) - break; - - CursorPos++; + CursorPos = FindWordLeft(CursorPos); ScrollLeft(); @@ -1017,7 +1008,22 @@ private int FindWordLeft(int pos) { return pos; } - private void DeleteWordLeft() { + private int FindWordRight(int pos) { + int count = TextStream.Count; + if (pos < count) { + CharClass charClass = GetCharClass(TextStream[pos]); + if (charClass != CharClass.Whitespace) + while (pos < count && GetCharClass(TextStream[pos]) == charClass) + pos++; + } + + while (pos < count && GetCharClass(TextStream[pos]) == CharClass.Whitespace) + pos++; + + return pos; + } + + private void DeleteWordLeft() { int start = FindWordLeft(CursorPos); if (start == CursorPos) return; @@ -1047,16 +1053,7 @@ private void ScrollLeft() { private void GotoWordRight() { SelectCheck(); - while (++CursorPos < TextStream.Count) - if (char.IsWhiteSpace(TextStream[CursorPos])) - break; - - while (++CursorPos < TextStream.Count) - if (!char.IsWhiteSpace(TextStream[CursorPos])) - break; - - if (CursorPos > TextStream.Count) - CursorPos = TextStream.Count; + CursorPos = FindWordRight(CursorPos); ScrollRight();