浏览代码

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

Fixes #1002. Added a AutoSize property to the View class.
Charlie Kindel 4 年之前
父节点
当前提交
204be65f2a
共有 1 个文件被更改,包括 45 次插入14 次删除
  1. 45 14
      Terminal.Gui/Core/View.cs

+ 45 - 14
Terminal.Gui/Core/View.cs

@@ -122,6 +122,7 @@ namespace Terminal.Gui {
 		View container = null;
 		View container = null;
 		View focused = null;
 		View focused = null;
 		Direction focusDirection;
 		Direction focusDirection;
+		bool autoSize;
 
 
 		TextFormatter textFormatter;
 		TextFormatter textFormatter;
 
 
@@ -1899,25 +1900,30 @@ namespace Terminal.Gui {
 			get => textFormatter.Text;
 			get => textFormatter.Text;
 			set {
 			set {
 				textFormatter.Text = value;
 				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 ();
 				SetNeedsLayout ();
 				SetNeedsDisplay ();
 				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>
 		/// <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"/>.
 		/// 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>
 		/// </summary>
@@ -1945,6 +1951,31 @@ namespace Terminal.Gui {
 			return $"{GetType ().Name}({Id})({Frame})";
 			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>
 		/// <summary>
 		/// Specifies the event arguments for <see cref="MouseEvent"/>
 		/// Specifies the event arguments for <see cref="MouseEvent"/>
 		/// </summary>
 		/// </summary>