Skip to content

Commit

Permalink
Fixes #1750. Erroneous suppression of Button Text updates. (#1752)
Browse files Browse the repository at this point in the history
* - Fix upstream issue 1750
- Unexplained breakage in odd test "Update_Only_On_Or_After_Initialize"

* Add workaround in Button.cs for erroneous text width caching in TextFormatter

* - Revert earlier attempted workaround for update issue

* Fix TextFormatter erroneous width caching issue when new runecount matched previous width in columns, add regression test, revert temporary workaround from Button.cs

* Add new unit test Update_Parameterless_Only_On_Or_After_Initialize
  • Loading branch information
jas88 authored May 28, 2022
1 parent d68a2e8 commit efb654e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Terminal.Gui/Core/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public virtual ustring Text {
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);
Expand Down
9 changes: 3 additions & 6 deletions Terminal.Gui/Views/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ public override ustring Text {
if (hotKey != hk) {
HotKey = hk;
}
if (IsInitialized)
Update ();
Update ();
}
}

Expand All @@ -150,8 +149,7 @@ public bool IsDefault {
get => is_default;
set {
is_default = value;
if (IsInitialized)
Update ();
Update ();
}
}

Expand Down Expand Up @@ -188,8 +186,7 @@ public override bool AutoSize {
get => base.AutoSize;
set {
base.AutoSize = value;
if (IsInitialized)
Update ();
Update ();
}
}

Expand Down
49 changes: 45 additions & 4 deletions UnitTests/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Constructors_Defaults ()
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);
Expand Down Expand Up @@ -166,12 +166,15 @@ public void KeyBindingExample ()
[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]
Expand Down Expand Up @@ -220,6 +223,44 @@ public void Update_Only_On_Or_After_Initialize ()
│ [ 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);
Expand Down
9 changes: 9 additions & 0 deletions UnitTests/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public void Basic_Usage ()
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 ()
{
Expand Down

0 comments on commit efb654e

Please sign in to comment.