Skip to content

Commit 168d233

Browse files
authored
Fixes gui-cs#4272 - Update ListView's AllowsMultipleSelection to adhere to docs (gui-cs#4273)
* gui-cs#4272 - Update ListView's AllowsMultipleSelection to adhere to docs * Add and update unit tests for ListView's marking
1 parent e352dde commit 168d233

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

Terminal.Gui/Views/ListView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Terminal.Gui.Views;
4343
public class ListView : View, IDesignable
4444
{
4545
private bool _allowsMarking;
46-
private bool _allowsMultipleSelection = true;
46+
private bool _allowsMultipleSelection = false;
4747
private int _lastSelectedItem = -1;
4848
private int _selected = -1;
4949
private IListDataSource _source;

Tests/UnitTests/Views/ListViewTests.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public void Constructors_Defaults ()
1616
Assert.Null (lv.Source);
1717
Assert.True (lv.CanFocus);
1818
Assert.Equal (-1, lv.SelectedItem);
19+
Assert.False (lv.AllowsMultipleSelection);
1920

2021
lv = new () { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
2122
Assert.NotNull (lv.Source);
@@ -37,6 +38,7 @@ public void Constructors_Defaults ()
3738
Assert.NotNull (lv.Source);
3839
Assert.Equal (-1, lv.SelectedItem);
3940
Assert.Equal (new (0, 1, 10, 20), lv.Frame);
41+
4042
}
4143

4244
[Fact]
@@ -524,10 +526,72 @@ public void ListViewProcessKeyReturnValue_WithMultipleCommands ()
524526
}
525527

526528
[Fact]
527-
public void AllowsMarking_True_SpaceWithShift_SelectsThenDown ()
529+
public void AllowsMarking_True_SpaceWithShift_SelectsThenDown_SingleSelection ()
530+
{
531+
var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
532+
lv.AllowsMarking = true;
533+
lv.AllowsMultipleSelection = false;
534+
535+
Assert.NotNull (lv.Source);
536+
537+
// first item should be deselected by default
538+
Assert.Equal (-1, lv.SelectedItem);
539+
540+
// nothing is ticked
541+
Assert.False (lv.Source.IsMarked (0));
542+
Assert.False (lv.Source.IsMarked (1));
543+
Assert.False (lv.Source.IsMarked (2));
544+
545+
// view should indicate that it has accepted and consumed the event
546+
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
547+
548+
// first item should now be selected
549+
Assert.Equal (0, lv.SelectedItem);
550+
551+
// none of the items should be ticked
552+
Assert.False (lv.Source.IsMarked (0));
553+
Assert.False (lv.Source.IsMarked (1));
554+
Assert.False (lv.Source.IsMarked (2));
555+
556+
// Press key combo again
557+
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
558+
559+
// second item should now be selected
560+
Assert.Equal (1, lv.SelectedItem);
561+
562+
// first item only should be ticked
563+
Assert.True (lv.Source.IsMarked (0));
564+
Assert.False (lv.Source.IsMarked (1));
565+
Assert.False (lv.Source.IsMarked (2));
566+
567+
// Press key combo again
568+
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
569+
Assert.Equal (2, lv.SelectedItem);
570+
Assert.False (lv.Source.IsMarked (0));
571+
Assert.True (lv.Source.IsMarked (1));
572+
Assert.False (lv.Source.IsMarked (2));
573+
574+
// Press key combo again
575+
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
576+
Assert.Equal (2, lv.SelectedItem); // cannot move down any further
577+
Assert.False (lv.Source.IsMarked (0));
578+
Assert.False (lv.Source.IsMarked (1));
579+
Assert.True (lv.Source.IsMarked (2)); // but can toggle marked
580+
581+
// Press key combo again
582+
Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
583+
Assert.Equal (2, lv.SelectedItem); // cannot move down any further
584+
Assert.False (lv.Source.IsMarked (0));
585+
Assert.False (lv.Source.IsMarked (1));
586+
Assert.False (lv.Source.IsMarked (2)); // untoggle toggle marked
587+
}
588+
589+
[Fact]
590+
public void AllowsMarking_True_SpaceWithShift_SelectsThenDown_MultipleSelection ()
528591
{
529592
var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
530593
lv.AllowsMarking = true;
594+
lv.AllowsMultipleSelection = true;
531595

532596
Assert.NotNull (lv.Source);
533597

0 commit comments

Comments
 (0)