From 0b040a383c5f6ad0fbaf50e2dbeae2a1bcbb667e Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 17 Jun 2022 15:51:47 +0100 Subject: [PATCH] Prevents HotKeySpecifier being negative. --- Terminal.Gui/Views/Checkbox.cs | 3 +- UnitTests/CheckboxTests.cs | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/Checkbox.cs b/Terminal.Gui/Views/Checkbox.cs index 430b97557..c61cbfad4 100644 --- a/Terminal.Gui/Views/Checkbox.cs +++ b/Terminal.Gui/Views/Checkbox.cs @@ -114,7 +114,8 @@ namespace Terminal.Gui { break; } - int w = TextFormatter.Size.Width - (TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0); + int w = TextFormatter.Size.Width - (TextFormatter.Text.Contains (HotKeySpecifier) + ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0); GetCurrentWidth (out int cWidth); var canSetWidth = SetWidth (w, out int rWidth); if (canSetWidth && (cWidth < rWidth || AutoSize)) { diff --git a/UnitTests/CheckboxTests.cs b/UnitTests/CheckboxTests.cs index af0d028a4..0f8d5c7ec 100644 --- a/UnitTests/CheckboxTests.cs +++ b/UnitTests/CheckboxTests.cs @@ -374,5 +374,97 @@ namespace Terminal.Gui.Views { pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new Rect (0, 0, 30, 5), pos); } + + [Fact, AutoInitShutdown] + public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier () + { + var checkBox = new CheckBox () { + Y = Pos.Center (), + Text = "Check this out 你" + }; + checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width); + + var win = new Window () { + Width = Dim.Fill (), + Height = Dim.Fill (), + Title = "Test Demo 你" + }; + win.Add (checkBox); + Application.Top.Add (win); + + Assert.True (checkBox.AutoSize); + + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ ╴ Check this out 你│ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + + Assert.True (checkBox.AutoSize); + checkBox.Text = "Check this out 你 changed"; + Assert.True (checkBox.AutoSize); + Application.Refresh (); + expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ ╴ Check this out 你 changed│ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + } + + [Fact, AutoInitShutdown] + public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier () + { + var checkBox = new CheckBox () { + Y = Pos.Center (), + Text = "C_heck this out 你" + }; + checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width); + + var win = new Window () { + Width = Dim.Fill (), + Height = Dim.Fill (), + Title = "Test Demo 你" + }; + win.Add (checkBox); + Application.Top.Add (win); + + Assert.True (checkBox.AutoSize); + + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ ╴ Check this out 你│ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + + Assert.True (checkBox.AutoSize); + checkBox.Text = "Check this out 你 changed"; + Assert.True (checkBox.AutoSize); + Application.Refresh (); + expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ ╴ Check this out 你 changed│ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + } } }