Skip to content

Commit efb654e

Browse files
authored
Fixes #1750. Erroneous suppression of Button Text updates. (#1752)
* - 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
1 parent d68a2e8 commit efb654e

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

Terminal.Gui/Core/TextFormatter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public virtual ustring Text {
136136
set {
137137
text = value;
138138

139-
if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) {
139+
if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.ConsoleWidth)) {
140140
// Provide a default size (width = length of longest line, height = 1)
141141
// TODO: It might makes more sense for the default to be width = length of first line?
142142
Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1);

Terminal.Gui/Views/Button.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ public override ustring Text {
137137
if (hotKey != hk) {
138138
HotKey = hk;
139139
}
140-
if (IsInitialized)
141-
Update ();
140+
Update ();
142141
}
143142
}
144143

@@ -150,8 +149,7 @@ public bool IsDefault {
150149
get => is_default;
151150
set {
152151
is_default = value;
153-
if (IsInitialized)
154-
Update ();
152+
Update ();
155153
}
156154
}
157155

@@ -188,8 +186,7 @@ public override bool AutoSize {
188186
get => base.AutoSize;
189187
set {
190188
base.AutoSize = value;
191-
if (IsInitialized)
192-
Update ();
189+
Update ();
193190
}
194191
}
195192

UnitTests/ButtonTests.cs

+45-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Constructors_Defaults ()
2626
Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
2727
Assert.Equal (Key.Null, btn.HotKey);
2828

29-
btn = new Button ("Test", true);
29+
btn = new Button ("ARGS", true) {Text="Test"};
3030
Assert.Equal ("Test", btn.Text);
3131
Application.Top.Add (btn);
3232
btn.Redraw (btn.Bounds);
@@ -166,12 +166,15 @@ public void KeyBindingExample ()
166166
[Fact]
167167
public void TestAssignTextToButton ()
168168
{
169-
View b = new Button ();
170-
b.Text = "heya";
169+
View b = new Button () {Text="heya"};
171170
Assert.Equal ("heya", b.Text);
171+
Assert.True (b.TextFormatter.Text.Contains ("heya"));
172+
b.Text = "heyb";
173+
Assert.Equal ("heyb", b.Text);
174+
Assert.True (b.TextFormatter.Text.Contains ("heyb"));
172175

173176
// with cast
174-
Assert.Equal ("heya", ((Button)b).Text);
177+
Assert.Equal ("heyb", ((Button)b).Text);
175178
}
176179

177180
[Fact]
@@ -220,6 +223,44 @@ public void Update_Only_On_Or_After_Initialize ()
220223
│ [ Say Hello 你 ] │
221224
│ │
222225
└────────────────────────────┘
226+
";
227+
228+
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
229+
Assert.Equal (new Rect (0, 0, 30, 5), pos);
230+
}
231+
232+
[Fact, AutoInitShutdown]
233+
public void Update_Parameterless_Only_On_Or_After_Initialize ()
234+
{
235+
var btn = new Button () {
236+
X = Pos.Center (),
237+
Y = Pos.Center (),
238+
Text = "Say Hello 你"
239+
};
240+
var win = new Window () {
241+
Width = Dim.Fill (),
242+
Height = Dim.Fill (),
243+
Title = "Test Demo 你"
244+
};
245+
win.Add (btn);
246+
Application.Top.Add (win);
247+
248+
Assert.False (btn.IsInitialized);
249+
250+
Application.Begin (Application.Top);
251+
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
252+
253+
Assert.True (btn.IsInitialized);
254+
Assert.Equal ("Say Hello 你", btn.Text);
255+
Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text);
256+
Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
257+
258+
var expected = @"
259+
┌ Test Demo 你 ──────────────┐
260+
│ │
261+
│ [ Say Hello 你 ] │
262+
│ │
263+
└────────────────────────────┘
223264
";
224265

225266
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);

UnitTests/TextFormatterTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public void Basic_Usage ()
6565
Assert.NotEmpty (tf.Lines);
6666
}
6767

68+
[Fact]
69+
public void TestSize_TextChange ()
70+
{
71+
var tf = new TextFormatter () { Text = "你" };
72+
Assert.Equal (2,tf.Size.Width);
73+
tf.Text = "你你";
74+
Assert.Equal (4, tf.Size.Width);
75+
}
76+
6877
[Fact]
6978
public void NeedsFormat_Sets ()
7079
{

0 commit comments

Comments
 (0)