浏览代码

Fixes some AutoSize issues with rules.

BDisp 3 年之前
父节点
当前提交
3160b4c914

+ 98 - 22
Terminal.Gui/Core/View.cs

@@ -522,7 +522,7 @@ namespace Terminal.Gui {
 				if (x is Pos.PosAbsolute) {
 					frame = new Rect (x.Anchor (0), frame.Y, frame.Width, frame.Height);
 				}
-				TextFormatter.Size = frame.Size;
+				TextFormatter.Size = GetBoundsSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -546,7 +546,7 @@ namespace Terminal.Gui {
 				if (y is Pos.PosAbsolute) {
 					frame = new Rect (frame.X, y.Anchor (0), frame.Width, frame.Height);
 				}
-				TextFormatter.Size = frame.Size;
+				TextFormatter.Size = GetBoundsSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -568,9 +568,11 @@ namespace Terminal.Gui {
 				}
 
 				width = value;
-				if (autoSize && value.Anchor (0) != TextFormatter.Size.Width
-					- (TextFormatter.IsHorizontalDirection (TextDirection)
-					&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0)) {
+
+				var isValidNewAutSize = autoSize ? IsValidAutoSizeWidth (width) : false;
+
+				if (autoSize && !isValidNewAutSize) {
+					TextFormatter.AutoSize = false;
 					autoSize = false;
 				}
 				SetMinWidthHeight ();
@@ -578,7 +580,7 @@ namespace Terminal.Gui {
 				if (width is Dim.DimAbsolute) {
 					frame = new Rect (frame.X, frame.Y, width.Anchor (0), frame.Height);
 				}
-				TextFormatter.Size = frame.Size;
+				TextFormatter.Size = GetBoundsSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -596,9 +598,11 @@ namespace Terminal.Gui {
 				}
 
 				height = value;
-				if (autoSize && value.Anchor (0) != TextFormatter.Size.Height
-					- (TextFormatter.IsVerticalDirection (TextDirection)
-					&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0)) {
+
+				var isValidNewAutSize = autoSize ? IsValidAutoSizeHeight (height) : false;
+
+				if (autoSize && !isValidNewAutSize) {
+					TextFormatter.AutoSize = false;
 					autoSize = false;
 				}
 				SetMinWidthHeight ();
@@ -606,7 +610,7 @@ namespace Terminal.Gui {
 				if (height is Dim.DimAbsolute) {
 					frame = new Rect (frame.X, frame.Y, frame.Width, height.Anchor (0));
 				}
-				TextFormatter.Size = frame.Size;
+				TextFormatter.Size = GetBoundsSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -626,17 +630,21 @@ namespace Terminal.Gui {
 
 		void SetMinWidthHeight ()
 		{
-			if (IsInitialized && !AutoSize && !ustring.IsNullOrEmpty (TextFormatter.Text)) {
+			if (!AutoSize && !ustring.IsNullOrEmpty (TextFormatter.Text)) {
 				switch (TextFormatter.IsVerticalDirection (TextDirection)) {
 				case true:
 					var colWidth = TextFormatter.GetSumMaxCharWidth (TextFormatter.Text, 0, 1);
 					if (Width == null || (Width is Dim.DimAbsolute && Width.Anchor (0) < colWidth)) {
 						width = colWidth;
+						Bounds = new Rect (Bounds.X, Bounds.Y, colWidth, Bounds.Height);
+						TextFormatter.Size = GetBoundsSize ();
 					}
 					break;
 				default:
 					if (Height == null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0)) {
 						height = 1;
+						Bounds = new Rect (Bounds.X, Bounds.Y, Bounds.Width, 1);
+						TextFormatter.Size = GetBoundsSize ();
 					}
 					break;
 				}
@@ -2202,7 +2210,7 @@ namespace Terminal.Gui {
 			Rect oldBounds = Bounds;
 			OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
 
-			TextFormatter.Size = Bounds.Size;
+			TextFormatter.Size = GetBoundsSize ();
 
 
 			// Sort out the dependencies of the X, Y, Width, Height properties
@@ -2308,10 +2316,11 @@ namespace Terminal.Gui {
 				TextFormatter.Text = value;
 				var prevSize = frame.Size;
 				var canResize = ResizeView (autoSize);
-				if (canResize && TextFormatter.Size != Bounds.Size) {
-					Bounds = new Rect (new Point (Bounds.X, Bounds.Y), TextFormatter.Size);
-				} else if (!canResize && TextFormatter.Size != Bounds.Size) {
-					TextFormatter.Size = Bounds.Size;
+				var txtFmtSize = GetTextFormatterSize ();
+				if (canResize && txtFmtSize != Bounds.Size) {
+					Bounds = new Rect (new Point (Bounds.X, Bounds.Y), txtFmtSize);
+				} else if (!canResize && txtFmtSize != Bounds.Size) {
+					TextFormatter.Size = GetBoundsSize ();
 				}
 				SetMinWidthHeight ();
 				SetNeedsLayout ();
@@ -2372,14 +2381,23 @@ namespace Terminal.Gui {
 			get => TextFormatter.Direction;
 			set {
 				if (TextFormatter.Direction != value) {
+					var isValidOldAutSize = autoSize ? IsValidAutoSize (out Size autSize) : false;
+					var directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction)
+						!= TextFormatter.IsHorizontalDirection (value);
+
 					TextFormatter.Direction = value;
-					if (AutoSize) {
+
+					if ((IsInitialized && AutoSize) || (directionChanged && AutoSize && isValidOldAutSize)) {
 						ResizeView (true);
+					} else if (directionChanged && AutoSize && !isValidOldAutSize) {
+						TextFormatter.AutoSize = false;
+						autoSize = false;
 					} else if (IsInitialized) {
 						var b = new Rect (Bounds.X, Bounds.Y, Bounds.Height, Bounds.Width);
 						SetWidthHeight (b);
 					}
-					TextFormatter.Size = Bounds.Size;
+
+					TextFormatter.Size = GetBoundsSize ();
 					SetNeedsDisplay ();
 				}
 			}
@@ -2396,6 +2414,10 @@ namespace Terminal.Gui {
 			set {
 				isInitialized = value;
 				SetMinWidthHeight ();
+				if (autoSize && !IsValidAutoSize (out _)) {
+					TextFormatter.AutoSize = false;
+					autoSize = false;
+				}
 			}
 		}
 
@@ -2476,7 +2498,8 @@ namespace Terminal.Gui {
 			if (TextFormatter.Size != nBounds.Size) {
 				TextFormatter.Size = nBounds.Size;
 			}
-			if ((TextFormatter.Size != Bounds.Size || TextFormatter.Size != nBounds.Size)
+			var fmtSize = GetTextFormatterSize ();
+			if ((fmtSize != Bounds.Size || fmtSize != nBounds.Size)
 				&& (((width == null || width is Dim.DimAbsolute) && (Bounds.Width == 0
 				|| autoSize && Bounds.Width != nBounds.Width))
 				|| ((height == null || height is Dim.DimAbsolute) && (Bounds.Height == 0
@@ -2489,8 +2512,10 @@ namespace Terminal.Gui {
 		bool SetWidthHeight (Rect nBounds)
 		{
 			bool aSize = false;
-			var canSizeW = SetWidth (nBounds.Width, out int rW);
-			var canSizeH = SetHeight (nBounds.Height, out int rH);
+			var canSizeW = SetWidth (nBounds.Width - (TextFormatter.IsHorizontalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0), out int rW);
+			var canSizeH = SetHeight (nBounds.Height - (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0), out int rH);
 			if (canSizeW) {
 				aSize = true;
 				width = rW;
@@ -2501,12 +2526,63 @@ namespace Terminal.Gui {
 			}
 			if (aSize) {
 				Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height);
-				TextFormatter.Size = Bounds.Size;
+				TextFormatter.Size = GetBoundsSize ();
 			}
 
 			return aSize;
 		}
 
+		bool IsValidAutoSize (out Size autoSize)
+		{
+			var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
+			autoSize = new Size (rect.Size.Width - (TextFormatter.IsHorizontalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0),
+				rect.Size.Height - (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0));
+			return !(!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)
+				|| frame.Size.Width != rect.Size.Width - (TextFormatter.IsHorizontalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0)
+				|| frame.Size.Height != rect.Size.Height - (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0));
+		}
+
+		bool IsValidAutoSizeWidth (Dim width)
+		{
+			var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
+			var dimValue = width.Anchor (0);
+			return !(!(width is Dim.DimAbsolute) || dimValue != rect.Size.Width
+				- (TextFormatter.IsHorizontalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0));
+		}
+
+		bool IsValidAutoSizeHeight (Dim height)
+		{
+			var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
+			var dimValue = height.Anchor (0);
+			return !(!(height is Dim.DimAbsolute) || dimValue != rect.Size.Height
+				- (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0));
+		}
+
+		Size GetTextFormatterSize ()
+		{
+			return new Size (TextFormatter.Size.Width - (TextFormatter.IsHorizontalDirection (TextDirection)
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0),
+				TextFormatter.Size.Height - (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0));
+		}
+
+		Size GetBoundsSize ()
+		{
+			if (TextFormatter.Text == null)
+				return Bounds.Size;
+
+			return new Size (frame.Size.Width + (TextFormatter.IsHorizontalDirection (TextDirection)
+					&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0),
+				frame.Size.Height + (TextFormatter.IsVerticalDirection (TextDirection)
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0));
+		}
+
 		/// <summary>
 		/// Specifies the event arguments for <see cref="MouseEvent"/>
 		/// </summary>

+ 1 - 1
Terminal.Gui/Core/Window.cs

@@ -320,7 +320,7 @@ namespace Terminal.Gui {
 		///   The text displayed by the <see cref="Label"/>.
 		/// </summary>
 		public override ustring Text {
-			get => contentView.Text;
+			get => contentView?.Text;
 			set {
 				base.Text = value;
 				if (contentView != null) {

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

@@ -241,7 +241,7 @@ namespace Terminal.Gui {
 		///   The text displayed by the <see cref="Label"/>.
 		/// </summary>
 		public override ustring Text {
-			get => contentView.Text;
+			get => contentView?.Text;
 			set {
 				base.Text = value;
 				if (contentView != null) {

+ 347 - 25
UnitTests/DimTests.cs

@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Globalization;
@@ -6,15 +6,20 @@ using System.IO;
 using System.Linq;
 using System.Threading;
 using Terminal.Gui;
+using Terminal.Gui.Views;
 using Xunit;
+using Xunit.Abstractions;
 
 // Alias Console to MockConsole so we don't accidentally use Console
 using Console = Terminal.Gui.FakeConsole;
 
 namespace Terminal.Gui.Core {
 	public class DimTests {
-		public DimTests ()
+		readonly ITestOutputHelper output;
+
+		public DimTests (ITestOutputHelper output)
 		{
+			this.output = output;
 			Console.OutputEncoding = System.Text.Encoding.Default;
 			// Change current culture
 			CultureInfo culture = CultureInfo.CreateSpecificCulture ("en-US");
@@ -641,6 +646,303 @@ namespace Terminal.Gui.Core {
 			Application.Shutdown ();
 		}
 
+		private string [] expecteds = new string [21] {
+@"
+┌────────────────────┐
+│View                │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+│Label 15            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+│Label 15            │
+│Label 16            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+│Label 15            │
+│Label 16            │
+│Label 17            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+│Label 15            │
+│Label 16            │
+│Label 17            │
+│Label 18            │
+└────────────────────┘",
+@"
+┌────────────────────┐
+│View                │
+│Label 0             │
+│Label 1             │
+│Label 2             │
+│Label 3             │
+│Label 4             │
+│Label 5             │
+│Label 6             │
+│Label 7             │
+│Label 8             │
+│Label 9             │
+│Label 10            │
+│Label 11            │
+│Label 12            │
+│Label 13            │
+│Label 14            │
+│Label 15            │
+│Label 16            │
+│Label 17            │
+│Label 18            │
+│Label 19            │
+└────────────────────┘"
+};
+
 		[Fact]
 		public void Dim_Add_Operator_With_Text ()
 		{
@@ -652,25 +954,36 @@ namespace Terminal.Gui.Core {
 			var view = new View ("View") { X = 0, Y = 0, Width = 20, Height = 0 };
 			var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
 			var count = 0;
+			var listLabels = new List<Label> ();
 
 			field.KeyDown += (k) => {
 				if (k.KeyEvent.Key == Key.Enter) {
-					field.Text = $"Label {count}";
-					var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
-					view.Add (label);
-					Assert.Equal ($"Label {count}", label.Text);
-					Assert.Equal ($"Pos.Absolute({count + 1})", label.Y.ToString ());
-
-					Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
-					view.Height += 1;
-					count++;
+					((FakeDriver)Application.Driver).SetBufferSize (22, count + 3);
+					var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expecteds [count], output);
+					Assert.Equal (new Rect (0, 0, 22, count + 3), pos);
+
+					if (count < 20) {
+						field.Text = $"Label {count}";
+						var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
+						view.Add (label);
+						Assert.Equal ($"Label {count}", label.Text);
+						Assert.Equal ($"Pos.Absolute({count + 1})", label.Y.ToString ());
+						listLabels.Add (label);
+						Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
+						view.Height += 1;
+						count++;
+					}
 					Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
 				}
 			};
 
 			Application.Iteration += () => {
-				while (count < 20) {
+				while (count < 21) {
 					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+					if (count == 20) {
+						field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+						break;
+					}
 				}
 
 				Application.RequestStop ();
@@ -685,6 +998,7 @@ namespace Terminal.Gui.Core {
 			Application.Run (top);
 
 			Assert.Equal (20, count);
+			Assert.Equal (count, listLabels.Count);
 
 			// Shutdown must be called to safely clean up Application if Init has been called
 			Application.Shutdown ();
@@ -768,32 +1082,39 @@ namespace Terminal.Gui.Core {
 				var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
 				view.Add (label);
 				Assert.Equal ($"Label {i}", label.Text);
-				Assert.Equal ($"Pos.Absolute({i})", label.Y.ToString ());
+				Assert.Equal ($"Pos.Absolute({i + 1})", label.Y.ToString ());
 				listLabels.Add (label);
 
-				Assert.Equal ($"Dim.Absolute({i})", view.Height.ToString ());
-				view.Height += 1;
 				Assert.Equal ($"Dim.Absolute({i + 1})", view.Height.ToString ());
+				view.Height += 1;
+				Assert.Equal ($"Dim.Absolute({i + 2})", view.Height.ToString ());
 			}
 
 			field.KeyDown += (k) => {
 				if (k.KeyEvent.Key == Key.Enter) {
-					Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
-					view.Remove (listLabels [count - 1]);
-
-					Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
-					view.Height -= 1;
-					count--;
-					if (count == 0)
+					((FakeDriver)Application.Driver).SetBufferSize (22, count + 3);
+					var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expecteds [count], output);
+					Assert.Equal (new Rect (0, 0, 22, count + 3), pos);
+
+					if (count > 0) {
+						Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
+						view.Remove (listLabels [count - 1]);
+						listLabels.RemoveAt (count - 1);
 						Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
-					else
-						Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
+						view.Height -= 1;
+						count--;
+					}
+					Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
 				}
 			};
 
 			Application.Iteration += () => {
-				while (count > 0) {
+				while (count > -1) {
 					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+					if (count == 0) {
+						field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+						break;
+					}
 				}
 
 				Application.RequestStop ();
@@ -808,6 +1129,7 @@ namespace Terminal.Gui.Core {
 			Application.Run (top);
 
 			Assert.Equal (0, count);
+			Assert.Equal (count, listLabels.Count);
 
 			// Shutdown must be called to safely clean up Application if Init has been called
 			Application.Shutdown ();

+ 2 - 1
UnitTests/PanelViewTests.cs

@@ -345,7 +345,7 @@ namespace Terminal.Gui.Views {
 		{
 			var top = Application.Top;
 			var win = new Window ();
-			var label = new Label ("Hello World") {
+			var label = new Label () {
 				Width = 24,
 				Height = 13,
 				ColorScheme = Colors.TopLevel,
@@ -370,6 +370,7 @@ namespace Terminal.Gui.Views {
 
 			Application.Begin (top);
 
+			Assert.False (label.AutoSize);
 			Assert.Equal (new Rect (0, 0, 24, 13), label.Frame);
 			Assert.Equal (new Rect (0, 0, 34, 23), pv.Frame);
 			Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);

+ 3 - 0
UnitTests/TextFormatterTests.cs

@@ -2965,6 +2965,7 @@ e
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
 
+			// All AutoSize are false because the Frame.Width != TextFormatter.Size.Width
 			Assert.False (lblLeft.AutoSize);
 			Assert.False (lblCenter.AutoSize);
 			Assert.False (lblRight.AutoSize);
@@ -3004,6 +3005,7 @@ e
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
 
+			// All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
 			Assert.False (lblLeft.AutoSize);
 			Assert.False (lblCenter.AutoSize);
 			Assert.False (lblRight.AutoSize);
@@ -3098,6 +3100,7 @@ e
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
 
+			// All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
 			Assert.False (lblLeft.AutoSize);
 			Assert.False (lblCenter.AutoSize);
 			Assert.False (lblRight.AutoSize);

+ 292 - 4
UnitTests/ViewTests.cs

@@ -1288,6 +1288,56 @@ namespace Terminal.Gui.Core {
 			Assert.Equal (19, cHeight);
 		}
 
+		[Fact]
+		public void AutoSize_False_If_Text_Emmpty ()
+		{
+			var view1 = new View ();
+			var view2 = new View ("");
+			var view3 = new View () { Text = "" };
+
+			Assert.False (view1.AutoSize);
+			Assert.False (view2.AutoSize);
+			Assert.False (view3.AutoSize);
+		}
+
+		[Fact]
+		public void AutoSize_False_If_Text_Is_Not_Emmpty ()
+		{
+			var view1 = new View ();
+			view1.Text = "Hello World";
+			var view2 = new View ("Hello World");
+			var view3 = new View () { Text = "Hello World" };
+
+			Assert.False (view1.AutoSize);
+			Assert.False (view2.AutoSize);
+			Assert.False (view3.AutoSize);
+		}
+
+		[Fact]
+		public void AutoSize_True_Label_If_Text_Emmpty ()
+		{
+			var label1 = new Label ();
+			var label2 = new Label ("");
+			var label3 = new Label () { Text = "" };
+
+			Assert.True (label1.AutoSize);
+			Assert.True (label2.AutoSize);
+			Assert.True (label3.AutoSize);
+		}
+
+		[Fact]
+		public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
+		{
+			var label1 = new Label ();
+			label1.Text = "Hello World";
+			var label2 = new Label ("Hello World");
+			var label3 = new Label () { Text = "Hello World" };
+
+			Assert.True (label1.AutoSize);
+			Assert.True (label2.AutoSize);
+			Assert.True (label3.AutoSize);
+		}
+
 		[Fact]
 		public void AutoSize_False_ResizeView_Is_Always_False ()
 		{
@@ -1296,7 +1346,7 @@ namespace Terminal.Gui.Core {
 			label.Text = "New text";
 
 			Assert.False (label.AutoSize);
-			Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
+			Assert.Equal ("{X=0,Y=0,Width=0,Height=1}", label.Bounds.ToString ());
 		}
 
 		[Fact]
@@ -1311,7 +1361,7 @@ namespace Terminal.Gui.Core {
 		}
 
 		[Fact]
-		public void AutoSize_True_ResizeView_With_Dim_Fill ()
+		public void AutoSize_False_ResizeView_With_Dim_Fill ()
 		{
 			var win = new Window (new Rect (0, 0, 30, 80), "");
 			var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
@@ -1320,22 +1370,60 @@ namespace Terminal.Gui.Core {
 			label.Text = "New text\nNew line";
 			win.LayoutSubviews ();
 
-			Assert.True (label.AutoSize);
+			Assert.False (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 ()
+		public void AutoSize_False_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);
 
+			// Text is empty so height=0
+			Assert.False (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
+
+			// Here the SetMinWidthHeight ensuring the minimum height
 			label.Text = "New text\nNew line";
 			win.LayoutSubviews ();
 
+			Assert.False (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
+		}
+
+		[Fact, AutoInitShutdown]
+		public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
+		{
+			var win = new Window (new Rect (0, 0, 30, 80), "");
+			var label = new Label () { Width = Dim.Fill () };
+			win.Add (label);
+			Application.Top.Add (win);
+			Application.Begin (Application.Top);
+
+			Assert.False (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
+
+			// Here the SetMinWidthHeight ensuring the minimum height
+			label.Text = "New text\nNew line";
+			Application.Refresh ();
+
+			Assert.False (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
+
+			label.AutoSize = true;
+			Application.Refresh ();
+
+			// Here the AutoSize ensuring the right height
 			Assert.True (label.AutoSize);
 			Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
+
+			label.Text = "New changed text\nNew changed line\nNew line";
+			Application.Refresh ();
+
+			Assert.True (label.AutoSize);
+			Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
 		}
 
 		[Fact, AutoInitShutdown]
@@ -2439,6 +2527,206 @@ Y
 			Assert.Equal (new Rect (0, 0, 22, 22), pos);
 		}
 
+		[Fact, AutoInitShutdown]
+		public void TextDirection_Toggle ()
+		{
+			var view = new View ();
+			var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
+			win.Add (view);
+			Application.Top.Add (win);
+
+			Application.Begin (Application.Top);
+			((FakeDriver)Application.Driver).SetBufferSize (22, 22);
+
+			Assert.False (view.AutoSize);
+			Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
+			Assert.Equal (Rect.Empty, view.Frame);
+			var expected = @"
+┌────────────────────┐
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+
+			view.AutoSize = true;
+			view.Text = "Hello World";
+			Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
+			Application.Refresh ();
+			expected = @"
+┌────────────────────┐
+│Hello World         │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+
+			view.TextDirection = TextDirection.TopBottom_LeftRight;
+			Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
+			Application.Refresh ();
+			expected = @"
+┌────────────────────┐
+│H                   │
+│e                   │
+│l                   │
+│l                   │
+│o                   │
+│                    │
+│W                   │
+│o                   │
+│r                   │
+│l                   │
+│d                   │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+
+			view.AutoSize = false;
+			view.Text = "Hello Worlds";
+			Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
+			Application.Refresh ();
+			expected = @"
+┌────────────────────┐
+│H                   │
+│e                   │
+│l                   │
+│l                   │
+│o                   │
+│                    │
+│W                   │
+│o                   │
+│r                   │
+│l                   │
+│d                   │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+
+			view.TextDirection = TextDirection.LeftRight_TopBottom;
+			Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
+			Application.Refresh ();
+			expected = @"
+┌────────────────────┐
+│Hello World         │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+
+			view.AutoSize = true;
+			Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
+			Application.Refresh ();
+			expected = @"
+┌────────────────────┐
+│Hello Worlds        │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+│                    │
+└────────────────────┘
+";
+
+			pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 22, 22), pos);
+		}
+
 		[Fact, AutoInitShutdown]
 		public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
 		{