From 21714b64b8bcf9d95c0956cc0cc257db0cba1f3a Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 18 Jun 2021 18:34:16 +0100 Subject: [PATCH] Fixes #1341. Now if AutoSize is true the Bounds size is always updated by using the Dim.Fill or the Dim.Absolute. --- Terminal.Gui/Core/View.cs | 10 +++++--- UnitTests/GraphViewTests.cs | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 4104b2a59..f721feedf 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1988,8 +1988,10 @@ namespace Terminal.Gui { get => textFormatter.Text; set { textFormatter.Text = value; - ResizeView (autoSize); - if (textFormatter.Size != Bounds.Size) { + var canResize = ResizeView (autoSize); + if (canResize && textFormatter.Size != Bounds.Size) { + Bounds = new Rect (new Point (Bounds.X, Bounds.Y), textFormatter.Size); + } else if (!canResize && textFormatter.Size != Bounds.Size) { textFormatter.Size = Bounds.Size; } SetNeedsLayout (); @@ -2085,7 +2087,9 @@ namespace Terminal.Gui { var aSize = autoSize; Rect nBounds = TextFormatter.CalcRect (Bounds.X, Bounds.Y, Text, textFormatter.Direction); - + if (textFormatter.Size != nBounds.Size) { + textFormatter.Size = nBounds.Size; + } if ((textFormatter.Size != Bounds.Size || textFormatter.Size != nBounds.Size) && (((width == null || width is Dim.DimAbsolute) && (Bounds.Width == 0 || autoSize && Bounds.Width != nBounds.Width)) diff --git a/UnitTests/GraphViewTests.cs b/UnitTests/GraphViewTests.cs index 0dde704a1..51273a981 100644 --- a/UnitTests/GraphViewTests.cs +++ b/UnitTests/GraphViewTests.cs @@ -1382,6 +1382,56 @@ namespace Terminal.Gui.Views { // Shutdown must be called to safely clean up Application if Init has been called Application.Shutdown (); } + + [Theory] + [InlineData (true)] + [InlineData (false)] + public void LabelChangeText_RendersCorrectly (bool useFill) + { + var driver = new FakeDriver (); + Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true))); + driver.Init (() => { }); + + // create a wide window + var mount = new View () { + Width = 100, + Height = 100 + }; + + try { + // Create a label with a short text + var lbl1 = new Label ("ff"); + + // Specify that the label should be very wide + if (useFill) { + lbl1.Width = Dim.Fill (); + } else { + lbl1.Width = 100; + } + + //put label into view + mount.Add (lbl1); + + // render view + lbl1.ColorScheme = new ColorScheme (); + Assert.Equal (1, lbl1.Height); + mount.Redraw (mount.Bounds); + + // should have the initial text + GraphViewTests.AssertDriverContentsAre ("ff", null); + + // change the text and redraw + lbl1.Text = "ff1234"; + mount.Redraw (mount.Bounds); + + // should have the new text rendered + GraphViewTests.AssertDriverContentsAre ("ff1234", null); + + + } finally { + Application.Shutdown (); + } + } } public class AxisIncrementToRenderTests {