Fixes the restriction of the AutoSize = true of only being true if both the width and the height are true.

This commit is contained in:
BDisp
2021-06-08 21:35:27 +01:00
parent 432e9cb801
commit ee75af620d
3 changed files with 67 additions and 5 deletions

View File

@@ -2098,17 +2098,20 @@ namespace Terminal.Gui {
bool SetWidthHeight (Rect nBounds)
{
bool aSize;
bool aSize = false;
var canSizeW = SetWidth (nBounds.Width, out int rW);
var canSizeH = SetHeight (nBounds.Height, out int rH);
if (canSizeW && canSizeH) {
if (canSizeW) {
aSize = true;
Bounds = nBounds;
width = rW;
}
if (canSizeH) {
aSize = true;
height = rH;
}
if (aSize) {
Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height);
textFormatter.Size = Bounds.Size;
} else {
aSize = false;
}
return aSize;

View File

@@ -23,6 +23,7 @@ namespace Terminal.Gui {
/// <inheritdoc/>
public Label ()
{
Initialize ();
}
/// <inheritdoc/>
@@ -33,6 +34,7 @@ namespace Terminal.Gui {
/// <inheritdoc/>
public Label (ustring text) : base (text)
{
Initialize ();
}
/// <inheritdoc/>
@@ -43,12 +45,19 @@ namespace Terminal.Gui {
/// <inheritdoc/>
public Label (int x, int y, ustring text) : base (x, y, text)
{
Initialize ();
}
/// <inheritdoc/>
public Label (ustring text, TextDirection direction)
: base (text, direction)
{
Initialize ();
}
void Initialize ()
{
AutoSize = true;
}
/// <summary>

View File

@@ -1282,5 +1282,55 @@ namespace Terminal.Gui.Views {
Assert.False (v.GetCurrentHeight (out cHeight));
Assert.Equal (19, cHeight);
}
[Fact]
public void AutoSize_False_ResizeView_Is_Always_False ()
{
var label = new Label () { AutoSize = false };
label.Text = "New text";
Assert.False (label.AutoSize);
Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
}
[Fact]
public void AutoSize_True_ResizeView_With_Dim_Absolute ()
{
var label = new Label ();
label.Text = "New text";
Assert.True (label.AutoSize);
Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
}
[Fact]
public void AutoSize_True_ResizeView_With_Dim_Fill ()
{
var win = new Window (new Rect (0, 0, 30, 80), "");
var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
label.Text = "New text\nNew line";
win.LayoutSubviews ();
Assert.True (label.AutoSize);
Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
}
[Fact]
public void AutoSize_True_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute ()
{
var win = new Window (new Rect (0, 0, 30, 80), "");
var label = new Label () { Width = Dim.Fill () };
win.Add (label);
label.Text = "New text\nNew line";
win.LayoutSubviews ();
Assert.True (label.AutoSize);
Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
}
}
}