diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index c0b6f9f44..c134d945e 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -136,7 +136,7 @@ namespace Terminal.Gui { set { text = value; - if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) { + if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.ConsoleWidth)) { // Provide a default size (width = length of longest line, height = 1) // TODO: It might makes more sense for the default to be width = length of first line? Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1); diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 4300c4e59..ca204fa13 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -137,8 +137,7 @@ namespace Terminal.Gui { if (hotKey != hk) { HotKey = hk; } - if (IsInitialized) - Update (); + Update (); } } @@ -150,8 +149,7 @@ namespace Terminal.Gui { get => is_default; set { is_default = value; - if (IsInitialized) - Update (); + Update (); } } @@ -188,8 +186,7 @@ namespace Terminal.Gui { get => base.AutoSize; set { base.AutoSize = value; - if (IsInitialized) - Update (); + Update (); } } diff --git a/UnitTests/ButtonTests.cs b/UnitTests/ButtonTests.cs index d7224438a..d18f69c61 100644 --- a/UnitTests/ButtonTests.cs +++ b/UnitTests/ButtonTests.cs @@ -26,7 +26,7 @@ namespace Terminal.Gui.Views { Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame); Assert.Equal (Key.Null, btn.HotKey); - btn = new Button ("Test", true); + btn = new Button ("ARGS", true) {Text="Test"}; Assert.Equal ("Test", btn.Text); Application.Top.Add (btn); btn.Redraw (btn.Bounds); @@ -166,12 +166,15 @@ namespace Terminal.Gui.Views { [Fact] public void TestAssignTextToButton () { - View b = new Button (); - b.Text = "heya"; + View b = new Button () {Text="heya"}; Assert.Equal ("heya", b.Text); + Assert.True (b.TextFormatter.Text.Contains ("heya")); + b.Text = "heyb"; + Assert.Equal ("heyb", b.Text); + Assert.True (b.TextFormatter.Text.Contains ("heyb")); // with cast - Assert.Equal ("heya", ((Button)b).Text); + Assert.Equal ("heyb", ((Button)b).Text); } [Fact] @@ -220,6 +223,44 @@ namespace Terminal.Gui.Views { │ [ Say Hello 你 ] │ │ │ └────────────────────────────┘ +"; + + var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + Assert.Equal (new Rect (0, 0, 30, 5), pos); + } + + [Fact, AutoInitShutdown] + public void Update_Parameterless_Only_On_Or_After_Initialize () + { + var btn = new Button () { + X = Pos.Center (), + Y = Pos.Center (), + Text = "Say Hello 你" + }; + var win = new Window () { + Width = Dim.Fill (), + Height = Dim.Fill (), + Title = "Test Demo 你" + }; + win.Add (btn); + Application.Top.Add (win); + + Assert.False (btn.IsInitialized); + + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + + Assert.True (btn.IsInitialized); + Assert.Equal ("Say Hello 你", btn.Text); + Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text); + Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds); + + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ [ Say Hello 你 ] │ +│ │ +└────────────────────────────┘ "; var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); diff --git a/UnitTests/TextFormatterTests.cs b/UnitTests/TextFormatterTests.cs index 05f6a88c8..82dba6258 100644 --- a/UnitTests/TextFormatterTests.cs +++ b/UnitTests/TextFormatterTests.cs @@ -65,6 +65,15 @@ namespace Terminal.Gui.Core { Assert.NotEmpty (tf.Lines); } + [Fact] + public void TestSize_TextChange () + { + var tf = new TextFormatter () { Text = "你" }; + Assert.Equal (2,tf.Size.Width); + tf.Text = "你你"; + Assert.Equal (4, tf.Size.Width); + } + [Fact] public void NeedsFormat_Sets () {