Răsfoiți Sursa

Fixes the restriction of the AutoSize = true of only being true if both the width and the height are true.

BDisp 4 ani în urmă
părinte
comite
ee75af620d
3 a modificat fișierele cu 67 adăugiri și 5 ștergeri
  1. 8 5
      Terminal.Gui/Core/View.cs
  2. 9 0
      Terminal.Gui/Views/Label.cs
  3. 50 0
      UnitTests/ViewTests.cs

+ 8 - 5
Terminal.Gui/Core/View.cs

@@ -2098,17 +2098,20 @@ namespace Terminal.Gui {
 
 		bool SetWidthHeight (Rect nBounds)
 		{
-			bool aSize;
+			bool aSize = false;
 			var canSizeW = SetWidth (nBounds.Width, out int rW);
 			var canSizeH = SetHeight (nBounds.Height, out int rH);
-			if (canSizeW && canSizeH) {
+			if (canSizeW) {
 				aSize = true;
-				Bounds = nBounds;
 				width = rW;
+			}
+			if (canSizeH) {
+				aSize = true;
 				height = rH;
+			}
+			if (aSize) {
+				Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height);
 				textFormatter.Size = Bounds.Size;
-			} else {
-				aSize = false;
 			}
 
 			return aSize;

+ 9 - 0
Terminal.Gui/Views/Label.cs

@@ -23,6 +23,7 @@ namespace Terminal.Gui {
 		/// <inheritdoc/>
 		public Label ()
 		{
+			Initialize ();
 		}
 
 		/// <inheritdoc/>
@@ -33,6 +34,7 @@ namespace Terminal.Gui {
 		/// <inheritdoc/>
 		public Label (ustring text) : base (text)
 		{
+			Initialize ();
 		}
 
 		/// <inheritdoc/>
@@ -43,12 +45,19 @@ namespace Terminal.Gui {
 		/// <inheritdoc/>
 		public Label (int x, int y, ustring text) : base (x, y, text)
 		{
+			Initialize ();
 		}
 
 		/// <inheritdoc/>
 		public Label (ustring text, TextDirection direction)
 			: base (text, direction)
 		{
+			Initialize ();
+		}
+
+		void Initialize ()
+		{
+			AutoSize = true;
 		}
 
 		/// <summary>

+ 50 - 0
UnitTests/ViewTests.cs

@@ -1282,5 +1282,55 @@ namespace Terminal.Gui.Views {
 			Assert.False (v.GetCurrentHeight (out cHeight));
 			Assert.Equal (19, cHeight);
 		}
+
+		[Fact]
+		public void AutoSize_False_ResizeView_Is_Always_False ()
+		{
+			var label = new Label () { AutoSize = false };
+
+			label.Text = "New text";
+
+			Assert.False (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
+		}
+
+		[Fact]
+		public void AutoSize_True_ResizeView_With_Dim_Absolute ()
+		{
+			var label = new Label ();
+
+			label.Text = "New text";
+
+			Assert.True (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
+		}
+
+		[Fact]
+		public void AutoSize_True_ResizeView_With_Dim_Fill ()
+		{
+			var win = new Window (new Rect (0, 0, 30, 80), "");
+			var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
+			win.Add (label);
+
+			label.Text = "New text\nNew line";
+			win.LayoutSubviews ();
+
+			Assert.True (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
+		}
+
+		[Fact]
+		public void AutoSize_True_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute ()
+		{
+			var win = new Window (new Rect (0, 0, 30, 80), "");
+			var label = new Label () { Width = Dim.Fill () };
+			win.Add (label);
+
+			label.Text = "New text\nNew line";
+			win.LayoutSubviews ();
+
+			Assert.True (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
+		}
 	}
 }