Browse Source

Merge pull request #33 from BDisp/tig_v2_2432-DimAuto

Fixes title size not being updating on view size change.
Tig 1 year ago
parent
commit
517f7039d3
3 changed files with 45 additions and 8 deletions
  1. 5 0
      Terminal.Gui/View/Layout/ViewLayout.cs
  2. 11 6
      Terminal.Gui/View/View.cs
  3. 29 2
      UnitTests/View/TitleTests.cs

+ 5 - 0
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -938,6 +938,11 @@ public partial class View
                 _height = Frame.Height;
                 _height = Frame.Height;
             }
             }
 
 
+            if (!string.IsNullOrEmpty (Title))
+            {
+                SetTitleTextFormatterSize ();
+            }
+
             SetNeedsLayout ();
             SetNeedsLayout ();
             SetNeedsDisplay ();
             SetNeedsDisplay ();
         }
         }

+ 11 - 6
Terminal.Gui/View/View.cs

@@ -456,12 +456,7 @@ public partial class View : Responder, ISupportInitializeNotification
                 _title = value;
                 _title = value;
                 TitleTextFormatter.Text = _title;
                 TitleTextFormatter.Text = _title;
 
 
-                TitleTextFormatter.Size = new (
-                                               TextFormatter.GetWidestLineLength (TitleTextFormatter.Text)
-                                               - (TitleTextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
-                                                      ? Math.Max (HotKeySpecifier.GetColumns (), 0)
-                                                      : 0),
-                                               1);
+                SetTitleTextFormatterSize ();
                 SetHotKeyFromTitle ();
                 SetHotKeyFromTitle ();
                 SetNeedsDisplay ();
                 SetNeedsDisplay ();
 #if DEBUG
 #if DEBUG
@@ -475,6 +470,16 @@ public partial class View : Responder, ISupportInitializeNotification
         }
         }
     }
     }
 
 
+    private void SetTitleTextFormatterSize ()
+    {
+        TitleTextFormatter.Size = new (
+                                       TextFormatter.GetWidestLineLength (TitleTextFormatter.Text)
+                                       - (TitleTextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
+                                              ? Math.Max (HotKeySpecifier.GetColumns (), 0)
+                                              : 0),
+                                       1);
+    }
+
     /// <summary>Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
     /// <summary>Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
     /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
     /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
     /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
     /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>

+ 29 - 2
UnitTests/View/TitleTests.cs

@@ -9,8 +9,8 @@ namespace Terminal.Gui.ViewTests;
 
 
 public class TitleTests
 public class TitleTests
 {
 {
-    private readonly ITestOutputHelper output;
-    public TitleTests (ITestOutputHelper output) { this.output = output; }
+    private readonly ITestOutputHelper _output;
+    public TitleTests (ITestOutputHelper output) { this._output = output; }
 
 
     [Fact]
     [Fact]
     public void Set_Title_Fires_TitleChanged ()
     public void Set_Title_Fires_TitleChanged ()
@@ -76,4 +76,31 @@ public class TitleTests
 
 
         Assert.Equal (Key.H, view.HotKey);
         Assert.Equal (Key.H, view.HotKey);
     }
     }
+
+    [SetupFakeDriver]
+    [Fact]
+    public void Change_View_Size_Update_Title_Size ()
+    {
+        var view = new View { Title = "_Hello World", Width = Dim.Auto (), Height = Dim.Auto (), BorderStyle = LineStyle.Single};
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
+
+        Assert.Equal (string.Empty, view.Text);
+        Assert.Equal (new (2, 2), view.Frame.Size);
+        TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌┐
+└┘", _output);
+
+        var text = "This text will increment the view size and display the title.";
+        view.Text = text;
+        top.Draw ();
+        Assert.Equal (text, view.Text);
+        // SetupFakeDriver only create a screen with 25 cols and 25 rows
+        Assert.Equal (new (25, 3), view.Frame.Size);
+        TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌┤Hello World├──────────┐
+│This text will incremen│
+└───────────────────────┘", _output);
+    }
 }
 }