From ffea15f66be339c1e8b070279b380c8aac312570 Mon Sep 17 00:00:00 2001 From: maciekwin3 Date: Thu, 31 Aug 2023 10:44:51 +0200 Subject: [PATCH 1/2] Add SearchText property to combobox --- Terminal.Gui/Views/ComboBox.cs | 34 ++++++++++++++++++++++---------- UnitTests/Views/ComboBoxTests.cs | 4 ++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index f9c53e868..7cb6befba 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -39,10 +39,7 @@ namespace Terminal.Gui { private void Initialize (ComboBox container, bool hideDropdownListOnClick) { - if (container == null) - throw new ArgumentNullException ("ComboBox container cannot be null.", nameof (container)); - - this.container = container; + this.container = container ?? throw new ArgumentNullException (nameof (container), "ComboBox container cannot be null."); HideDropdownListOnClick = hideDropdownListOnClick; } @@ -236,7 +233,7 @@ namespace Terminal.Gui { readonly TextField search; readonly ComboListView listview; bool autoHide = true; - int minimumHeight = 2; + readonly int minimumHeight = 2; /// /// Public constructor @@ -296,7 +293,7 @@ namespace Terminal.Gui { listview.Y = Pos.Bottom (search); listview.OpenSelectedItem += (object sender, ListViewItemEventArgs a) => Selected (); - this.Add (search, listview); + Add (search, listview); // On resize LayoutComplete += (object sender, LayoutEventArgs a) => { @@ -723,7 +720,19 @@ namespace Terminal.Gui { return text; } set { - search.Text = text = value; + SetSearchText (value); + } + } + + /// + /// Current search text + /// + public string SearchText { + get { + return search.Text; + } + set { + SetSearchText (value); } } @@ -779,7 +788,7 @@ namespace Terminal.Gui { private void Reset (bool keepSearchText = false) { if (!keepSearchText) { - search.Text = text = ""; + SetSearchText (string.Empty); } ResetSearchSet (); @@ -791,6 +800,10 @@ namespace Terminal.Gui { search.SetFocus (); } } + private void SetSearchText (string value) + { + search.Text = text = value; + } private void ResetSearchSet (bool noCopy = false) { @@ -847,7 +860,7 @@ namespace Terminal.Gui { listview.SetSource (searchset); listview.Clear (); // Ensure list shrinks in Dialog as you type listview.Height = CalculatetHeight (); - this.SuperView?.BringSubviewToFront (this); + SuperView?.BringSubviewToFront (this); } /// @@ -861,7 +874,8 @@ namespace Terminal.Gui { OnOpenSelectedItem (); } var rect = listview.ViewToScreen (listview.Bounds); - Reset (SelectedItem > -1); + Reset (keepSearchText: true); + listview.Clear (rect); listview.Clear (rect); listview.TabStop = false; SuperView?.SendSubviewToBack (this); diff --git a/UnitTests/Views/ComboBoxTests.cs b/UnitTests/Views/ComboBoxTests.cs index 30bf7fd86..a7b2951a9 100644 --- a/UnitTests/Views/ComboBoxTests.cs +++ b/UnitTests/Views/ComboBoxTests.cs @@ -101,7 +101,7 @@ namespace Terminal.Gui.ViewsTests { cb.Text = "Tw"; Assert.True (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()))); Assert.True (opened); - Assert.Equal ("", cb.Text); + Assert.Equal ("Tw", cb.Text); Assert.False (cb.IsShow); cb.SetSource (null); Assert.False (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()))); @@ -262,7 +262,7 @@ Three Assert.False (cb.IsShow); Assert.Equal (2, cb.Source.Count); Assert.Equal (-1, cb.SelectedItem); - Assert.Equal ("", cb.Text); + Assert.Equal ("T", cb.Text); Assert.True (cb.ProcessKey (new KeyEvent (Key.Esc, new KeyModifiers ()))); Assert.False (cb.IsShow); Assert.Equal (-1, cb.SelectedItem); // retains last accept selected item From 8a5d076c578d6386798cf7b16e1b1c5abd46d7b4 Mon Sep 17 00:00:00 2001 From: maciekwin3 Date: Thu, 31 Aug 2023 10:50:41 +0200 Subject: [PATCH 2/2] Fix line duplicate --- Terminal.Gui/Views/ComboBox.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index 7cb6befba..211a11d52 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -876,7 +876,6 @@ namespace Terminal.Gui { var rect = listview.ViewToScreen (listview.Bounds); Reset (keepSearchText: true); listview.Clear (rect); - listview.Clear (rect); listview.TabStop = false; SuperView?.SendSubviewToBack (this); SuperView?.SetNeedsDisplay (rect);