From 03e0f086c39392bf5e19da568bbebb74da32db59 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 3 Jan 2023 13:18:09 +0000 Subject: [PATCH] Fixes #2267. Toplevel.EnsureVisibleBounds throws an exception if border is null. --- Terminal.Gui/Core/Toplevel.cs | 9 +++++---- UnitTests/ToplevelTests.cs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/Toplevel.cs b/Terminal.Gui/Core/Toplevel.cs index a170d0c56..fddabb692 100644 --- a/Terminal.Gui/Core/Toplevel.cs +++ b/Terminal.Gui/Core/Toplevel.cs @@ -613,8 +613,9 @@ namespace Terminal.Gui { } nx = Math.Max (x, 0); nx = nx + top.Frame.Width > l ? Math.Max (l - top.Frame.Width, 0) : nx; - if (nx + (top.Border != null && top.Border.DrawMarginFrame ? 2 : 1) > top.Frame.X + top.Frame.Width) { - nx = Math.Max (top.Frame.Right - (top.Border.DrawMarginFrame ? 2 : 1), 0); + var mfLength = top.Border?.DrawMarginFrame == true ? 2 : 1; + if (nx + mfLength > top.Frame.X + top.Frame.Width) { + nx = Math.Max (top.Frame.Right - mfLength, 0); } //System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}"); bool m, s; @@ -653,8 +654,8 @@ namespace Terminal.Gui { } ny = Math.Min (ny, l); ny = ny + top.Frame.Height >= l ? Math.Max (l - top.Frame.Height, m ? 1 : 0) : ny; - if (ny + (top.Border != null && top.Border.DrawMarginFrame ? 2 : 1) > top.Frame.Y + top.Frame.Height) { - ny = Math.Max (top.Frame.Bottom - (top.Border.DrawMarginFrame ? 2 : 1), 0); + if (ny + mfLength > top.Frame.Y + top.Frame.Height) { + ny = Math.Max (top.Frame.Bottom - mfLength, 0); } //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}"); diff --git a/UnitTests/ToplevelTests.cs b/UnitTests/ToplevelTests.cs index a528a3a61..16a6daf5d 100644 --- a/UnitTests/ToplevelTests.cs +++ b/UnitTests/ToplevelTests.cs @@ -967,5 +967,18 @@ namespace Terminal.Gui.Core { Application.Run (); } + + [Fact, AutoInitShutdown] + public void EnsureVisibleBounds_With_Border_Null_Not_Throws () + { + var top = new Toplevel (); + Application.Begin (top); + + var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10)); + Assert.Null (exception); + + exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0)); + Assert.Null (exception); + } } } \ No newline at end of file