diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index c515cd705..51067b10c 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -938,6 +938,11 @@ public partial class View _height = Frame.Height; } + if (!string.IsNullOrEmpty (Title)) + { + SetTitleTextFormatterSize (); + } + SetNeedsLayout (); SetNeedsDisplay (); } diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 8eab04bba..05ab9f2d9 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -456,12 +456,7 @@ public partial class View : Responder, ISupportInitializeNotification _title = value; 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 (); SetNeedsDisplay (); #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); + } + /// Called when the has been changed. Invokes the event. /// The that is/has been replaced. /// The new to be replaced. diff --git a/UnitTests/View/TitleTests.cs b/UnitTests/View/TitleTests.cs index 1262d1cab..8ce54fd2a 100644 --- a/UnitTests/View/TitleTests.cs +++ b/UnitTests/View/TitleTests.cs @@ -9,8 +9,8 @@ namespace Terminal.Gui.ViewTests; 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] public void Set_Title_Fires_TitleChanged () @@ -76,4 +76,31 @@ public class TitleTests 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); + } }