Ver Fonte

Fixes #2269. ListView.EnsuresVisibilitySelectedItem must be public.

BDisp há 2 anos atrás
pai
commit
b44df1c609
2 ficheiros alterados com 68 adições e 3 exclusões
  1. 6 3
      Terminal.Gui/Views/ListView.cs
  2. 62 0
      UnitTests/ListViewTests.cs

+ 6 - 3
Terminal.Gui/Views/ListView.cs

@@ -743,7 +743,10 @@ namespace Terminal.Gui {
 			return base.OnLeave (view);
 			return base.OnLeave (view);
 		}
 		}
 
 
-		void EnsuresVisibilitySelectedItem ()
+		/// <summary>
+		/// Ensures the selected item is always visible on the screen.
+		/// </summary>
+		public void EnsuresVisibilitySelectedItem ()
 		{
 		{
 			SuperView?.LayoutSubviews ();
 			SuperView?.LayoutSubviews ();
 			if (selected < top) {
 			if (selected < top) {
@@ -840,7 +843,7 @@ namespace Terminal.Gui {
 			if (src == null || src?.Count == 0) {
 			if (src == null || src?.Count == 0) {
 				return 0;
 				return 0;
 			}
 			}
-			
+
 			int maxLength = 0;
 			int maxLength = 0;
 			for (int i = 0; i < src.Count; i++) {
 			for (int i = 0; i < src.Count; i++) {
 				var t = src [i];
 				var t = src [i];
@@ -924,7 +927,7 @@ namespace Terminal.Gui {
 						return i;
 						return i;
 					}
 					}
 				} else if (t is string s) {
 				} else if (t is string s) {
-					if (s.ToUpperInvariant ().StartsWith (search.ToUpperInvariant ())) {
+					if (s.StartsWith (search, StringComparison.InvariantCultureIgnoreCase)) {
 						return i;
 						return i;
 					}
 					}
 				}
 				}

+ 62 - 0
UnitTests/ListViewTests.cs

@@ -451,5 +451,67 @@ namespace Terminal.Gui.Views {
 			lv.SetSourceAsync (null);
 			lv.SetSourceAsync (null);
 			Assert.NotNull (lv.Source);
 			Assert.NotNull (lv.Source);
 		}
 		}
+
+		[Fact]
+		public void ListWrapper_StartsWith ()
+		{
+			var lw = new ListWrapper (new List<string> { "One", "Two", "Three" });
+
+			Assert.Equal (1, lw.StartsWith ("t"));
+			Assert.Equal (1, lw.StartsWith ("tw"));
+			Assert.Equal (2, lw.StartsWith ("th"));
+			Assert.Equal (1, lw.StartsWith ("T"));
+			Assert.Equal (1, lw.StartsWith ("TW"));
+			Assert.Equal (2, lw.StartsWith ("TH"));
+
+			lw = new ListWrapper (new List<NStack.ustring> { "One", "Two", "Three" });
+
+			Assert.Equal (1, lw.StartsWith ("t"));
+			Assert.Equal (1, lw.StartsWith ("tw"));
+			Assert.Equal (2, lw.StartsWith ("th"));
+			Assert.Equal (1, lw.StartsWith ("T"));
+			Assert.Equal (1, lw.StartsWith ("TW"));
+			Assert.Equal (2, lw.StartsWith ("TH"));
+		}
+
+		[Fact, AutoInitShutdown]
+		public void EnsuresVisibilitySelectedItem_SelectedItem ()
+		{
+			var source = new List<string> ();
+			for (int i = 0; i < 10; i++) {
+				source.Add ($"Item {i}");
+			}
+			var lv = new ListView (source) {
+				Width = 10,
+				Height = 5
+			};
+			Application.Top.Add (lv);
+			Application.Begin (Application.Top);
+
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+Item 0
+Item 1
+Item 2
+Item 3
+Item 4", output);
+
+			lv.SelectedItem = 6;
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+Item 0
+Item 1
+Item 2
+Item 3
+Item 4", output);
+
+			lv.EnsuresVisibilitySelectedItem ();
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+Item 2
+Item 3
+Item 4
+Item 5
+Item 6", output);
+		}
 	}
 	}
 }
 }