From 8f7bc395c758dc780d858cd93dcff3acae4558b0 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 13 Jun 2022 23:39:20 +0100 Subject: [PATCH] Fixes #1791. View not turn off AutoSize if TextFormatter.Size fit the Anchor. (#1798) * View not turn off AutoSize if TextFormatter.Size fit the Anchor. * Added a Button unit test with HotKeySpecifier. --- Terminal.Gui/Core/View.cs | 10 ++- UnitTests/ViewTests.cs | 139 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 69096c997..24c2a1b88 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -568,7 +568,10 @@ namespace Terminal.Gui { } width = value; - if (autoSize && value.Anchor (0) != TextFormatter.Size.Width) { + if (autoSize && value.Anchor (0) != TextFormatter.Size.Width + - (TextFormatter.IsHorizontalDirection (TextDirection) + && TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0)) { + autoSize = false; } SetMinWidthHeight (); @@ -594,7 +597,10 @@ namespace Terminal.Gui { } height = value; - if (autoSize && value.Anchor (0) != TextFormatter.Size.Height) { + if (autoSize && value.Anchor (0) != TextFormatter.Size.Height + - (TextFormatter.IsVerticalDirection (TextDirection) + && TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0)) { + autoSize = false; } SetMinWidthHeight (); diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index e061c1305..24bfa58e1 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -2438,5 +2438,144 @@ Y pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new Rect (0, 0, 22, 22), pos); } + + [Fact, AutoInitShutdown] + public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit () + { + var text = $"Fi_nish 終"; + var horizontalView = new View () { + AutoSize = true, + HotKeySpecifier = '_', + Text = text + }; + var verticalView = new View () { + Y = 3, + AutoSize = true, + HotKeySpecifier = '_', + Text = text, + TextDirection = TextDirection.TopBottom_LeftRight + }; + var win = new Window () { + Width = Dim.Fill (), + Height = Dim.Fill (), + Text = "Window" + }; + win.Add (horizontalView, verticalView); + Application.Top.Add (win); + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (22, 22); + + Assert.True(horizontalView.AutoSize); + Assert.True(verticalView.AutoSize); + Assert.Equal (new Rect (0, 0, 10, 1), horizontalView.Frame); + Assert.Equal (new Rect (0, 3, 2, 9), verticalView.Frame); + var expected = @" +┌────────────────────┐ +│Finish 終 │ +│ │ +│ │ +│F │ +│i │ +│n │ +│i │ +│s │ +│h │ +│ │ +│終 │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────┘ +"; + + var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + Assert.Equal (new Rect (0, 0, 22, 22), pos); + + verticalView.Text = $"最初_の行二行目"; + Application.Top.Redraw (Application.Top.Bounds); + Assert.True (horizontalView.AutoSize); + Assert.True (verticalView.AutoSize); + Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame); + expected = @" +┌────────────────────┐ +│Finish 終 │ +│ │ +│ │ +│最 │ +│初 │ +│の │ +│行 │ +│二 │ +│行 │ +│目 │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────┘ +"; + + pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + Assert.Equal (new Rect (0, 0, 22, 22), pos); + } + + [Fact, AutoInitShutdown] + public void AutoSize_Stays_True_Center_HotKeySpecifier () + { + var btn = new Button () { + X = Pos.Center (), + Y = Pos.Center (), + Text = "Say He_llo 你", + AutoSize = true + }; + + var win = new Window () { + Width = Dim.Fill (), + Height = Dim.Fill (), + Title = "Test Demo 你" + }; + win.Add (btn); + Application.Top.Add (win); + + Assert.True (btn.AutoSize); + + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ [ Say Hello 你 ] │ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + + Assert.True (btn.AutoSize); + btn.Text = "Say He_llo 你 changed"; + Assert.True (btn.AutoSize); + Application.Refresh (); + expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ [ Say Hello 你 changed ] │ +│ │ +└────────────────────────────┘ +"; + + GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + } } }