Skip to content

Commit

Permalink
Fixes gui-cs#2970. ScrollView doesn't remove a view that was previous…
Browse files Browse the repository at this point in the history
…ly added on both versions.
  • Loading branch information
BDisp committed Nov 10, 2023
1 parent e16205a commit cd9e6ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
41 changes: 31 additions & 10 deletions Terminal.Gui/Views/ScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,37 @@ public override void Add (View view)
SetNeedsLayout ();
}

/// <summary>
/// Removes the view from the scrollview.
/// </summary>
/// <param name="view">The view to remove from the scrollview.</param>
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;
}
}

/// <summary>
/// Removes all widgets from this container.
/// </summary>
public override void RemoveAll ()
{
_contentView.RemoveAll ();
}

void View_MouseLeave (object sender, MouseEventEventArgs e)
{
if (Application.MouseGrabView != null && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal) {
Expand Down Expand Up @@ -318,16 +349,6 @@ public bool ShowHorizontalScrollIndicator {
}
}

/// <summary>
/// Removes all widgets from this container.
/// </summary>
/// <remarks>
/// </remarks>
public override void RemoveAll ()
{
_contentView.RemoveAll ();
}

/// <summary>
/// Gets or sets the visibility for the vertical scroll indicator.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions UnitTests/Views/ScrollViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit cd9e6ae

Please sign in to comment.