Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public override bool ProcessKey (KeyEvent kb)
var newItem = KeystrokeNavigator?.GetNextMatchingItem (SelectedItem, (char)kb.KeyValue);
if (newItem is int && newItem != -1) {
SelectedItem = (int)newItem;
EnsuresVisibilitySelectedItem ();
EnsureSelectedItemVisible ();
SetNeedsDisplay ();
return true;
}
Expand Down Expand Up @@ -727,7 +727,7 @@ public override bool OnEnter (View view)
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);

if (lastSelectedItem == -1) {
EnsuresVisibilitySelectedItem ();
EnsureSelectedItemVisible ();
}

return base.OnEnter (view);
Expand All @@ -743,7 +743,10 @@ public override bool OnLeave (View view)
return base.OnLeave (view);
}

void EnsuresVisibilitySelectedItem ()
/// <summary>
/// Ensures the selected item is always visible on the screen.
/// </summary>
public void EnsureSelectedItemVisible ()
{
SuperView?.LayoutSubviews ();
if (selected < top) {
Expand Down Expand Up @@ -840,7 +843,7 @@ int GetMaxLengthItem ()
if (src == null || src?.Count == 0) {
return 0;
}

int maxLength = 0;
for (int i = 0; i < src.Count; i++) {
var t = src [i];
Expand Down Expand Up @@ -924,7 +927,7 @@ public int StartsWith (string search)
return i;
}
} else if (t is string s) {
if (s.ToUpperInvariant ().StartsWith (search.ToUpperInvariant ())) {
if (s.StartsWith (search, StringComparison.InvariantCultureIgnoreCase)) {
return i;
}
}
Expand Down
64 changes: 63 additions & 1 deletion UnitTests/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void RowRender_Event ()

[Fact]
[AutoInitShutdown]
public void EnsuresVisibilitySelectedItem_Top ()
public void EnsureSelectedItemVisible_Top ()
{
var source = new List<string> () { "First", "Second" };
ListView lv = new ListView (source) { Width = Dim.Fill (), Height = 1 };
Expand Down Expand Up @@ -451,5 +451,67 @@ public void SetSource_Preserves_ListWrapper_Instance_If_Not_Null ()
lv.SetSourceAsync (null);
Assert.NotNull (lv.Source);
}

[Fact]
public void ListWrapper_StartsWith ()
{
var lw = new ListWrapper (new List<string> { "One", "Two", "Three" });

Assert.Equal (1, lw.StartsWith ("t"));
Assert.Equal (1, lw.StartsWith ("tw"));
Assert.Equal (2, lw.StartsWith ("th"));
Assert.Equal (1, lw.StartsWith ("T"));
Assert.Equal (1, lw.StartsWith ("TW"));
Assert.Equal (2, lw.StartsWith ("TH"));

lw = new ListWrapper (new List<NStack.ustring> { "One", "Two", "Three" });

Assert.Equal (1, lw.StartsWith ("t"));
Assert.Equal (1, lw.StartsWith ("tw"));
Assert.Equal (2, lw.StartsWith ("th"));
Assert.Equal (1, lw.StartsWith ("T"));
Assert.Equal (1, lw.StartsWith ("TW"));
Assert.Equal (2, lw.StartsWith ("TH"));
}

[Fact, AutoInitShutdown]
public void EnsureSelectedItemVisible_SelectedItem ()
{
var source = new List<string> ();
for (int i = 0; i < 10; i++) {
source.Add ($"Item {i}");
}
var lv = new ListView (source) {
Width = 10,
Height = 5
};
Application.Top.Add (lv);
Application.Begin (Application.Top);

TestHelpers.AssertDriverContentsWithFrameAre (@"
Item 0
Item 1
Item 2
Item 3
Item 4", output);

lv.SelectedItem = 6;
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (@"
Item 0
Item 1
Item 2
Item 3
Item 4", output);

lv.EnsureSelectedItemVisible ();
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (@"
Item 2
Item 3
Item 4
Item 5
Item 6", output);
}
}
}