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
26 changes: 25 additions & 1 deletion Terminal.Gui/Views/ListView/ListView.Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public partial class ListView
/// clears any existing multi-selection.
/// </param>
/// <returns><see langword="true"/> if the selection was moved.</returns>
/// <remarks>
/// <para>
/// When the selection is already at the last item and <see cref="View.TabStop"/> is
/// <see cref="TabBehavior.NoStop"/>, the selection wraps around to the first item.
/// Otherwise the method returns <see langword="false"/> without changing the selection.
/// </para>
/// </remarks>
public bool MoveDown (bool extend = false)
{
if (Source is null || Source.Count == 0)
Expand All @@ -30,13 +37,18 @@ public bool MoveDown (bool extend = false)
// Can move down by one.
newItem = SelectedItem.Value + 1;
}
else if (SelectedItem >= Viewport.Y + Viewport.Height)
else if (Viewport.Height > 0 && SelectedItem >= Viewport.Y + Viewport.Height)
{
// Just scroll viewport
Viewport = Viewport with { Y = Source.Count - Viewport.Height };

return true;
}
else if (TabStop == TabBehavior.NoStop)
{
// Wrap to top
newItem = 0;
}
Comment thread
tig marked this conversation as resolved.
else
{
// Already at bottom
Expand Down Expand Up @@ -189,6 +201,13 @@ public bool MovePageUp (bool extend = false)
/// clears any existing multi-selection.
/// </param>
/// <returns><see langword="true"/> if the selection was moved.</returns>
/// <remarks>
/// <para>
/// When the selection is already at the first item and <see cref="View.TabStop"/> is
/// <see cref="TabBehavior.NoStop"/>, the selection wraps around to the last item.
/// Otherwise the method returns <see langword="false"/> without changing the selection.
/// </para>
/// </remarks>
public bool MoveUp (bool extend = false)
{
if (Source is null || Source.Count == 0)
Expand Down Expand Up @@ -221,6 +240,11 @@ public bool MoveUp (bool extend = false)

return true;
}
else if (TabStop == TabBehavior.NoStop)
{
// Wrap to bottom
newItem = Source.Count - 1;
}
else
{
// Already at top
Expand Down
84 changes: 80 additions & 4 deletions Tests/UnitTestsParallelizable/Views/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,14 +1076,14 @@ public void ShowMarks_True_SpaceWithShift_RadioButton_MarksAndMovesDown ()

// Press Space+Shift again - cannot move down further
// In radio button mode, item should toggle: marked → unmarked
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.False (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.Equal (2, lv.SelectedItem); // Still at item 2
Assert.False (lv.Source.IsMarked (0));
Assert.False (lv.Source.IsMarked (1));
Assert.False (lv.Source.IsMarked (2)); // Toggled off

// Press key combo again - should toggle back to marked
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.False (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.Equal (2, lv.SelectedItem); // Still at item 2
Assert.False (lv.Source.IsMarked (0));
Assert.False (lv.Source.IsMarked (1));
Expand Down Expand Up @@ -1137,14 +1137,14 @@ public void ShowMarks_True_SpaceWithShift_SelectsThenDown_MultipleSelection ()
Assert.False (lv.Source.IsMarked (2));

// Press key combo again
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.False (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.Equal (2, lv.SelectedItem); // cannot move down any further
Assert.True (lv.Source.IsMarked (0));
Assert.True (lv.Source.IsMarked (1));
Assert.True (lv.Source.IsMarked (2)); // but can toggle marked

// Press key combo again
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.False (lv.NewKeyDownEvent (Key.Space.WithShift));
Assert.Equal (2, lv.SelectedItem); // cannot move down any further
Assert.True (lv.Source.IsMarked (0));
Assert.True (lv.Source.IsMarked (1));
Expand Down Expand Up @@ -3129,4 +3129,80 @@ public void RowRender_Event_Receives_Correct_Row_Indices_After_Scroll ()
}

#endregion RowRender RowAttribute Override

#region Movement Cycling

// Copilot
[Fact]
public void MoveDown_AtBottom_WhenTabStopIsNoStop_WrapsToTop ()
{
ObservableCollection<string> source = ["one", "two", "three"];
ListView lv = new () { Source = new ListWrapper<string> (source), TabStop = TabBehavior.NoStop };
lv.SetFocus ();

// Move to last item
Assert.True (lv.MoveDown ()); // selects 0
Assert.True (lv.MoveDown ()); // selects 1
Assert.True (lv.MoveDown ()); // selects 2
Assert.Equal (2, lv.SelectedItem);

// Should wrap to top
Assert.True (lv.MoveDown ());
Assert.Equal (0, lv.SelectedItem);
}

// Copilot
[Fact]
public void MoveDown_AtBottom_WhenTabStopIsNotNoStop_ReturnsFalse ()
{
ObservableCollection<string> source = ["one", "two", "three"];
ListView lv = new () { Source = new ListWrapper<string> (source), TabStop = TabBehavior.TabStop };
lv.SetFocus ();

// Move to last item
Assert.True (lv.MoveDown ()); // selects 0
Assert.True (lv.MoveDown ()); // selects 1
Assert.True (lv.MoveDown ()); // selects 2
Assert.Equal (2, lv.SelectedItem);

// Should NOT wrap — returns false
Assert.False (lv.MoveDown ());
Assert.Equal (2, lv.SelectedItem);
}

// Copilot
[Fact]
public void MoveUp_AtTop_WhenTabStopIsNoStop_WrapsToBottom ()
{
ObservableCollection<string> source = ["one", "two", "three"];
ListView lv = new () { Frame = new Rectangle (0, 0, 10, 20), Source = new ListWrapper<string> (source), TabStop = TabBehavior.NoStop };
lv.SetFocus ();

// Select first item
Assert.True (lv.MoveDown ());
Assert.Equal (0, lv.SelectedItem);

// Should wrap to bottom
Assert.True (lv.MoveUp ());
Assert.Equal (2, lv.SelectedItem);
}

// Copilot
[Fact]
public void MoveUp_AtTop_WhenTabStopIsNotNoStop_ReturnsFalse ()
{
ObservableCollection<string> source = ["one", "two", "three"];
ListView lv = new () { Frame = new Rectangle (0, 0, 10, 20), Source = new ListWrapper<string> (source), TabStop = TabBehavior.TabStop };
lv.SetFocus ();

// Select first item
Assert.True (lv.MoveDown ());
Assert.Equal (0, lv.SelectedItem);

// Should NOT wrap — returns false
Assert.False (lv.MoveUp ());
Assert.Equal (0, lv.SelectedItem);
}

#endregion Movement Cycling
}
Loading