Merge pull request #1004 from BDisp/view-auto-size

Fixes #1002. Added a AutoSize property to the View class.
This commit is contained in:
Charlie Kindel
2020-11-17 09:31:22 -07:00
committed by GitHub

View File

@@ -122,6 +122,7 @@ namespace Terminal.Gui {
View container = null;
View focused = null;
Direction focusDirection;
bool autoSize;
TextFormatter textFormatter;
@@ -1899,25 +1900,30 @@ namespace Terminal.Gui {
get => textFormatter.Text;
set {
textFormatter.Text = value;
if (textFormatter.Size != Bounds.Size && (((width == null || width is Dim.DimAbsolute) && Bounds.Width == 0)
|| ((height == null || height is Dim.DimAbsolute) && Bounds.Height == 0))) {
Bounds = new Rect (Bounds.X, Bounds.Y, textFormatter.Size.Width, textFormatter.Size.Height);
if (width == null) {
width = Bounds.Width;
} else if (width is Dim.DimAbsolute) {
width = Math.Max (Bounds.Width, height.Anchor (Bounds.Width));
}
if (height == null) {
height = Bounds.Height;
} else if (height is Dim.DimAbsolute) {
height = Math.Max (Bounds.Height, height.Anchor (Bounds.Height));
}
}
ResizeView (autoSize);
SetNeedsLayout ();
SetNeedsDisplay ();
}
}
/// <summary>
/// Used by <see cref="Text"/> to resize the view's <see cref="Bounds"/> with the <see cref="TextFormatter.Size"/>.
/// Setting <see cref="Auto"/> to true only work if the <see cref="Width"/> and <see cref="Height"/> are null or
/// <see cref="LayoutStyle.Absolute"/> values and doesn't work with <see cref="LayoutStyle.Computed"/> layout,
/// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
/// </summary>
public virtual bool AutoSize {
get => autoSize;
set {
var v = ResizeView (value);
if (autoSize != v) {
autoSize = v;
SetNeedsLayout ();
SetNeedsDisplay ();
}
}
}
/// <summary>
/// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will redisplay the <see cref="View"/>.
/// </summary>
@@ -1945,6 +1951,31 @@ namespace Terminal.Gui {
return $"{GetType ().Name}({Id})({Frame})";
}
bool ResizeView (bool autoSize)
{
if (textFormatter.Size != Bounds.Size && (((width == null || width is Dim.DimAbsolute) && (Bounds.Width == 0
|| autoSize && Bounds.Width != textFormatter.Size.Width))
|| ((height == null || height is Dim.DimAbsolute) && (Bounds.Height == 0
|| autoSize && Bounds.Height != textFormatter.Size.Height)))) {
Bounds = new Rect (Bounds.X, Bounds.Y, textFormatter.Size.Width, textFormatter.Size.Height);
if (width == null) {
width = Bounds.Width;
} else if (width is Dim.DimAbsolute) {
width = Math.Max (Bounds.Width, height.Anchor (Bounds.Width));
} else {
return false;
}
if (height == null) {
height = Bounds.Height;
} else if (height is Dim.DimAbsolute) {
height = Math.Max (Bounds.Height, height.Anchor (Bounds.Height));
} else {
return false;
}
}
return autoSize;
}
/// <summary>
/// Specifies the event arguments for <see cref="MouseEvent"/>
/// </summary>