From 4cb0630e195f8db25014ba77fce719dce10cfb11 Mon Sep 17 00:00:00 2001 From: William Chen Date: Thu, 16 Oct 2025 21:27:10 -0400 Subject: [PATCH 1/2] Don't internally store text input value This ensures that the value used is the value provided, which allows for better control. (cherry picked from commit 5691fa42fa8de6b5847ed33f93ba0e56fa9a46cc) --- Paper/ElementBuilder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Paper/ElementBuilder.cs b/Paper/ElementBuilder.cs index 2d6fbba..ff880d2 100644 --- a/Paper/ElementBuilder.cs +++ b/Paper/ElementBuilder.cs @@ -1322,8 +1322,8 @@ private TextInputState LoadTextInputState(string initialValue, bool isMultiLine) { var defaultState = new TextInputState { - Value = initialValue ?? "", - CursorPosition = (initialValue ?? "").Length, + Value = initialValue, + CursorPosition = initialValue.Length, SelectionStart = -1, SelectionEnd = -1, ScrollOffsetX = 0.0, @@ -1333,6 +1333,7 @@ private TextInputState LoadTextInputState(string initialValue, bool isMultiLine) }; var state = _paper.GetElementStorage(_handle, "TextInputState", defaultState); + state.Value = initialValue; state.IsFocused = _paper.IsElementFocused(_handle.Data.ID); state.IsMultiLine = isMultiLine; // Ensure consistency state.ClampValues(); From 3264ef821157593cda591827e9352d11d125d025 Mon Sep 17 00:00:00 2001 From: William Chen Date: Mon, 3 Nov 2025 14:55:42 -0500 Subject: [PATCH 2/2] Change text field onChange parameter to be required --- Paper/ElementBuilder.cs | 12 ++++++------ Samples/Shared/PaperDemo.Components.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Paper/ElementBuilder.cs b/Paper/ElementBuilder.cs index ff880d2..9ddd8f1 100644 --- a/Paper/ElementBuilder.cs +++ b/Paper/ElementBuilder.cs @@ -1709,7 +1709,7 @@ private bool ProcessKeyCommand(ref TextInputState state, PaperKey key, TextInput public ElementBuilder TextField( string value, TextInputSettings settings, - Action onChange = null, + Action onChange, [System.Runtime.CompilerServices.CallerLineNumber] int intID = 0) { return CreateTextInput(value, settings, onChange, false, intID); @@ -1730,7 +1730,7 @@ public ElementBuilder TextField( public ElementBuilder TextField( string value, FontFile font, - Action onChange = null, + Action onChange, Color? textColor = null, string placeholder = "", Color? placeholderColor = null, @@ -1756,7 +1756,7 @@ public ElementBuilder TextField( public ElementBuilder TextArea( string value, TextInputSettings settings, - Action onChange = null, + Action onChange, [System.Runtime.CompilerServices.CallerLineNumber] int intID = 0) { return CreateTextInput(value, settings, onChange, true, intID); @@ -1777,7 +1777,7 @@ public ElementBuilder TextArea( public ElementBuilder TextArea( string value, FontFile font, - Action onChange = null, + Action onChange, string placeholder = "", Color? textColor = null, Color? placeholderColor = null, @@ -1959,7 +1959,7 @@ private ElementBuilder CreateTextInput( SaveTextInputState(currentState); if (valueChanged) - onChange?.Invoke(currentState.Value); + onChange.Invoke(currentState.Value); }); // Handle character input @@ -1983,7 +1983,7 @@ private ElementBuilder CreateTextInput( EnsureCursorVisible(ref currentState, settings, isMultiLine); SaveTextInputState(currentState); - onChange?.Invoke(currentState.Value); + onChange.Invoke(currentState.Value); }); // Render cursor and selection diff --git a/Samples/Shared/PaperDemo.Components.cs b/Samples/Shared/PaperDemo.Components.cs index 03845db..f00ca37 100644 --- a/Samples/Shared/PaperDemo.Components.cs +++ b/Samples/Shared/PaperDemo.Components.cs @@ -158,7 +158,7 @@ public static void DefineStyles() .Register(); } - public static ElementBuilder Secondary(string stringID, string value, Action onChange = null, string placeholder = "", int intID = 0, [CallerLineNumber] int lineID = 0) + public static ElementBuilder Secondary(string stringID, string value, Action onChange, string placeholder = "", int intID = 0, [CallerLineNumber] int lineID = 0) { ElementBuilder parent = PaperDemo.Gui.Box(stringID, intID, lineID).Style("shadcs-text-field-secondary").TabIndex(0); using (parent.Enter())