From cd9e6aeef1f9b3366f0ca92cfb0f320e9b82a120 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 10 Nov 2023 17:15:42 +0000 Subject: [PATCH] Fixes #2970. ScrollView doesn't remove a view that was previously added on both versions. --- Terminal.Gui/Views/ScrollView.cs | 41 ++++++++++++++++++++++-------- UnitTests/Views/ScrollViewTests.cs | 23 +++++++++++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index bb83466360..e4b8f3a1a5 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/Terminal.Gui/Views/ScrollView.cs @@ -277,6 +277,37 @@ public override void Add (View view) SetNeedsLayout (); } + /// + /// Removes the view from the scrollview. + /// + /// The view to remove from the scrollview. + public override void Remove (View view) + { + if (view == null) { + return; + } + + SetNeedsDisplay (); + var container = view?.SuperView; + if (container == this) { + base.Remove (view); + } else { + container?.Remove (view); + } + + if (_contentView.InternalSubviews.Count < 1) { + this.CanFocus = false; + } + } + + /// + /// Removes all widgets from this container. + /// + public override void RemoveAll () + { + _contentView.RemoveAll (); + } + void View_MouseLeave (object sender, MouseEventEventArgs e) { if (Application.MouseGrabView != null && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal) { @@ -318,16 +349,6 @@ public bool ShowHorizontalScrollIndicator { } } - /// - /// Removes all widgets from this container. - /// - /// - /// - public override void RemoveAll () - { - _contentView.RemoveAll (); - } - /// /// Gets or sets the visibility for the vertical scroll indicator. /// diff --git a/UnitTests/Views/ScrollViewTests.cs b/UnitTests/Views/ScrollViewTests.cs index f8354df095..676eb5d8c0 100644 --- a/UnitTests/Views/ScrollViewTests.cs +++ b/UnitTests/Views/ScrollViewTests.cs @@ -927,5 +927,28 @@ public void DrawTextFormatter_Respects_The_Clip_Bounds () pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new Rect (1, 1, 21, 14), pos); } + + [Fact, AutoInitShutdown] + public void Remove_Added_View_Is_Allowed () + { + var sv = new ScrollView () { + Width = 20, + Height = 20, + ContentSize = new Size (100, 100) + }; + sv.Add (new View () { Width = Dim.Fill (), Height = Dim.Fill (50), Id = "View1" }, + new View () { Y = 51, Width = Dim.Fill (), Height = Dim.Fill (), Id = "View2" }); + + Application.Top.Add (sv); + Application.Begin (Application.Top); + + Assert.Equal (4, sv.Subviews.Count); + Assert.Equal (2, sv.Subviews [0].Subviews.Count); + + sv.Remove (sv.Subviews [0].Subviews [1]); + Assert.Equal (4, sv.Subviews.Count); + Assert.Single (sv.Subviews [0].Subviews); + Assert.Equal ("View1", sv.Subviews [0].Subviews [0].Id); + } } }