Tig Kindel 1 year ago
parent
commit
660a467007

+ 37 - 29
Terminal.Gui/Views/ComboBox.cs

@@ -10,7 +10,7 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Text;
 using System.Text;
 
 
-namespace Terminal.Gui; 
+namespace Terminal.Gui;
 
 
 /// <summary>
 /// <summary>
 /// Provides a drop-down list of items the user can select from.
 /// Provides a drop-down list of items the user can select from.
@@ -32,6 +32,7 @@ public class ComboBox : View {
 		{
 		{
 			_container = container ?? throw new ArgumentNullException (nameof (container), "ComboBox container cannot be null.");
 			_container = container ?? throw new ArgumentNullException (nameof (container), "ComboBox container cannot be null.");
 			HideDropdownListOnClick = hideDropdownListOnClick;
 			HideDropdownListOnClick = hideDropdownListOnClick;
+			AddCommand (Command.LineUp, () => _container.MoveUpList ());
 		}
 		}
 
 
 		public bool HideDropdownListOnClick {
 		public bool HideDropdownListOnClick {
@@ -589,18 +590,23 @@ public class ComboBox : View {
 
 
 	bool? MoveUp ()
 	bool? MoveUp ()
 	{
 	{
-		if (_search.HasFocus) {
-			// stop odd behavior on KeyUp when search has focus
-			return true;
+		if (HasItems ()) {
+			_listview.MoveUp ();
 		}
 		}
+		return true;
+	}
 
 
+	bool? MoveUpList ()
+	{
 		if (_listview.HasFocus && _listview.SelectedItem == 0 && _searchset?.Count > 0) // jump back to search
 		if (_listview.HasFocus && _listview.SelectedItem == 0 && _searchset?.Count > 0) // jump back to search
 		{
 		{
 			_search.CursorPosition = _search.Text.GetRuneCount ();
 			_search.CursorPosition = _search.Text.GetRuneCount ();
 			_search.SetFocus ();
 			_search.SetFocus ();
-			return true;
+		} else {
+			MoveUp ();
 		}
 		}
-		return null;
+
+		return true;
 	}
 	}
 
 
 	bool? MoveDown ()
 	bool? MoveDown ()
@@ -612,6 +618,8 @@ public class ComboBox : View {
 				_listview.SetFocus ();
 				_listview.SetFocus ();
 				if (_listview.SelectedItem > -1) {
 				if (_listview.SelectedItem > -1) {
 					SetValue (_searchset [_listview.SelectedItem]);
 					SetValue (_searchset [_listview.SelectedItem]);
+				} else {
+					_listview.SelectedItem = 0;
 				}
 				}
 			} else {
 			} else {
 				_listview.TabStop = false;
 				_listview.TabStop = false;
@@ -731,18 +739,18 @@ public class ComboBox : View {
 		_isShow = false;
 		_isShow = false;
 	}
 	}
 
 
-		private int GetSelectedItemFromSource (string searchText)
-		{
-			if (_source is null) {
-				return -1;
-			}
-			for (int i = 0; i < _searchset.Count; i++) {
-				if (_searchset [i].ToString () == searchText) {
-					return i;
-				}
-			}
+	private int GetSelectedItemFromSource (string searchText)
+	{
+		if (_source is null) {
 			return -1;
 			return -1;
 		}
 		}
+		for (int i = 0; i < _searchset.Count; i++) {
+			if (_searchset [i].ToString () == searchText) {
+				return i;
+			}
+		}
+		return -1;
+	}
 
 
 	/// <summary>
 	/// <summary>
 	/// Reset to full original list
 	/// Reset to full original list
@@ -784,20 +792,20 @@ public class ComboBox : View {
 		}
 		}
 	}
 	}
 
 
-		private void Search_Changed (object sender, TextChangedEventArgs e)
-		{
-			if (_source is null) { // Object initialization		
-				return;
-			}
+	private void Search_Changed (object sender, TextChangedEventArgs e)
+	{
+		if (_source is null) { // Object initialization		
+			return;
+		}
 
 
-			if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (e.OldValue)) {
-				ResetSearchSet ();
-			} else if (_search.Text != e.OldValue) {
-				if (_search.Text.Length < e.OldValue.Length) {
-					_selectedItem = -1;
-				}
-				_isShow = true;
-				ResetSearchSet (noCopy: true);
+		if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (e.OldValue)) {
+			ResetSearchSet ();
+		} else if (_search.Text != e.OldValue) {
+			if (_search.Text.Length < e.OldValue.Length) {
+				_selectedItem = -1;
+			}
+			_isShow = true;
+			ResetSearchSet (noCopy: true);
 
 
 			foreach (object item in _source.ToList ()) {
 			foreach (object item in _source.ToList ()) {
 				// Iterate to preserver object type and force deep copy
 				// Iterate to preserver object type and force deep copy

+ 4 - 4
Terminal.Gui/Views/TextView.cs

@@ -3180,15 +3180,15 @@ public class TextView : View {
 	/// <param name="toAdd">Text to add</param>
 	/// <param name="toAdd">Text to add</param>
 	public void InsertText (string toAdd)
 	public void InsertText (string toAdd)
 	{
 	{
-		foreach (var ch in toAdd) {
-			KeyCode key;
+		foreach (char ch in toAdd) {
+			Key key;
 			try {
 			try {
-				key = (KeyCode)ch;
+				key = new Key(ch);
 			} catch (Exception) {
 			} catch (Exception) {
 				throw new ArgumentException ($"Cannot insert character '{ch}' because it does not map to a Key");
 				throw new ArgumentException ($"Cannot insert character '{ch}' because it does not map to a Key");
 			}
 			}
 
 
-			InsertText (new Key (key));
+			InsertText (key);
 
 
 			if (NeedsDisplay) {
 			if (NeedsDisplay) {
 				Adjust ();
 				Adjust ();

+ 4 - 0
UICatalog/Properties/launchSettings.json

@@ -65,6 +65,10 @@
     "MenuBarScenario": {
     "MenuBarScenario": {
       "commandName": "Project",
       "commandName": "Project",
       "commandLineArgs": "MenuBar"
       "commandLineArgs": "MenuBar"
+    },
+    "ListView & ComboBox": {
+      "commandName": "Project",
+      "commandLineArgs": "\"ListView & ComboBox\""
     }
     }
   }
   }
 }
 }

+ 9 - 0
UnitTests/Input/ResponderTests.cs

@@ -139,6 +139,9 @@ public class ResponderTests {
 	[Fact]
 	[Fact]
 	public void Responder_Not_Notifying_Dispose ()
 	public void Responder_Not_Notifying_Dispose ()
 	{
 	{
+		// Only clear before because need to test after assert
+		Responder.Instances.Clear ();
+
 		var container1 = new View () { Id = "Container1" };
 		var container1 = new View () { Id = "Container1" };
 
 
 		var view = new View () { Id = "View" };
 		var view = new View () { Id = "View" };
@@ -175,6 +178,9 @@ public class ResponderTests {
 	[Fact]
 	[Fact]
 	public void Disposing_Event_Notify_All_Subscribers_On_The_Second_Container ()
 	public void Disposing_Event_Notify_All_Subscribers_On_The_Second_Container ()
 	{
 	{
+		// Only clear before because need to test after assert
+		Responder.Instances.Clear ();
+
 		var container1 = new View () { Id = "Container1" };
 		var container1 = new View () { Id = "Container1" };
 
 
 		var view = new View () { Id = "View" };
 		var view = new View () { Id = "View" };
@@ -212,6 +218,9 @@ public class ResponderTests {
 	[Fact]
 	[Fact]
 	public void Disposing_Event_Notify_All_Subscribers_On_The_First_Container ()
 	public void Disposing_Event_Notify_All_Subscribers_On_The_First_Container ()
 	{
 	{
+		// Only clear before because need to test after assert
+		Responder.Instances.Clear ();
+
 		var container1 = new View () { Id = "Container1" };
 		var container1 = new View () { Id = "Container1" };
 		var count = 0;
 		var count = 0;
 
 

+ 1 - 1
UnitTests/Text/AutocompleteTests.cs

@@ -153,7 +153,7 @@ namespace Terminal.Gui.TextTests {
 			Assert.Equal (3, g.AllSuggestions.Count);
 			Assert.Equal (3, g.AllSuggestions.Count);
 			Assert.True (tv.NewKeyDownEvent (new (tv.Autocomplete.SelectionKey)));
 			Assert.True (tv.NewKeyDownEvent (new (tv.Autocomplete.SelectionKey)));
 			tv.PositionCursor ();
 			tv.PositionCursor ();
-			Assert.Equal ($"fortunately Fortunately super feature.", tv.Text);
+			Assert.Equal ($"Fortunately Fortunately super feature.", tv.Text);
 			Assert.Equal (new Point (11, 0), tv.CursorPosition);
 			Assert.Equal (new Point (11, 0), tv.CursorPosition);
 			Assert.Empty (tv.Autocomplete.Suggestions);
 			Assert.Empty (tv.Autocomplete.Suggestions);
 			Assert.Equal (3, g.AllSuggestions.Count);
 			Assert.Equal (3, g.AllSuggestions.Count);