diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 9661da7a6..add994704 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -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 (); } } + /// + /// Used by to resize the view's with the . + /// Setting to true only work if the and are null or + /// values and doesn't work with layout, + /// to avoid breaking the and settings. + /// + public virtual bool AutoSize { + get => autoSize; + set { + var v = ResizeView (value); + if (autoSize != v) { + autoSize = v; + SetNeedsLayout (); + SetNeedsDisplay (); + } + } + } + /// /// Gets or sets how the View's is aligned horizontally when drawn. Changing this property will redisplay the . /// @@ -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; + } + /// /// Specifies the event arguments for ///