From a8d6f846d74e99ba53ba2e63eb378e679b8c77e9 Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 3 Apr 2024 09:41:59 -0600 Subject: [PATCH] Fixed MessagBox paint issue --- Terminal.Gui/Views/MessageBox.cs | 119 +++++++++++++++---------------- UICatalog/Scenarios/Buttons.cs | 2 - 2 files changed, 59 insertions(+), 62 deletions(-) diff --git a/Terminal.Gui/Views/MessageBox.cs b/Terminal.Gui/Views/MessageBox.cs index 04e4c25b7..9b00337d7 100644 --- a/Terminal.Gui/Views/MessageBox.cs +++ b/Terminal.Gui/Views/MessageBox.cs @@ -386,66 +386,6 @@ public static class MessageBox messageLabel.TextFormatter.MultiLine = !wrapMessage; d.Add (messageLabel); - d.Loaded += (s, e) => - { - if (width != 0 || height != 0) - { - return; - } - - // TODO: replace with Dim.Fit when implemented - Rectangle maxBounds = d.SuperView?.Bounds ?? Application.Top.Bounds; - - Thickness adornmentsThickness = d.GetAdornmentsThickness (); - - if (wrapMessage) - { - messageLabel.TextFormatter.Size = new ( - maxBounds.Size.Width - - adornmentsThickness.Horizontal, - maxBounds.Size.Height - - adornmentsThickness.Vertical - ); - } - - string msg = messageLabel.TextFormatter.Format (); - Size messageSize = messageLabel.TextFormatter.FormatAndGetSize (); - - // Ensure the width fits the text + buttons - int newWidth = Math.Max ( - width, - Math.Max ( - messageSize.Width + adornmentsThickness.Horizontal, - d.GetButtonsWidth () + d.Buttons.Length + adornmentsThickness.Horizontal - ) - ); - - if (newWidth > d.Frame.Width) - { - d.Width = newWidth; - } - - // Ensure height fits the text + vspace + buttons - if (messageSize.Height == 0) - { - d.Height = Math.Max (height, 3 + adornmentsThickness.Vertical); - } - else - { - string lastLine = messageLabel.TextFormatter.GetLines () [^1]; - - // INTENT: Instead of the check against \n or \r\n, how about just Environment.NewLine? - d.Height = Math.Max ( - height, - messageSize.Height - + (lastLine.EndsWith ("\r\n") || lastLine.EndsWith ('\n') ? 1 : 2) - + adornmentsThickness.Vertical - ); - } - - d.SetRelativeLayout (d.SuperView?.Frame ?? Application.Top.Frame); - }; - // Setup actions Clicked = -1; @@ -466,10 +406,69 @@ public static class MessageBox } } + d.Loaded += Dialog_Loaded; + // Run the modal; do not shutdown the mainloop driver when done Application.Run (d); d.Dispose (); return Clicked; + + void Dialog_Loaded (object s, EventArgs e) + { + if (width != 0 || height != 0) + { + return; + } + + // TODO: replace with Dim.Fit when implemented + Rectangle maxBounds = d.SuperView?.Bounds ?? Application.Top.Bounds; + + Thickness adornmentsThickness = d.GetAdornmentsThickness (); + + if (wrapMessage) + { + messageLabel.TextFormatter.Size = new ( + maxBounds.Size.Width + - adornmentsThickness.Horizontal, + maxBounds.Size.Height + - adornmentsThickness.Vertical); + } + + string msg = messageLabel.TextFormatter.Format (); + Size messageSize = messageLabel.TextFormatter.FormatAndGetSize (); + + // Ensure the width fits the text + buttons + int newWidth = Math.Max ( + width, + Math.Max ( + messageSize.Width + adornmentsThickness.Horizontal, + d.GetButtonsWidth () + d.Buttons.Length + adornmentsThickness.Horizontal)); + + if (newWidth > d.Frame.Width) + { + d.Width = newWidth; + } + + // Ensure height fits the text + vspace + buttons + if (messageSize.Height == 0) + { + d.Height = Math.Max (height, 3 + adornmentsThickness.Vertical); + } + else + { + string lastLine = messageLabel.TextFormatter.GetLines () [^1]; + + // INTENT: Instead of the check against \n or \r\n, how about just Environment.NewLine? + d.Height = Math.Max ( + height, + messageSize.Height + + (lastLine.EndsWith ("\r\n") || lastLine.EndsWith ('\n') ? 1 : 2) + + adornmentsThickness.Vertical); + } + + d.SetRelativeLayout (d.SuperView?.Frame ?? Application.Top.Frame); + d.LayoutSubviews (); + } } } diff --git a/UICatalog/Scenarios/Buttons.cs b/UICatalog/Scenarios/Buttons.cs index 545b4bf97..068c15a7c 100644 --- a/UICatalog/Scenarios/Buttons.cs +++ b/UICatalog/Scenarios/Buttons.cs @@ -61,8 +61,6 @@ public class Buttons : Scenario { ColorScheme = colorScheme.Value, X = Pos.Right (prev) + 2, - - //X = x, Y = Pos.Y (colorButtonsLabel), Text = $"_{colorScheme.Key}" };