2
0
Эх сурвалжийг харах

Fixes ComboBox text and list if the height is equal to one. (#1606)

* Fixes ComboBox text and list if the height is equal to one. End and Home navigate on text.

* Trying fix the all scenarios unit test.

* Removing unnecessary code.

* Avoiding the Collection was modified after the enumerator was instantiated exception.

* Also cover the IndexOfValue inside the lock.

* Disposing the DrawContent event.

* Increasing the time to test.

* Revert "Increasing the time to test."

This reverts commit b3c2ac871c251a3f78f95ae5aea463c6b87f8466.

* CharacterMap take more time to load.
BDisp 3 жил өмнө
parent
commit
1557f7a71e

+ 11 - 6
Terminal.Gui/Core/MainLoop.cs

@@ -159,10 +159,12 @@ namespace Terminal.Gui {
 		/// This method also returns <c>false</c> if the timeout is not found.
 		public bool RemoveTimeout (object token)
 		{
-			var idx = timeouts.IndexOfValue (token as Timeout);
-			if (idx == -1)
-				return false;
-			timeouts.RemoveAt (idx);
+			lock (timeouts) {
+				var idx = timeouts.IndexOfValue (token as Timeout);
+				if (idx == -1)
+					return false;
+				timeouts.RemoveAt (idx);
+			}
 			return true;
 		}
 
@@ -177,8 +179,11 @@ namespace Terminal.Gui {
 				if (k < now) {
 					if (timeout.Callback (this))
 						AddTimeout (timeout.Span, timeout);
-				} else
-					timeouts.Add (k, timeout);
+				} else {
+					lock (timeouts) {
+						timeouts.Add (k, timeout);
+					}
+				}
 			}
 		}
 

+ 10 - 8
Terminal.Gui/Views/ComboBox.cs

@@ -76,12 +76,8 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Public constructor
 		/// </summary>
-		public ComboBox () : base ()
+		public ComboBox () : this (string.Empty)
 		{
-			search = new TextField ("");
-			listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true, TabStop = false };
-
-			Initialize ();
 		}
 
 		/// <summary>
@@ -244,7 +240,7 @@ namespace Terminal.Gui {
 		public override bool MouseEvent (MouseEvent me)
 		{
 			if (me.X == Bounds.Right - 1 && me.Y == Bounds.Top && me.Flags == MouseFlags.Button1Pressed
-			&& autoHide) {
+				&& autoHide) {
 
 				if (isShow) {
 					isShow = false;
@@ -372,16 +368,22 @@ namespace Terminal.Gui {
 			return true;
 		}
 
-		bool MoveEnd ()
+		bool? MoveEnd ()
 		{
+			if (!isShow && search.HasFocus) {
+				return null;
+			}
 			if (HasItems ()) {
 				listview.MoveEnd ();
 			}
 			return true;
 		}
 
-		bool MoveHome ()
+		bool? MoveHome ()
 		{
+			if (!isShow && search.HasFocus) {
+				return null;
+			}
 			if (HasItems ()) {
 				listview.MoveHome ();
 			}

+ 1 - 1
Terminal.Gui/Views/ListView.cs

@@ -719,7 +719,7 @@ namespace Terminal.Gui {
 			if (selected < top) {
 				top = selected;
 			} else if (Frame.Height > 0 && selected >= top + Frame.Height) {
-				top = Math.Max (selected - Frame.Height + 2, 0);
+				top = Math.Max (selected - Frame.Height + 1, 0);
 			}
 		}
 

+ 3 - 2
Terminal.Gui/Views/TextField.cs

@@ -406,8 +406,9 @@ namespace Terminal.Gui {
 			int offB = OffSetBackground ();
 			if (point < first) {
 				first = point;
-			} else if (first + point - (Frame.Width + offB) == 0 ||
-				  TextModel.DisplaySize (text, first, point).size >= Frame.Width + offB) {
+			} else if (Frame.Width > 0 && (first + point - (Frame.Width + offB) == 0 ||
+				  TextModel.DisplaySize (text, first, point).size >= Frame.Width + offB)) {
+
 				first = Math.Max (TextModel.CalculateLeftColumn (text, first,
 					point, Frame.Width + offB), 0);
 			}

+ 10 - 1
UICatalog/Scenarios/CharacterMap.cs

@@ -102,7 +102,7 @@ namespace UICatalog.Scenarios {
 		public const int H_SPACE = 2;
 		public const int V_SPACE = 2;
 
-		public static int MaxCodePointVal => 0xE0FFF;
+		public static int MaxCodePointVal => 0x10FFFF;
 
 		// Row Header + space + (space + char + space)
 		public static int RowHeaderWidth => $"U+{MaxCodePointVal:x5}".Length;
@@ -145,6 +145,9 @@ namespace UICatalog.Scenarios {
 			}
 			for (int row = 0, y = 0; row < viewport.Height / 2 - 1; row++, y+= V_SPACE) {
 				int val = (-viewport.Y + row) * 16;
+				if (val >= 0x00D800 && val <= 0x00DFFF) {
+					continue;
+				}
 				if (val < MaxCodePointVal) {
 					var rowLabel = $"U+{val / 16:x4}x";
 					Move (0, y + 1);
@@ -183,5 +186,11 @@ namespace UICatalog.Scenarios {
 			}
 			return base.ProcessKey (kb);
 		}
+
+		protected override void Dispose (bool disposing)
+		{
+			DrawContent -= CharMap_DrawContent;
+			base.Dispose (disposing);
+		}
 	}
 }

+ 16 - 0
UnitTests/ComboBoxTests.cs

@@ -114,6 +114,22 @@ namespace Terminal.Gui.Views {
 			Assert.True (cb.IsShow);
 			Assert.Equal (0, cb.SelectedItem);
 			Assert.Equal ("One", cb.Text);
+			Assert.True (cb.ProcessKey (new KeyEvent (Key.F4, new KeyModifiers ())));
+			Assert.False (cb.IsShow);
+			Assert.Equal (0, cb.SelectedItem);
+			Assert.Equal ("One", cb.Text);
+			Assert.True (cb.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
+			Assert.False (cb.IsShow);
+			Assert.Equal (0, cb.SelectedItem);
+			Assert.Equal ("One", cb.Text);
+			Assert.True (cb.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
+			Assert.False (cb.IsShow);
+			Assert.Equal (0, cb.SelectedItem);
+			Assert.Equal ("One", cb.Text);
+			Assert.True (cb.ProcessKey (new KeyEvent (Key.F4, new KeyModifiers ())));
+			Assert.True (cb.IsShow);
+			Assert.Equal (0, cb.SelectedItem);
+			Assert.Equal ("One", cb.Text);
 			Assert.True (cb.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
 			Assert.True (cb.IsShow);
 			Assert.Equal (2, cb.SelectedItem);

+ 32 - 3
UnitTests/ListViewTests.cs

@@ -18,7 +18,7 @@ namespace Terminal.Gui.Views {
 			lv = new ListView (new List<string> () { "One", "Two", "Three" });
 			Assert.NotNull (lv.Source);
 
-			lv = new ListView (new NewListDataSource());
+			lv = new ListView (new NewListDataSource ());
 			Assert.NotNull (lv.Source);
 
 			lv = new ListView (new Rect (0, 1, 10, 20), new List<string> () { "One", "Two", "Three" });
@@ -95,11 +95,40 @@ namespace Terminal.Gui.Views {
 			lv.RowRender += _ => rendered = true;
 			Application.Top.Add (lv);
 			Application.Begin (Application.Top);
-			Assert.False(rendered);
+			Assert.False (rendered);
 
 			lv.SetSource (source);
 			lv.Redraw (lv.Bounds);
-			Assert.True(rendered);
+			Assert.True (rendered);
+		}
+
+		[Fact]
+		[AutoInitShutdown]
+		public void EnsuresVisibilitySelectedItem_Top ()
+		{
+			var source = new List<string> () { "First", "Second" };
+			ListView lv = new ListView (source) { Width = Dim.Fill (), Height = 1 };
+			lv.SelectedItem = 1;
+			Application.Top.Add (lv);
+			Application.Begin (Application.Top);
+
+			Assert.Equal ("Second ", GetContents (0));
+			Assert.Equal (new (' ', 7), GetContents (1));
+
+			lv.MoveUp ();
+			lv.Redraw (lv.Bounds);
+
+			Assert.Equal ("First  ", GetContents (0));
+			Assert.Equal (new (' ', 7), GetContents (1));
+
+			string GetContents (int line)
+			{
+				var item = "";
+				for (int i = 0; i < 7; i++) {
+					item += (char)Application.Driver.Contents [line, i, 0];
+				}
+				return item;
+			}
 		}
 	}
 }

+ 6 - 1
UnitTests/ScenarioTests.cs

@@ -66,7 +66,12 @@ namespace Terminal.Gui {
 					}
 				};
 
-				var ms = 1000;
+				int ms;
+				if (scenarioClass.Name == "CharacterMap") {
+					ms = 1500;
+				}else {
+					ms = 1000;
+				}
 				var abortCount = 0;
 				Func<MainLoop, bool> abortCallback = (MainLoop loop) => {
 					abortCount++;

+ 22 - 0
UnitTests/TextFieldTests.cs

@@ -1093,5 +1093,27 @@ namespace Terminal.Gui.Views {
 			Assert.True (tf.ProcessKey (new KeyEvent (Key.Backspace | Key.CtrlMask, new KeyModifiers ())));
 			Assert.Equal ("to jump between text ", tf.Text);
 		}
+
+		[Fact]
+		[AutoInitShutdown]
+		public void Adjust_First ()
+		{
+			TextField tf = new TextField ();
+			tf.Width = Dim.Fill ();
+			tf.Text = "This is a test.";
+			Application.Top.Add (tf);
+			Application.Begin (Application.Top);
+
+			Assert.Equal ("This is a test. ", GetContents ());
+
+			string GetContents ()
+			{
+				var item = "";
+				for (int i = 0; i < 16; i++) {
+					item += (char)Application.Driver.Contents [0, i, 0];
+				}
+				return item;
+			}
+		}
 	}
 }