Browse Source

Partial fix

Tig 1 year ago
parent
commit
a8b9ddb2aa

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

@@ -50,7 +50,7 @@ public class TextFormatter
         }
     }
 
-    private Size GetAutoSize ()
+    internal Size GetAutoSize ()
     {
         Size size = CalcRect (0, 0, Text, Direction, TabWidth).Size;
         return size with

+ 37 - 18
Terminal.Gui/View/ViewContent.cs

@@ -307,24 +307,43 @@ public partial class View
                 return new (_viewportLocation, Frame.Size);
             }
 
-            // BUGBUG: This is a hack. Viewport_get should not have side effects.
-            if (Frame.Size == Size.Empty)
-            {
-                // The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
-                // 
-                if ((Width is Dim.DimAuto widthAuto && widthAuto._style.HasFlag(Dim.DimAutoStyle.Text))
-                    || (Height is Dim.DimAuto heightAuto && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
-                {
-                    if (TextFormatter.NeedsFormat)
-                    {
-                        // This updates TextFormatter.Size to the text size
-                        TextFormatter.AutoSize = true;
-
-                        // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
-                        ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
-                    }
-                }
-            }
+            //// BUGBUG: This is a hack. Viewport_get should not have side effects.
+            //if (Frame.Size == Size.Empty)
+            //{
+            //    // The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
+            //    // 
+            //    // Use _width & _height instead of Width & Height to avoid debug spew
+            //    Dim.DimAuto widthAuto = _width as Dim.DimAuto;
+            //    Dim.DimAuto heightAuto = _height as Dim.DimAuto;
+
+            //    if ((widthAuto is { } && widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            //        || (heightAuto is { } && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
+            //    {
+            //        //if (TextFormatter.NeedsFormat)
+            //        {
+            //            // We always use TF in autosize = false mode
+            //            TextFormatter.AutoSize = false;
+
+            //            var size = TextFormatter.GetAutoSize ();
+            //            if (widthAuto is null || !widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            //            {
+            //                size.Width = _width.Anchor (0);
+            //            }
+
+            //            if (heightAuto is null || !heightAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            //            {
+            //                size.Height = _height.Anchor (0);
+            //            }
+
+            //            // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
+            //            //ContentSize = size;//TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
+            //            TextFormatter.Size = size; ;
+
+            //            // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
+            //            ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
+            //        }
+            //    }
+            //}
 
             Thickness thickness = GetAdornmentsThickness ();
             return new (

+ 21 - 5
Terminal.Gui/View/ViewText.cs

@@ -179,17 +179,33 @@ public partial class View
 
         // TODO: This is a hack. Figure out how to move this into DimDimAuto
         // Use _width & _height instead of Width & Height to avoid debug spew
-        if ((_width is Dim.DimAuto widthAuto && widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
-            || (_height is Dim.DimAuto heightAuto && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
+        Dim.DimAuto widthAuto = _width as Dim.DimAuto;
+        Dim.DimAuto heightAuto = _height as Dim.DimAuto;
+        if ((widthAuto is {} && widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            || (heightAuto is {} && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
         {
-            // This updates TextFormatter.Size to the text size
-            TextFormatter.AutoSize = true;
+            // We always use TF in autosize = false mode
+            TextFormatter.AutoSize = false;
+
+            var size = TextFormatter.GetAutoSize ();
+
+            if (widthAuto is null || !widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            {
+                size.Width = _width.Anchor (0);
+            }
+
+            if (heightAuto is null || !heightAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            {
+                size.Height = _height.Anchor (0);
+            }
 
             // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
-            ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
+            //ContentSize = size;//TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
+            TextFormatter.Size = size;
             return;
         }
 
+        // We always use TF in autosize = false mode
         TextFormatter.AutoSize = false;
         TextFormatter.Size = new Size (ContentSize.GetValueOrDefault ().Width, ContentSize.GetValueOrDefault ().Height);
     }

+ 16 - 16
UnitTests/View/Layout/Dim.AutoTests.cs

@@ -735,7 +735,7 @@ public class DimAutoTests (ITestOutputHelper output)
             Text = "_1234",
             Width = Dim.Auto ()
         };
-        Assert.True (view.TextFormatter.AutoSize);
+        Assert.False (view.TextFormatter.AutoSize);
         Assert.NotEqual (Size.Empty, view.Frame.Size);
 
         view.TextAlignment = TextAlignment.Justified;
@@ -757,7 +757,7 @@ public class DimAutoTests (ITestOutputHelper output)
             Width = Dim.Auto ()
         };
         view.HotKeySpecifier = (Rune)'*';
-        Assert.True (view.TextFormatter.AutoSize);
+        Assert.False (view.TextFormatter.AutoSize);
         Assert.NotEqual (Size.Empty, view.Frame.Size);
 
         view = new ()
@@ -766,23 +766,23 @@ public class DimAutoTests (ITestOutputHelper output)
             Width = Dim.Auto ()
         };
         view.Text = "*ABC";
-        Assert.True (view.TextFormatter.AutoSize);
+        Assert.False (view.TextFormatter.AutoSize);
         Assert.NotEqual (Size.Empty, view.Frame.Size);
     }
 
-    [Fact]
-    public void DimAuto_TextFormatter_Is_Auto ()
-    {
-        View view = new ();
-        Assert.False (view.TextFormatter.AutoSize);
-        view.Width = Dim.Auto ();
-        Assert.True (view.TextFormatter.AutoSize);
-
-        view = new ();
-        Assert.False (view.TextFormatter.AutoSize);
-        view.Height = Dim.Auto ();
-        Assert.True (view.TextFormatter.AutoSize);
-    }
+    //[Fact]
+    //public void DimAuto_TextFormatter_Is_Auto ()
+    //{
+    //    View view = new ();
+    //    Assert.False (view.TextFormatter.AutoSize);
+    //    view.Width = Dim.Auto ();
+    //    Assert.True (view.TextFormatter.AutoSize);
+
+    //    view = new ();
+    //    Assert.False (view.TextFormatter.AutoSize);
+    //    view.Height = Dim.Auto ();
+    //    Assert.True (view.TextFormatter.AutoSize);
+    //}
 
     [Theory]
     [InlineData ("1234", 4)]

+ 0 - 10
UnitTests/View/Text/AutoSizeTrueTests.cs

@@ -1855,11 +1855,6 @@ Y
         Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
 
-        Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
-
         if (autoSize)
         {
             Size expectedSize = new (11, 1);
@@ -1983,11 +1978,6 @@ Y
         Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
 
-        Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
-        Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
-
         if (autoSize)
         {
             Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);

+ 20 - 0
UnitTests/View/Text/TextTests.cs

@@ -130,6 +130,26 @@ public class TextTests (ITestOutputHelper output)
         Assert.Equal (new Rectangle (0, 0, 5, 1), view.Viewport);
     }
 
+    // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
+    [Fact]
+    public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
+    {
+        var view = new View
+        {
+            Text = "01234",
+            TextDirection = TextDirection.LeftRight_TopBottom,
+            TextAlignment = Alignment.Centered,
+            Width = 10,
+            Height = Dim.Auto (Dim.DimAutoStyle.Text)
+        };
+        view.BeginInit ();
+        view.EndInit ();
+        Assert.Equal (new Rectangle (0, 0, 10, 1), view.Frame);
+        Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
+
+        Assert.Equal (new (10, 1), view.TextFormatter.Size);  
+    }
+
     [Fact]
     public void TextDirection_Vertical_Dims_Correct ()
     {