Skip to content

Commit

Permalink
Added TextChangedEventArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Mar 12, 2023
1 parent 6a33f14 commit fe2710a
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 36 deletions.
36 changes: 36 additions & 0 deletions Terminal.Gui/Core/EventArgs/TextChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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 {

/// <summary>
/// Event args for events where text is changed
/// </summary>
public class TextChangedEventArgs : EventArgs {

/// <summary>
/// Creates a new instance of the <see cref="TextChangedEventArgs"/> class
/// </summary>
/// <param name="oldValue"></param>
/// <param name="newValue"></param>
public TextChangedEventArgs (ustring oldValue, ustring newValue)
{
OldValue = oldValue;
NewValue = newValue;
}

/// <summary>
/// The old value before the text changed
/// </summary>
public ustring OldValue { get; }

/// <summary>
/// The new value
/// </summary>
public ustring NewValue { get; }
}
}
5 changes: 4 additions & 1 deletion Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Terminal.Gui/Views/DateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/TextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TextField : View {
/// <remarks>
/// The passed <see cref="EventArgs"/> is a <see cref="NStack.ustring"/> containing the old value.
/// </remarks>
public event Action<ustring> TextChanged;
public event EventHandler<TextChangedEventArgs> TextChanged;

/// <summary>
/// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions Terminal.Gui/Views/TimeField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Windows/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions UICatalog/Scenarios/AllViewsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/CharacterMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/CsvEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/DynamicMenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/DynamicStatusBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions UICatalog/Scenarios/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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") {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions UICatalog/Scenarios/Progress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions UICatalog/Scenarios/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/TileViewNesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void Setup ()
Text = "2",
};

textField.TextChanged += (s) => SetupTileView ();
textField.TextChanged += (s, e) => SetupTileView ();


cbHorizontal = new CheckBox ("Horizontal") {
Expand Down
8 changes: 4 additions & 4 deletions UnitTests/UICatalog/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions UnitTests/Views/TextFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fe2710a

Please sign in to comment.