From 84d06a638b86338332be52aa0d7ba4ba50f88a65 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 15 Nov 2020 11:36:43 +0000 Subject: [PATCH 1/2] Fixes #1002. Added a AutoSize property to the View class. --- Terminal.Gui/Core/View.cs | 56 +++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 9661da7a6..dc6374904 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,27 @@ 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 . + /// + 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 +1948,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 /// From b460ee712dc94ffd83e05db72b0611f78e8a5f6b Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 17 Nov 2020 16:12:01 +0000 Subject: [PATCH 2/2] Added more documentation to the AutoSize property. --- Terminal.Gui/Core/View.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index dc6374904..add994704 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1908,6 +1908,9 @@ namespace Terminal.Gui { /// /// 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;