Browse Source

Fixes #992. TextFormatter class now respect the view dimensions. Some typo fixing too.

BDisp 4 years ago
parent
commit
1cd80ab3a4

+ 16 - 16
Terminal.Gui/Core/TextFormatter.cs

@@ -28,7 +28,7 @@ namespace Terminal.Gui {
 	}
 
 	/// <summary>
-	/// Provides text formatting capabilities for console apps. Supports, hotkeys, horizontal alignment, multille lines, and word-based line wrap.
+	/// Provides text formatting capabilities for console apps. Supports, hotkeys, horizontal alignment, multiple lines, and word-based line wrap.
 	/// </summary>
 	public class TextFormatter {
 		List<ustring> lines = new List<ustring> ();
@@ -48,8 +48,8 @@ namespace Terminal.Gui {
 				text = value;
 
 				if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) {
-					// Proivde a default size (width = length of longest line, height = 1)
-					// TODO: It might makem more sense for the default to be width = length of first line?
+					// Provide a default size (width = length of longest line, height = 1)
+					// TODO: It might makes more sense for the default to be width = length of first line?
 					Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1);
 				}
 
@@ -71,7 +71,7 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		///  Gets or sets the size of the area the text will be constrainted to when formatted. 
+		///  Gets or sets the size of the area the text will be constrained to when formatted.
 		/// </summary>
 		public Size Size {
 			get => size;
@@ -82,7 +82,7 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// The specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'. 
+		/// The specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
 		/// </summary>
 		public Rune HotKeySpecifier { get; set; } = (Rune)0xFFFF;
 
@@ -108,7 +108,7 @@ namespace Terminal.Gui {
 		public int CursorPosition { get; set; }
 
 		/// <summary>
-		/// Gets the formatted lines. 
+		/// Gets the formatted lines.
 		/// </summary>
 		/// <remarks>
 		/// <para>
@@ -143,12 +143,12 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute)"/> is called. 
+		/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute)"/> is called.
 		/// If it is <c>false</c> when Draw is called, the Draw call will be faster.
 		/// </summary>
 		/// <remarks>
 		/// <para>
-		/// This is set to true when the properties of <see cref="TextFormatter"/> are set. 
+		/// This is set to true when the properties of <see cref="TextFormatter"/> are set.
 		/// </para>
 		/// </remarks>
 		public bool NeedsFormat { get => needsFormat; set => needsFormat = value; }
@@ -202,7 +202,7 @@ namespace Terminal.Gui {
 		/// Formats the provided text to fit within the width provided using word wrapping.
 		/// </summary>
 		/// <param name="text">The text to word wrap</param>
-		/// <param name="width">The width to contrain the text to</param>
+		/// <param name="width">The width to contain the text to</param>
 		/// <returns>Returns a list of word wrapped lines.</returns>
 		/// <remarks>
 		/// <para>
@@ -232,7 +232,7 @@ namespace Terminal.Gui {
 					end -= 1;
 				if (end == start)
 					end = start + width;
-				lines.Add (ustring.Make (runes.GetRange (start, end - start))); 
+				lines.Add (ustring.Make (runes.GetRange (start, end - start)));
 				start = end;
 				if (runes[end] == ' ') {
 					start++;
@@ -281,7 +281,7 @@ namespace Terminal.Gui {
 		/// <param name="text"></param>
 		/// <param name="width"></param>
 		/// <param name="spaceChar">Character to replace whitespace and pad with. For debugging purposes.</param>
-		/// <returns>The justifed text.</returns>
+		/// <returns>The justified text.</returns>
 		public static ustring Justify (ustring text, int width, char spaceChar = ' ')
 		{
 			if (width < 0) {
@@ -388,7 +388,7 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// Computes the maximum width needed to render the text (single line or multple lines) given a minimum width.
+		/// Computes the maximum width needed to render the text (single line or multiple lines) given a minimum width.
 		/// </summary>
 		/// <returns>Max width of lines.</returns>
 		/// <param name="text">Text, may contain newlines.</param>
@@ -439,8 +439,8 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// <param name="text">The text to look in.</param>
 		/// <param name="hotKeySpecifier">The hotkey specifier (e.g. '_') to look for.</param>
-		/// <param name="firstUpperCase">If <c>true</c> the legacy behavior of identifying the first upper case character as the hotkey will be eanbled. 
-		/// Regardless of the value of this parameter, <c>hotKeySpecifier</c> takes precidence.</param>
+		/// <param name="firstUpperCase">If <c>true</c> the legacy behavior of identifying the first upper case character as the hotkey will be enabled.
+		/// Regardless of the value of this parameter, <c>hotKeySpecifier</c> takes precedence.</param>
 		/// <param name="hotPos">Outputs the Rune index into <c>text</c>.</param>
 		/// <param name="hotKey">Outputs the hotKey.</param>
 		/// <returns><c>true</c> if a hotkey was found; <c>false</c> otherwise.</returns>
@@ -502,8 +502,8 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// Replaces the Rune at the index specfiied by the <c>hotPos</c> parameter with a tag identifying 
-		/// it as the hotkey. 
+		/// Replaces the Rune at the index specified by the <c>hotPos</c> parameter with a tag identifying 
+		/// it as the hotkey.
 		/// </summary>
 		/// <param name="text">The text to tag the hotkey in.</param>
 		/// <param name="hotPos">The Rune index of the hotkey in <c>text</c>.</param>

+ 13 - 1
Terminal.Gui/Core/View.cs

@@ -1899,9 +1899,21 @@ namespace Terminal.Gui {
 			get => textFormatter.Text;
 			set {
 				textFormatter.Text = value;
-				if (textFormatter.Size != Bounds.Size && (width == null && Bounds.Width == 0) || (height == null && Bounds.Height == 0)) {
+				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));
+					}
 				}
+				SetNeedsLayout ();
 				SetNeedsDisplay ();
 			}
 		}

+ 4 - 4
UICatalog/Scenarios/Scrolling.cs

@@ -124,17 +124,17 @@ namespace UICatalog {
 				ShowHorizontalScrollIndicator = true,
 			};
 
-			const string rule = "|123456789";
-			var horizontalRuler = new Label ("") {
+			const string rule = "0123456789";
+			var horizontalRuler = new Label () {
 				X = 0,
 				Y = 0,
 				Width = Dim.Fill (1),  // BUGBUG: I don't think this should be needed; DimFill() should respect container's frame. X does.
 				ColorScheme = Colors.Error
 			};
 			scrollView.Add (horizontalRuler);
-			const string vrule = "|\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
+			const string vrule = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
 
-			var verticalRuler = new Label ("") {
+			var verticalRuler = new Label () {
 				X = 0,
 				Y = 0,
 				Width = 1,

+ 1 - 1
UICatalog/Scenarios/Threading.cs

@@ -92,7 +92,7 @@ namespace UICatalog {
 			_btnQuit.Clicked += Application.RequestStop;
 
 			Win.Add (_itemsList, _btnActionCancel, _logJob, text, _btnAction, _btnLambda, _btnHandler, _btnSync, _btnMethod, _btnClearData, _btnQuit);
-
+			_btnActionCancel.SetFocus ();
 		}
 
 		private async void LoadData ()