From 1cd80ab3a48f4384638ae9292930da033a0862b2 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 7 Nov 2020 15:03:03 +0000 Subject: [PATCH] Fixes #992. TextFormatter class now respect the view dimensions. Some typo fixing too. --- Terminal.Gui/Core/TextFormatter.cs | 32 +++++++++++++++--------------- Terminal.Gui/Core/View.cs | 14 ++++++++++++- UICatalog/Scenarios/Scrolling.cs | 8 ++++---- UICatalog/Scenarios/Threading.cs | 2 +- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index f2e3ba4b8..6b702bb33 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -28,7 +28,7 @@ namespace Terminal.Gui { } /// - /// 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. /// public class TextFormatter { List lines = new List (); @@ -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 { } /// - /// 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. /// public Size Size { get => size; @@ -82,7 +82,7 @@ namespace Terminal.Gui { } /// - /// 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'. /// public Rune HotKeySpecifier { get; set; } = (Rune)0xFFFF; @@ -108,7 +108,7 @@ namespace Terminal.Gui { public int CursorPosition { get; set; } /// - /// Gets the formatted lines. + /// Gets the formatted lines. /// /// /// @@ -143,12 +143,12 @@ namespace Terminal.Gui { } /// - /// Gets or sets whether the needs to format the text when is called. + /// Gets or sets whether the needs to format the text when is called. /// If it is false when Draw is called, the Draw call will be faster. /// /// /// - /// This is set to true when the properties of are set. + /// This is set to true when the properties of are set. /// /// 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. /// /// The text to word wrap - /// The width to contrain the text to + /// The width to contain the text to /// Returns a list of word wrapped lines. /// /// @@ -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 { /// /// /// Character to replace whitespace and pad with. For debugging purposes. - /// The justifed text. + /// The justified text. public static ustring Justify (ustring text, int width, char spaceChar = ' ') { if (width < 0) { @@ -388,7 +388,7 @@ namespace Terminal.Gui { } /// - /// 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. /// /// Max width of lines. /// Text, may contain newlines. @@ -439,8 +439,8 @@ namespace Terminal.Gui { /// /// The text to look in. /// The hotkey specifier (e.g. '_') to look for. - /// If true the legacy behavior of identifying the first upper case character as the hotkey will be eanbled. - /// Regardless of the value of this parameter, hotKeySpecifier takes precidence. + /// If true the legacy behavior of identifying the first upper case character as the hotkey will be enabled. + /// Regardless of the value of this parameter, hotKeySpecifier takes precedence. /// Outputs the Rune index into text. /// Outputs the hotKey. /// true if a hotkey was found; false otherwise. @@ -502,8 +502,8 @@ namespace Terminal.Gui { } /// - /// Replaces the Rune at the index specfiied by the hotPos parameter with a tag identifying - /// it as the hotkey. + /// Replaces the Rune at the index specified by the hotPos parameter with a tag identifying + /// it as the hotkey. /// /// The text to tag the hotkey in. /// The Rune index of the hotkey in text. diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index d5602b9c6..9661da7a6 100644 --- a/Terminal.Gui/Core/View.cs +++ b/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 (); } } diff --git a/UICatalog/Scenarios/Scrolling.cs b/UICatalog/Scenarios/Scrolling.cs index 8b4a81dee..76c4fe4dd 100644 --- a/UICatalog/Scenarios/Scrolling.cs +++ b/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, diff --git a/UICatalog/Scenarios/Threading.cs b/UICatalog/Scenarios/Threading.cs index 37fbe12a7..479c49e0a 100644 --- a/UICatalog/Scenarios/Threading.cs +++ b/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 ()