From fe2710ad2d8338f1a28228274a3eed95f8970f73 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 12 Mar 2023 09:08:58 +0000 Subject: [PATCH] Added TextChangedEventArgs --- .../Core/EventArgs/TextChangedEventArgs.cs | 36 +++++++++++++++++++ Terminal.Gui/Views/ComboBox.cs | 5 ++- Terminal.Gui/Views/DateField.cs | 8 ++--- Terminal.Gui/Views/TextField.cs | 4 +-- Terminal.Gui/Views/TimeField.cs | 8 ++--- Terminal.Gui/Windows/FileDialog.cs | 4 +-- UICatalog/Scenarios/AllViewsTester.cs | 8 ++--- UICatalog/Scenarios/CharacterMap.cs | 2 +- UICatalog/Scenarios/CsvEditor.cs | 2 +- UICatalog/Scenarios/DynamicMenuBar.cs | 2 +- UICatalog/Scenarios/DynamicStatusBar.cs | 2 +- UICatalog/Scenarios/Editor.cs | 6 ++-- UICatalog/Scenarios/Progress.cs | 4 +-- UICatalog/Scenarios/Text.cs | 4 +-- UICatalog/Scenarios/TileViewNesting.cs | 2 +- UnitTests/UICatalog/ScenarioTests.cs | 8 ++--- UnitTests/Views/TextFieldTests.cs | 7 ++-- 17 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 Terminal.Gui/Core/EventArgs/TextChangedEventArgs.cs diff --git a/Terminal.Gui/Core/EventArgs/TextChangedEventArgs.cs b/Terminal.Gui/Core/EventArgs/TextChangedEventArgs.cs new file mode 100644 index 0000000000..778448b158 --- /dev/null +++ b/Terminal.Gui/Core/EventArgs/TextChangedEventArgs.cs @@ -0,0 +1,36 @@ +using NStack; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Terminal.Gui { + + /// + /// Event args for events where text is changed + /// + public class TextChangedEventArgs : EventArgs { + + /// + /// Creates a new instance of the class + /// + /// + /// + public TextChangedEventArgs (ustring oldValue, ustring newValue) + { + OldValue = oldValue; + NewValue = newValue; + } + + /// + /// The old value before the text changed + /// + public ustring OldValue { get; } + + /// + /// The new value + /// + public ustring NewValue { get; } + } +} diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index b204d33b80..2ae0729860 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -805,8 +805,11 @@ private void SetSearchSet () searchset.Add (item); } } - private void Search_Changed (ustring text) + { + Search_Changed (this, new TextChangedEventArgs (text, text)); + } + private void Search_Changed (object sender, TextChangedEventArgs args) { if (source == null) { // Object initialization return; diff --git a/Terminal.Gui/Views/DateField.cs b/Terminal.Gui/Views/DateField.cs index d88648a41a..cf3375356d 100644 --- a/Terminal.Gui/Views/DateField.cs +++ b/Terminal.Gui/Views/DateField.cs @@ -106,13 +106,13 @@ void Initialize (DateTime date, bool isShort = false) AddKeyBinding (Key.F | Key.CtrlMask, Command.Right); } - void DateField_Changed (ustring e) + void DateField_Changed (object sender, TextChangedEventArgs e) { try { - if (!DateTime.TryParseExact (GetDate (Text).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) - Text = e; + if (!DateTime.TryParseExact (GetDate (e.NewValue).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) + Text = e.OldValue; } catch (Exception) { - Text = e; + Text = e.OldValue; } } diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 18b3a9a95b..4fd8233e0a 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -53,7 +53,7 @@ public class TextField : View { /// /// The passed is a containing the old value. /// - public event Action TextChanged; + public event EventHandler TextChanged; /// /// Initializes a new instance of the class using positioning. @@ -308,7 +308,7 @@ public override Rect Frame { , HistoryText.LineStatus.Replaced); } - TextChanged?.Invoke (oldText); + TextChanged?.Invoke (this, new TextChangedEventArgs(oldText, newText.NewText)); if (point > text.Count) { point = Math.Max (TextModel.DisplaySize (text, 0).size - 1, 0); diff --git a/Terminal.Gui/Views/TimeField.cs b/Terminal.Gui/Views/TimeField.cs index 132803dbc0..8e195e4273 100644 --- a/Terminal.Gui/Views/TimeField.cs +++ b/Terminal.Gui/Views/TimeField.cs @@ -106,13 +106,13 @@ void Initialize (TimeSpan time, bool isShort = false) AddKeyBinding (Key.F | Key.CtrlMask, Command.Right); } - void TextField_TextChanged (ustring e) + void TextField_TextChanged (object sender, TextChangedEventArgs e) { try { - if (!TimeSpan.TryParseExact (Text.ToString ().Trim (), format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result)) - Text = e; + if (!TimeSpan.TryParseExact (e.NewValue.ToString ().Trim (), format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result)) + Text = e.OldValue; } catch (Exception) { - Text = e; + Text = e.OldValue; } } diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index 2d23079673..b78d106bc2 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -649,7 +649,7 @@ public FileDialog (ustring title, ustring prompt, ustring nameDirLabel, ustring Y = 1 + msgLines, Width = Dim.Fill () - 1, }; - dirEntry.TextChanged += (e) => { + dirEntry.TextChanged += (s,e) => { DirectoryPath = dirEntry.Text; nameEntry.Text = ustring.Empty; }; @@ -731,7 +731,7 @@ public FileDialog (ustring title, ustring prompt, ustring nameDirLabel, ustring }; AddButton (this.prompt); - nameEntry.TextChanged += (e) => { + nameEntry.TextChanged += (s, e) => { if (nameEntry.Text.IsEmpty) { this.prompt.Enabled = false; } else { diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index 2c52dc7d5b..733c8690c9 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -126,7 +126,7 @@ public override void Setup () }; _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView); _xText = new TextField ($"{_xVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 }; - _xText.TextChanged += (args) => { + _xText.TextChanged += (s,args) => { try { _xVal = int.Parse (_xText.Text.ToString ()); DimPosChanged (_curView); @@ -142,7 +142,7 @@ public override void Setup () label = new Label ("y:") { X = Pos.Right (_xRadioGroup) + 1, Y = 0 }; _locationFrame.Add (label); _yText = new TextField ($"{_yVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 }; - _yText.TextChanged += (args) => { + _yText.TextChanged += (s, args) => { try { _yVal = int.Parse (_yText.Text.ToString ()); DimPosChanged (_curView); @@ -174,7 +174,7 @@ public override void Setup () }; _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView); _wText = new TextField ($"{_wVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 }; - _wText.TextChanged += (args) => { + _wText.TextChanged += (s,args) => { try { switch (_wRadioGroup.SelectedItem) { case 0: @@ -197,7 +197,7 @@ public override void Setup () label = new Label ("height:") { X = Pos.Right (_wRadioGroup) + 1, Y = 0 }; _sizeFrame.Add (label); _hText = new TextField ($"{_hVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 }; - _hText.TextChanged += (args) => { + _hText.TextChanged += (s, args) => { try { switch (_hRadioGroup.SelectedItem) { case 0: diff --git a/UICatalog/Scenarios/CharacterMap.cs b/UICatalog/Scenarios/CharacterMap.cs index 6728d09e43..40551691c9 100644 --- a/UICatalog/Scenarios/CharacterMap.cs +++ b/UICatalog/Scenarios/CharacterMap.cs @@ -39,7 +39,7 @@ public override void Setup () Win.Add (jumpEdit); var unicodeLabel = new Label ("") { X = Pos.Right (jumpEdit) + 1, Y = Pos.Y (_charMap) }; Win.Add (unicodeLabel); - jumpEdit.TextChanged += (s) => { + jumpEdit.TextChanged += (s,e) => { uint result = 0; if (jumpEdit.Text.Length == 0) return; try { diff --git a/UICatalog/Scenarios/CsvEditor.cs b/UICatalog/Scenarios/CsvEditor.cs index 04d49b6f2c..62e550efdd 100644 --- a/UICatalog/Scenarios/CsvEditor.cs +++ b/UICatalog/Scenarios/CsvEditor.cs @@ -99,7 +99,7 @@ public override void Setup () SetupScrollBar (); } - private void SelectedCellLabel_TextChanged (ustring last) + private void SelectedCellLabel_TextChanged (object sender, TextChangedEventArgs args) { // if user is in the text control and editing the selected cell if (!selectedCellLabel.HasFocus) diff --git a/UICatalog/Scenarios/DynamicMenuBar.cs b/UICatalog/Scenarios/DynamicMenuBar.cs index 542c6897a1..e1cfbca413 100644 --- a/UICatalog/Scenarios/DynamicMenuBar.cs +++ b/UICatalog/Scenarios/DynamicMenuBar.cs @@ -89,7 +89,7 @@ public DynamicMenuBarSample (ustring title) : base (title) X = Pos.Center (), Width = 2, }; - _txtDelimiter.TextChanged += (_) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text; + _txtDelimiter.TextChanged += (s,e) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text; _frmDelimiter.Add (_txtDelimiter); Add (_frmDelimiter); diff --git a/UICatalog/Scenarios/DynamicStatusBar.cs b/UICatalog/Scenarios/DynamicStatusBar.cs index d78327a63e..082d4e007d 100644 --- a/UICatalog/Scenarios/DynamicStatusBar.cs +++ b/UICatalog/Scenarios/DynamicStatusBar.cs @@ -77,7 +77,7 @@ public DynamicStatusBarSample (ustring title) : base (title) X = Pos.Center (), Width = 2, }; - _txtDelimiter.TextChanged += (_) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text; + _txtDelimiter.TextChanged += (s,e) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text; _frmDelimiter.Add (_txtDelimiter); Add (_frmDelimiter); diff --git a/UICatalog/Scenarios/Editor.cs b/UICatalog/Scenarios/Editor.cs index 60029e0491..482d407b59 100644 --- a/UICatalog/Scenarios/Editor.cs +++ b/UICatalog/Scenarios/Editor.cs @@ -813,7 +813,7 @@ private View FindTab () btnFindPrevious.Clicked += (s,e) => FindPrevious (); d.Add (btnFindPrevious); - txtToFind.TextChanged += (e) => { + txtToFind.TextChanged += (s,e) => { _textToFind = txtToFind.Text.ToString (); _textView.FindTextChanged (); btnFindNext.Enabled = !txtToFind.Text.IsEmpty; @@ -908,7 +908,7 @@ private View ReplaceTab () Y = Pos.Top (label), Width = 20 }; - txtToReplace.TextChanged += (e) => _textToReplace = txtToReplace.Text.ToString (); + txtToReplace.TextChanged += (s,e) => _textToReplace = txtToReplace.Text.ToString (); d.Add (txtToReplace); var btnFindPrevious = new Button ("Replace _Previous") { @@ -933,7 +933,7 @@ private View ReplaceTab () btnReplaceAll.Clicked += (s,e) => ReplaceAll (); d.Add (btnReplaceAll); - txtToFind.TextChanged += (e) => { + txtToFind.TextChanged += (s, e) => { _textToFind = txtToFind.Text.ToString (); _textView.FindTextChanged (); btnFindNext.Enabled = !txtToFind.Text.IsEmpty; diff --git a/UICatalog/Scenarios/Progress.cs b/UICatalog/Scenarios/Progress.cs index a6869607dc..40cbb5aa98 100644 --- a/UICatalog/Scenarios/Progress.cs +++ b/UICatalog/Scenarios/Progress.cs @@ -167,7 +167,7 @@ public override void Setup () systemTimerDemo.PulseProgressBar.Fraction = 1F; }; systemTimerDemo.Speed.Text = $"{_systemTimerTick}"; - systemTimerDemo.Speed.TextChanged += (a) => { + systemTimerDemo.Speed.TextChanged += (s, a) => { uint result; if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) { _systemTimerTick = result; @@ -210,7 +210,7 @@ public override void Setup () }; mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}"; - mainLoopTimeoutDemo.Speed.TextChanged += (a) => { + mainLoopTimeoutDemo.Speed.TextChanged += (s, a) => { uint result; if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text.ToString (), out result)) { _mainLooopTimeoutTick = result; diff --git a/UICatalog/Scenarios/Text.cs b/UICatalog/Scenarios/Text.cs index 7ed9835282..8c37b828e6 100644 --- a/UICatalog/Scenarios/Text.cs +++ b/UICatalog/Scenarios/Text.cs @@ -41,7 +41,7 @@ void TextField_TextChanging (object sender, TextChangingEventArgs e) }; Win.Add (labelMirroringTextField); - textField.TextChanged += (prev) => { + textField.TextChanged += (s, prev) => { labelMirroringTextField.Text = textField.Text; }; @@ -159,7 +159,7 @@ void TextView_DrawContent (object sender, DrawEventArgs e) }; Win.Add (labelMirroringDateField); - dateField.TextChanged += (prev) => { + dateField.TextChanged += (s, prev) => { labelMirroringDateField.Text = dateField.Text; }; diff --git a/UICatalog/Scenarios/TileViewNesting.cs b/UICatalog/Scenarios/TileViewNesting.cs index 3cee1b1954..8d7fd551cc 100644 --- a/UICatalog/Scenarios/TileViewNesting.cs +++ b/UICatalog/Scenarios/TileViewNesting.cs @@ -36,7 +36,7 @@ public override void Setup () Text = "2", }; - textField.TextChanged += (s) => SetupTileView (); + textField.TextChanged += (s, e) => SetupTileView (); cbHorizontal = new CheckBox ("Horizontal") { diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index 1104154b3d..a2f0518878 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -312,7 +312,7 @@ public void Run_All_Views_Tester_Scenario () _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView); - _xText.TextChanged += (args) => { + _xText.TextChanged += (s,args) => { try { _xVal = int.Parse (_xText.Text.ToString ()); DimPosChanged (_curView); @@ -321,7 +321,7 @@ public void Run_All_Views_Tester_Scenario () } }; - _yText.TextChanged += (args) => { + _yText.TextChanged += (s, args) => { try { _yVal = int.Parse (_yText.Text.ToString ()); DimPosChanged (_curView); @@ -334,7 +334,7 @@ public void Run_All_Views_Tester_Scenario () _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView); - _wText.TextChanged += (args) => { + _wText.TextChanged += (s, args) => { try { _wVal = int.Parse (_wText.Text.ToString ()); DimPosChanged (_curView); @@ -343,7 +343,7 @@ public void Run_All_Views_Tester_Scenario () } }; - _hText.TextChanged += (args) => { + _hText.TextChanged += (s, args) => { try { _hVal = int.Parse (_hText.Text.ToString ()); DimPosChanged (_curView); diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index 4eaa588ca8..ed68e74935 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -681,8 +681,9 @@ public void TextChanging_Event () [TextFieldTestsAutoInitShutdown] public void TextChanged_Event () { - _textField.TextChanged += (e) => { - Assert.Equal ("TAB to jump between text fields.", e); + _textField.TextChanged += (s,e) => { + Assert.Equal ("TAB to jump between text fields.", e.OldValue); + Assert.Equal ("changed", e.NewValue); }; _textField.Text = "changed"; @@ -1138,7 +1139,7 @@ public void DeleteSelectedText_InsertText_DeleteCharLeft_DeleteCharRight_Cut () var tf = new TextField () { Width = 10, Text = "-1" }; tf.TextChanging += (s,e) => newText = e.NewText.ToString (); - tf.TextChanged += (e) => oldText = e.ToString (); + tf.TextChanged += (s,e) => oldText = e.ToString (); Application.Top.Add (tf); Application.Begin (Application.Top);