Przeglądaj źródła

Fixed multiline dialog layout (#160)

Cameron MacFarland 6 lat temu
rodzic
commit
a88cb16b30
2 zmienionych plików z 15 dodań i 2 usunięć
  1. 2 2
      Terminal.Gui/Dialogs/MessageBox.cs
  2. 13 0
      Terminal.Gui/Views/Label.cs

+ 2 - 2
Terminal.Gui/Dialogs/MessageBox.cs

@@ -52,7 +52,7 @@ namespace Terminal.Gui {
 
 		static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
 		{
-			int lines = Label.MeasureLines (message, width);
+			int textWidth = Label.MaxWidth (message, width);
 			int clicked = -1, count = 0;
 
 			var d = new Dialog (title, width, height);
@@ -69,7 +69,7 @@ namespace Terminal.Gui {
 				d.AddButton (b);
 			}
 			if (message != null) {
-				var l = new Label ((width - 4 - message.Length) / 2, 0, message);
+				var l = new Label ((width - 4 - textWidth) / 2, 0, message);
 				d.Add (l);
 			}
 

+ 13 - 0
Terminal.Gui/Views/Label.cs

@@ -209,6 +209,19 @@ namespace Terminal.Gui {
 			return result.Count;
 		}
 
+		/// <summary>
+		/// Computes the the max width of a line or multilines needed to render by the Label control
+		/// </summary>
+		/// <returns>Max width of lines.</returns>
+		/// <param name="text">Text, may contain newlines.</param>
+		/// <param name="width">The width for the text.</param>
+		public static int MaxWidth(ustring text, int width)
+		{
+			var result = new List<ustring>();
+			Recalc(text, result, width, TextAlignment.Left);
+			return result.Max(s => s.RuneCount);
+		}
+
 		/// <summary>
 		///   The text displayed by this widget.
 		/// </summary>