From ad445292f68a5065777a43806c39b6bbafa7b55f Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 31 Jul 2020 23:56:55 +0100 Subject: [PATCH 1/2] Fixes #836. Allows a Label display the text initially empty. --- Terminal.Gui/Core/TextFormatter.cs | 4 ++-- Terminal.Gui/Core/View.cs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 1cfaf62d7..7fe96445d 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -47,7 +47,7 @@ namespace Terminal.Gui { set { text = value; - if (Size.IsEmpty) { + if (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount) { // Proivde a default size (width = length of longest line, height = 1) // TODO: It might makem more sense for the default to be width = length of first line? Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1); @@ -405,7 +405,7 @@ namespace Terminal.Gui { public static Rect CalcRect (int x, int y, ustring text) { if (ustring.IsNullOrEmpty (text)) - return Rect.Empty; + return new Rect (new Point (x, y), Size.Empty); int mw = 0; int ml = 1; diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 69d0ed5c7..fde54913c 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1784,6 +1784,9 @@ namespace Terminal.Gui { get => textFormatter.Text; set { textFormatter.Text = value; + if (textFormatter.Size != Bounds.Size && (width == null || Bounds.Width == 0 || height == null || Bounds.Height == 0)) { + Bounds = new Rect (Bounds.X, Bounds.Y, textFormatter.Size.Width, textFormatter.Size.Height); + } SetNeedsDisplay (); } } From 805fdeb3440726d419f6afd0f53c143fbb9455a4 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 1 Aug 2020 00:27:32 +0100 Subject: [PATCH 2/2] Fixing TextFormatter unit test. --- Terminal.Gui/Core/TextFormatter.cs | 2 +- UnitTests/TextFormatterTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 7fe96445d..98e58c2b8 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -47,7 +47,7 @@ namespace Terminal.Gui { set { text = value; - if (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount) { + if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) { // Proivde a default size (width = length of longest line, height = 1) // TODO: It might makem more sense for the default to be width = length of first line? Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1); diff --git a/UnitTests/TextFormatterTests.cs b/UnitTests/TextFormatterTests.cs index 17200127b..c20fafb10 100644 --- a/UnitTests/TextFormatterTests.cs +++ b/UnitTests/TextFormatterTests.cs @@ -533,8 +533,8 @@ namespace Terminal.Gui { { Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null)); Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, "")); - Assert.Equal (Rect.Empty, TextFormatter.CalcRect (1, 2, "")); - Assert.Equal (Rect.Empty, TextFormatter.CalcRect (-1, -2, "")); + Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, "")); + Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, "")); } [Fact]