Skip to content

Commit

Permalink
Fixes #1989. ListView: Ensures SelectedItem visibility on MoveDown an…
Browse files Browse the repository at this point in the history
…d MoveUp. (#1990)

* Ensures visibility SelectedItem on MoveDown and MoveUp.

* Fix VisibleItems to Frame.Height.
  • Loading branch information
BDisp authored Sep 5, 2022
1 parent 9505987 commit 3866e65
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,17 @@ public virtual bool MoveDown ()
top++;
} else if (selected < top) {
top = selected;
} else if (selected < top) {
top = selected;
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected == 0) {
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected >= top + Frame.Height) {
top = source.Count - Frame.Height;
SetNeedsDisplay ();
}

return true;
Expand Down Expand Up @@ -561,6 +566,9 @@ public virtual bool MoveUp ()
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected < top) {
top = selected;
SetNeedsDisplay ();
}
return true;
}
Expand Down
210 changes: 210 additions & 0 deletions UnitTests/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Terminal.Gui.Views {
public class ListViewTests {
readonly ITestOutputHelper output;

public ListViewTests (ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void Constructors_Defaults ()
{
Expand Down Expand Up @@ -221,5 +229,207 @@ string GetContents (int line)
return item;
}
}

[Fact]
[AutoInitShutdown]
public void Ensures_Visibility_SelectedItem_On_MoveDown_And_MoveUp ()
{
var source = new List<string> ();
for (int i = 0; i < 20; i++) {
source.Add ($"Line{i}");
}
var lv = new ListView (source) { Width = Dim.Fill (), Height = Dim.Fill () };
var win = new Window ();
win.Add (lv);
Application.Top.Add (win);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (12, 12);
Application.Refresh ();

Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.ScrollDown (10));
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (1, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
│Line10 │
└──────────┘", output);

Assert.True (lv.MoveEnd ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line19 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────┘", output);

Assert.True (lv.ScrollUp (20));
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.ScrollUp (20));
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.MoveHome ());
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.ScrollDown (20));
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line19 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────┘", output);

Assert.True (lv.MoveUp ());
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);
}
}
}

0 comments on commit 3866e65

Please sign in to comment.