Răsfoiți Sursa

Fixes #1797. AutoSize is true by default on CheckBox.

BDisp 3 ani în urmă
părinte
comite
0f759517e5
2 a modificat fișierele cu 70 adăugiri și 2 ștergeri
  1. 16 2
      Terminal.Gui/Views/Checkbox.cs
  2. 54 0
      UnitTests/CheckboxTests.cs

+ 16 - 2
Terminal.Gui/Views/Checkbox.cs

@@ -78,8 +78,8 @@ namespace Terminal.Gui {
 			Checked = is_checked;
 			Checked = is_checked;
 			Text = s;
 			Text = s;
 			CanFocus = true;
 			CanFocus = true;
-			Height = 1;
-			Width = s.RuneCount + 4;
+			AutoSize = true;
+			Update ();
 
 
 			// Things this view knows how to do
 			// Things this view knows how to do
 			AddCommand (Command.ToggleChecked, () => ToggleChecked ());
 			AddCommand (Command.ToggleChecked, () => ToggleChecked ());
@@ -89,6 +89,17 @@ namespace Terminal.Gui {
 			AddKeyBinding (Key.Space, Command.ToggleChecked);
 			AddKeyBinding (Key.Space, Command.ToggleChecked);
 		}
 		}
 
 
+		private void Update ()
+		{
+			TextFormatter.Text = text;
+			var h = 1;
+			var w = text.ConsoleWidth + 4;
+			TextFormatter.Size = new Size (w, h);
+			Height = h;
+			TextFormatter.Size = new Size (w, h);
+			Width = w;
+		}
+
 		/// <summary>
 		/// <summary>
 		///    The state of the <see cref="CheckBox"/>
 		///    The state of the <see cref="CheckBox"/>
 		/// </summary>
 		/// </summary>
@@ -119,6 +130,9 @@ namespace Terminal.Gui {
 					}
 					}
 					i++;
 					i++;
 				}
 				}
+				if (AutoSize) {
+					Update ();
+				}
 			}
 			}
 		}
 		}
 
 

+ 54 - 0
UnitTests/CheckboxTests.cs

@@ -4,9 +4,17 @@ using System.Linq;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using Xunit;
 using Xunit;
+using Xunit.Abstractions;
 
 
 namespace Terminal.Gui.Views {
 namespace Terminal.Gui.Views {
 	public class CheckboxTests {
 	public class CheckboxTests {
+		readonly ITestOutputHelper output;
+
+		public CheckboxTests (ITestOutputHelper output)
+		{
+			this.output = output;
+		}
+
 		[Fact]
 		[Fact]
 		public void Constructors_Defaults ()
 		public void Constructors_Defaults ()
 		{
 		{
@@ -59,5 +67,51 @@ namespace Terminal.Gui.Views {
 			Assert.True (ckb.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
 			Assert.True (ckb.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
 			Assert.True (isChecked);
 			Assert.True (isChecked);
 		}
 		}
+
+
+		[Fact, AutoInitShutdown]
+		public void AutoSize_StaysVisible ()
+		{
+			var checkBox = new CheckBox () {
+				X = 1,
+				Y = Pos.Center (),
+				Text = "Check this out 你"
+			};
+			var win = new Window () {
+				Width = Dim.Fill (),
+				Height = Dim.Fill (),
+				Title = "Test Demo 你"
+			};
+			win.Add (checkBox);
+			Application.Top.Add (win);
+
+			Assert.False (checkBox.IsInitialized);
+
+			var runstate = Application.Begin (Application.Top);
+			((FakeDriver)Application.Driver).SetBufferSize (30, 5);
+
+			Assert.True (checkBox.IsInitialized);
+			Assert.Equal ("Check this out 你", checkBox.Text);
+
+			var expected = @"
+┌ Test Demo 你 ──────────────┐
+│                            │
+│ ╴ Check this out 你        │
+│                            │
+└────────────────────────────┘
+";
+
+			// Positive test
+			var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 30, 5), pos);
+
+			// Negative test
+			checkBox.AutoSize = true;
+			bool first = false;
+			Application.RunMainLoopIteration (ref runstate, true, ref first);
+
+			GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 30, 5), pos);
+		}
 	}
 	}
 }
 }