Fix text and auto size behavior.

This commit is contained in:
BDisp
2023-11-24 21:12:21 +00:00
parent 7b3d8eed2b
commit b0e5ada1fb
2 changed files with 26 additions and 11 deletions

View File

@@ -978,7 +978,7 @@ namespace Terminal.Gui {
#endregion // Static Members
List<string> _lines = new List<string> ();
string _text;
string _text = null;
TextAlignment _textAlignment;
VerticalTextAlignment _textVerticalAlignment;
TextDirection _textDirection;
@@ -997,13 +997,17 @@ namespace Terminal.Gui {
public virtual string Text {
get => _text;
set {
if (AutoSize || (_text == null && value != null && Size.IsEmpty)) {
Size = CalcRect (0, 0, value, _textDirection).Size;
}
_text = value;
if (_text != null && _text.GetRuneCount () > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != _text.GetColumns ())) {
// 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);
}
//if (_text != null && _text.GetRuneCount () > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != _text.GetColumns ())) {
// // 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);
//}
NeedsFormat = true;
}

View File

@@ -65,13 +65,23 @@ namespace Terminal.Gui.TextTests {
Assert.NotEmpty (tf.Lines);
}
[Fact]
public void TestSize_TextChange ()
[Theory]
[InlineData (TextDirection.LeftRight_TopBottom, false)]
[InlineData (TextDirection.TopBottom_LeftRight, true)]
public void TestSize_TextChange (TextDirection textDirection, bool autoSize)
{
var tf = new TextFormatter () { Text = "你" };
var tf = new TextFormatter () { Direction = textDirection, Text = "你", AutoSize = autoSize };
Assert.Equal (2, tf.Size.Width);
tf.Text = "你你";
Assert.Equal (4, tf.Size.Width);
if (autoSize) {
if (textDirection == TextDirection.LeftRight_TopBottom) {
Assert.Equal (4, tf.Size.Width);
} else {
Assert.Equal (2, tf.Size.Width);
}
} else {
Assert.Equal (2, tf.Size.Width);
}
}
[Fact]
@@ -1472,12 +1482,13 @@ ssb
var text = "Les Mise\u0328\u0301rables";
var tf = new TextFormatter ();
tf.Text = text;
tf.Direction = textDirection;
tf.Text = text;
if (textDirection == TextDirection.LeftRight_TopBottom) {
Assert.Equal (new Size (width, height), tf.Size);
} else {
Assert.Equal (new Size (1, text.GetColumns ()), tf.Size);
tf.Size = new Size (width, height);
}
tf.Draw (new Rect (0, 0, width, height), new Attribute (ColorName.White, ColorName.Black), new Attribute (ColorName.Blue, ColorName.Black));