From ee75af620d9cfe165799676c554ea4f764eddba0 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 8 Jun 2021 21:35:27 +0100 Subject: [PATCH] Fixes the restriction of the AutoSize = true of only being true if both the width and the height are true. --- Terminal.Gui/Core/View.cs | 13 ++++++---- Terminal.Gui/Views/Label.cs | 9 +++++++ UnitTests/ViewTests.cs | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 6c66b92ab..4104b2a59 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -2098,17 +2098,20 @@ namespace Terminal.Gui { bool SetWidthHeight (Rect nBounds) { - bool aSize; + bool aSize = false; var canSizeW = SetWidth (nBounds.Width, out int rW); var canSizeH = SetHeight (nBounds.Height, out int rH); - if (canSizeW && canSizeH) { + if (canSizeW) { aSize = true; - Bounds = nBounds; width = rW; + } + if (canSizeH) { + aSize = true; height = rH; + } + if (aSize) { + Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height); textFormatter.Size = Bounds.Size; - } else { - aSize = false; } return aSize; diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index 69f7e825e..2c438bddd 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -23,6 +23,7 @@ namespace Terminal.Gui { /// public Label () { + Initialize (); } /// @@ -33,6 +34,7 @@ namespace Terminal.Gui { /// public Label (ustring text) : base (text) { + Initialize (); } /// @@ -43,12 +45,19 @@ namespace Terminal.Gui { /// public Label (int x, int y, ustring text) : base (x, y, text) { + Initialize (); } /// public Label (ustring text, TextDirection direction) : base (text, direction) { + Initialize (); + } + + void Initialize () + { + AutoSize = true; } /// diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 4c14b9773..d6abee6d2 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -1282,5 +1282,55 @@ namespace Terminal.Gui.Views { Assert.False (v.GetCurrentHeight (out cHeight)); Assert.Equal (19, cHeight); } + + [Fact] + public void AutoSize_False_ResizeView_Is_Always_False () + { + var label = new Label () { AutoSize = false }; + + label.Text = "New text"; + + Assert.False (label.AutoSize); + Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ()); + } + + [Fact] + public void AutoSize_True_ResizeView_With_Dim_Absolute () + { + var label = new Label (); + + label.Text = "New text"; + + Assert.True (label.AutoSize); + Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ()); + } + + [Fact] + public void AutoSize_True_ResizeView_With_Dim_Fill () + { + var win = new Window (new Rect (0, 0, 30, 80), ""); + var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () }; + win.Add (label); + + label.Text = "New text\nNew line"; + win.LayoutSubviews (); + + Assert.True (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 () + { + var win = new Window (new Rect (0, 0, 30, 80), ""); + var label = new Label () { Width = Dim.Fill () }; + win.Add (label); + + label.Text = "New text\nNew line"; + win.LayoutSubviews (); + + Assert.True (label.AutoSize); + Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ()); + } } }