浏览代码

Fixed Horizontal FormatAndGetSize Fill issue; Vertical is still broken

Tig 1 年之前
父节点
当前提交
e566dce521
共有 2 个文件被更改,包括 19 次插入4 次删除
  1. 14 1
      Terminal.Gui/Text/TextFormatter.cs
  2. 5 3
      UnitTests/View/TextTests.cs

+ 14 - 1
Terminal.Gui/Text/TextFormatter.cs

@@ -1,4 +1,5 @@
 using System.Diagnostics;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
 
 namespace Terminal.Gui;
 
@@ -659,9 +660,21 @@ public class TextFormatter
             return Size.Empty;
         }
 
+        // HACK: This is a total hack to work around the fact that TextFormatter.Format does not match TextFormatter.Draw.
         Size prevSize = Size;
         Size = constrainSize.Value;
+
+        // HACK: Fill normally will fill the entire constraint size, but we need to know the actual size of the text.
+        Alignment prevAlignment = Alignment;
+        if (Alignment == Alignment.Fill)
+        {
+            Alignment = Alignment.Start;
+        }
+
         List<string> lines = GetLines ();
+
+        // Undo hacks
+        Alignment = prevAlignment;
         Size = prevSize;
 
         if (lines.Count == 0)
@@ -679,7 +692,7 @@ public class TextFormatter
         }
         else
         {
-            width = lines.Max (static line => line.GetColumns());
+            width = lines.Max (static line => line.GetColumns ());
             height = lines.Count;
         }
 

+ 5 - 3
UnitTests/View/TextTests.cs

@@ -810,13 +810,15 @@ w ";
         Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
 
+        // frame.Width is width + border wide (20 + 2) and 6 high
+
         if (autoSize)
         {
             Size expectedSize = new (11, 1);
             Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
             Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
             Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
-           // Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
+            Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
         }
         else
         {
@@ -824,7 +826,7 @@ w ";
             Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
             Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
             Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
-            //Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
+            Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
         }
 
         Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
@@ -943,7 +945,7 @@ w ";
             Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
             Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
             Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
-           // Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
+            Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
             Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
         }
         else