Browse Source

Merge branch 'v2_develop' into v2_view2_experiment

Tigger Kindel 2 years ago
parent
commit
24155ac43b

+ 7 - 17
Terminal.Gui/Views/ListView.cs

@@ -244,7 +244,7 @@ namespace Terminal.Gui {
 				if (source == null || source.Count == 0) {
 					return;
 				}
-				if (value < 0 || value >= source.Count) {
+				if (value < -1 || value >= source.Count) {
 					throw new ArgumentException ("value");
 				}
 				selected = value;
@@ -684,9 +684,8 @@ namespace Terminal.Gui {
 			if (selected != lastSelectedItem) {
 				var value = source?.Count > 0 ? source.ToList () [selected] : null;
 				SelectedItemChanged?.Invoke (this, new ListViewItemEventArgs (selected, value));
-				if (HasFocus) {
-					lastSelectedItem = selected;
-				}
+				lastSelectedItem = selected;
+				EnsureSelectedItemVisible ();
 				return true;
 			}
 
@@ -722,25 +721,16 @@ namespace Terminal.Gui {
 		///<inheritdoc/>
 		public override bool OnEnter (View view)
 		{
-			Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
-
-			if (lastSelectedItem == -1) {
+			if (IsInitialized) {
+				Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
+			}
+			if (lastSelectedItem != selected) {
 				EnsureSelectedItemVisible ();
 			}
 
 			return base.OnEnter (view);
 		}
 
-		///<inheritdoc/>
-		public override bool OnLeave (View view)
-		{
-			if (lastSelectedItem > -1) {
-				lastSelectedItem = -1;
-			}
-
-			return base.OnLeave (view);
-		}
-
 		/// <summary>
 		/// Ensures the selected item is always visible on the screen.
 		/// </summary>

+ 8 - 4
UnitTests/UICatalog/ScenarioTests.cs

@@ -64,7 +64,7 @@ namespace UICatalog.Tests {
 
 				// Press QuitKey 
 				Assert.Empty (FakeConsole.MockKeyPresses);
-				// BUGBUG: For some reason ReadKey is not returning the QuitKey for some Scenarios
+				// BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
 				// by adding this Space it seems to work.
 				FakeConsole.PushMockKeyPress (Key.Space);
 				FakeConsole.PushMockKeyPress (Application.QuitKey);
@@ -72,7 +72,10 @@ namespace UICatalog.Tests {
 				// The only key we care about is the QuitKey
 				Application.Top.KeyPress += (object sender, KeyEventEventArgs args) => {
 					output.WriteLine ($"  Keypress: {args.KeyEvent.Key}");
-					Assert.Equal (Application.QuitKey, args.KeyEvent.Key);
+					// BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
+					// by adding this Space it seems to work.
+					// See #2474 for why this is commented out
+					//Assert.Equal (Application.QuitKey, args.KeyEvent.Key);
 					args.Handled = false;
 				};
 
@@ -125,9 +128,9 @@ namespace UICatalog.Tests {
 			var generic = scenarios [item];
 
 			Application.Init (new FakeDriver ());
-			// BUGBUG: For some reason ReadKey is not
-			// returning the QuitKey for some Scenarios
+			// BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
 			// by adding this Space it seems to work.
+
 			FakeConsole.PushMockKeyPress (Key.Space);
 			FakeConsole.PushMockKeyPress (Application.QuitKey);
 
@@ -153,6 +156,7 @@ namespace UICatalog.Tests {
 			var token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
 
 			Application.Top.KeyPress += (object sender, KeyEventEventArgs args) => {
+				// See #2474 for why this is commented out
 				Assert.Equal (Key.CtrlMask | Key.Q, args.KeyEvent.Key);
 			};
 

+ 21 - 9
UnitTests/Views/ListViewTests.cs

@@ -511,23 +511,35 @@ Item 2
 Item 3
 Item 4", output);
 
+			// EnsureSelectedItemVisible is auto enabled on the OnSelectedChanged
 			lv.SelectedItem = 6;
 			Application.Refresh ();
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
-Item 0
-Item 1
-Item 2
-Item 3
-Item 4", output);
-
-			lv.EnsureSelectedItemVisible ();
-			Application.Refresh ();
-			TestHelpers.AssertDriverContentsWithFrameAre (@"
 Item 2
 Item 3
 Item 4
 Item 5
 Item 6", output);
 		}
+
+		[Fact]
+		public void SelectedItem_Get_Set ()
+		{
+			var lv = new ListView (new List<string> { "One", "Two", "Three" });
+			Assert.Equal (-1, lv.SelectedItem);
+			Assert.Throws<ArgumentException> (() => lv.SelectedItem = 3);
+			var exception = Record.Exception (() => lv.SelectedItem = -1);
+			Assert.Null (exception);
+		}
+
+		[Fact]
+		public void OnEnter_Does_Not_Throw_Exception ()
+		{
+			var lv = new ListView ();
+			var top = new View ();
+			top.Add (lv);
+			var exception = Record.Exception (lv.SetFocus);
+			Assert.Null (exception);
+		}
 	}
 }